Language: Java Implement the Shape hierarchy -- create an abstract class called
ID: 3694434 • Letter: L
Question
Language: Java
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.
Explanation / Answer
abstract class Shape{}
class TwoDimensionalShape extends Shape{
public abstarct float getArea();
public abstarct float getPerimeter();
}
class ThreeDimensionalShape extends Shape{
public abstarct float getArea();
public abstarct float getVolume();
}
class Circle extends TwoDimensionalShape{
Private float radius;
Circle(float r){
radius=r;
}
public float getArea(){
float area=3.14*radius*radius;
return area;
}
public float getPerimeter(){
float peri=2*3.14*radius;
return peri;
}
}
class Square extends TwoDimensionalShape{
Private float side;
Square(float s){
side=s;
}
public float getArea(){
float area=side*side;
return area;
}
public float getPerimeter(){
float peri=3*side;
return peri;
}
}
class Triangle extends TwoDimensionalShape{
private float base;
private float side2;
private float side3;
private float height;
Triangle(float s1,float s2,float s3,float h){
base=s1;
side=s2;
side=s3;
height=h;
}
public float getArea(){
float area=(base*height)/2;
return area;
}
public float getPerimeter(){
float peri=base+side2+side3;
return peri;
}
}
class Cube extends ThreeDimensionalShape{
Private float side;
Cube(float s){
side=s;
}
public float getArea(){
float area=6*side*side;
return area;
}
public float getVolume(){
float volume=side*side*side;
return volume;
}
}
class Sphere extends ThreeDimensionalShape{
Private float radius;
Shape(float r){
radius=r;
}
public float getArea(){
float area=4*3.14*radius*radius;
return area;
}
public float getVolume(){
float volume=(4*3.14*radius*radius)/3
return volume;
}
}
class Tetrahedron extends ThreeDimensionalShape{
Private float edge;
Cube(float s){
edge=s;
}
public float getArea(){
float area=3^-2*edge*edge;
return area;
}
public float getVolume(){
float volume=(edge*edge*edge)/(6*2^-2);
return volume;
}
}
class Driver{
public static void main(String... str){
int choice;
System.out.println("Enter the choice");
System.out.println("Enter 1 for Circle");
System.out.println("Enter 2 for square");
System.out.println("Enter 3 for triangle");
System.out.println("Enter 4 for Cube");
System.out.println("Enter 4 for sphere");
System.out.println("Enter 4 for tetrahedron");
Scanner sc=new Scanner(System.in);
choice=sc.nextInt();
switch(choice){
case 1:
System.out.println("Enter radius for Circle");
Scanner sc=new Scanner(System.in);
float rad=sc.nextFloat();
TwoDimensionalShape c=new Circle(rad)
System.out.println("area of circle:"+c.getArea());
System.out.println("perimeter of circle:"+c.getPerimeter());
break;
case 2:
System.out.println("Enter side for square");
Scanner sc=new Scanner(System.in);
float side1=sc.nextFloat();
TwoDimensionalShape s=new Square(side1)
System.out.println("area of square:"+s.getArea());
System.out.println("Perimeter of square:"+s.getPerimeter());
break;
case 3:
System.out.println("Enter base for triangle");
Scanner sc=new Scanner(System.in);
float s1=sc.nextFloat();
System.out.println("Enter side2 for triangle");
Scanner sc=new Scanner(System.in);
float s2=sc.nextFloat();
System.out.println("Enter side3 for triangle");
Scanner sc=new Scanner(System.in);
float s3=sc.nextFloat();
System.out.println("Enter height for triangle");
Scanner sc=new Scanner(System.in);
float hh=sc.nextFloat();
TwoDimensionalShape r=new Triangle(s1,s2,s3,hh)
System.out.println("area of triangle:"+r.getArea());
System.out.println("volume of triangle:"+r.getPerimeter());
break;
case 4:
System.out.println("Enter side for cube");
Scanner sc=new Scanner(System.in);
float s=sc.nextFloat();
ThreeDimensionalShape cc=new Cube(s)
System.out.println("area of cube:"+cc.getArea());
System.out.println("volume of cube:"+cc.getVolume());
break;
case 5:
System.out.println("Enter radius for sphere");
Scanner sc=new Scanner(System.in);
float rad=sc.nextFloat();
ThreeDimensionalShape c=new Sphere(rad)
System.out.println("area of sphere:"+c.getArea());
System.out.println("perimeter of sphere:"+c.getVolume());
break;
case 6:
System.out.println("Enter side for tetrahedron");
Scanner sc=new Scanner(System.in);
float s=sc.nextFloat();
ThreeDimensionalShape cc=new Tetrahedron(s);
System.out.println("area of Tetrahedron:"+cc.getArea());
System.out.println("volume of Tetrahedron:"+cc.getVolume());
break;
}}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.