KnowledgeBoat Logo

Computer Applications

Design a class overloading a method calculate() as follows:

  1. void calculate(int m, char ch) with one integer argument and one character argument. It checks whether the integer argument is divisible by 7 or not, if ch is 's', otherwise, it checks whether the last digit of the integer argument is 7 or not.
  2. void calculate(int a, int b, char ch) with two integer arguments and one character argument. It displays the greater of integer arguments if ch is 'g' otherwise, it displays the smaller of integer arguments.

Java

User Defined Methods

83 Likes

Answer

import java.util.Scanner;

public class KboatCalculate
{
    public void calculate(int m, char ch) {
        if (ch == 's') {
            if (m % 7 == 0)
                System.out.println("It is divisible by 7");
            else
                System.out.println("It is not divisible by 7");
        }
        else {
            if (m % 10 == 7)
                System.out.println("Last digit is 7");
            else
                System.out.println("Last digit is not 7");
        }
    }
    
    public void calculate(int a, int b, char ch) {
        if (ch == 'g')
            System.out.println(a > b ? a : b);
        else
            System.out.println(a < b ? a : b);
    }
    
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        KboatCalculate obj = new KboatCalculate();
        
        System.out.print("Enter a number: ");
        int n1 = in.nextInt();
        obj.calculate(n1, 's');
        obj.calculate(n1, 't');
        
        System.out.print("Enter first number: ");
        n1 = in.nextInt();
        System.out.print("Enter second number: ");
        int n2 = in.nextInt();
        obj.calculate(n1, n2, 'g');
        obj.calculate(n1, n2, 'k');
        
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Design a class overloading a function calculate() as follows: (a) void calculate(int m, char ch) with one integer argument and one character argument. It checks whether the integer argument is divisible by 7 or not, if ch is 's', otherwise, it checks whether the last digit of the integer argument is 7 or not. (b) void calculate(int a, int b, char ch) with two integer arguments and one character argument. It displays the greater of integer arguments if ch is 'g' otherwise, it displays the smaller of integer arguments.

Answered By

19 Likes


Related Questions