-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompareMethod.java
More file actions
146 lines (129 loc) · 3.48 KB
/
CompareMethod.java
File metadata and controls
146 lines (129 loc) · 3.48 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//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 CompareMethod
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Program to denmonstrate the Overloading of Methods.");
MethodOverloaded mo = new MethodOverloaded();
System.out.println("Enter two Integers :-");
System.out.println("First - ");
int n1 = sc.nextInt();
System.out.println("Second - ");
int n2 = sc.nextInt();
mo.compare(n1,n2);
System.out.println("Enter two Characters :-");
System.out.println("First - ");
char c1 = sc.next().charAt(0);
System.out.println("Second - ");
char c2 = sc.next().charAt(0);
mo.compare(c1,c2);
System.out.println("Enter two Strings :-");
System.out.println("First - ");
String s1 = sc.next();
System.out.println("Second - ");
String s2 = sc.next();
mo.compare(s1,s2);
}
}
class MethodOverloaded
{
public void compare(int x, int y)
{
if(x>y)
System.out.println("First number is Greater.");
else
System.out.println("Second number is Greater.");
}
public void compare(char ch1, char ch2)
{
int x = (int) ch1;
int y = (int) ch2;
if(x>y)
System.out.println("First Character is Greater.");
else
System.out.println("Second Character is Greater.");
}
public void compare(String str1, String str2)
{
if(str1.compareTo (str2) >0)
System.out.println("First String is Greater.");
else
System.out.println("Second String is Greater.");
}
}
//OR
//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 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.next();
System.out.print("\nSecond: ");
String s2 = reader.next();
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 = (int) 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");
}
}
}