This program should be called TestShapes.java. For this program write 5 classes.
ID: 3699918 • Letter: T
Question
This program should be called TestShapes.java. For this program write 5 classes. The requirements are listed below. Program should be done in Java. I am having trouble because I don't know if I have to create 5 classes inside one "package" or "project" - or if the whole program can be written in one "class". Please answer this question as well as the code.
• 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.
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
import java.util.*;
abstract class Shape // abstract base class
{
private String color;
public Shape() //default constructor
{
color = "red";
}
public Shape(String color) //argument constructor
{
this.color = color;
}
//abstract method
abstract public double area();
public String getColor()
{
return color;
}
public void setColor(String color)
{
this.color = color;
}
public void print()
{
System.out.println("Color : "+color);
}
}
class Square extends Shape
{
private double sideLength;
public Square()
{
sideLength = 1;
}
public Square(String color,double sideLength)
{
super(color);
this.sideLength = sideLength;
}
public void print()
{
super.print();
System.out.printf("SideLength : %.2f Area : %.2f",sideLength,area());
}
public double area()
{
return sideLength*sideLength;
}
}
class Rectangle extends Shape //derived class
{
private double length,width;
public Rectangle()
{
length = 1;
width = 1;
}
public Rectangle(String color,double length, double width)//argument constructor
{
super(color);// call to base class constructor
this.length = length;
this.width = width;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
//override base class abstract methods
public double area()
{
return length*width;
}
public void print()
{
super.print();
System.out.printf("Length : %.2f Width : %.2f Area : %.2f",length,width,area());
}
}
class Circle extends Shape
{
private double radius;
public Circle()
{
radius = 1;
}
public Circle(String color,double radius)
{
super(color);
this.radius = radius;
}
public double getRadius()
{
return radius;
}
public double area()
{
return 3.14*radius*radius;
}
public void print()
{
super.print();
System.out.printf("Radius: %.2f Area :%.2f ",radius,area());
}
}
class TestShapes
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of shapes: ");
int n = input.nextInt();
Shape[] s = new Shape[n]; // array of n shapes
int choice;
String color;
double radius,sideLength,length,width;
for(int i = 0;i<3;i++)
{
System.out.println("Enter the choice (Square, Rectangle or Circle): ");
choice = input.nextInt();
System.out.println("Enter the color: ");
color= input.next();
switch(choice)
{
case 1:
System.out.println("Enter the side length of the square:");
sideLength = input.nextDouble();
s[i] = new Square(color,sideLength);
break;
case 2:
System.out.println("Enter the length of the rectangle: ");
length = input.nextDouble();
System.out.println("Enter the width of the rectangle: ");
width = input.nextDouble();
s[i] = new Rectangle(color,length,width);
break;
case 3:
System.out.println("Enter the radius of the circle: ");
radius = input.nextDouble();
s[i] = new Circle(color,radius);
break;
default: System.out.println("Invalid option");
break;
}
}
for(int i =0;i<3;i++) // print all shapes
{
System.out.println(" Shape "+(i+1));
s[i].print();
}
}
}
Output:
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
SideLength : 12.00
Area : 144.00
Shape 2
Color : Purple
Radius: 3.00
Area :28.26
Shape 3
Color : blue
Length : 5.00 Width : 9.00
Area : 45.00
Do ask if any doubt. Please upvote. I have taken all classes in one program. You can use package with different programs(class names) also.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.