Question
Part A: a class to represent a Square. This should include the following: A single attribute named length. A default constructor which should set the
Part A:
a class to represent a Square. This should include the following:
A single attribute named length.
A default constructor which should set the length attribute to a default value of 1.
A general constructor which should set the length attribute to a value which is passed to the constructor.
Getter and setter methods for the length attribute.
A method, called getArea(), to calculate and return the area of the Square.
A method called toString() that returns the value of the attribute length (see sample output in bold below for format).
Part B:
a main method in a new class called TestSquare and in this method:
an array containing 4 Square objects.
Use a loop to set each object to default values when created.
Use the setter method on the 1st and 3rd objects in the array of Square objects to set the length to 10.
Print out the length of each of Square object in the array.
Print out the area of the 1st and 2nd objects in the array.
Call the toString() method on the 3rd Square object in the array.
Output
The TestSquare class should print to the screen the correct message to match each question given above.
Sample Output - output should be exactly as below:
Square 1 has a side length of 10
Square 2 has a side length of 1
Square 3 has a side length of 10
Square 4 has a side length of 1
Square 1 has an area of 100
Square 2 has an area of 1
LENGTH: 10
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started