Write a simple Java class, Square to define a geometric Square shape. As with any other class,
Question:
Write a simple Java class, Square to define a geometric Square shape.
As with any other class, you need to set up:
Instance variables: What are the basic properties or characteristics of a square?
Instance methods: What actions can a square take? For the purposes of this exercise, define a method to compute the area and one to compute the
perimeter of the square. Of course, do not forget the toString() method! which is used to display the results.
A main() method for testing purposes.
Task 2: Write the Main Method to Test Each Shape Class
The Shape class provides the base for the hierarchy of geometric shapes. Create the Circle class. Then add a main method, create some Circle objects, and test the instance methods. Below is some sample output:
java Circle Testing toString():
dot1 = circle of area 28.274 (radius: 3.0)
dot2 = circle of area 105.683 (radius: 5.8)
dot3 = circle of area 0 (radius: 0.0)
Testing area():
dot1 area (expected: 28.27) Got: 28.274333882308138
dot2 area (expected: 105.68) Got: 105.68317686676063
dot3 area (expected: 0.0) Got: 0.0
Testing perimeter():
dot1 perimeter (expected:18.85 ) Got: 18.84955592153876
dot2 perimeter (expected: 36.44) Got: 36.4424747816416
dot3 perimeter (expected: 0.0) Got: 0.0
Write main methods to thoroughly test the Rectangle and the Triangle classes.
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch