Coders Packet

Volume calculator using Java

By TANUJA.D

This is a java implementation packet for calculating the volume of various geometric figures. This is developed using Netbeans IDE.

This packet calculates the volume of geometric figures by using basic formulas.

It uses a switch statement that allows the user to select the figure for which the volume is calculated. 

If the user enters invalid input, it prints an "invalid input message" by default.

KEY FORMULAS TO BE KNOWN:

1. Volume of the sphere = (4/3)*π*Radius*Radius*Radius

2. Volume of the cone = (1/3)*π*Base Radius*Base Radius*Height

3. Volume of the cube = Edge Length*Edge Length*Edge Length

4. Volume of the cylinder = π*Radius*Radius*Height

5. Volume of the capsule = [(4/3)*π*Base Radius*Base Radius*Base Radius]+[π*Base Radius*Base Radius*Height]

6. Volume of the conical frustrum=(1/3)*π*Height[(Top Radius*Top Radius)+(Top Radius*Bottom Radius)+(Bottom Radius*Bottom Radius)]

7. Volume of the ellipsoid = (4/3)*π*(Axis 1*Axis 2*Axis 3)

8. Volume of the square pyramid = (1/3)*Base Edge*Base Edge*Height

9. Volume of the rectangular tank = Length*Width*Height

10. Volume of the tube = π*[((Outer Diameter*Outer Diameter)-(Inner Diameter*Inner Diameter))/4]*Length

Here, π=3.14285714286 and the other dimensions are obtained from the user.

SOURCE CODE:

import java.util.Scanner;

public class VolumeCalculator {
   public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("List"+"\n\n"+
        "0. To calculate the volume of the sphere"+"\n"+
        "1. To calculate the volume of the cone"+"\n"+
        "2. To calculate the volume of the cube"+"\n"+
        "3. To calculate the volume of the cylinder"+"\n"+
        "4. To calculate the volume of the capsule"+"\n"+
        "5. To calculate the volume of the conical frustrum"+"\n"+
        "6. To calculate the volume of the ellipsoid"+"\n"+
        "7. To calculate the volume of the square pyramid"+"\n"+
        "8. To calculate the volume of the rectangular tank"+"\n"+
        "9. To calculate the volume of the tube"+"\n\n");
System.out.print("Enter a number from the list: ");
int calc=s.nextInt();
System.out.println("\n");
switch(calc)
{
    case 0:
    {
        System.out.println("SPHERE"+"\n");
        System.out.print("Enter the radius of the sphere in feet: ");
        double sphereradius=s.nextDouble();
        double sphere=(4.0/3.0)*3.14285714286*(sphereradius*sphereradius*sphereradius);
        System.out.println("\n"+"The volume of the sphere is "+sphere+" "+"feet cube");
        break;
    }
        
    case 1:
    {
    System.out.println("CONE"+"\n");
    System.out.print("Enter the base radius of the cone in feet: ");
    double conebaseradius=s.nextDouble();
    System.out.print("Enter the height of the cone in feet: ");
    double coneheight=s.nextDouble();
    double cone=(1.0/3.0)*3.14285714286*(conebaseradius*conebaseradius)*coneheight;
    System.out.println("\n"+"The volume of the cone is "+cone+" "+"feet cube");
    break;
    }
    
    case 2:
    {
    System.out.println("CUBE"+"\n"); 
    System.out.print("Enter the edge length of the cube in feet: ");
    double cubeedgelength=s.nextDouble();
    double cube=cubeedgelength*cubeedgelength*cubeedgelength;
    System.out.println("\n"+"The volume of the cube is "+cube+" "+"feet cube");
    break;
    }
    
    case 3:
    {
    System.out.println("CYLINDER"+"\n");
    System.out.print("Enter the base radius of the cylinder in feet: ");
    double cylinderbaseradius=s.nextDouble();
    System.out.print("Enter the height of the cylinder in feet: ");
    double cylinderheight=s.nextDouble();
    double cylinder=3.14285714286*(cylinderbaseradius*cylinderbaseradius)*cylinderheight;
    System.out.println("\n"+"The volume of the cylinder is "+cylinder+" "+"feet cube");
    break;
    }
    
    case 4:
    {
    System.out.println("CAPSULE"+"\n");
    System.out.print("Enter the base radius of the capsule in feet: ");
    double capsulebaseradius=s.nextDouble();
    System.out.print("Enter the height of the capsule in feet: ");
    double capsuleheight=s.nextDouble();
    double capsule=((4.0/3.0)*3.14285714286*(capsulebaseradius*capsulebaseradius*capsulebaseradius)+(3.14285714286*(capsulebaseradius*capsulebaseradius)*capsuleheight));
    System.out.println("\n"+"The volume of the capsule is "+capsule+" "+"feet cube");
    break;
    }
    
    case 5:
    {
    System.out.println("CONICAL FRUSTRUM"+"\n");
    System.out.print("Enter the top radius of the conical frustrum in feet: ");
    double frustrumtopradius=s.nextDouble();
    System.out.print("Enter the bottom radius of the conical frustrum in feet: ");
    double frustrumbottomradius=s.nextDouble();
    System.out.print("Enter the height of the conical frustrum in feet: ");
    double frustrumheight=s.nextDouble();
    double frustrum=(1.0/3.0)*3.14285714286*frustrumheight*((frustrumtopradius*frustrumtopradius)+(frustrumtopradius*frustrumbottomradius)+(frustrumbottomradius*frustrumbottomradius));
    System.out.println("\n"+"The volume of the conical frustrum is "+frustrum+" "+"feet cube");
    break;
    }
    
    case 6:
    {
    System.out.println("ELLIPSOID"+"\n");
    System.out.print("Enter the axis 1 of the ellipsoid in feet: ");
    double ellipsoidaxis1=s.nextDouble();
    System.out.print("Enter the axis 2 of the ellipsoid in feet: ");
    double ellipsoidaxis2 =s.nextDouble();
    System.out.print("Enter the axis 3 of the ellipsoid in feet: ");
    double ellipsoidaxis3=s.nextDouble();
    double ellipsoid=(4.0/3.0)*3.14285714286*(ellipsoidaxis1*ellipsoidaxis2*ellipsoidaxis3);
    System.out.println("\n"+"The volume of the ellipsoid is "+ellipsoid+" "+"feet cube");
    break;
    }
    
    case 7:
    {
    System.out.println("SQUARE PYRAMID"+"\n");
    System.out.print("Enter the base edge of the square pyramid in feet: ");
    double pyramidbaseedge=s.nextDouble();
    System.out.print("Enter the height of the square pyramid in feet: ");
    double pyramidheight=s.nextDouble();
    double sqpyramid =(1.0/3.0)*(pyramidbaseedge*pyramidbaseedge)*pyramidheight;
    System.out.println("\n"+"The volume of the square pyramid is "+sqpyramid+" "+"feet cube");
    break;
    }
    
    case 8:
    {
    System.out.println("RECTANGULAR TANK"+"\n");
    System.out.print("Enter the length of the rectangular tank in feet: ");
    double tanklength=s.nextDouble();
    System.out.print("Enter the width of the rectangular tank in feet: ");
    double tankwidth=s.nextDouble();
    System.out.print("Enter the height of the rectangular tank in feet: ");
    double tankheight=s.nextDouble();
    double tank= tanklength*tankwidth*tankheight;
    System.out.println("\n"+"The volume of the rectangular tank is "+tank+" "+"feet cube");
    break;
     }
    
    case 9:
    {
        System.out.println("TUBE"+"\n");
        System.out.print("Enter the outer diameter of the tube in feet: ");
        double tubeout=s.nextDouble();
        System.out.print("Enter the inner diameter of the tube in feet: ");
        double tubein=s.nextDouble();
        System.out.print("Enter the length of the tube in feet: ");
        double tubelength=s.nextDouble();
        double tube=3.14285714286*(((tubeout*tubeout)-(tubein*tubein))/4.0)*tubelength;
        System.out.println("\n"+"The volume of the tube is "+tube+" "+"feet cube");
        break;
    }

    default :
    {
        System.out.println("Invalid input number");
        break;
    }

    }
  }
}

SAMPLE OUTPUT:

VOLUME CALCULATOR

Download project

Reviews Report

Submitted by TANUJA.D (Tanuja)

Download packets of source code on Coders Packet