Question
Can anyone help me with this? Write an abstract Java class Shape that represents a geometric shape, and create concrete (ie, non-abstract) subclasses Circle and
Can anyone help me with this?
Write an abstract Java class Shape that represents a geometric shape, and create concrete (ie, non-abstract) subclasses Circle and Rectangle. These classes should all be immutable. All shapes must support the fol- lowing methods: Double perimeter() Returns the perimeter of the shape. For circles, this is the circumference. Double area() Returns the area of the shape. String toString() Returns a string showing the constructor used to con- struct the object (that is, it returns a string of the class name, an open parenthesis, the argument(s) to the constructor separated by commas, and a close parenthesis. Do not include any whitespace. The Circle class should have a constructor that takes the radius as input; the Rectangle class's constructor should take a width and height as input, in that order. All these arguments should be Doubles. The key requirement is that Shape should be a Java type that supports the three methods above. For example, it must be possible to do this: Shape shape = new Circle(1.0); System.out.println("Area of unit circle is " + shape.area());
Note that the class of the shape variable is Shape. Declaring it to be of type Circle is not good enough. Reminder: The formulas for perimeter and area of these shapes are: Shape Perimeter Area Circle(r) 2r r^2 Rectangle(w,h) 2w + 2h wh
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