In: Computer Science
Write an application, CylinderStats, that reads the radius and height of a cylinder and prints its surface area and volume. Use the following formulas. Print the output to five decimal places. In the formulas, represents the radius and the height.Surface are
2πr(r+ h)
Volume:πr2h
Design and implement a class Cylinder that contains instance data that represents the cylinder’s radius and height. Define a Cylinder constructor to accept and initialize the radius and height.Include getter and setter methods for all instance variables. Include methods that calculate and return the volume and surface area of the cylinder (see preceding problem for formulas). Include a toString method that returns a two-line description of the cylinder(radius and height on separate lines). Create a driver class, MultiCylinder, that instantiates and updates two Cylinder objects, printing their radius and height before and after modification, and prints the final volume and surface area of each cylinder
1) Cylinder .java
public class Cylinder {
final float pie =3.14f;
int radius;
int height;
//constructor to initialise values
public Cylinder(int radius, int height) {
super();
this.radius = radius;
this.height = height;
}
//getter setter of radius and height of cylinder
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
//volume function to calculate volume of
cylinder
public double volume()
{
return pie*radius*2*height;
}
//surface area function to calculate surface area of
cylinder
public double surfacearea()
{
return 2*pie*radius*(radius+
height);
}
//tostring method to print object in string
format
@Override
public String toString() {
return "Cylinder [pie=" + pie + ",
radius=" + radius + ", height=" + height + "]";
}
}
/////
MultiCylinder.java
public class MultiCylinder {
public static void main(String[] args) {
//instantiate objecs of cylinder and assigning the value radius and
height through constructor
Cylinder c1=new
Cylinder(5,143);
Cylinder c2=new
Cylinder(8,159);
System.out.println(c1.getRadius());
System.out.println(c1.getHeight());
System.out.println(c2.getRadius());
System.out.println(c2.getHeight());
System.out.println(c1);
System.out.println(c2);
//calling methods to print volume
and surface area of cylinder
System.out.println("volume of
cylinder 1 is"+c1.volume());
System.out.println("volume of
cylinder 2 is"+c2.volume());
System.out.println("Surface area of
cyinder 1 is"+c1.surfacearea());
System.out.println("Surface area of
cyinder 1 is"+c2.surfacearea());
}
}
//////
Below i am attaching the screenshots of running code so that you can refer to it
screenshot of main function
You can refer the above screnshots for your code identation.
Thank You!!!