Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Class Exercise: Shape class Shape.java Shape Driver - ShapeTest.java Write a cla

ID: 3816165 • Letter: C

Question

Class Exercise: Shape class Shape.java Shape Driver - ShapeTest.java Write a class called Shape that contains instance data that represents the name and number of sides of the shape, Define a constructor to initialize these values. Include mutator(setter) methods - with the this reference - for the instance data, and a toString method that returns a the shape data. Create a static variable to keep track of the number of shapes and a static method to return the number of shapes entered. Create a driver class called ShapeTest, whose main method instantiates the objects and updates several Shape objects by prompting the user for the information. In the Shape Class, you will need: Two private members to store data A static variable to store the number of shapes. Enter info on 3 shapes: One constructor, which should not accept any values during instantiation Enter the name of the shape:Square Mutators for each private member of the class Enter the number of sides: 4 Astatic method which returns the number of shapes Enter the name of the shape: Hexagon A tostring method to output the information Enter the number of sides: 6 Enter the name of the shape Octagon In the ShapeTest driver. you will need: Enter the number of sides: At three Shape objects (no hardcoded info this time) Here is the info you entered for the 3 shapes: A Scaner Object to get information for all of the objects Shape: Square Local variables to store information No. of sides:4 To reference the static variable (with the updated number of shapes) in the driver Shape: Hexagon No. of sides:6 Here is some sample output: Shape: Octagon No. of sides:8

Explanation / Answer

import java.util.Scanner;

/**
*
* @author Sam
*/
public class Shape {

    private String name;
    private int sides;
    static int count = 0;

    public Shape() { //constuctor will increase count
        count++;
    }

    public void setName(String name) { //mutattor
        this.name = name;
    }

    public void setSides(int sides) { //mutator
        this.sides = sides;
    }

    public static int getCount() { //getter to get number of shape
        return count;
    }

    @Override
    public String toString() {// return info about shape
        return "Shape: " + name + " No of Side:" + sides + ' ';
    }

}

class ShapeTest {

    public static void main(String[] args) {

        Shape s0 = new Shape(); //shape object
        Shape s1 = new Shape(); //shape Object
        Shape s2 = new Shape(); //shape Object
        String name; //local variable
        int sides; //local variable
        Scanner sc = new Scanner(System.in); //scanner class

        System.out.println("Enter info on three shapes: ");
        System.out.print("Enter name of shape: ");
        name = sc.next(); //read name
        System.out.print("Enter number of sides: ");
        sides = sc.nextInt(); //read side of shape
        s0.setName(name); //set name
        s0.setSides(sides);//set side
        System.out.print("Enter name of shape: ");
        name = sc.next(); //read next name
        System.out.print("Enter number of sides: ");
        sides = sc.nextInt();//read next number of sides
        s1.setName(name); //set name for 2nd object
        s1.setSides(sides);//set sides for 2nd object
        System.out.print("Enter name of shape: ");
        name = sc.next();
        System.out.print("Enter number of sides: ");
        sides = sc.nextInt();
        s2.setName(name);
        s2.setSides(sides);

        System.out.println("Here is the info you entered about the " + Shape.getCount() + " shapes:");
        System.out.println(s0);//print 1st shape
        System.out.println(s1);//print 2nd shape
        System.out.println(s2);//print 3rd shape
    }
}

I have commented the code as far as possible to make your life easy. But still, if you face any trouble, please feel free to comment below. I shall be glad to help you with the code.