ICSE Class 10 Computer Applications
Question 36 of 46
String Handling — Question 36
Back to all questions 36
Question Question 23
Design a class to overload a function compare( ) as follows:
- void compare(int, int) - to compare two integer values and print the greater of the two integers.
- void compare(char, char) - to compare the numeric value of two characters with higher numeric value.
- void compare(String, String) - to compare the length of the two strings and print the longer of the two.
import java.util.Scanner;
public class KboatCompare
{
public void compare(int a, int b) {
if (a > b) {
System.out.println(a);
}
else {
System.out.println(b);
}
}
public void compare(char a, char b) {
int x = (int)a;
int y = (int)b;
if (x > y) {
System.out.println(a);
}
else {
System.out.println(b);
}
}
public void compare(String a, String b) {
int l1 = a.length();
int l2 = b.length();
if (l1 > l2) {
System.out.println(a);
}
else {
System.out.println(b);
}
}
}Output





