Computer Applications

Design a class to overload a function num_calc() as follows:

  1. void num_calc(int mini, char ch) with one integer argument and one character argument, computes the square of integer argument if choice ch is 's' otherwise finds its cube.
  2. void num_calc (int a, int b, char ch) with two integer arguments and one character argument. It computes the product of integer arguments if ch is 'p' else adds the integers.
  3. void num_calc (String s1, String s2) with two string arguments, which prints whether the strings are equal or not.

Java

Java String Handling

16 Likes

Answer

import java.util.Scanner;

public class KboatChoiceOverload
{
    void num_calc(int mini, char ch) {
        if (ch == 's') {
            long sq = mini * mini;
            System.out.println("Square = " + sq );
        }
        else {
            long cube = mini * mini * mini;
            System.out.println("Cube = " + cube);
        }
    }
    
    void num_calc(int a, int b, char ch) {
        if (ch == 'p') {
            long prod = a * b;
            System.out.println("Product = " + prod );
        }
        else {
            long sum = a + b;
            System.out.println("Sum = " + sum);
        }
    }
    
    void num_calc(String s1, String s2)    {
        if(s1.equals(s2))   
            System.out.println("Strings are equal");
        else
            System.out.println("Strings are not equal");
    }
    
}

Output

Answered By

3 Likes


Related Questions