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.

Homework Answers

Answer #1

abstract class Shape {
}
_________________________
abstract class TwoDimensionalShape extends Shape {
abstract double getArea();
abstract double getPerimeter();
}
_________________________
abstract class ThreeDimensionalShape extends Shape {
public abstract double getArea();
public abstract double getVolume();
}
_________________________
class Circle extends TwoDimensionalShape {
private double radius;
Circle(double r) {
radius = r;
}
public double getArea() {
double area = Math.PI * Math.pow(radius, 2);
  
return Math.round(area * 100.00) / 100.00;
}
public double getPerimeter() {
double peri = 2 * Math.PI * radius;
return Math.round(peri * 100.00) / 100.00;
}
}
_________________________
class Square extends TwoDimensionalShape {
private double side;
Square(double s) {
side = s;
}
public double getArea() {
double area = Math.pow(side, 2);
return Math.round(area * 100.0) / 100.0;
}
public double getPerimeter() {
double peri = side + side + side + side;
return Math.round(peri * 100.0) / 100.0;
}
}
_________________________
class Triangle extends TwoDimensionalShape {
private double base;
private double side2;
private double side3;
  
Triangle(double s1, double s2, double s3) {
base = s1;
side2 = s2;
side3 = s3;
  
  
}
public double getArea() {
double s = (base + side2 + side3) / 2;
double area = Math.sqrt(s * (s - base) * (s - side2) * (s - side3));
return Math.round(area * 100.0) / 100.0;
}
public double getPerimeter() {
double peri = base + side2 + side3;
return Math.round(peri * 100.0) / 100.0;
}
}
_________________________
class Cube extends ThreeDimensionalShape {
private double side;
Cube(double s) {
side = s;
}
public double getArea() {
double area = 6 * Math.pow(side, 2);
return area;
}
public double getVolume() {
double volume = Math.pow(side, 3);
return Math.round(volume * 100.00) / 100.00;
}
}
_________________________
class Sphere extends ThreeDimensionalShape {
private double radius;
Sphere(double r) {
radius = r;
}
public double getArea() {
double area = 4 * Math.PI * Math.pow(radius, 2);
return Math.round(area * 100.0) / 100.0;
}
public double getVolume() {
double volume = (1.33333 * Math.PI * Math.pow(radius, 3));
return Math.round(volume * 100.0) / 100.0;
}
}
_________________________
class Tetrahedron extends ThreeDimensionalShape {
private double edge;
Tetrahedron(double s) {
edge = s;
}
public double getArea() {
double area = Math.sqrt(3) * Math.pow(edge, 2);
return Math.round(area * 100.00) / 100.00;
}
public double getVolume() {
double volume = Math.pow(edge, 3) / (6 * Math.sqrt(2));
return Math.round(volume * 100.00) / 100.00;
}
}
______________________
// Driver.java
import java.util.Scanner;
public class Driver {
public static void main(String... str) {
int choice = 0;
int c2 = 0;
int c3 = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter");
System.out.println("1)Two dimensional shape");
System.out.println("2)Three dimensional shape");
System.out.print("Enter Choice :");
choice = sc.nextInt();
if (choice == 1) {
System.out.println("Enter");
System.out.println("1)Circle");
System.out.println("2)Square");
System.out.println("3)Triangle");
System.out.print("Enter Choice :");
c2 = sc.nextInt();
switch (c2) {
case 1:
System.out.print("Enter radius of circle:");
double rad = sc.nextDouble();
TwoDimensionalShape c = new Circle(rad);
System.out.println("Area: " + c.getArea() + " ");
System.out.println("Perimeter:"
+ String.format("%.2f", c.getPerimeter()));
break;
case 2:
System.out.print("Enter side of square:");
double side1 = sc.nextDouble();
TwoDimensionalShape s = new Square(side1);
System.out.println("Area: " + s.getArea());
System.out.println("Perimeter:"+ String.format("%.2f", s.getPerimeter()));
break;
case 3:
System.out.print("Enter side of triangle:");
double s1 = sc.nextDouble();
System.out.print("Enter side of triangle:");
double s2 = sc.nextDouble();
System.out.print("Enter side of triangle:");
double s3 = sc.nextDouble();
TwoDimensionalShape r = new Triangle(s1, s2, s3);
System.out.println("Area: " + r.getArea() + " ");
System.out.println("Perimeter: "+ String.format("%.2f", r.getPerimeter()));
break;
  
}
} else if (choice == 2) {
System.out.println("Enter");
System.out.println("1)Sphere");
System.out.println("2)Cube");
System.out.println("3)Tetrahedron");
System.out.print("Enter Choice :");
c3 = sc.nextInt();
switch (c3) {
case 1:
System.out.print("Enter radius of sphere:");
double radx = sc.nextDouble();
ThreeDimensionalShape cc2 = new Sphere(radx);
System.out.println("Surface area: "
+ String.format("%.2f", cc2.getArea()) + " ");
System.out.println("Volume: "
+ String.format("%.2f", cc2.getVolume()));
break;
case 2:
System.out.print("Enter side of cube: ");
double sx = sc.nextDouble();
ThreeDimensionalShape cc1 = new Cube(sx);
System.out.println("Area: "
+ String.format("%.2f", cc1.getArea()) + " ");
System.out.println("Volume: "
+ String.format("%.2f", cc1.getVolume()));
break;
case 3:
System.out.print("Enter side of tetrahedron: ");
double tx = sc.nextDouble();
ThreeDimensionalShape cc3 = new Tetrahedron(tx);
System.out.println("Area: " + cc3.getArea() + " ");
System.out.println("Volume: " + cc3.getVolume());
break;
  
}
} else {
System.out.println("choice is invalid");
}
sc.close();
}
}
___________________
Output:
Enter
1)Two dimensional shape
2)Three dimensional shape
Enter Choice :1
Enter
1)Circle
2)Square
3)Triangle
Enter Choice :1
Enter radius of circle:1.56
Area: 7.65
Perimeter:9.80
___________________________
Output#2:
Enter
1)Two dimensional shape
2)Three dimensional shape
Enter Choice :1
Enter
1)Circle
2)Square
3)Triangle
Enter Choice :2
Enter side of square:3
Area: 9.0
Perimeter:12.00
_________________________
Output#3:
Enter
1)Two dimensional shape
2)Three dimensional shape
Enter Choice :1
Enter
1)Circle
2)Square
3)Triangle
Enter Choice :3
Enter side of triangle:3
Enter side of triangle:4
Enter side of triangle:5
Area: 6.0
Perimeter: 12.00
________________________
Output#4:
Enter
1)Two dimensional shape
2)Three dimensional shape
Enter Choice :2
Enter
1)Sphere
2)Cube
3)Tetrahedron
Enter Choice :1
Enter radius of sphere:5.5
Surface area: 380.13
Volume: 696.91

________________Thank You!!! please vote

Know the answer?
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
Similar Questions
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a class named Triangle that extends GeometricObject. The class contains: Three double fields named side1, side2, and side3 with default values = 1.0. These denote the three sides of the triangle A no-arg constructor that creates a default triangle A constructor that creates a triangle with parameters specified for side1, side2, and side3. An accessor method for each side. (No mutator methods for the sides)...
Create a Shape (Object) Class Create a class (Shapes) that prompts the user to select a...
Create a Shape (Object) Class 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...
Java code Problem 1. Create a Point class to hold x and y values for a...
Java code Problem 1. Create a Point class to hold x and y values for a point. Create methods show(), add() and subtract() to display the Point x and y values, and add and subtract point coordinates. Tip: Keep x and y separate in the calculation. Create another class Shape, which will form the basis of a set of shapes. The Shape class will contain default functions to calculate area and circumference of the shape, and provide the coordinates (Points)...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
Create a C# application You are to create a class object called “Employee” which included eight...
Create a C# application You are to create a class object called “Employee” which included eight private variables: firstN lastN dNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week. regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods:  constructor  properties  CalcPay(): Calculate the regular...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following 4 fields: int x (x-coordinate for its top left corner) int y (y coordinate for its top left corner) double height (height of the rectangle) double width (width of the rectangle) Your rectangle objects should have the following methods: public Rectangle(int newx, int newy, int newwidth, int newheight) Constructor that initializes the x-coordinate, y-coordinate for the top left corner and its width and height....
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also define...
Design a Java class named Polygon that contains:  A private int data field named numSides...
Design a Java class named Polygon that contains:  A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.  A private double data field named sideLength that defines the length of each side. The default value should be 5.0.  A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.  A private double...
Language is C# 1. Create the following classes: Vessel, which is an abstract class and represents...
Language is C# 1. Create the following classes: Vessel, which is an abstract class and represents any water-going craft. Ship, which is a Vessel. Fun Fact: A Ship is any vessel that is large enough to carry smaller Boats. Boat, which is a Vessel. and Cat, which is just a cat. All Vessels should have a float speed; and string name; Ships should have an int fuel and an int maxFuel. Boats should have an int oars; Cats should have...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...