Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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


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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

1 2 3 4 5 6 7 8 9 Squarejava Java public class Square private double side public Squaredouble side thisside side public double getSide return side pub... 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

Java An Introduction To Problem Solving And Programming

Authors: Walter Savitch

8th Edition

0134462033, 978-0134462035

More Books

Students also viewed these Programming questions