/** * * @Author: Aghatise Osazuwa * Website: www.cscprogrammingtutorials.com * * Exercise 2.2 - Compute The Volume Of A Cylinder * */ import java.util.Scanner; public class VolumeOfCylinder { public static void main (String [] args) { Scanner input = new Scanner(System.in); System.out.println("This Program Computes The Area And Volume Of A Cylinder.\n"); System.out.println("Enter the radius and length of a cylinder separated " + "by a space or by pressing the ENTER key: "); double radius = input.nextDouble(); double length = input.nextDouble(); double area = radius * radius * Math.PI; double volume = area * length; System.out.printf ("%s%.2f%s%.2f%s", "The Area is ", area, "\nThe Volume is ", volume, "\n"); } }

Click here to see other solutions to Introduction to Java Programming.

part of this exercise is to use type casting to truncate the area's decimal places so the answer won't be as long. The area is shown with only 4 decimal numbers and the volume shows only one. Would you happen to know how to add that to the computation?

ReplyDeleteThe question did not specify how many decimal places the answer should be. But if you want yours to be like the sample run used in the textbook, you only need to change the format of the System.out.printf statement like this

DeleteSystem.out.printf ("%s%.4f%s%.1f%s", "The Area is ", area, "\nThe Volume is ", volume, "\n");