Answer:
Following are the code to this question:
Explanation:
//For Solid:
public class Solid//defining a class Solid
{
private String myName;//defining a String variable
public Solid(String name)//defining parameterized constructor
{
myName = name;//holding String value in myName variable
}
public String getName()//defining getName method
{
return myName;//return value of myName
}
public double volume()//defining method volume that overriden its subclass
{
return 0;//return value
}
public double surfaceArea()//defining method surfaceArea that overriden its subclass
{
return 0;//return value
}
}
//For RectangularPrism:
public class RectangularPrism extends Solid//defining a class RectangularPrism that inherits Solid
{
protected double length,width,height;//defining double variable
RectangularPrism(String str,double l,double w,double h)//defining parameterized constructor
{
super(str);//use super method
this.length=l;//use this to hold value
this.width=w;//use this to hold value
this.height=h;//use this to hold value
}
public double volume()//defining volume method
{
return length*width*height;//calculate and return volume value
}
public double surfaceArea()//defining a method surfaceArea
{
return 2*(length*width+width*height+height*length);//calculate and return the surfaceArea
}
}
// For Cube:
public class Cube extends RectangularPrism//defining Cube class that inherits RectangularPrism
{
Cube(String name, double side) //defining parameterized constructor
{
super(name, side, side, side);//use super method
}
public double volume()//defining volume method
{
return length * length * length;//calculate and return volume
}
public double surfaceArea()//defining method surfaceArea
{
return 6 * length*length;//calculate and return surfaceArea
}
}
//for Cylinder:
import java.lang.*;//import lang package
public class Cylinder extends Solid//defining Cylinder class that inherits Solid
{
private double radius, height;//defining double variable
public Cylinder(String str, double r, double h)//defining parameterized constructor
{
super(str);//use super Method
this.radius = r;//use this keyword to hold value
this.height = h;//use this keyword to hold value
}
public double volume()//defining volume method
{
return Math.PI * radius * radius * height;//calculate and return volume
}
public double surfaceArea()//defining surfaceArea method
{
return 2 * Math.PI * radius * (height + radius);//calculate and return surfaceArea
}
}
//For Pyramid
import java.lang.*;//import package
public class Pyramid extends Solid//defining a Pyramid class that inherits Solid
{
private double length, width,height;//defining double variable
public Pyramid(String str, double l, double w, double h)//defining parameterized constructor
{
super(str);//use super method
this.length = l;//use this keyword to hold value
this.width = w;//use this keyword to hold value
this.height = h;//use this keyword to hold value
}
public double volume()//defining volume method
{
return (length*width*height)/3.0;//calculate and return volume
}
public double surfaceArea()//defining a method surfaceArea
{
return (length*width)+length*Math.sqrt((Math.pow((width/2),2)+(height*height)))+(width*Math.sqrt((Math.pow((length/2),2)+(height*height))));//calculate and return surfaceArea
}
}
//For Sphere
import java.lang.*;//import package
public class Sphere extends Solid //defining Sphere class that inherits Solid
{
private double radius;//defining double variable
public Sphere(String str, double r)//defining parameterized constructor
{
super(str);//use super method
this.radius = r;//use this to hold value
}
public double volume()//defining volume method
{
return (4.0/3.0)*Math.PI*radius*radius*radius;//calculate and return volume
}
public double surfaceArea()//defining method surfaceArea
{
return 4 * Math.PI * radius * radius;//calculate and return surfaceArea
}
}
please find attachment.