Computer Science
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.
Answer
import java.util.Scanner;
class Display {
int a;
int b;
int c;
void Accept() {
Scanner in = new Scanner(System.in);
System.out.print("Enter value of a: ");
a = in.nextInt();
System.out.print("Enter value of b: ");
b = in.nextInt();
}
void Max() {
c = a > b ? a : b;
System.out.println("Greater Number = " + c);
}
void Min() {
c = a < b ? a : b;
System.out.println("Smaller Number = " + c);
}
void Diff() {
c = a - b;
System.out.println("Difference = " + c);
}
public static void main(String args[]) {
Display obj = new Display();
obj.Accept();
obj.Max();
obj.Min();
obj.Diff();
}
}
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.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.