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.
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();
}
}
Related Questions
An object is referred to as an instance of a class. Explain.
Class and Object are inter-related. Justify this statement.
Design a class 'AddDist' with the following specifications: class AddDist
Data members/Instance variables
int km, mts, cm
Member methods:
void getdist( ) : to accept a distance in km, mts and cm. void showdist( ) : to display the distance in km, mts and cm.
Write a main function to create an object of class 'Add_Dist' and call the member methods.Write a class template 'Calculator' with the following specifications:
Class : Display
Data members/Instance variables
int a,b,c;
Member Methods:Name Purpose void Accept() to accept the values of a and b. void Max() to find greater of the two number 'a' and 'b' and store the result in c. Display the result. void Min() to find smaller of the two number 'a' and 'b' and store the result in c. Display the result. void Diff() to store the difference between 'a' and 'b' and store the result in c. Display the result. Write a main class to create an object of class 'Display' and call the member methods.