Question

In: Computer Science

Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...

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.

Solutions

Expert Solution

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


Related Solutions

Create a class (Shapes) that prompts the user to select a shape to be drawn on...
Create a class (Shapes) that prompts the user to select a shape to be drawn on the screen. Objects: Circle, X, box, box with an x inside, or any other object of your choice. Depending on the user’s choice, you will then ask for the number of rows, columns or any requirements needed to draw the shape. Finally, you will display the shape selected by the user to the screen. Your class should have at least one instance variable. Your...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions:...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions: getArea() setArea() printArea() Create classes to inherit from the base class Circle Square Rectangle Both implement the functions derived from the abstract base class AND must have private variables and functions unique to them like double Radius double length calculateArea() Use the spreadsheet info.txt read in information about the circle, rectangle, or square text file: circle   3.5   square   3   rectangle   38   36 circle   23  ...
Both homework are linked. Create a class (Shapes) that prompts the user to select a shape...
Both homework are linked. Create a class (Shapes) that prompts the user to select a shape to be drawn on the screen. Objects: Circle, X, box, box with an x ​​inside, or any other object of your choice. Depending on the user’s choice, you will then ask for the number of rows, columns or any requirements needed to draw the shape. Finally, you will display the shape selected by the user to the screen. Your class should have at least...
(a) Create a Card class that represents a playing card. It should have an int instance...
(a) Create a Card class that represents a playing card. It should have an int instance variable named rank and a char variable named suit. Include the following methods: A constructor with two arguments for initializing the two instance variables. A copy constructor. A method equals — with one argument — which compares the calling object with another Card and returns true if and only if the corresponding ranks and suits are equal. Make sure your method will not generate...
Specifications: This project will have two data classes and a tester class. Design: Create a solution...
Specifications: This project will have two data classes and a tester class. Design: Create a solution for this programming task. You will need to have the following parts: Text file to store data between runs. Two classes that implement the given interfaces. You may add methods beyond those in the interfaces A tester class that tests all of the methods of the data classes. Here are the interfaces for your data classes: package project1; import java.util.ArrayList; public interface Person {...
Specifications: This project will have two data classes and a tester class. Design: Create a solution...
Specifications: This project will have two data classes and a tester class. Design: Create a solution for this programming task. You will need to have the following parts: Text file to store data between runs. Two classes that implement the given interfaces. You may add methods beyond those in the interfaces A tester class that tests all of the methods of the data classes. Here are the interfaces for your data classes: package project1; import java.util.ArrayList; public interface Person {...
Create a Java class named Package that contains the following: Package should have three private instance...
Create a Java class named Package that contains the following: Package should have three private instance variables of type double named length, width, and height. Package should have one private instance variable of the type Scanner named input, initialized to System.in. No-args (explicit default) public constructor, which initializes all three double instance variables to 1.0.   Initial (parameterized) public constructor, which defines three parameters of type double, named length, width, and height, which are used to initialize the instance variables of...
a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields Item Name Item Serial No Item Unit Price Item Stock Level Item Reorder Level It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level. b) Extend the Retail Item class of part a) above...
a) Design a Java Class which represents a Retail Item. It is to have the following fields
 a) Design a Java Class which represents a Retail Item. It is to have the following fields  Item Name  Item Serial No  Item Unit Price  Item Stock Level  Item Reorder Level  It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level.    b) Extend...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT