KnowledgeBoat Logo

Computer Applications

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 with higher numeric value.
  3. void compare(String, String) - to compare the length of the two strings and print the longer of the two.

Java

Java String Handling

2 Likes

Answer

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

BlueJ output of 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 value of two characters with higher numeric value. (c) void compare(String, String) - to compare the length of the two strings and print the longer of the two.BlueJ output of 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 value of two characters with higher numeric value. (c) void compare(String, String) - to compare the length of the two strings and print the longer of the two.BlueJ output of 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 value of two characters with higher numeric value. (c) void compare(String, String) - to compare the length of the two strings and print the longer of the two.BlueJ output of 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 value of two characters with higher numeric value. (c) void compare(String, String) - to compare the length of the two strings and print the longer of the two.BlueJ output of 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 value of two characters with higher numeric value. (c) void compare(String, String) - to compare the length of the two strings and print the longer of the two.BlueJ output of 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 value of two characters with higher numeric value. (c) void compare(String, String) - to compare the length of the two strings and print the longer of the two.

Answered By

2 Likes


Related Questions