Using jave (jGRASP ) I need the codes to run this program Surface Area of a Cyli
ID: 3836415 • Letter: U
Question
Using jave (jGRASP )
I need the codes to run this program
Surface Area of a Cylinder
The surface area of a cylinder is written as:
A = 2rh + 2r2
Write a program that prompts the user for a radius and height of a cylinder and uses a method to calculate the surface area.
You will need:
A scanner object to read the radius and the height of the cylinder
Variables to store:
The radius (as a double)
The height (as a double)
(Optional: The area, which stores the returned area from the method. Again, keep in mind that you can use the method call directly in an output statement.)
A method that:
Accepts 2 parameters – the radius and height
Calculates the area
Returns the area
Comments where necessary
Tip: Use the PI constant (from the Math Class) – i.e. Math.PI - for .
A sample of the output is shown below:
Enter the cylinder's radius: 5.5
Enter the cylinder's height: 10
The surface area of a cylinder is: 535.6415474370597
Explanation / Answer
// Cylinder.java
import java.util.Scanner;
class Cylinder
{
public static double getArea(double radius, double height)
{
double area = 2*Math.PI*radius*height + 2*Math.PI*radius*radius;
return area;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the cylinder's radius: ");
double radius = sc.nextDouble();
System.out.println("Enter the cylinder's height: ");
double height = sc.nextDouble();
double area = getArea(radius,height);
System.out.println("The surface area of a cylinder is: " + area);
}
} output
/*
output:
Enter the cylinder's radius:
5.5
Enter the cylinder's height:
10
The surface area of a cylinder is: 535.6415474370597
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.