Wednesday, 28 October 2020

Design a class to overload a function num_calc() as follows  void num_calc(int num,char ch ) with one integer argument and one character argument, computes the cube of integer argument if choice ch is ‘c’/ ‘C’ otherwise finds its square.  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 void num_calc(String s1,String s2) with two string arguments,which prints whether the strings are equal or not

 import java.util.*;
public class assignment_9
{
  void num_calc(int num,char ch)
   {
      int cube =1;
      int sq = 1;
       if(ch=='c' || ch=='C') 
      
       {cube = num*num*num;
       System.out.println("Cube of " + num + " = "+ cube);
       }
      else
       {sq = num*num;
       System.out.println("Square of " + num + " = "+ sq);
       }
   }
  void num_calc(int a,int b,char ch)
   {
      int prod =1;
      int sum = 0;
       if(ch=='p')
        
       {prod = a*b;
       System.out.println("Product of " + a + " and "+ b+" = "+ prod);
       }
      else
      {sum=a+b;
       System.out.println("Sum of "+ a + " and "+ b+" = "+ sum);   
       }
   }
  void num_calc(String s1,String s2)
   {
       if(s1.equals(s2))
      
       System.out.println("The strings are equal");
       else
       System.out.println("The strings are not equal");
    }
  public static void main(String [] args)
    {
      Scanner sc = new Scanner(System.in);
      System.out.print("Enter the integer for the first function : ");
      int a = sc.nextInt();
      System.out.print("Enter the character for the first function : ");
      char c = sc.next().charAt(0);
      System.out.println("Enter two integers for the second function : ");
      int n1 = sc.nextInt();
      int n2 = sc.nextInt();
      System.out.print("Enter the character for the second function : ");
      char ch = sc.next().charAt(0);
      System.out.println("Enter the first string for the third function : ");
      String st1 = sc.next();
      System.out.println("Enter the second string for the third function : ");
      String st2 = sc.next();
      assignment_9 obj = new assignment_9();
      obj.num_calc(a,c);
      obj.num_calc(n1,n2,ch);
      obj.num_calc(st2,st1);
    }
 }

No comments:

Post a Comment