Question

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 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.

Must include:

System.out.print("Enter\n1)Two dimensional shape\n2)Three dimensional shape:");
System.out.print("Enter\n1)Circle\n2)Square\n3)Triangle:");
System.out.print("Enter radius of circle:");
System.out.print("Enter side of square:");
System.out.print("Enter side of triangle:");
System.out.printf("Area: %.2f \nPerimeter: %.2f", shape.getArea(), shape.getPerimeter());
System.out.print("Enter\n1)Sphere\n2)Cube\n3)Tetrahedron:");
System.out.print("Enter radius of sphere:");
System.out.print("Enter side of cube:");
System.out.print("Enter side of tetrahedron:");
System.out.printf("Surface area: %.2f \nVolume: %.2f", shape.getArea(), shape.getVolume());
System.out.print("Enter\n1)Two dimensional shape\n2)Three dimensional shape:");
System.out.print("Enter\n1)Circle\n2)Square\n3)Triangle:");
System.out.print("Enter radius of circle:");
System.out.print("Enter side of square:");
System.out.print("Enter side of triangle:");
System.out.printf("Area: %.2f \nPerimeter: %.2f", shape.getArea(), shape.getPerimeter());
System.out.print("Enter\n1)Sphere\n2)Cube\n3)Tetrahedron:");
System.out.print("Enter radius of sphere:");
System.out.print("Enter side of cube:");
System.out.print("Enter side of tetrahedron:");
System.out.printf("Surface area: %.2f \nVolume: %.2f", shape.getArea(), shape.getVolume());
0 0
Add a comment Improve this question Transcribed image text
Answer #1

*****This requires a lot of effort so please drop a like if you are satisfied with the solution Happy Chegging!!******

main.java code:

import java.util.Scanner;
class Shape{
  
}
class TwoDimensionalShape extends Shape{
double getArea(double r){
return (22.0/7.0)*(r*r);
}
double getArea(double s,String square){
return (s*s);
}
double getArea(double a,double b,double c){
double s=(a+b+c)/(2.0);
return Math.sqrt((s)*(s-a)*(s-b)*(s-c));
}
double getPerimeter(double r){
return (2.0)*(22.0/7.0)*r;
}
double getPerimeter(double s,String square){
return (4.0)*s;
}
double getPerimeter(double a,double b,double c){
return (a+b+c)/(2.0);
}
}
class ThreeDimensionalShape extends Shape{
double getArea(double s){
return (6.0)*s*s;
}
double getArea(double r,String sphere){
return (4.0)*(22.0/7.0)*(r*r);
}
double getAreaTetrahedron(double a){
return Math.sqrt(3)*(a*a);
}
double getVolume(double s){
return s*s*s;
}
double getVolume(double r,String sphere){
return (4.0/3.0)*(22.0/7.0)*(r*r*r);
}
double getVolumeTetrahedron(double a){
return (a*a*a)/((6.0)*(Math.sqrt(2)));
}
}
class Circle extends TwoDimensionalShape{
double radius;
Circle(){
this.radius=0;
}
void setCircle(double r){
this.radius=r;
}
}
class Square extends TwoDimensionalShape{
double side;
Square(){
this.side=0;
}
void setSquare(double s){
this.side=s;
}
}
class Triangle extends TwoDimensionalShape{
double a;
double b;
double c;
Triangle(){
this.a=0;
this.b=0;
this.c=0;
}
void setTriangle(double a,double b,double c){
this.a=a;
this.b=b;
this.c=c;
}
}
class Sphere extends ThreeDimensionalShape{
double radius;
Sphere(){
this.radius=0;
}
void setSphere(double r){
this.radius=r;
}
}
class Cube extends ThreeDimensionalShape{
double side;
Cube(){
this.side=0;
}
void setCube(double s){
this.side=s;
}
}
class RegularTetrahedron extends ThreeDimensionalShape{
double a;
RegularTetrahedron(){
this.a=0;
}
void setRegularTetrahedron(double a){
this.a=a;
}
}
public class Main
{
   public static void main(String[] args) {
   Scanner in=new Scanner(System.in);
       System.out.print("Enter\n1)Two dimensional shape\n2)Three dimensional shape: ");
       int choice=in.nextInt();
       if(choice == 1){
       System.out.print("Enter\n1)Circle\n2)Square\n3)Triangle: ");
       choice=in.nextInt();
       if(choice == 1){
       Circle circle=new Circle();
       System.out.print("Enter radius of circle: ");
       double r=in.nextDouble();
       circle.setCircle(r);
       System.out.printf("Circle Area: %.2f \nCircle Perimeter: %.2f", circle.getArea(circle.radius), circle.getPerimeter(circle.radius));
       }
       else if(choice==2){
       Square square=new Square();
       System.out.print("Enter side of square: ");
       double s=in.nextDouble();
       square.setSquare(s);
       System.out.printf("Square Area: %.2f \nSquare Perimeter: %.2f", square.getArea(square.side,"square"), square.getPerimeter(square.side,"square"));
       }
       else{
       Triangle triangle=new Triangle();
       System.out.print("Enter first side of triangle: ");
       double a=in.nextDouble();
       System.out.print("Enter second side of triangle: ");
       double b=in.nextDouble();
       System.out.print("Enter third side of triangle: ");
       double c=in.nextDouble();
       triangle.setTriangle(a,b,c);
       System.out.printf("Triangle Area: %.2f \nTriangle Perimeter: %.2f", triangle.getArea(triangle.a,triangle.b,triangle.c), triangle.getPerimeter(triangle.a,triangle.b,triangle.c));
       }
       }
       else{
       System.out.print("Enter\n1)Sphere\n2)Cube\n3)Tetrahedron: ");
       int choice2 = in.nextInt();
       if(choice2 == 1){
       Sphere sphere=new Sphere();
       System.out.print("Enter radius of sphere: ");
       double r=in.nextDouble();
       sphere.setSphere(r);
       System.out.printf("Sphere Surface area: %.2f \nSphere Volume: %.2f", sphere.getArea(sphere.radius,"sphere"), sphere.getVolume(sphere.radius,"sphere"));
       }
       else if(choice2 == 2){
       Cube cube = new Cube();
       System.out.print("Enter side of cube: ");
       double s=in.nextDouble();
       cube.setCube(s);
       System.out.printf("Cube Surface area: %.2f \nCube Volume: %.2f", cube.getArea(cube.side), cube.getVolume(cube.side));
       }
       else{
       RegularTetrahedron tetrahedron=new RegularTetrahedron();
       System.out.print("Enter side of tetrahedron: ");
       double a=in.nextDouble();
       tetrahedron.setRegularTetrahedron(a);
       System.out.printf("Tetrahedron Surface area: %.2f \nTetrahedron Volume: %.2f", tetrahedron.getAreaTetrahedron(tetrahedron.a), tetrahedron.getVolumeTetrahedron(tetrahedron.a));
       }
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT