In Section 1, write a class called Rectangle that represents a Rectangle object. Rectangle objects will havelength and width attributes, and they will have perimeter() and area() methods. The Rectangle class should also have get methods for all attributes and set methods for all attributes. The Rectangle class should also have a getType() method that returns or outputs "Rectangle."
In Section 2, write a class called Square that is a child class of Rectangle. A square is a rectangle with equal sides. It should inherit the perimeter() and area() methods. It should also inherit the get and set methods for each attribute, if that is appropriate. The Square class should override the getType() method from the superclass so that it returns or outputs "Square."
In Section 3, write a class called TestDriver that demonstrates all of the code in your Rectangle class and in your Square class. There is example code in the text (Chapters 9 and 10) and in the PowerPoint for Chapters 9 and 10 in Doc Sharing.
Students, please note: You are being evaluated on your ability to write Java and understand Java. As a demonstration of your understanding, please use many, many (every line is a good idea) comments throughout your code assignments.
Three files: Rectangle.java, Square.java, TestDriver.java.
At the beginning of all of your programs, put your name and a brief description of the program.
Example:
/*Sally Smith
This program will calculate area of a
triangle, etc...*/
Add many comments to the body of your code so as to demonstrate a thorough understanding of your program.
SECTION 1: Rectangle.java
Write a class called Rectangle. It should have length and width attributes that default to 1 if they are not set. It should have appropriate get and set methods for getting and setting the attributes. It should also have a perimeter() method that returns the perimeter of the rectangle. It should also have an area() method that returns the area of the rectangle. It should also have a getType() method that either returns a String with the value of "Rectangle" or prints that string value out.
SECTION 2: Square.java
Write a class called Square that is a child class of Rectangle. A square is a rectangle with equal sides. It should inherit the perimeter() and area() methods. It should also inherit the get and set methods for each attribute, if that is appropriate. The Square class should override the getType() method from the superclass so that it returns or outputs "Square."
SECTION 3: TestDriver.java
Write a class called TestDriver that demonstrates all of the code in your Rectangle class and your Square class. The TestDriver class should do the following:
Create a Rectangle and a Square.
Go into a loop where it asks the user if they want a Rectangle or Square.
Read user answer.
If user wants to quit, then exit the program.
If user wants a Rectangle, ask user for width and length. If user wants a Square, ask user for the length of one side.
Display the type (Rectangle or Square), the length, width, area, and perimeter.