Question

In: Computer Science

Write an application, CylinderStats, that reads the radius and height of a cylinder and prints its...

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

Solutions

Expert Solution

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!!!


Related Solutions

Write a program that reads in the radius and length of a cylinder and computes volume...
Write a program that reads in the radius and length of a cylinder and computes volume using the following formulas: area = radius * radius * PI volume = area * length
Design and implement an application that reads an integer value and prints the sum of all...
Design and implement an application that reads an integer value and prints the sum of all even integers between 2 and the input value, inclusive. Print an error message if the input value is less than 2 and prompt accordingly so that the user can enter the right number. Your program file will be called YourLastNameExamQ2
Write a program that reads and prints a joke and its punch line from two different...
Write a program that reads and prints a joke and its punch line from two different files. The first file contains a joke, but not its punch line. The second file has the punch line as its last line, preceded by “garbage.” The main function of your program should open the two files then call two functions, passing each one the file it needs. The first function should read and display each line in the file it is passed (the...
Optimization of a cylinder container with a height of 4 inches and a radius of 1.25...
Optimization of a cylinder container with a height of 4 inches and a radius of 1.25 inches in terms of either volume OR surface area. Please provide: (A) Calculation of surface area/volume of the container. (B) Primary and secondary constraint equations (C) Derivative of primary equation (D) Optimized dimensions and how they were determined (number-line analysis to show that the dimensions result in an optimized solution. (E) Graph of optimization (in terms of volume OR surface area in terms of...
The volume of a right circular cylinder with base radius ? and height ℎ is given...
The volume of a right circular cylinder with base radius ? and height ℎ is given by: ? = ??^2ℎ. If the base radius is decreasing at a rate of 3 inches per minute and the height is increasing at a rate of 2 inches per minute, at what rate is the volume of the cylinder changing when the radius is 8 inches and the height is 3 inches. Will the volume be increasing or decreasing at this instant? Be...
Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
Write a pyhton program that reads a stream of bits, e.g. 11001, and calculates and prints...
Write a pyhton program that reads a stream of bits, e.g. 11001, and calculates and prints the decimal value of the binary number represented by the entered bits, i.e. 25 in this case.
Write a c program that reads a .img file by rows and columns and prints out...
Write a c program that reads a .img file by rows and columns and prints out the arrays. The .img file contains h(the height) and w(the width) of the text size. An example .img file would be: 2 4 DFJSK HJ5JF HFDY5
Write an application that prints a table of the binary and octal equivalent of the decimal...
Write an application that prints a table of the binary and octal equivalent of the decimal numbers in the range 1 through 256. **Write in JAVA**
USING MATLAB: Create a BMI calculator application that reads the user’s weight in pounds and height...
USING MATLAB: Create a BMI calculator application that reads the user’s weight in pounds and height in inches (or, if you prefer, the user’s weight in kilograms and height in meters), then calculates and displays the user’s body mass index. Also, the application should display the following information from the Department of Health and Human Services/National Institutes of Health so the user can evaluate his/her BMI: BMI VALUES ("Underweight : Less than 18.5 Normal : between 18.5 and 24.9 Overweight...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT