-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompareMethod1.java
74 lines (67 loc) · 1.67 KB
/
CompareMethod1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//Program to design a class to overload a method compare() to return the greater of two as follows:
// void compare(int, int)
// void compare(char, char)
// void compare(String, String)
import java.util.Scanner;
public class CompareMethod1
{
public static void main(String args[])
{
System.out.print("\nProgram to demonstate the Overloading of methods");
//MethodOverloaded mo = new MethodOverloaded();
Scanner reader = new Scanner(System.in);
System.out.print("\n\nEnter two integers: ");
System.out.print("\nFirst: ");
int num1 = reader.nextInt();
System.out.print("\nSecond: ");
int num2 = reader.nextInt();
compare(num1, num2);
System.out.print("\n\nEnter two characters: ");
System.out.print("\nFirst: ");
char c1 = reader.next().charAt(0) ;
System.out.print("\nSecond: ");
char c2 = reader.next().charAt(0) ;
compare(c1, c2);
System.out.print("\n\nEnter two Strings: ");
System.out.print("\nFirst: ");
String s1 = reader.next();
System.out.print("\nSecond: ");
String s2 = reader.next();
compare(s1, s2);
}
void compare(int x, int y)
{
if (x > y)
{
System.out.print("\nFirst number is greater.");
}
else
{
System.out.print("\nSecond number is greater");
}
}
void compare(char ch1, char ch2)
{
int x = (int) ch1;
int y = (int) ch2;
if (x > y)
{
System.out.print("\nFirst character is greater.");
}
else
{
System.out.print("\nSecond character is greater");
}
}
void compare(String str1, String str2)
{
if(str1.compareTo(str2) > 0)
{
System.out.print("\nFirst String is greater.");
}
else
{
System.out.print("\nSecond String is greater");
}
}
}