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
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)...
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...
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,...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. // A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. // At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the...
MATH125: Unit 1 Individual Project Answer Form Mathematical Modeling and Problem Solving ALL questions below regarding...
MATH125: Unit 1 Individual Project Answer Form Mathematical Modeling and Problem Solving ALL questions below regarding SENDING A PACKAGE and PAINTING A BEDROOM must be answered. Show ALL step-by-step calculations, round all of your final answers correctly, and include the units of measurement. Submit this modified Answer Form in the Unit 1 IP Submissions area. All commonly used formulas for geometric objects are really mathematical models of the characteristics of physical objects. For example, a basketball, because it is a...
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...