ICSE Class 10 Computer Applications Question 35 of 46

String Handling — Question 35

Back to all questions
35
Question

Question 22

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.
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
BlueJ output of KboatChoiceOverload.java
BlueJ output of KboatChoiceOverload.java
BlueJ output of KboatChoiceOverload.java
BlueJ output of KboatChoiceOverload.java
BlueJ output of KboatChoiceOverload.java
BlueJ output of KboatChoiceOverload.java
Answer