-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverload.java
83 lines (75 loc) · 2.94 KB
/
Overload.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
75
76
77
78
79
80
81
82
83
// Write a Comparator Class with the following 3 overloaded compare methods:
// boolean compare(int a, int b): Return true if int a =int b, otherwise return false.
// boolean compare(string a, string b): Return true if string a=string b, otherwise return false.
// boolean compare(int[] a, int[] b): Return true if both of the following conditions hold true:
// Arrays a and b are of equal length.
// For each index i (where 0<= i <= |a|, |b|), a[i] = b[i].
// Otherwise, return false.
//SOLUTION:
import java.io.*;
import java.util.*;
class Comparator{
public boolean compare(String s1, String s2){
if(Objects.equals(s1, s2))
return true;
else
return false;
}
public boolean compare(int num1,int num2) {
if (num1 == num2)
return true;
else
return false;
}
public boolean compare(int array1[],int array2[]) {
Object[] objArray1 = {array1};
Object[] objArray2 = {array2};
if (Arrays.deepEquals(objArray1,objArray2))
return true;
else
return false;
}
}
class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Comparator comp = new Comparator();
int testCases = Integer.parseInt(scan.nextLine());
while(testCases-- > 0){
int condition = Integer.parseInt(scan.nextLine());
switch(condition){
case 1:
String s1=scan.nextLine().trim();
String s2=scan.nextLine().trim();
System.out.println( (comp.compare(s1,s2)) ? "Same" : "Different" );
break;
case 2:
int num1 = scan.nextInt();
int num2 = scan.nextInt();
System.out.println( (comp.compare(num1,num2)) ? "Same" : "Different");
if(scan.hasNext()){ // avoid exception if this last test case
scan.nextLine(); // eat space until next line
}
break;
case 3:
// create and fill arrays
int[] array1 = new int[scan.nextInt()];
int[] array2 = new int[scan.nextInt()];
for(int i = 0; i < array1.length; i++){
array1[i] = scan.nextInt();
}
for(int i = 0; i < array2.length; i++){
array2[i] = scan.nextInt();
}
System.out.println( comp.compare(array1, array2) ? "Same" : "Different");
if(scan.hasNext()){ // avoid exception if this last test case
scan.nextLine(); // eat space until next line
}
break;
default:
System.err.println("Invalid input.");
}// end switch
}// end while
scan.close();
}
}