Computer Science, asked by debskd, 9 months ago

Program 5: (Method Overloading)

Specify the class Check with following overloaded functions:

(i)void compare(int x, int y) – to compare and print smallest from x, y. If both the numbers are same then print suitable message.

(ii)void compare(char, char) – to compare the numeric value of the two characters and print the character with higher numeric value. If both characters are same then print any one of them.

Write a main method to call the overloaded function.

Answers

Answered by sthiti494
12

Answer:

//Program to overload a method to do the above mentioned

import java.io.*;

import java.util.*;

class Check{

//Smallest of the two

public static void compare(int x,int y){

if(x<y){

System.out.println(x+"is the smallest of the two.");

}

else if(x>y){

System.out.println(y+"is the smallest of the two.");

}

else{

System.out.println(x+"and"+y+"are same.");

}

}   //Closing of first method

//Highest numeric value

public static void compare(char ch1,char ch2){

//Declaring the variables

int i,j;

i=(char)ch1;   //Storing the numeric value of ch1

j=(char)ch2;   //Storing the numeric value of ch2

if(i>j){

System.out.println(ch1+"has the highest numeric value.");

}

else if(i<j){

System.out.println(ch2+"has the highest numeric value.");

}

else{

System.out.println("Both have the same numeric value.");

System.out.println(ch1);

}

}  //Closing of the second method

//main( ) begins

public static void main(String args[ ]){

Scanner sc = new Scanner(System.in);

//Declaring the variables

int x1,y1; char c,ch;

System.out.println("Enter the first integer:");

x1=sc.nextInt( );

System.out.println("Enter the second integer:");

y1=sc.nextInt( );

System.out.println("Enter the first character:");

c=sc.next( );

System.out.println("Enter the second character:");

ch=sc.next( );

compare(x1,y1);

compare(c,ch);

}

} //Closing of class

HOPE IT HELPS YOU !!!

Explanation:

Answered by chinnathangalnilesh
2

Answer:

import java.util.Scanner;

public class olm

{

   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 static void main(String args[]) {

       Scanner in = new Scanner(System.in);

       olm obj = new olm();

       

       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);

 }

}

Explanation:

Similar questions