Answered step by step
Verified Expert Solution
Question
1 Approved Answer
NOTE : Make sure to answer each exercise independent to the others. If a problem asks to modify the solution of a previous problem
Exercise 1. (10 pts) Create a Square class, which declares a side instance variable of type int. Then create a public method called perimeter () that returns the perimeter of the square, and a public area () method that returns the area of the square. i Remember that the perimeter is the sum of the sides of the square, while the area is calculated by multiplying the side by itself. Finally, the symbol to perform a multiplication in Java is the asterisk *. Exercise 2. (10 pts) Create a SquareTest class that contains a main () method that instantiates an object of type Square, with side of value 5. Then print the perimeter and the area of the object just created. Exercise 3. (10 pts) After doing the previous exercise, you should have set the variable side with a statement like the following: objectName.side = 5; To avoid to write this statement, create a constructor in the Square class of the Exercise 1, which takes the value of the variable side as input. Once done, compile the Square class. The SquareTest class, on the other hand, will no longer compile due to the instruction specified above and the non-use of the new constructor. Modify the code of the SquareTest class so that it compiles and runs correctly. Exercise 4. (10 pts) In the Square class created in the Exercise 1, replace the value 4 used to calculate the perimeter, with an instance constant named SIDES_NUMBER. This should not affect the SquareTest class. i Note that for the constant, a name consisting of only uppercase letters separated with an underscore symbol was used. This is a convention that is used for all constants.
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