Design a class to overload a function compare() as follows:
(a) void compare(int, int): to compare two integer values and print the greater of the two integers.
(b) void compare(char, char): to compare the numeric values of two characters and print the character
with higher numeric value.
(c) void compare(String, String): to compare the length of the two strings and print the longer of
the two.
Program
class Comp
{
public void compare(int a, int b)
{
int large = (a > b)? a : b;
System.out.println(large);
}
public void compare(char a, char b)
{
char large = (a > b)? a : b;
System.out.println(large);
}
public void compare(String a, String b)
{
String large = (a.length() > b.length())? a : b;
System.out.println(large);
}
public static void main(String args[])
{
Comp ob=new Comp();
ob.compare(9,18);
ob.compare('o','x');
ob.compare("Neymar","Messi");
}
}
{
public void compare(int a, int b)
{
int large = (a > b)? a : b;
System.out.println(large);
}
public void compare(char a, char b)
{
char large = (a > b)? a : b;
System.out.println(large);
}
public void compare(String a, String b)
{
String large = (a.length() > b.length())? a : b;
System.out.println(large);
}
public static void main(String args[])
{
Comp ob=new Comp();
ob.compare(9,18);
ob.compare('o','x');
ob.compare("Neymar","Messi");
}
}
No comments:
Post a Comment