Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 users choice. Create an object of the appropriate class according to the users 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Brad Bulger, Jay Greenspan, David Wall

2nd Edition

ISBN: 0764549634, 9780764549632

More Books

Students also viewed these Databases questions