Question
Java Lab: In this lab you will be provided with a set of classes that exist in an inheritance hierarchy at which the top is
Java Lab:
In this lab you will be provided with a set of classes that exist in an inheritance hierarchy at which the top is the Shape class. You will create a class that is a collection of Shapes. It will be able to hold all types of Shapes. A test program is provided.
Concepts Covered
Polymorphism
Abstract Classes
Abstract Methods
Interfaces
Arrays of Reference Types
Down-Casting
instanceof Operator
Create a new project called Lab6Polymorphism. In this project create a package called "shapes". This is where all of the Shape classes will go.
Shape.java
package shapes;
/**
* Abstract class for the minimal requirements of a Shape.
*/
public abstract class Shape
{
/**
* Area of a two-dimensional shape.
*/
private double area;
/**
* Sets the area of the Shape.
* @param areaIn Area of the Shape.
*/
public void setArea(double areaIn)
{
area = areaIn;
}
/**
* Retrieves the area of the Shape.
* @return Area of the Shape.
*/
public double getArea()
{
return area;
}
/**
* Retrieves the number of dimensions of the shape.
* @return Dimensions of the shape.
*/
public abstract int getDimensions();
}
Rectangle.java
package shapes;
/**
* Represents a Rectangle which inherits from the abstract Shape class.
*/
public class Rectangle extends Shape
{
/**
* Length of the Rectangle.
*/
private double length;
/**
* Width of the Rectangle.
*/
private double width;
/**
* Constructor of Rectangle, accepts length and width of the Rectangle.
* @param lengthIn Length of the Rectangle.
* @param widthIn Width of the Rectangle.
*/
public Rectangle(double lengthIn, double widthIn)
{
setDimensions(lengthIn, widthIn);
}
/**
* Sets Rectangle's Length.
* @param lengthIn Length of the Rectangle.
*/
public void setLength(double lengthIn)
{
length = lengthIn;
}
/**
* Sets Rectangle's Width.
* @param widthIn Width of the Rectangle.
*/
public void setWidth(double widthIn)
{
width = widthIn;
}
/**
* Sets length, width, and area of the Rectangle.
* @param lengthIn Length of the Rectangle.
* @param widthIn Width of the Rectangle.
*/
public void setDimensions(double lengthIn, double widthIn)
{
setLength(lengthIn);
setWidth(widthIn);
setArea(lengthIn * widthIn);
}
/**
* Retrieves the length of the Rectangle.
* @return Length of the Rectangle.
*/
public double getLength()
{
return length;
}
/**
* Retrieves the width of the Rectangle.
* @return Width of the Rectangle.
*/
public double getWidth()
{
return width;
}
@Override
/**
* Retrieves number of dimensions of the Rectangle.
* @return Returns 2.
*/
public int getDimensions()
{
return 2;
}
/**
* Returns a formatted String representation of the Rectangle.
* @return String representation of the Rectangle.
*/
public String toString()
{
return String.format("This Rectangle is %.2f x %.2f", getLength(), getWidth());
}
}
Circle.java
package shapes;
/**
* Represents a Circle which inherits from the abstract Shape class.
*/
public class Circle extends Shape
{
/**
* Radius of the Circle.
*/
private double radius;
/**
* Circle constructor, accepts radius of the Circle.
* @param radiusIn Radius of the Circle.
*/
public Circle(double radiusIn)
{
setRadius(radiusIn);
}
/**
* Sets the radius and area of the Circle.
* @param radiusIn Radius of the Circle.
*/
public void setRadius(double radiusIn)
{
radius = radiusIn;
setArea(Math.PI * radius * radius);
}
/**
* Retrieves the radius of the Circle.
* @return Radius of the Circle.
*/
public double getRadius()
{
return radius;
}
@Override
/**
* Retrieves number of dimensions of the Circle.
* @return Returns 2.
*/
public int getDimensions()
{
return 2;
}
/**
* Returns a formatted String representation of the Circle.
* @return String representation of the Circle.
*/
public String toString()
{
return String.format("This Circle has a radius of %.2f", getRadius());
}
}
Cube.java
package shapes;
/**
* Represents a Cube which inherits from the abstract Shape class and implements ThreeDimensionalShape interface.
*/
public class Cube extends Rectangle implements ThreeDimensionalShape
{
/**
* Height of the Cube.
*/
private double height;
/**
* Constructor of Cube, accepts all Cube dimensions.
* @param lengthIn Length of the Cube.
* @param widthIn Width of the Cube.
* @param heightIn Height of the Cube.
*/
public Cube(double lengthIn, double widthIn, double heightIn)
{
super(lengthIn, widthIn);
setHeight(heightIn);
}
/**
* Sets Cube's height.
* @param heightIn Height of the Cube.
*/
public void setHeight(double heightIn)
{
height = heightIn;
}
/**
* Sets the length, width, and height of the Cube.
* @param lengthIn Length of the Cube.
* @param widthIn Width of the Cube.
* @param heightIn Height of the Cube.
*/
public void setDimensions(double lengthIn, double widthIn, double heightIn)
{
setDimensions(lengthIn, widthIn);
setHeight(heightIn);
}
/**
* Retrieves the height of the Cube.
* @return Height of the Cube.
*/
public double getHeight()
{
return height;
}
@Override
/**
* Calculates and returns the volume of the Cube.
* @return Volume of the Cube.
*/
public double getVolume()
{
return getLength() * getWidth() * getHeight();
}
@Override
/**
* Calculates and returns the surface area of the Cube.
* @return Surface area of the Cube.
*/
public double getSurfaceArea()
{
//Magic number for calculating surface area of a cube.
return getArea() * 6;
}
@Override
/**
* Retrieves number of dimensions of the Cube.
* @return Returns 3.
*/
public int getDimensions()
{
//Magic number for shape with 3 dimensions.
return 3;
}
/**
* Returns a formatted String representation of the Cube.
* @return String representation of the Cube.
*/
public String toString()
{
return String.format("This Cube is %.2f x %.2f x %.2f", getLength(), getWidth(), getHeight());
}
}
Sphere.java
package shapes;
/**
* Represents a Sphere which inherits from the abstract Shape class and implements ThreeDimensionalShape interface.
*/
public class Sphere extends Circle implements ThreeDimensionalShape
{
/**
* Constructor of Sphere, accepts radius of Sphere.
* @param radiusIn Radius of Sphere.
*/
public Sphere(double radiusIn)
{
super(radiusIn);
}
@Override
/**
* Calculates and returns the volume of the Sphere.
* @return Volume of the Sphere.
*/
public double getVolume()
{
//Magic numbers used for calculating volume of sphere.
return 4.0 / 3.0 * Math.PI * Math.pow(getRadius(), 3);
}
@Override
/**
* Calculates and returns the surface area of the Sphere.
* @return Surface area of the Sphere.
*/
public double getSurfaceArea()
{
//Magic numbers used for calculating surface area of sphere.
return 4.0 * Math.PI * Math.pow(getRadius(), 2);
}
@Override
/**
* Retrieves number of dimensions of the Sphere.
* @return Returns 3.
*/
public int getDimensions()
{
return 3;
}
/**
* Returns a formatted String representation of the Sphere.
* @return String representation of the Sphere.
*/
public String toString()
{
return String.format("This Sphere has a radius of %.2f", getRadius());
}
}
ThreeDimensionalShape.java
package shapes;
/**
* Interface used as an extension of a basic two dimensional shape.
*/
public interface ThreeDimensionalShape
{
/**
* Calculates and returns the volume of the 3-D shape.
* @return Volume of the Shape.
*/
double getVolume();
/**
* Calculates and returns the surface area of the 3-D shape.
* @return Surface area of the Shape.
*/
double getSurfaceArea();
}
Create a package called "collections". In this package create the ShapeCollection class and write the class. This class will have an array of Shapes, which due to Polymorphism will be able to hold anything that has an "IS-A" relationship with Shape. You will create methods that add different types of shapes to the collection as well as methods that access all the shapes and some that only act on some of the shapes. Recall that the instanceof operator and down-casting will allow to "program in the specific".
Your ShapeCollection will be tested with a provided test driver linked below. Place this file in the default package.
ShapeCollectionTest.java
import shapes.Cube;
import collections.ShapeCollection;
/**
* Test class for ShapeCollectionTests. Adds Shapes and performs tests to ensure the ShapeCollection functions.
*/
public class ShapeCollectionTest
{
/**
* Main method of test class. Creates an instance of ShapeCollection class and tests public methods.
* @param args Not used.
*/
public static void main(String[] args)
{
ShapeCollection shapes = new ShapeCollection();
//Add some Shapes
addShapesToCollection(shapes);
//Print the current five Shapes
System.out.println("***Printing Current Shapes***");
shapes.printAllShapes();
//Add some more Shapes
addShapesToCollection(shapes);
//Print the current ten Shapes
System.out.println(" ***Printing Current Shapes***");
shapes.printAllShapes();
//Print only the Circles
System.out.println(" ***Printing Circles***");
shapes.printCircles();
//Print only the Rectangles
System.out.println(" ***Printing Rectangles***");
shapes.printRectangles();
//Print only the Spheres
System.out.println(" ***Printing Spheres***");
shapes.printSpheres();
//Print only the Cubes
System.out.println(" ***Printing Cubes***");
shapes.printCubes();
//Try to add 11th Shape
System.out.println(" ***Attempt to add too many Shapes***");
addShapesToCollection(shapes);
//Test Area of 2-D Shapes
System.out.printf(" Sum of areas of 2-D Shapes: %.2f", shapes.totalAreaOf2DShapes());
//Test Surface Area of 3-D Shapes
System.out.printf(" Sum of surface areas of 3-D Shapes: %.2f", shapes.totalSurfaceAreaOf3DShapes());
//Test Volume of 3-D Shapes
System.out.printf(" Sum of volumes of 3-D Shapes: %.2f", shapes.totalVolumeOf3DShapes());
}
/**
* Adds predefined Shapes to the given ShapeCollection for testing purposes.
* Uses the boolean return of the add methods and reports if the Shape was not added due to size restrictions.
* Use of Magic numbers for predefined Shapes for testing purposes.
* @param shapes ShapeCollection to which the Shapes will be added.
*/
public static void addShapesToCollection(ShapeCollection shapes)
{
if (!shapes.addNewCircle(5.7))
{
System.out.println("New Circle not added!");
}
if (!shapes.addNewRectangle(7.1, 3.7))
{
System.out.println("New Rectangle not added!");
}
if (!shapes.addNewSphere(9.2))
{
System.out.println("New Sphere not added!");
}
if (!shapes.addNewCube(1.0, 2.6, 3.6))
{
System.out.println("New Cube not added!");
}
if (!shapes.addShape(new Cube(4.4, 4.2, 2.8)))
{
System.out.println("Shape not added!");
}
}
}
The test program will run a number of tests on your classes. The exact expected output is shown below.
EXPECTED OUTPUT
***Printing Current Shapes*** This Circle has a radius of 5.70 This Rectangle is 7.10 x 3.70 This Sphere has a radius of 9.20 This Cube is 1.00 x 2.60 x 3.60 This Cube is 4.40 x 4.20 x 2.80
***Printing Current Shapes*** This Circle has a radius of 5.70 This Rectangle is 7.10 x 3.70 This Sphere has a radius of 9.20 This Cube is 1.00 x 2.60 x 3.60 This Cube is 4.40 x 4.20 x 2.80 This Circle has a radius of 5.70 This Rectangle is 7.10 x 3.70 This Sphere has a radius of 9.20 This Cube is 1.00 x 2.60 x 3.60 This Cube is 4.40 x 4.20 x 2.80
***Printing Circles*** This Circle has a radius of 5.70 This Sphere has a radius of 9.20 This Circle has a radius of 5.70 This Sphere has a radius of 9.20
***Printing Rectangles*** This Rectangle is 7.10 x 3.70 This Cube is 1.00 x 2.60 x 3.60 This Cube is 4.40 x 4.20 x 2.80 This Rectangle is 7.10 x 3.70 This Cube is 1.00 x 2.60 x 3.60 This Cube is 4.40 x 4.20 x 2.80
***Printing Spheres*** This Sphere has a radius of 9.20 This Sphere has a radius of 9.20
***Printing Cubes*** This Cube is 1.00 x 2.60 x 3.60 This Cube is 4.40 x 4.20 x 2.80 This Cube is 1.00 x 2.60 x 3.60 This Cube is 4.40 x 4.20 x 2.80
***Attempt to add too many Shapes*** New Circle not added! New Rectangle not added! New Sphere not added! New Cube not added! Shape not added!
Sum of areas of 2-D Shapes: 256.68 Sum of surface areas of 3-D Shapes: 2380.20 Sum of volumes of 3-D Shapes: 6645.73
OVERVIEW PACKAGE CLASS USE TREE DEPRECATED INDEX HELP PREV CLASS NEXT CLASS SUMMARY: NESTED | FIELD CONSTR | ETHOD DETAIL: FIELD CONSTR I METHOD FRAMES NO FRAMES ALL CLASSES collections Class ShapeCollection java.lang.Object collections.ShapeCollection public class ShapeCollection extends java.lang.object A class that stores a collection of all different kinds of Shapes in a single array Field Summary Fields Modifier and Type private Shape[ al1shapes Field and Description Array that stores the Shapes in the collection currentNumberOfShapes Count of how many shapes are currently in the array private int private static int MAXIMUMSHAPES Maximum number of shapes that can be stored in the collection. Constructor Summary Constructors Constructor and Description ShapeCollection () Constructor of ShapeCollection Method Summary All Methods Instance Methods Concrete Methods
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