// Cylinder class // I NEED INHERITANCE THIS PROGRAM // I NEED HELP PLEASE!!!!!!
ID: 3728904 • Letter: #
Question
// Cylinder class
// I NEED INHERITANCE THIS PROGRAM
// I NEED HELP PLEASE!!!!!!!!!!!
public class Cylinder
{
private double height;
private double radius;
public Cylinder(double radius, double height)
{
this.radius = radius;
this.height = height;
}
// Calculate the volume
public double volumeCylinder()
{
double volume = Math.PI * Math.pow(radius,2.0) * height;
return volume;
}
// Calculate the surface
public double SurfaceArea()
{
double diameter = 2 * radius;
double circumference = Math.PI * diameter;
double circleArea = Math.PI * Math.pow(radius,2.0);
double areaCylinder = (2 * circleArea) + (circumference * height);
return areaCylinder;
}
public double Radius()
{
return this.radius; // find radius
}
public double Height()
{
return this.height; // find the height
}
public void Height(double h)
{
this.height = h;
}
public void Radius(double rad)
{
this.radius = rad;
}
// toString method
public String toString()
{
String str = " Radius: " + radius + " " + " Height " + height;
return str;
}
public static void main(String[] args)
{
Cylinder cylinderTest1 = new Cylinder(2, 10);
Cylinder cylinderTest2 = new Cylinder(3, 7);
Cylinder cylinderTest3 = new Cylinder(6, 4);
// Print the dadius and the height
System.out.println(cylinderTest1);
System.out.println(cylinderTest2);
System.out.println(cylinderTest3);
// Display the volumes
System.out.println(" The volume is " +cylinderTest1.volumeCylinder());
System.out.println(" The volume2 is " +cylinderTest2.volumeCylinder());
System.out.println(" The volume3 is " +cylinderTest3.volumeCylinder());
// Display the areas
System.out.println(" The area is " +cylinderTest1.SurfaceArea());
System.out.println(" The area2 is " +cylinderTest2.SurfaceArea());
System.out.println(" The area3 is " +cylinderTest3.SurfaceArea());
}
}
Explanation / Answer
Answer:
//BeanCylender.java
public class BeanCylender {
private double height;
private double radius;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public BeanCylender(double radius, double height)
{
this.radius = radius;
this.height = height;
}
public double volumeCylinder(){
double volume = Math.PI * Math.pow(radius,2.0) * height;
return volume ;
}
public double SurfaceArea()
{
double diameter = 2 * radius;
double circumference = Math.PI * diameter;
double circleArea = Math.PI * Math.pow(radius,2.0);
double areaCylinder = (2 * circleArea) + (circumference * height);
return areaCylinder;
}
public String toString()
{
String str = " Radius: " + radius + " " + " Height " + height;
return str;
}
}
//Cylinder.java
public class Cylinder extends BeanCylender{
public Cylinder(double radius, double height) {
super(radius, height);
}
private double height;
private double radius;
public static void main(String[] args)
{
BeanCylender cylinderTest1 = new BeanCylender(2, 10);
BeanCylender cylinderTest2 = new BeanCylender(3, 7);
BeanCylender cylinderTest3 = new BeanCylender(6, 4);
// Print the dadius and the height
System.out.println(cylinderTest1);
System.out.println(cylinderTest2);
System.out.println(cylinderTest3);
// Display the volumes
System.out.println(" The volume is " +cylinderTest1.volumeCylinder());
System.out.println(" The volume2 is " +cylinderTest2.volumeCylinder());
System.out.println(" The volume3 is " +cylinderTest3.volumeCylinder());
// Display the areas
System.out.println(" The area is " +cylinderTest1.SurfaceArea());
System.out.println(" The area2 is " +cylinderTest2.SurfaceArea());
System.out.println(" The area3 is " +cylinderTest3.SurfaceArea());
}
}
Output :
Radius: 2.0
Height 10.0
Radius: 3.0
Height 7.0
Radius: 6.0
Height 4.0
The volume is 125.66370614359172
The volume2 is 197.92033717615698
The volume3 is 452.3893421169302
The area is 150.79644737231007
The area2 is 188.4955592153876
The area3 is 376.99111843077515
Thanks...