Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and write classes in a class hierarchy to describe different types of geo

ID: 3860264 • Letter: D

Question

Design and write classes in a class hierarchy to describe different types of geometric shapes. Provide information in the classes in order to compute the area of each of the shapes. Create three separate classes (Triangle,Rectangle,Circle) that are derived from a separate base class (Shape). Write a program that allows a user to create any of the different shape types. Prompt the user to select the shape type and enter the necessary dimensions (i.e. refer to formulas below; rectangle needs width and height, circle needs radius, triangle needs height and base). After the user has finished creating the shape, compute and print out the area.

Use the following formulas for your program:

Triangle:

area = ½ * base * height

Rectangle:

area = width * height

Circle:

area = PI * radius2

Triangle:

area = ½ * base * height

Rectangle:

area = width * height

Circle:

area = PI * radius2

Explanation / Answer

Below is your code: -

Shape.java

public class Shape {

private String type;

Shape(String t) {

this.type = t;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public String toString() {

return this.type;

}

}

Triangle.java

public class Triangle extends Shape {

private double base;

private double height;

public Triangle(double base, double height) {

super("Triangle");

this.base = base;

this.height = height;

}

public double getBase() {

return base;

}

public void setBase(double base) {

this.base = base;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

public double area() {

return (1 / (double)2) * this.base * this.height;

}

public String toString() {

return super.toString() + " [ Area: " + this.area() + " ]";

}

}

Rectangle.java

public class Rectangle extends Shape {

private double width;

private double height;

public Rectangle(double width, double height) {

super("Rectangle");

this.width = width;

this.height = height;

}

public double getWidth() {

return width;

}

public void setWidth(double width) {

this.width = width;

}

public double getHeight() {

return height;

}

public void setHeight(double height) {

this.height = height;

}

public double area() {

return this.width * this.height;

}

public String toString() {

return super.toString() + " [ Area: " + this.area() + " ]";

}

}

Circle.java

public class Circle extends Shape {

private double radius;

public Circle(double radius) {

super("Circle");

this.radius = radius;

}

public double getRadius() {

return radius;

}

public void setRadius(double radius) {

this.radius = radius;

}

public double area() {

return Math.PI * this.radius * this.radius;

}

public String toString() {

return super.toString() + " [ Area: " + this.area() + " ]";

}

}

ShapeDriver.java

import java.util.Scanner;

public class ShapeDriver {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int choice = 0;

System.out.println("Please enter a choice of shape: ");

System.out.println("1. Triangle");

System.out.println("2. Rectangle");

System.out.println("3. Circle");

choice = Integer.parseInt(sc.next());

switch (choice) {

case 1:

System.out.println("Enter base of triangle: ");

double base = Double.parseDouble(sc.next());

System.out.println("Enter height of triangle: ");

double height = Double.parseDouble(sc.next());

Triangle tri = new Triangle(base, height);

System.out.println(tri);

break;

case 2:

System.out.println("Enter width of Rectangle: ");

double width = Double.parseDouble(sc.next());

System.out.println("Enter height of Rectangle: ");

double height1 = Double.parseDouble(sc.next());

Rectangle rec = new Rectangle(width, height1);

System.out.println(rec);

break;

case 3:

System.out.println("Enter radius of Circle: ");

double radius = Double.parseDouble(sc.next());

Circle cir = new Circle(radius);

System.out.println(cir);

break;

default:

System.out.println("You have not entered a valid choice . Exiting...");

}

sc.close();

}

}

Sample Outut: -

Please enter a choice of shape:
1. Triangle
2. Rectangle
3. Circle
1
Enter base of triangle:
2.4
Enter height of triangle:
5.6
Triangle [ Area: 6.72 ]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote