Thursday, November 5, 2015


Volume Of A Cylinder Calculator in Java

The program below is the answer to Liang's Introduction to Java Programming (9th Edition) Chapter 2 Exercise 2.2.
 * @Author: Aghatise Osazuwa
 * Website:
 * 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.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.


  1. 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?

    1. The 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
      System.out.printf ("%s%.4f%s%.1f%s", "The Area is ", area, "\nThe Volume is ", volume, "\n");


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately. To get notified of replies or follow-up comments, click the box next to notify me.