-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareMethod.java
More file actions
72 lines (66 loc) · 1.62 KB
/
CompareMethod.java
File metadata and controls
72 lines (66 loc) · 1.62 KB
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
import java.util.Scanner;
public class CompareMethod
{
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();
mo.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) ;
mo.compare(c1, c2);
System.out.print("\n\nEnter two Strings: ");
System.out.print("\nFirst: ");
String s1 = reader.nextLine();
System.out.print("\nSecond: ");
String s2 = reader.nextLine();
mo.compare(s1, s2);
}
}
class MethodOverloaded
{
public void compare(int x, int y)
{
if (x > y)
{
System.out.print("\nFirst number is greater.");
}
else
{
System.out.print("\nSecond number is greater");
}
}
public void compare(char ch1, char ch2)
{
int x = (int) ch1;
int y = (char) ch2;
if (x > y)
{
System.out.print("\nFirst character is greater.");
}
else
{
System.out.print("\nSecond character is greater");
}
}
public 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");
}
}
}