public abstract class Solid
{
private String name;
//constructor
public Solid(String solidName)
{ name = solidName; }
public String getName()
{ return name; }
public abstract double volume();
}
public class Sphere : Solid
{
private double radius;
//constructor
public Sphere(String sphereName, double sphereRadius) : base(sphereName)
{
radius = sphereRadius;
}
public override double volume()
{ return (4.0/3.0) * Math.PI * radius * radius * radius; }
}
public class RectangularPrism : Solid
{
private double length;
private double width;
private double height;
//constructor
public RectangularPrism(String prismName, double l, double w, double h) : base(prismName)
{
length = l;
width = w;
height = h;
}
public override double volume()
{ return length * width * height; }
}
Here is a program that prints the volume of a solid:
public class SolidMain
{
/** Output volume of Solid s. */
public static void printVolume(Solid s)
{
System.out.println("Volume = " + s.volume() + " cubic units");
}
public static void main(String[] args)
{
Solid sol;
Solid sph = new Sphere("sphere", 4);
Solid rec = new RectangularPrism("box", 3, 6, 9);
Random rnd = new Random();
int flipCoin = (int) (rnd.Next(2)); //0 or 1
if (flipCoin == 0)
sol = sph;
else
sol = rec;
printVolume(sol);
}
}
Which is a true statement about this program?
(A) It will output the volume of the sphere or box, as intended.
(B) It will output the volume of the default Solid s, which is neither a sphere nor a box.
(C) A ClassCastException will be thrown.
(D) A compile-time error will occur because there is no implementation code for volume in the Solid class.
(E) A run-time error will occur because of parameter type mismatch in the method call printVolume(sol).