Using function overloading compare two integers for greatest one, two characters for greatest ASCII value and two string for greatest length. Finally print the greatest value in that respective function only.
Take all required inputs from main functions and invoke the function using class object.
Java
import java.util.*;
public class Compare
{
public void doCompare(int a,int b)
{
if(a>b)
{
System.out.println("Greatest integer: "+a);
}
else
{
System.out.println("Greatest integer: "+b);
}
}
public void doCompare(char a,char b)
{
if(a>b)
{
System.out.println("Greatest ASCII valued character: "+a);
}
else
{
System.out.println("Greatest ASCII valued character: "+b);
}
}
public void doCompare(String a,String b)
{
if(a.length()>b.length())
{
System.out.println("Longest string: "+a);
}
else
{
System.out.println("Longest string: "+b);
}
}
public static void main(String args[])
{
int n1=0,n2=0;
char c1=' ',c2=' ';
String s1="",s2="";
Scanner sc=new Scanner(System.in);
Compare ob=new Compare();
System.out.println("Enter two numbers:-");
System.out.print("A: ");
n1=sc.nextInt();
System.out.print("B: ");
n2=sc.nextInt();
System.out.println("Enter two characters:-");
System.out.print("A: ");
c1=sc.next().charAt(0);
System.out.print("B: ");
c2=sc.next().charAt(0);
System.out.println("Enter two strings:-");
System.out.print("A: ");
s1=sc.nextLine();
sc.nextLine();
System.out.print("B: ");
s2=sc.nextLine();
ob.doCompare(n1,n2);
ob.doCompare(c1,c2);
ob.doCompare(s1,s2);
}
}Java