ps://mycourselink lakeheadu.ca/d2/le/content/47992/viewcontent/550919/View Desig
ID: 3881455 • Letter: P
Question
ps://mycourselink lakeheadu.ca/d2/le/content/47992/viewcontent/550919/View Design and implementation of a Shape hierarchy using Ohject Oriented principles Implement the Shape hierarchy shown in the UML class diagram below. Each TwoDimensionalShape should contain polymorphic methods getArea and getPerimeter to calculate the area and perimeter of the two-dimensional shape. Each ThreeDimensionalShape should have polymorphic methods getArea and getVolume to calculate the surface area and volume of the three-dimensional shape. Create a program that uses an array of Shape references to objects of each concrete class in the hierarchy. The program should print a text description of the object to which each array element refers. Also, in the loop that processes all the shapes in the array, determine whether each shape is a TwoDimensionalShape or a ThreeDimensionalShape. If it's a TwoDimensionalShape, display its area and perimeter. If it's a ThreeDimensionalShape, display its area and volume. shape ThreeDimensionalshape TwoDimensionalshape 1 112Explanation / Answer
Note : I have to write the test class for these classes.
_____________
Shape.java
public class Shape {
//Declaring instance variables
private String name;
//Parameterized constructor
public Shape(String name) {
this.name = name;
}
//getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Shape is "+name;
}
}
________________
public abstract class TwoDimensionalShape extends Shape {
public TwoDimensionalShape(String name) {
super(name);
}
public abstract double getArea();
public abstract double getPerimeter();
}
_____________________
public class Circle extends TwoDimensionalShape {
//Declaring instance variables
private double radius;
//Parameterized constructor
public Circle(String name, double radius) {
super(name);
this.radius = radius;
}
//This method will calculate the area of the Circle
@Override
public double getArea() {
return Math.PI*radius*radius;
}
//This method will calculate the perimeter of the Circle
@Override
public double getPerimeter() {
return 2*Math.PI*radius;
}
}
__________________
public class Square extends TwoDimensionalShape {
//Declaring instance variables
private double side;
//Parameterized constructor
public Square(String name,double side) {
super(name);
this.side=side;
}
//This method will calculate the area of the Square
@Override
public double getArea() {
return side*side;
}
//This method will calculate the perimeter of the Square
@Override
public double getPerimeter() {
return 4*side;
}
}
_________________
public class Triangle extends TwoDimensionalShape {
//Declaring instance variables
private double base;
private double height;
//Parameterized constructor
public Triangle(String name, double base, double height) {
super(name);
this.base = base;
this.height = height;
}
//This method will calculate the area of the Triangle
@Override
public double getArea() {
return 0.5*base*height;
}
//This method will calculate the Perimeter of the Triangle
@Override
public double getPerimeter() {
return base+height+Math.sqrt((base*base)+(height*height));
}
}
________________
public abstract class ThreeDimensionalShape extends Shape {
public ThreeDimensionalShape(String name) {
super(name);
}
public abstract double getArea();
public abstract double getVolume();
}
________________
public class Cube extends ThreeDimensionalShape {
//Declaring instance variables
private double side;
//Parameterized constructor
public Cube(String name, double side) {
super(name);
this.side = side;
}
//This method will calculate the area of the Cube
@Override
public double getArea() {
return 6*side*side;
}
//This method will calculate the volume of the Cube
@Override
public double getVolume() {
return Math.pow(side,3);
}
}
_______________
public class Sphere extends ThreeDimensionalShape {
//Declaring instance variables
private double raidus;
//Parameterized constructor
public Sphere(String name, double raidus) {
super(name);
this.raidus = raidus;
}
//This method will calculate the area of the Sphere
@Override
public double getArea() {
return 4*Math.PI*raidus*raidus;
}
//This method will calculate the volume of the Sphere
@Override
public double getVolume() {
return (4.0/3.0)*Math.PI*Math.pow(raidus,3);
}
}
_________________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.