127k views
1 vote
Given the Solid class, extend it with:

Pyramid
Cylinder
RectangularPrism
Sphere
Make sure to create the constructor and override the volume and surfaceArea methods.
Also extend RectangularPrism with Cube.
HINT: You can look up formulas for how to compute the volume and surface area of a certain type of shape online.
import java.lang.Math;
public class Pyramid extends Solid
{
// Code goes here
}
public class Cube extends RectangularPrism
{

// Code goes here
}
public class RectangularPrism extends Solid
{
// Code goes here
}
public class Solid
{
private String myName;
public Solid(String name)
{
myName = name;
}
public String getName()
{
return myName;
}
// This should be overriden in the subclass
public double volume()
{
return 0;
}
// This should be overriden in the subclass
public double surfaceArea()
{
return 0;
}
}
import java.lang.Math;
public class Sphere extends Solid
{
// Code goes here
}
import java.lang.Math;
public class Cylinder extends Solid
{

// Code goes here
}
public class SolidTester
{
public static void main(String[] args)
{
String name;
double volume;
double surfaceArea;
Pyramid pyramid = new Pyramid("My pyramid", 1, 3, 5);
name = pyramid.getName();
volume = round(pyramid.volume(), 2);
surfaceArea = round(pyramid.surfaceArea(), 2);
System.out.println("Pyramid '" + name + "' has volume: " + volume +
" and surface area: " + surfaceArea + ".");
Sphere sphere = new Sphere("My sphere", 4);
name = sphere.getName();
volume = round(sphere.volume(), 2);
surfaceArea = round(sphere.surfaceArea(), 2);
System.out.println("Sphere '" + name + "' has volume: " + volume +
" and surface area: " + surfaceArea + ".");
RectangularPrism rectangularPrism = new RectangularPrism("My rectangular prism",
5, 8, 3);
name = rectangularPrism.getName();
volume = round(rectangularPrism.volume(), 2);
surfaceArea = round(rectangularPrism.surfaceArea(), 2);
System.out.println("RectangularPrism '" + name + "' has volume: " +
volume + " and surface area: " + surfaceArea + ".");
Cylinder cylinder = new Cylinder("My cylinder", 4, 9);
name = cylinder.getName();
volume = round(cylinder.volume(), 2);
surfaceArea = round(cylinder.surfaceArea(), 2);
System.out.println("Cylinder '" + name + "' has volume: " + volume +
" and surface area: " + surfaceArea + ".");
Cube cube = new Cube("My cube", 4);
name = cube.getName();
volume = round(cube.volume(), 2);
surfaceArea = round(cube.surfaceArea(), 2);
System.out.println("Cube '" + name + "' has volume: " + volume +
" and surface area: " + surfaceArea + ".");
}
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
long factor = (long) Math.pow(10, places);
value = value * factor;
long tmp = Math.round(value);
return (double) tmp / factor;
}
}

1 Answer

4 votes

Answer:

Following are the code to this question:

Step-by-step 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.

Given the Solid class, extend it with: Pyramid Cylinder RectangularPrism Sphere Make-example-1
Given the Solid class, extend it with: Pyramid Cylinder RectangularPrism Sphere Make-example-2
Given the Solid class, extend it with: Pyramid Cylinder RectangularPrism Sphere Make-example-3
Given the Solid class, extend it with: Pyramid Cylinder RectangularPrism Sphere Make-example-4
User EGlu
by
5.0k points