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

2. (12 points) Solve the following problem (Ch. 9) (a) Write an abstract supercl

ID: 3738985 • Letter: 2

Question

2. (12 points) Solve the following problem (Ch. 9) (a) Write an abstract superclass encapsulating the concept of a shape. The superclass has two abstract methods: one returning the perimeter of the shape, and another returning the area of the shape. It also has a constant field PI (t). This class has two non-abstract subclasses: one encapsulating a circle, and the other encapsulating a rectangle. A circle has one additional attribute, its radius. A rectangle has two additional attributes, its width and height. Include a constructor, getters, and toString methods. (b) The circle class has a subclass encapsulating a cylinder. A cylinder has a circle as its base, and another attribute, its length. It has two methods calculating and returning its area and volume Include a constructor, getters, and toString methods. (c) The rectangle class has a subclass encapsulating a parallelepiped, or box. A parallelepiped has a rectangle as its base, and another attribute for its length. It has two methods that calculate and return its area and volume. Include a constructor, getters, and toString methods (d) Write a client class to test all the classes

Explanation / Answer

abstract class Shape // abstract base class
{
//abstract methods
public abstract double perimeter();
public abstract double area();

public final double PI = 3.14; // constant attribute
}

class Circle extends Shape
{
private double radius;
public Circle(double radius)//constructor
{
this.radius = radius;
}
public double getRadius()
{
return radius;
}
// implementing base class abstract methods
public double perimeter()
{
return 2*PI*radius;
}
public double area()
{
return PI*getRadius()*getRadius();
}

public String toString()
{
return "Circle Radius : "+radius+ " Area : "+area() + " Perimeter : "+perimeter();
}
}

class Cylinder extends Circle
{
private double height;
public Cylinder(double radius,double height)
{
super(radius);// passing argument to base class constructor
this.height = height;
}
// overriding base class methods
public double perimeter()
{
return 2*PI*getRadius()*height;
}
public double area()
{
return PI*getRadius()*getRadius()*height;
}
public String toString()
{
return "Cylinder Height : "+height + " Area : "+area() + " Perimeter : "+perimeter();
}
}

class Rectangle extends Shape
{
private double width,height;
public Rectangle(double height,double width)
{
this.height = height;
this.width = width;
}
public double getHeight()
{
return height;
}
public double getWidth()
{
return width;
}
// implementing base class abstract methods
public double perimeter()
{
return 2*(height+width);
}
public double area()
{
return height*width;
}
public String toString()
{
return "Rectangle Height : "+height + " Width : "+width+ " Area : "+area() + " Perimeter : "+perimeter();
}
}

class Parallelepiped extends Rectangle
{
private double length;

public Parallelepiped(double height,double width,double length)
{
  super(height,width);// passing argument to base class constructor
  this.length = length;
}

public String toString()
{
  return "Parallelepiped : length : "+length + " Area : "+area() + " Perimeter : "+perimeter();
}
//overriding base class methods
public double area()
{
return getHeight()*getWidth()*length;
}
public double perimeter()
{
  return 2*(getHeight()*getWidth())*length;
}

}
class Test
{
public static void main (String[] args)
{
Circle c = new Circle(4.5);
System.out.println(c);

Cylinder cy = new Cylinder(3.4,5.6);
System.out.println(cy);

Rectangle r = new Rectangle(3.6,7.2);
System.out.println(r);

Parallelepiped p = new Parallelepiped(5.6,4.8,6.2);
System.out.println(p);

}
}

Output:

Circle Radius : 4.5 Area : 63.585 Perimeter : 28.26
Cylinder Height : 5.6 Area : 203.27104 Perimeter : 119.57119999999999
Rectangle Height : 3.6 Width : 7.2 Area : 25.92 Perimeter : 21.6
Parallelepiped : length : 6.2 Area : 166.656 Perimeter : 333.312

Do ask if any doubt. Please upvote.

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