Question: 1) Create java classes for concrete geometric shapes such as Circle, Rectangle, Triangle, etc. with methods and members to allow the calculation of area and
1) Create java classes for concrete geometric shapes such as Circle, Rectangle, Triangle, etc. with methods and members to allow the calculation of area and perimeter of the shape. The concrete shapes will implement the interface Shape. Use encapsulation such that primitive members are private and accessed using getters. Class attributes may be set using constructors or setters.
Your shapes should of type Shape and implement the (abstract) Shape interface.
interface Shape { public double area(); public double perimeter(); }
Example:
class Circle implements Shape {
// Add implementation details here }
You should create a main class called TestShape to test your calculations. Inside this class, concrete shape classes should be assigned to type Shape as follows (not the shape subclass):
Shape circle = new Circle(radius);
That is, don't do this: // Don't do this: Circle circle = new Circle(radius);
2) Now create an ArrayList and add your shapes to the ArrayList. Notice the use of the generic
ArrayList
3) In your main class or in a method called from main, iIterate over the results, printing the className of your shape followed by the area and perimeter calculations, e.g. as follows using a for, or foreach loop:
// Limit precision to two decimal points. DecimalFormat df = new DecimalFormat(); df.setMaximumFractionDigits(2);
for (Shape shape:shapes) { System.out.println(shape.getClass().getSimpleName() + " area: " + df.format(shape.area())); System.out.println(shape.getClass().getSimpleName() + " perimeter " + df.format(shape.perimeter())); }
or using an iterator:
System.out.println(" "); ListIterator
Note that the println statement prints the class name of the concrete Shape class, e.g. Rectangle, Triangle, Circle.
If you still a little confused, you can download a starter file here. You will need to fill in the sections indicated by "Implement me".
Submission: Submit working source code with no compilation errors, plus a screenshot of test result output.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
