Design a class to overload a function compare( ) as follows:

ICSE Class 10 Computer Applications PYQ · 2011 Paper · Section B

8Written · 2011 · Section B · 2011 Paper

Design a class to overload a function compare( ) as follows:

Show SolutionHide Solution↓
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);
        }

    }
    
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        KboatCompare obj = new KboatCompare();
        
        System.out.print("Enter first integer: ");
        int n1 = in.nextInt();
        System.out.print("Enter second integer: ");
        int n2 = in.nextInt();
        obj.compare(n1, n2);
        
        System.out.print("Enter first character: ");
        char c1 = in.next().charAt(0);
        System.out.print("Enter second character: ");
        char c2 = in.next().charAt(0);
        in.nextLine();
        obj.compare(c1, c2);
        
        System.out.print("Enter first string: ");
        String s1 = in.nextLine();
        System.out.print("Enter second string: ");
        String s2 = in.nextLine();
        obj.compare(s1, s2);
    }
}
BlueJ output of KboatCompare.java
← Previous questionNext question →

Browse the free Computer Applications sample bank