Question

In: Computer Science

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.

(This must be in Javascript)

Solutions

Expert Solution

ANSWER :

Code :-

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

Hope it helps... please give an upvote. it's very important to me... please don't dislike plzz..thank you:)


Related Solutions

In C++ Create an abstract class called Shape Shape should have the following pure virtual functions:...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions: getArea() setArea() printArea() Create classes to inherit from the base class Circle Square Rectangle Both implement the functions derived from the abstract base class AND must have private variables and functions unique to them like double Radius double length calculateArea() Use the spreadsheet info.txt read in information about the circle, rectangle, or square text file: circle   3.5   square   3   rectangle   38   36 circle   23  ...
Java - Write an abstract class called Shape with a string data field called colour. Write...
Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()
Exercise #1: Create an abstract class called GameTester. The GameTester class includes a name for the...
Exercise #1: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the user to choose game tester type...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
CS Using the following UML outline, create the following abstract parent class along with the 4...
CS Using the following UML outline, create the following abstract parent class along with the 4 subclasses and Driver class. Implement the Classes, listed attributes and methods along with any additional things that you may need. Add a driver class that utilizes the methods/attributes in the classes to output the following based on the vehicle type (therefore, you must create an instance for all 4 subclasses in your driver): 1. Vehicle Type (i.e., Car, Airplane, etc.) 2. Transportation method (wheels,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape,...
Java Solution Create a class hierarchy that represents shapes. It should have the following classes: Shape, Two Dimensional Shape, Three Dimensional Shape, Square, Circle, Cube, Rectangular Prism, and Sphere. Cube should inherit from Rectangular Prism. The two dimensional shapes should include methods to calculate Area. The three dimensional shapes should include methods to calculate surface area and volume. Use as little methods as possible (total, across all classes) to accomplish this, think about what logic should be written at which...
Write in drjava Problem Design and implement these 4 files: A parent class called Plant with...
Write in drjava Problem Design and implement these 4 files: A parent class called Plant with name (eg Rose, Douglas Fir) and lifespan (could be in days, weeks, months or years) attributes Tree inherits from Plant and adds a height attribute Flower inherits from Plant and adds a color attribute A driver file to test the 3 classes above. The classes described in #1, 2 and 3 above should have the usual constructors (default and parameterized), get (accessor) and set...
- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount...
- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount for the purchase of items. DiscountPolicy knows the name of the item and its cost as well as the number of items being purchased. - Class BulkDiscount, derived from DiscountPolicy, has two fields, minimum and percentage. computeDiscount method will return the discount based on the percentage applied, if the quantity of items purchased is more than minimum. For example: if minimum is 3 and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT