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

Program name is TestShapes.java. For this program you are required to write 5 cl

ID: 3738714 • Letter: P

Question

Program name is TestShapes.java.

For this program you are required to write 5 classes.

The requirements are listed below. Naming guidelines: Define a class called Shape. – This class just has one attribute - color. – Write a default constructor that sets color to “red”. – Write a parametrized constructor and accessor and mutator methods. – Write a method called print, that takes no parameters and prints the color. – Also define a method called “area” that returns double, but leave it empty.

Define a class called Square. – This class inherits from the Shape class. – The class has the attribute sideLength - double. – Write a default constructor that sets sideLength to 1. – Write the parametrized constructor, accessor and mutator methods. Make sure to invoke the superclass’ constructor appropriately in both of the constructors. – Override the print and area methods. In the print method, call the superclass’ print method as well. The print method should then print the sideLength and the area of the square. In the area method, just calculate the area and return it.

Define a class called Rectangle – This class inherits from the Shape class. – The class has two attributes length - double and width - double. – Write a default constructor that sets length and width to 1. – Write the parametrized constructor, accessor and mutator methods. Make sure to invoke the superclass’ constructor appropriately in both of the constructors. – Override the print and area methods. In the print method, call the superclass’ print method as well. The print method should then print the length and width, and the area of the rectangle. In the area method, just calculate the area and return it.

Define a class called Circle – This class inherits from the Shape class. – The class has the attribute radius - double. – Write a default constructor that sets radius to 1. – Write the parametrized constructor, accessor and mutator methods. Make sure to invoke the superclass’ constructor appropriately in both of the constructors. – Override the print and area methods. In the print method, call the superclass’ print method as well. The print method should then print the radius and the area of the circle. In the area method, just calculate the area and return it.

Define a class called TestShapes. – This class should only contain the main method. – Accept a number ‘N’ from the user. Then create an array of type Shape of size N. – Ask the user to choose between the 3 shapes and enter an integer to denote their choice. If the user enters 1, it is a square. If the user enters 2, it is a rectangle, and if the user enters 3, it is a circle. You may assume that the user will only enter 1, 2 or 3. – Read in the required attributes (color, and whatever is needed for the user’s choice. Create an object of the appropriate class according to the user’s choice and attach it to the reference in the array. – Once the array of objects is created, invoke the print method for each object in the array one by one.

The program has to be done in Java.

Sample run:

Enter the number of shapes: 3

Enter the choice (Square, Rectangle or Circle): 1

Enter the color: Green

Enter the side length of the square: 12

Enter the choice (Square, Rectangle or Circle): 3

Enter the color: Purple

Enter the radius of the circle: 3

Enter the choice (Square, Rectangle or Circle): 2

Enter the color: Blue

Enter the length of the rectangle: 5

Enter the width of the rectangle: 9

Shape 1:

Color: Green

Side Length: 12

Area: 144

Shape 2:

Color: Purple

Radius: 3

Area: 28.27

Shape 3:

Color: Blue

Length: 5

Width: 9

Area: 45

Explanation / Answer

I've given my best.. Except color everything is working fine..

Code:

import java.util.Scanner;

class Shape

{

public String color;

Shape()

{

color="red";

}

public String getColor()

{

return color;

}

public void setColor(String c)

{

color=c;

}

public void print()

{

System.out.println(color);

}

public double area()

{

return 0.0;

}

}

class Square extends Shape

{

double sideLength;

Square()

{

super();

sideLength=1;

}

Square(double sl)

{

super();

sideLength=sl;

}

public double getSideLength()

{

return sideLength;

}

public void setSideLength(double sl)

{

sideLength=sl;

}

public void print()

{

super.print();

System.out.println(getSideLength()+" "+area());

}

public double area()

{

double a=sideLength*sideLength;

return a;

}

}

class Rectangle extends Shape

{

double length,width;

Rectangle()

{

super();

length=1;

width=1;

}

Rectangle(double l,double w)

{

super();

length=l;

width=w;

}

public double getLength()

{

return length;

}

public void setLength(double l)

{

length=l;

}

public double getWidth()

{

return width;

}

public void setWidth(double w)

{

width=w;

}

public void print()

{

super.print();

System.out.println(getLength()+" "+getWidth()+" "+area());

}

public double area()

{

double a=length*width;

return a;

}

}

class Circle extends Shape

{

double radius;

Circle()

{

super();

radius=1;

}

Circle(double r)

{

super();

radius=r;

}

public double getRadius()

{

return radius;

}

public void setRadius(double r)

{

radius=r;

}

public void print()

{

super.print();

System.out.println(getRadius()+" "+area());

}

public double area()

{

double a=radius*radius*(22/7);

return a;

}

}

class TestShapes

{

public static void main(String args[]){

Scanner input =new Scanner(System.in);

System.out.println("Enter N");

int N=input.nextInt();

Shape sh[]=new Shape[N];

for(int i=0;i<N;i++)

{

System.out.println("1.Sqare 2.Rectangle 3.Circle");

int k=input.nextInt();

if(k==1)

{

Square s=new Square();

double sideLength;

//s.setColor(input.nextLine());

System.out.println("Enter sideLength");

sideLength=input.nextDouble();

s.setSideLength(sideLength);

sh[i]=s;

}

else if(k==2)

{

Rectangle r=new Rectangle();

//r.setColor(input.nextLine());

double length,width;

System.out.println("Enter Length and Width:");

length=input.nextDouble();

width=input.nextDouble();

r.setLength(length);

r.setWidth(width);

sh[i]=r;

}

else

{

Circle c=new Circle();

//c.setColor(input.nextLine());

System.out.println("Enter Radius:");

double radius;

radius=input.nextDouble();

c.setRadius(radius);

sh[i]=c;

}

}

for(int i=0;i<N;i++)

sh[i].print();

}

}