Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming - Please look at the sample run!! Implement the Shape hierarchy

ID: 3906700 • Letter: J

Question

Java Programming - Please look at the sample run!!

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 class should have a member variable containing its dimensions -- for example, the Circle class should have a member variable describing its radius, while the Triangle class should have three member variables describing the length of each side. Note that the Tetrahedron cass should describe a regular tetrahedron, and as such, should only have one member variable.

Create a Driver class with a main method to test your Shape hierarchy. The program should prompt the user to enter the type of shape they'd like to create, and then the shape's dimensions. If the shape is two dimensional, the program should print its area and its perimeter, and if it's a three dimensional shape, its surface area and volume.

SAMPLE RUN #1: java Driver

Enter?

1)Two·dimensional·shape?

2)Three·dimensional·shape:1?

Enter?

1)Circle?

2)Square?

3)Triangle:1?

Enter·radius·of·circle:1.56?

Area:·7.65·?

Perimeter:·9.80?

Explanation / Answer

Screenshot

---------------------------------------------------------------------

Program

//Package for I/O
import java.text.DecimalFormat;
import java.util.*;
//Create an abstract class
abstract class Shape{
   //Abstract method , you have to implement all extended classes
   abstract void getArea();
   //two member functions
   void getPerimeter() {
   }
    void getVolume() {
      
   }
}
//create a class two dimension which extends shape
class TwoDimension extends Shape{
   //Two member functions
   void getArea() {
   }
   void getPerimeter() {
      
   }
}
//create a class three dimension which extends shape
class ThreeDimension extends Shape{
   //Two member functions
   void getArea() {
   }
   void getVolume() {
      
   }
}
//Inherit twodimension class and redefine it's member functions
class circle extends TwoDimension{
   double radius;
   DecimalFormat df=new DecimalFormat("#.##");
   circle(double radius){
       this.radius=radius;
   }
   void getArea() {
       System.out.println("Circle Area="+df.format((3.14*(radius*radius))));
   }
    void getPerimeter() {
       System.out.println("Circle Perimeter="+df.format((2*3.14*radius)));
   }

}
class square extends TwoDimension{
   double side;
   DecimalFormat df=new DecimalFormat("#.##");
   square(double side){
       this.side=side;
   }
   void getArea() {
       System.out.println("Square Area="+df.format((side*side)));
   }
    void getPerimeter() {
       System.out.println("Square Perimeter="+df.format((4*side)));
   }

}
class triangle extends TwoDimension{
   double side1,side2,side3;
   DecimalFormat df=new DecimalFormat("#.##");
   triangle(double side1,double side2,double side3){
       this.side1=side1;
       this.side2=side2;
       this.side3=side3;
   }
   void getArea() {
       double s=(side1+side2+side3)/2;
       double a=Math.sqrt(s*(s - side1)* (s - side2)* (s - side3));
       System.out.println("Square Area="+df.format(a));
   }
    void getPerimeter() {
       System.out.println("Square Perimeter="+df.format((side1+side2+side3)));
   }

}
//Inherit three dimension class and redefine it's member functions
class sphere extends ThreeDimension{
   double radius;
   DecimalFormat df=new DecimalFormat("#.##");
   sphere(double radius){
       this.radius=radius;
   }
   void getArea() {
       System.out.println("Shere Area="+df.format((4*3.14*(radius*radius))));
   }
    void getVolume() {
       System.out.println("Shere Volume="+df.format(((4/3)*3.14*(radius*radius*radius))));
   }

}
class cube extends ThreeDimension{
   double side;
   DecimalFormat df=new DecimalFormat("#.##");
   cube(double side){
       this.side=side;
   }
   void getArea() {
       System.out.println("Cube Area="+df.format(6*(side*side)));
   }
    void getVolume() {
       System.out.println("Cube Volume="+df.format((side*side*side)));
   }

}
class tetrahedron extends ThreeDimension{
   double side;
   DecimalFormat df=new DecimalFormat("#.##");
   tetrahedron(double side){
       this.side=side;
   }
   void getArea() {
       System.out.println("Cube Area="+df.format(((Math.sqrt(3))*(side*side))));
   }
    void getVolume() {
       System.out.println("Cube Volume="+df.format((((Math.sqrt(3))/12)*(side*side*side))));
   }

}
//Driver class
public class JavaDriverClass {

   public static void main(String[] args) {
       //scanner for Input read
       Scanner sc=new Scanner(System.in);
       //Menu choices
       System.out.println("Enter?");
       System.out.println("1)Two·dimensional·shape? 2)Three·dimensional·shape?:");
       int choice=sc.nextInt();
       //If two dimension selcect again prompt different two dimensional shapes selection
       if(choice==1) {
           System.out.println("Enter?");
           System.out.println("1)Circle? 2)Square? 3)Triangle?:");
           int select=sc.nextInt();
           //According selection call area and perimeter of correspoding shape
           switch(select) {
           case 1:
               System.out.println("Please enter the radius of the circle:");
               double r=sc.nextDouble();
               Shape cir=new circle(r);
               cir.getArea();
               cir.getPerimeter();
               break;
           case 2:
               System.out.println("Please enter the side length of the square:");
               double side=sc.nextDouble();
               Shape sqr=new square(side);
               sqr.getArea();
               sqr.getPerimeter();
               break;
           case 3:
               System.out.println("Please enter the sides of the triangle:");
               double side1=sc.nextDouble();
               double side2=sc.nextDouble();
               double side3=sc.nextDouble();
               Shape tri=new triangle(side1,side2,side3);
               tri.getArea();
               tri.getPerimeter();
               break;
           }
       }
       //If three dimension selcect again prompt different two dimensional shapes selection
       else if(choice==2) {
           System.out.println("Enter?");
           System.out.println("1)Shere? 2)Cube? 3)Tetrahedron?:");
           int select=sc.nextInt();
           //According selection call area and volume of correspoding shape
           switch(select) {
           case 1:
               System.out.println("Please enter the radius of the shere:");
               double r=sc.nextDouble();
               Shape sp=new sphere(r);
               sp.getArea();
               sp.getVolume();
               break;
           case 2:
               System.out.println("Please enter the side length of the Cube:");
               double side=sc.nextDouble();
               Shape cu=new cube(side);
               cu.getArea();
               cu.getVolume();
               break;
           case 3:
               System.out.println("Please enter the side of the Tetrahedron:");
               double side1=sc.nextDouble();
               Shape tet=new tetrahedron(side1);
               tet.getArea();
               tet.getVolume();
               break;
           }
       }

   }

}

-----------------------------------------------------------------------------

Output

Enter?
1)Two·dimensional·shape?
2)Three·dimensional·shape?:
1
Enter?
1)Circle?
2)Square?
3)Triangle?:
1
Please enter the radius of the circle:
1.56
Circle Area=7.64
Circle Perimeter=9.8

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote