Assignment   3 SOLUTION

The goal of this assignment is to gain basic Object-­‐Oriented Programming experience working with Java classes and interfaces. For this assignment, you’ll implement a hierarchy of Java classes that share a common interface. You’ll then be able to create a data structure using this interface type and populate it with various instances of classes inheriting from the interface.
1. [10] Implement a new interface named AreaMeasurable with a single method named getArea() which takes no arguments and returns a double.
2. [10] Implement a new class named Circle which implements AreaMeasurable. Provide a class constructor which takes arguments appropriate for creating a 2D circle and implement the getArea() method to return the area of the circle.
3. [10] Implement a new class named Rectangle which implements AreaMeasurable. Provide a class constructor which takes arguments appropriate for creating a 2D rectangle and implement the getArea() method to return the area of the rectangle.
4. [10] Implement a new class named Square which extends Rectangle. Provide a class constructor which takes arguments appropriate for creating a 2D.
5. [10] Implement a new class named Sphere which implements AreaMeasurable. Provide a class constructor which takes arguments appropriate for creating a 3D sphere and implement the getArea() method to return the surface area of the sphere.
6. [10] Implement a new class named Box which implements AreaMeasurable. Provide a class constructor which takes arguments appropriate for creating a 3D box and implement the getArea() method to return the total surface area of the box.
7. [10] Implement a new class named Cube which extends Box. Provide a class constructor which takes arguments appropriate for creating a 3D cube.
8. [30] Implement a new class named Main with a public static main() method and private static nextRandomDouble() and calculateSum() methods: • (10) The nextRandomDouble () method should simply return a double on the range (0.0, 1.0] (i.e., 0.0 exclusive to 1.0 inclusive). Hint: see java.util.Random nextDouble and java.lang.Double MIN_VALUE. • (10) The calculateSum () method should take an ArrayList of type AreaMeasurable as an argument and return the sum of all areas in the list. • (10) The main() method will need to take a String[] as an argument (as usual). The method should create an ArrayList of type AreaMeasurable and populate that list with 1000 random instances of your AreaMeasurable classes from parts 2-­‐7 (i.e., 1-­‐in-­‐6 chance of creating an instance of the six classes). Each AreaMeasurable should be created with random dimensions using the nextRandomDouble() method above. Finally call the calculateSum() method and print the result. Your output should look like: circles: 180 rects: 168 squares: 148 spheres: 163 boxes: 163 cubes: 178 sum: 1587.7837804408477 Zip the Assignment3 folder in your Eclipse workspace directory and upload the .zip file to Blackboard (see Assignment 3 assignment in the Assignments area).
Powered by