USING JAVA PROGRAMMING A circular prism, or cylinder, has a radius and height, s
ID: 3743073 • Letter: U
Question
USING JAVA PROGRAMMING
A circular prism, or cylinder, has a radius and height, see the
diagram on the right. Write a Java program that prompts and
reads from the user an integer value for the radius and
likewise an integer value for the height. Then the program will
calculate the total surface area and finally display the values
for the radius, height, and the total surface area. We note that
the total surface area for the circular prism on the right is 2r
2
+ 2rh.
If a student does the work merely within the main method, the work will not receive
more than 2/3 of the total allocated marks. To achieve the full mark, students need to
create a constructor that takes radius and height as the parameters to create a cylinder
object, and create a method for the class to do the calculation of the surface area.
Explanation / Answer
class Cylinder { private double radius; private double height; public Cylinder(double radius, double height) { this.radius = radius; this.height = height; } public double getRadius() { return radius; } public double getHeight() { return height; } public double getSurfaceArea() { return 2 * Math.PI * radius * radius + 2 * Math.PI * radius * height; } } import java.util.Scanner; public class CylinderDriver { public static void main(String[] args) { Scanner in = new Scanner(System.in); double radius, height; System.out.print("Enter radius: "); radius = in.nextDouble(); System.out.print("Enter height: "); height = in.nextDouble(); Cylinder cylinder = new Cylinder(radius, height); System.out.println("Surface are is " + cylinder.getSurfaceArea()); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.