Basics

Design a class to overload a function compare( ) , Function Overloading based program

  • Design a class to overload a function compare( ) as follows:
    1. void compare(int, int)  – to compare two integer values and print the greater of the two integers.
    2. void compare(char, char)  – to compare the numeric value of two characters and print the character with higher numeric value.
    3. void compare(String, String)  – to compare the length of the two strings and print the longer of the two.

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

class Overloaded
{
	void compare( int x, int y)
	{
		int max = 0;
		if( x > y )
			max = x ;
		else
			max = y ;	
		System.out.println(" The largest number is =" + max );
	}
	void compare( char c1, char c2 )
	{
		char max;
		if( c1 > c2 )
			max = c1 ;
		else
			max = c2 ;	
		System.out.println(" The largest character is =" + max );
	}
	void compare( String s1, String s2 )
	{
		int max = 0;
		if(s1.length() > s2.length())
			System.out.println("The largest string is =" + s1);
		else
			System.out.println("The largest string is =" + s2);
	}
  public static void main(String args[ ])
  {
  	Scanner sc = new Scanner(System.in);
  	Overloaded ob = new Overloaded( );
  	ob.compare( 5, 8 );
  	ob.compare( 'B', 'K' );
  	ob.compare( "Raman", "Namans" );
  }
}

Click to comment

Leave a Reply

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

To Top