Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to calculate the volume and surface area of a cylinder. Allow th

ID: 3700700 • Letter: W

Question

Write a program to calculate the volume and surface area of a cylinder. Allow the user to input the data initially, validate user's input only when all input data are positive the program will calculate. If the data is not valid, all user to input again, maximum three times. Allow the user to change the radius and height of a cylinder. In the TestCylinder class, you need to create objects using different constructors at least once. for instance Cyelinder cyd new CyclinderO: sure the user can input the radius and For each object, make height with valid values and display the final volume and surface area. You need two constructors for the Cylinder class: Cylinder) and Cylinder(double r, double h) You need to define following method in the Cylinder class: /lcalculate the volume of a cylinder with radius of I, and height of h public void findVolume(double I, double h) lcalculate the volume of a cylinder public void findVolume0 l all accessor methods public double findRadius) public double findHeighto Il all mutator methods to change the radius and the height of the cylinder. The input data is from user. public void setLength double I) public void setHeith(double h) In the TestCylinder class: Create two Cylinder object using different constructor You need to display the volume of two Cylinder objects in GUI A class diagram in UML notation is shown in following: Cvlinder radius: double height: double TestCvlinder -PI: double main( Stringl): Cylindero Cylinder(double, getRadius0: double t getHeight0: double + setRadius(double): void setHeight(double): void + findVolume): double + findVolume(double le): double + findSurArea0: double

Explanation / Answer


//Code to copy
//TestCylinder.java
import java.util.Scanner;
public class TestCylinder {

   public static void main(String[] args) {

       //Create an instance of Cylinder class
       Cylinder cyd1=new Cylinder();
       Scanner scan=new Scanner(System.in);

       double radius;
       double height;
       int counter=0;
      
       //read radius and height
       do
       {          
           System.out.println("Enter radius: ");
           radius=Double.parseDouble(scan.nextLine());          
           System.out.println("Enter height: ");
           height=Double.parseDouble(scan.nextLine());

           if(radius<0 && height<0)
           {
               counter++;
               System.out.println("***Invalid Input***");
           }          
       }while(radius<0 && height<0 && counter<3);
      
       //set radius and height
       cyd1.setRadius(radius);
       cyd1.setHeight(height);
      
      
       Cylinder cyd2=new Cylinder(5, 10);
       Cylinder cyd3=new Cylinder();
       cyd3.findVolume(5, 10);
      
       //print volume and surface area
       System.out.println("Cylinder1");
       System.out.println("Volume : "+cyd1.findVolume());
       System.out.println("Surface area : "+cyd1.findSurfaceArea());
      
       //print volume and surface area
       System.out.println("Cylinder2");
       System.out.println("Volume : "+cyd2.findVolume());
       System.out.println("Surface area : "+cyd2.findSurfaceArea());
      
       //print volume and surface area
       System.out.println("Cylinder3");
       System.out.println("Volume : "+cyd3.findVolume());
       System.out.println("Surface area : "+cyd3.findSurfaceArea());

   }
}

---------------------------------------------------

//Cylinder.java
public class Cylinder {
  
   private double radius;
   private double height;
   private double PI;
   private double volume;
  
   public Cylinder() {
       radius=0;
       height=0;
       PI=Math.PI;
       volume=0;
   }
  
   public Cylinder(double r,double h) {
       radius=r;
       height=h;
       PI=Math.PI;
       volume=0;
   }
  
   public double getRadius(){
       return radius;
   }
  
   public double getHeight(){
       return height;
   }
  
   public void setRadius(double r){
       radius=r;
   }
  
   public void setHeight(double h){
       height=h;
   }
   //find volume of cylinder
   public double findVolume(){
       volume=PI*radius*radius*height;
       return volume;
   }
   //find volume of cylinder if radius and height is given
   public double findVolume(double l, double h){
       volume=PI*l*l*h;
       return volume;
   }
   //find surface area of cylinder
   public double findSurfaceArea(){
       return 2*PI*radius*(radius+height);
   }

}

---------------------------------------------------

Sample Output:

Enter radius:
5
Enter height:
10
Cylinder1
Volume : 785.3981633974483
Surface area : 471.23889803846896
Cylinder2
Volume : 785.3981633974483
Surface area : 471.23889803846896
Cylinder3
Volume : 0.0
Surface area : 0.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote