Computer Applications

Design a class to overload a function num_calc( ), Function Overloading Program

  • Design a class to overload a function num_calc( ) as follows:
    1. 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.
    2. void num_calc( int a, int b, char ch ) with two integer argument and one character argument. It computes the product of integer arguments if ch is ‘p’ else adds the integers.
    3. void num_calc( Sting s1, String s2) with two string arguments, which prints whether the strings are equal or not.

import java.io.*;
import java.util.*;

class Overloaded1
{
	void num_calc( int num, char ch )
	{
		int s = 0;
		if(ch == 's' )
			s = num * num;
		else
			s = num * num * num;
		System.out.println(" s = " + s );
	}
	void num_calc( int a, int b, char ch )
	{
		int s = 0;
		if(ch == 'p' )
			s = a * b;
		else
			s = a + b;
		System.out.println(" s = " + s );
	}
	void num_calc( String s1, String s2 )
	{
		if(s1.equals(s2))
			System.out.println("Both Strings are equal.");
		else
			System.out.println("Both Strings are not equal.");
	}
  public static void main(String args[ ])
  {
  	Scanner sc = new Scanner(System.in);
  	Overloaded1 ob = new Overloaded1( );
  	ob.num_calc( 5, 's' );
  	ob.num_calc( 8, 3, 'n' );
  	ob.num_calc( "Raman", "Naman" );
  }
}

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top