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

Write a Java program that uses abstract methods to input, calculate, and display

ID: 3769784 • Letter: W

Question

Write a Java program that uses abstract methods to input, calculate, and display some quantities associated with a variety of 2- and 3-dimensional objects. Design an abstract base class Shape from which you will derive classes Rectangle, Circle, Triangle, Box, Can, Cone, and Ball. The class Shape should contain abstract methods display, getDimensions, area, perimeter, and volume. For each of the derived classes, write methods display, getDimensions, area, perimeter, and volume that will do the following:

display - output the type and dimensions for an object of the class.

getDimensions - get the dimensions for an object of the class.

perimeter - Calculate and display the perimeter of an object of a 2-dimensional shape class; do nothing for an object of a 3-dimensional shape class.

area - Calculate and display the area of an object of a 2-dimensional shape class; calculate and display the surface area for an object of a 3-dimensional shape class.

volume - Calculate and display the volume of an object of a 3-dimensional shape class; do nothing for an object of a 2-dimensional shape class.

Include appropriate constructors and any other methods that you think are necessary.

Write a program driver that will allocate an array of 20 elements of class type Shape and then repeatedly prompt the user, up to a maximum 20 times, to enter a shape type, create an object for an appropriate shape type and store the object in the array, and then prompt for and read the appropriate dimensions for the shape. After the input of shapes is complete, the program should then loop through the array elements and display the shape type, dimensions, area, perimeter and/or volume for each of the input shape objects.

Explanation / Answer

Program:

import java.util.Scanner;
import java.lang.Math;

// Abstract class with abstract methods
abstract class Shape
{
abstract void display();
abstract void getDimensions();
abstract void area();
abstract void perimeter();
abstract void volume();
}

// Circle class extends Shape
class Circle extends Shape
{
// radius is the onlt dimension of circle
int radius;
// constructor which call getDimensions method
Circle()
{
getDimensions();
}

// display - output the type and dimensions of Circle
void display()
{
System.out.println("This is Circle Object");
System.out.println("Radius : "+radius);
}

// getDimensions - get the dimensions for an object of the class
void getDimensions()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter radius of the circle : ");
radius = scanner.nextInt();
}

// area - Calculate and display the area of an object of Circle
void area()
{
double area;
area = Math.PI*Math.pow(radius,2);
System.out.println("Area of the circle is : "+area);
}

// perimeter - Calculate and display the perimeter of circle
void perimeter()
{
double perimeter;
perimeter = 2*Math.PI*radius;
System.out.println("Perimeter of the circle is : "+perimeter);
}

// volume - No volume for circle
void volume()
{
// do nothing
}
}

class Rectangle extends Shape
{
// length and breadth are dimensions of rectangle
int length, breadth;
// constructor which call getDimensions method
Rectangle()
{
getDimensions();
}

// display - output the type and dimensions of Rectangle
void display()
{
System.out.println("This is Rectangle Object");
System.out.println("Length : "+length);
System.out.println("Breadth : "+breadth);
}

// getDimensions - get the dimensions for an object of the class
void getDimensions()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter length of the rectangle : ");
length = scanner.nextInt();
System.out.println("Enter breadth of the rectangle : ");
breadth = scanner.nextInt();
}

// area - Calculate and display the area of rectangle
void area()
{
double area;
area = length*breadth;
System.out.println("Area of the rectangle is : "+area);
}

// perimeter - Calculate and display the perimeter of rectangle
void perimeter()
{
double perimeter;
perimeter = 2*(length+breadth);
System.out.println("Perimeter of the rectangle is : "+perimeter);
}

// volume - No volume for rectangle
void volume()
{
// do nothing
}
}

class Triangle extends Shape
{
int side1, side2, side3;
Triangle()
{
getDimensions();
}
void display()
{
System.out.println("This is Triangle Object");
System.out.println("Side 1 : "+side1);
System.out.println("Side 2 : "+side2);
System.out.println("Side 3 : "+side3);
}
void getDimensions()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter side1 of the triangle : ");
side1 = scanner.nextInt();
System.out.println("Enter side2 of the triangle : ");
side2 = scanner.nextInt();
System.out.println("Enter side3 of the triangle : ");
side3 = scanner.nextInt();
}
void area()
{
double area;
double s = (side1 + side2 + side3)/2;
area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
System.out.println("Area of the triangle is : "+area);
}
void perimeter()
{
double perimeter;
perimeter = (side1 + side2 + side3);
System.out.println("Perimeter of the triangle is : "+perimeter);
}
void volume()
{
// do nothing
}
}

class Box extends Shape
{
int length, breadth, height;
Box()
{
getDimensions();
}
void display()
{
System.out.println("This is Box Object");
System.out.println("Length : "+length);
System.out.println("Breadth : "+breadth);
System.out.println("Height : "+height);
}
void getDimensions()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter length of the box : ");
length = scanner.nextInt();
System.out.println("Enter breadth of the box : ");
breadth = scanner.nextInt();
System.out.println("Enter height of the box : ");
height = scanner.nextInt();
}
void area()
{
double area;
area = 2*(length*breadth + breadth*height + height*length);
System.out.println("Surface Area of the Box is : "+area);
}
void perimeter()
{
// fo nothing
}
void volume()
{
double volume;
volume = length*breadth*height;
System.out.println("Volume of the Box is : "+volume);
}
}

class Ball extends Shape
{
int radius;
Ball()
{
getDimensions();
}
void display()
{
System.out.println("This is Ball Object");
System.out.println("Radius : "+radius);
}
void getDimensions()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter radius of the Ball : ");
radius = scanner.nextInt();
}
void area()
{
double area;
area = 4*Math.PI*Math.pow(radius,2);
System.out.println("Area of the ball is : "+area);
}
void perimeter()
{
// do nothing
}
void volume()
{
double volume;
volume = (4/3)*Math.PI*Math.pow(radius,3);
System.out.println("Volume of the ball is : "+volume);
}
}

class Can extends Shape
{
int radius, height;
Can()
{
getDimensions();
}
void display()
{
System.out.println("This is Can Object");
System.out.println("Radius : "+radius);
System.out.println("Height : "+height);
}
void getDimensions()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter radius of the Can : ");
radius = scanner.nextInt();
System.out.println("Enter height of the Can : ");
height = scanner.nextInt();
}
void area()
{
double area;
area = 2*Math.PI*radius*(height+radius);
System.out.println("Area of the Can is : "+area);
}
void perimeter()
{
// do nothing
}
void volume()
{
double volume;
volume = Math.PI*Math.pow(radius,2)*height;
System.out.println("Volume of the Can is : "+volume);
}
}

class Cone extends Shape
{
int radius, height;
Cone()
{
getDimensions();
}
void display()
{
System.out.println("This is Cone Object");
System.out.println("Radius : "+radius);
System.out.println("Height : "+height);
}
void getDimensions()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter radius of the Cone : ");
radius = scanner.nextInt();
System.out.println("Enter height of the Cone : ");
height = scanner.nextInt();
}
void area()
{
double area;
area = Math.PI*radius*(radius+Math.sqrt(Math.pow(height,2)+Math.pow(radius,2)));
System.out.println("Area of the Cone is : "+area);
}
void perimeter()
{
// do nothing
}
void volume()
{
double volume;
volume = Math.PI*Math.pow(radius,2)*height/3;
System.out.println("Volume of the Cone is : "+volume);
}
}

class ShapeDriver
{
// main method
public static void main(String[] args)
{
Shape[] shapes = new Shape[20];
int option;
Scanner scanner = new Scanner(System.in);
// create 20 objects by asking used about the type of shape
for(int i=0; i<20; i++)
{
System.out.println("Select the type of shape");
System.out.println("1. Circle");
System.out.println("2, Rectangle");
System.out.println("3. Triangle");
System.out.println("4. Box");
System.out.println("5. Can");
System.out.println("6. Cone");
System.out.println("7. Ball");
System.out.println("Enter your Option : ");
option = scanner.nextInt();
// create an instance depending on the input from user
switch(option)
{
case 1:
shapes[i] = new Circle();
break;
case 2:
shapes[i] = new Rectangle();
break;
case 3:
shapes[i] = new Triangle();
break;
case 4:
shapes[i] = new Box();
break;
case 5:
shapes[i] = new Can();
break;
case 6:
shapes[i] = new Cone();
break;
case 7:
shapes[i] = new Ball();
break;
default:
System.out.println("Invalid Option");
i--;
}
}
// call all the methods of each object
for(int i=0; i<20; i++)
{
shapes[i].display();
shapes[i].perimeter();
shapes[i].area();
shapes[i].volume();
}
}
}

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