Solved programming problem

Find two Box Volume in Java by Creating Different Class.

Source code

Main Method:

package Box;

public class TestBox 

{

    public static void main(String[] args) 

   {

        Box1Input B1; ///create object of box input 

        Box2Input B2;

        B1 = new Box1Input(); 

        B1.inputBox1(); ///call the method 

        B2 = new Box2Input();

        B2.inputBox2();  ///call the method 

    }

}

  

Box1Input Class: 

package Box;

import java.util.Scanner;

public class Box1Input 

{

     double w,l,h;

     Volume V1;

     Scanner S = new Scanner(System.in);

     

     public void inputBox1()

     {

       System.out.println("Enter the 1st Box Length, Hight and Width: ");

        

       ///input box 1

       w = S.nextDouble();

       l = S.nextDouble();

       h = S.nextDouble();

          

       V1 = new Volume(l,w,h);  ///creat an object   

       V1. getVolume();  ///calculate the volume 

       System.out.println("Volumes Box 1: "+V1.returnVolume()); ///find the return value 

     }

    

}

 

 

Box2Input Class:

 

package Box;

 

import java.util.Scanner;

 

public class Box2Input 

{

   private  double w,l,h;

   Scanner S = new Scanner(System.in);

   Volume V2;

   

   public void inputBox2()

   {

     System.out.println("Enter the 2nd Box Length, Hight and Width: ");

     

     w = S.nextDouble();

     l = S.nextDouble();

     h = S.nextDouble();

     

     V2 = new Volume(l,w,h);

     V2. getVolume();

     

      System.out.println("Volume Box 2: "+V2.returnVolume());

     

   }

}

 

 

Volume Class:

 

package Box;

 

 

public class Volume 

{

    private double  length, width, height,area; 

    

    ///constructor

    public Volume(double length, double width, double height)

    {

        this.height = height;

        this.length = length;

        this.width  = width;    

    }

    

    public void getVolume()

    {

         area =  length * width * height ;                       

    }

    

    

    public double returnVolume()

    {

        return area;

    }

       

}

Output

Enter the 1st Box Length, Hight and Width:

2 4 1

Volumes Box 1: 8.0

Enter the 2nd Box Length, Hight and Width:

4.3 12 2.5

Volumes Box 2:

129.0

Join the discussion

Add your comment