In: Computer Science
Java Solution
Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which level of the hierarchy and what can be shared. Write an appropriate main to test your classes and demonstrate functionality.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// Shape.java
public class Shape {
// Declaring instance variables
private String name;
private int x; // x coordinate
private int y; // y coordinate
// Parameterized constructor
public Shape(String name, int x, int y) {
this.name = name;
this.x=x;
this.y=y;
}
/**
* @return the x
*/
public int getX() {
return x;
}
/**
* @param x
* the x to set
*/
public void setX(int x) {
this.x = x;
}
/**
* @return the y
*/
public int getY() {
return y;
}
/**
* @param y
* the y to set
*/
public void setY(int y) {
this.y = y;
}
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return getName() + ": [" + getX() +
"," + getY() + "}";
}
}
=======================================
=======================================
// TwoDimensionalShape.java
public abstract class TwoDimensionalShape extends Shape {
public TwoDimensionalShape(String name,
int x, int y) {
super(name, x, y);
}
public abstract double calcArea();
}
=======================================
=======================================
// ThreeDimensionalShape.java
public abstract class ThreeDimensionalShape extends Shape {
public ThreeDimensionalShape(String
name, int x, int y) {
super(name, x, y);
}
public abstract double calcSurfaceArea();
public abstract double
calcVolume();
}
=======================================
=======================================
// Circle.java
public class Circle extends TwoDimensionalShape {
// Declaring instance variables
private double radius;
// Parameterized constructor
public Circle(String name, int x, int y, double
radius) {
super(name, x, y);
this.radius = radius;
}
// This method will calculate the area
of the Circle
@Override
public double calcArea() {
return Math.PI * radius *
radius;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return super.toString() + "
radius=" + Math.round(radius)+"\nCircle's area is
"+Math.round(calcArea())+"\n";
}
}
=======================================
=======================================
// Square.java
public class Square extends TwoDimensionalShape {
// Declaring instance variables
private double side;
// Parameterized constructor
public Square(String name, int x, int y, double side)
{
super(name, x, y);
this.side = side;
}
// This method will calculate the area
of the Square
@Override
public double calcArea() {
return side * side;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return super.toString() + "
Radius:" + Math.round(side) + "\n" + super.getName()+"'s area is "
+ Math.round(calcArea())+"\n";
}
}
=======================================
=======================================
// RectangularPrism.java
public class RectangularPrism extends
ThreeDimensionalShape {
private double length;
private double width;
private double height;
/**
* @param name
* @param x
* @param y
* @param length
* @param width
* @param height
*/
public RectangularPrism(String name, int x, int y,
double length,
double width,
double height) {
super(name, x, y);
this.length = length;
this.width = width;
this.height = height;
}
/**
* @return the length
*/
public double getLength() {
return length;
}
/**
* @param length the length to set
*/
public void setLength(double length) {
this.length = length;
}
/**
* @return the width
*/
public double getWidth() {
return width;
}
/**
* @param width the width to set
*/
public void setWidth(double width) {
this.width = width;
}
/**
* @return the height
*/
public double getHeight() {
return height;
}
/**
* @param height the height to set
*/
public void setHeight(double height) {
this.height = height;
}
@Override
public double calcSurfaceArea() {
return 2 * ((length * width) +
(width * height) + (height * length));
}
@Override
public double calcVolume() {
return (length * width *
height);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return super.toString()+" Length:"
+ Math.round(length) + " Width:"+Math.round(width)+"
Height:"+Math.round(height) +"\n"+ super.getName()
+ "'s area is " + Math.round(calcSurfaceArea())
+ "\n" + super.getName()
+ "'s volume is " + Math.round(calcVolume()) +
"\n";
}
}
=======================================
=======================================
// Cube.java
public class Cube extends RectangularPrism {
// Parameterized constructor
public Cube(String name, int x, int y, double side)
{
super(name, x,
y,side,side,side);
}
// This method will calculate the area
of the Cube
@Override
public double calcSurfaceArea() {
return
super.calcSurfaceArea();
}
// This method will calculate the
volume of the Cube
@Override
public double calcVolume() {
return super.calcVolume();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Cube:["+getX()+","+getY() +
"] side:" + Math.round(super.getLength()) + "\n" +
super.getName()
+ "'s area is " + Math.round(calcSurfaceArea())
+ "\n" + super.getName()
+ "'s volume is " + Math.round(calcVolume()) +
"\n";
}
}
=======================================
=======================================
// Sphere.java
public class Sphere extends ThreeDimensionalShape {
// Declaring instance variables
private double raidus;
// Parameterized constructor
public Sphere(String name, int x, int y, double
raidus) {
super(name, x, y);
this.raidus = raidus;
}
// This method will calculate the area
of the Sphere
@Override
public double calcSurfaceArea() {
return 4 * Math.PI * raidus *
raidus;
}
// This method will calculate the
volume of the Sphere
@Override
public double calcVolume() {
return (4.0 / 3.0) * Math.PI *
Math.pow(raidus, 3);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return super.toString() + "
radius:" + Math.round(raidus) + "\n"
+ super.getName() + "'s area is " +
Math.round(calcSurfaceArea())
+ "\nSphere's volume is " +
Math.round(calcVolume()) + "\n";
}
}
=======================================
=======================================
// Test.java
public class Test {
public static void main(String[] args) {
Shape shapes[] =
{
new Circle("Circle", 22, 88, 4),
new Square("Square", 71, 96,
10),
new Sphere("Sphere", 8, 89, 2),
new RectangularPrism("Rectangular Prism", 3, 4,
4, 5, 7),
new Cube("Cube", 79, 61, 8) };
for (int i = 0; i < shapes.length; i++) {
System.out.println(shapes[i]);
}
}
}
=======================================
=======================================
Output:
=====================Could you plz rate me well.Thank You