Basics

Define a class called Cuboid, Class and Object based Program

Define a class called Cuboid with the following description:

Instance variables/Data members:          length, breadth, height

Member methods:

  • input( ) – to input the data members values
  • display( )             – to display the values of data members
  • volume( ) – to calculate the volume of 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;
	
	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