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: 3767504 • 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

public abstract class Shape
{
public abstract void display();
public abstract void getDimensions(double ,double double );
public abstract double area();
public abstract double perimeter();
public abstract double volume();
}

public class Rectangle extends Shape
{
private double width,length,height;
public void getDimensions(double width,double length,double height)
{
this.width=width;
this.length=length;
this.height=height;
}
public double area()
{
return width*length;
}
public double perimeter()
{
return 2*(length+width);
}
public double volume()
{
return lenght*width*height;
}
}

class Circle extends Shape
{
private double radius,diameter;
    final double pi = Math.PI;
public void getDimensions(double radius,double diameter,double x)
{
this.radius=radius;
this.diameter=diameter;
}
public double area()
{
return pi*Math.pow(radius,2);
}
public double perimeter()
{
return 2*pi*radius;
}
public double volume()
{
return pi*diameter;
}
}
class Triangle extends Shape
{
private final double a, b, c;

  
    public void getDimensions(double a, double b, double c)
{
        this.a = a;
        this.b = b;
        this.c = c;
    }

  
    public double area()
{
     
        double s = (a + b + c) / 2;
        return Math.sqrt(s * (s - a) * (s - b) * (s - c));
    }

  
    public double perimeter()
{
      
        return a + b + c;
    }
public double volume()
{

}

}

class Box extends Shape
{
double side;
Box(double s)
{
side=s;
}
public void getDimensions(double s,double s,double s)
{
}
double volume()
{
return side*side*side;
}
double area()
{
return 6*side*side;
}
double perimeter()
{
}
}

class Can extends Shape
{
double final pi=Math.PI;
double r,h;
public Can(double radius,double height)
{
r=radius;
h=height;
}
public void getDimensions(double r,double r,double r)
{
}
public double perimeter()
{
}
public double volume()
{
return (pi*r*r*h);
}
public double area()
{
return 2*pi*r*(r+h);
}

}


class Cone extends Shape
{
double final pi=Math.PI;
double r,h,s;
public void getDimensions(double radius,double height,double side)
{
r=radius;
h=height;
s=side;
}

public double perimeter()
{
}
public double volume()
{
return (1/3*pi*r*r*h);
}
public double area()
{
return pi*r*(r+h);
}

}

class Ball extends Shape
{
double final pi=Math.PI;
double r;
public Ball(double radius)
{
r=radius;
}
public void getDimensions(double r,double r,double r)
{
}
public double perimeter()
{
}
public double volume()
{
return (4/3*pi*r*r*r);
}
public double area()
{
return 4*pi*r*r;
}
}

i need some time please contact for queries

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