Computer Science

Write a class template 'Calculator' with the following specifications:
Class : Display
Data members/Instance variables
int a,b,c;
Member Methods:

NamePurpose
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.

Objects & Classes

7 Likes

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();
    }
}

Answered By

3 Likes


Related Questions