Saturday, December 17, 2022

ICSE Function Overloading Program On Different Values

 A design a class to overload a function num_calc() as follows:


a) void num_calc(int num, 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.   


b) void num_calc(int a, int b, char ch) with two integer arguments and one character argument,

computes the product of integer arguments if ch is ā€˜p’ else adds the integers.


c) void num_calc(String s1, String s2) with two string arguments, which prints whether the 

strings are equal or not.


Program


                
class Diff
{
    public void num_calc(int num, char ch)
  {
        if(ch == 's' || ch == 'S')
      {
            int s = num * num;
            System.out.println("Square = " + s);
        }
        else
    {
            int c = num * num * num;
            System.out.println("Cube = " + c);
        }
    }
    public void num_calc(int a, int b, char ch)
{
        if(ch == 'p' || ch == 'P')
{
            int p = a * b;
            System.out.println("Product = " + p);
        }
        else
{
            int s = a + b;
            System.out.println("Sum = " + s);
        }
    }
    public void num_calc(String s1, String s2)
{
        if(s1.equalsIgnoreCase(s2))
            System.out.println("They are equal");
        else
            System.out.println("They are unequal");
    }
  public static void main(String args[])
    {
Diff ob=new Diff();
     ob.num_calc(1,'s');
      ob.num_calc(1,2,'p');
     ob.num_calc("java","j2se");
    }
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner