Computer Science

Design a class 'Cuboid' with the following specifications:
class Cuboid
Data members/Instance variables
int len, br, ht, vol;
Member methods:
void input( ) : to accept length, breadth and height of a cuboid.
void calculate( ) : to calculate volume of a cuboid.
void display( ) : to print volume of cuboid.
Write a main function to create an object of class 'Volume' and call the member methods.

Objects & Classes

19 Likes

Answer

import java.util.Scanner;

class Cuboid {
    int len;
    int br;
    int ht;
    int vol;

    void input() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter length of Cuboid: ");
        len = in.nextInt();
        System.out.print("Enter breadth of Cuboid: ");
        br = in.nextInt();
        System.out.print("Enter height of Cuboid: ");
        ht = in.nextInt(); 
    }

    void calculate() {
        vol = len * br * ht;
    }

    void display() {
        System.out.print("Volume of Cuboid = " + vol);
    }

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

Answered By

10 Likes


Related Questions