icse-promo

Question

 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 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.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					public class program8
{

    public void compare(int a, int b)
    {
        int max = Math.max(a, b);
        System.out.println(max);
    }

    public void compare(char a, char b)
    {
        char max = (char) Math.max(a, b);
        System.out.println(max);
    }

    public void compare(String a, String b)
    {
        int max=Math.max(a.length(),b.length());
        
        if (max == a.length()) 
        {
            System.out.println(a+" string has greater length than "+b);
        } 
        else 
        {
            System.out.println(b+" string has greater length than "+a);
        } 
        
    }
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *