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
  •           TASK 1: Create class diagram for abstract class Shape. TASK 2: Create class diagrams for abstract subclasses...

              TASK 1: Create class diagram for abstract class Shape. TASK 2: Create class diagrams for abstract subclasses TwoDimensionalShape and ThreeDimensionalShape which extend abstract superclassShape. TwoDimensionalShape contains method getArea, and ThreeDimensionalShape contains methods getArea and getVolume. TASK 3: Create class diagrams for concrete classes Circle, Square, Triangle, Sphere, Cube and Tetrahedron.

  • Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you...

    Language is JAVA. Clarification for the Shape class (highlighted): The following requirements specify what fields you are expected to implement in your Shape class; - A private String color that specifies the color of the shape - A private boolean filled that specifies whether the shape is filled - A private date (java.util.date) field dateCreated that specifies the date the shape was created Your Shape class will provide the following constructors; - A two-arg constructor that creates a shape with...

  • Create class definitions using the following: Classes: Circle, Cube, Shape, Sphere, Square, ThreeDimensionalShape, TwoDimensionalShape Functions: Draw,...

    Create class definitions using the following: Classes: Circle, Cube, Shape, Sphere, Square, ThreeDimensionalShape, TwoDimensionalShape Functions: Draw, GetArea, GetDimensions, GetHeight, GetLength, GetSurfaceArea, GetVolume Members: height, length, width – all of type double Don’t actually write function implementations, just define the classes as you would in a header file. Use inheritance as appropriate. Use keywords such as “const” and “virtual” as appropriate. Make functions and members public, private, or protected as appropriate. Don’t forget constructors and destructors. Some classes may be abstract,...

  • Create a program that calculates the area of various shapes. Console Specifications Create an abstract class...

    Create a program that calculates the area of various shapes. Console Specifications Create an abstract class named Shape. This class should contain virtual member function named get_area() that returns a double type. Create a class named Circle that inherits the Shape class and contains these constructors and member functions: Circle(double radius) double get_radius() void set_radius(double radius) double get_area() Create a class named Square that inherits the Shape class and contains these constructors and member functions: Square(double width) double get_width() void...

  • A general shape class is shown below. Shape has a dimension. It also defines constructors, getters,...

    A general shape class is shown below. Shape has a dimension. It also defines constructors, getters, setters and a toString method. class Shape{ int dimension; public Shape(){} public Shape(int newDimension){ dimension = newDimension; } public int getDimension(){ return dimension; } public void setDimension(int newDimension){ dimension = newDimension; } public String toString(){ return "Shape has dimension "+dimension; } } a. Define classes Circle and Square, which inherit from Shape class. b. Both classes must have two constructors similar to Shape class....

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The clas...

    Pure Abstract Base Class Project. Define a class called BasicShape which will be a pure abstract class. The class will have one protected data member that will be a double called area. It will provide a function called getArea which should return the value of the data member area. It will also provide a function called calcArea which must be a pure virtual function. Define a class called Circle. It should be a derived class of the BasicShape class. This...

  • Using Python Write the following well-documented (commented) program. Create a class called Shape that has a...

    Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....

  • This assignment helps you understand Inheritance. We want to create a superclass or parent class called...

    This assignment helps you understand Inheritance. We want to create a superclass or parent class called Shape. This shape class will have just one method to setBorderColor. Now we need 3 subclasses that extend the Shape class, Circle, Square and Rectangle. In these classes we will need to compute the area of each of the shapes and print it to the console using System.out.println You will need to take in an additional parameter in each of these subclasses -- side...

  • in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base cla...

    in C++ use Inheritance for the following Shape Circle Sphere Cylinder Rectangle Square Cube You must define one class for each shape. And, use public inheritance when deriving from base classes. Circle int r; void area(); void perimeter(); void volume(); //No volume for circle Sphere int r; void area();//Sphere surface area= 4 × pi × radius2 void perimeter(); //No perimeter for Sphere void volume();//Sphere volume= 4/3 × pi × radius2 Cylinder int r, height; void area();// Cylinder surface area=perimeter of...

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