Computer Applications

Define a class called Cuboid, Constructor based program

Define a class called Cuboid with the following description:

Instance variables/Data members:          length, breadth, height

Member methods:

  • Cuboid( )     – default constructor to initialize the data members with zero.
  • input( )        – to input the data members values
  • display( )    – to display the values of data members
  • volume( )    – to calculate the volume of the Cuboid

Write a main( ) method where user will create an object of the class and call all the member functions.

import java.io.*;
import java.util.*;

class Cuboid
{
	int length;
	int breadth;
	int height;
	
	Cuboid( )                      //constructor
	{
		length = 0;
		breadth = 0;
		height = 0;
	}

	void input( )
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter length =");
		length = sc.nextInt();
		System.out.println("Enter breadth =");
		breadth = sc.nextInt();
		System.out.println("Enter height =");
		height = sc.nextInt();
	}

	void display( )
	{
		System.out.println("Length = "+length);
		System.out.println("Breadth = "+breadth);
		System.out.println("Height = "+height);
	}

	void volume( )
	{
		int vol = length*breadth*height;
		System.out.println("Volume of Cuboid ="+vol);
	}

	public static void main(String args[ ])
	{
		Cuboid pan = new Cuboid( );
		pan.input( );
		pan.display( );
		pan.volume( );
	}
}

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top