-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPairs.java
60 lines (52 loc) · 1.57 KB
/
Pairs.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
import java.util.Scanner;
public class Pairs {
int[] nums;
public Pairs(int... nums) {
this.nums = nums;
}
public String howManyPairs() {
int count = 0;
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
for (int j = i + 1; j < nums.length; j++) {
if (num == nums[j]) {
count++;
}
}
}
if (count != 2) {
return "There are not 2 pairs.";
} else {
return "There are 2 pairs.";
}
}
public int[] sort() {
int[] sorted = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
int j = i - 1;
while (j >= 0 && sorted[j] > num) {
sorted[j + 1] = sorted[j];
j--;
}
sorted[j + 1] = num;
}
return sorted;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n1, n2, n3, n4;
System.out.print("Enter integer 1: ");
n1 = in.nextInt();
System.out.print("Enter integer 2: ");
n2 = in.nextInt();
System.out.print("Enter integer 3: ");
n3 = in.nextInt();
System.out.print("Enter integer 4: ");
n4 = in.nextInt();
Pairs p = new Pairs(n1, n2, n3, n4);
System.out.print("Sorted: " + p.sort()[0] + " " + p.sort()[1] + " " + p.sort()[2] + " " + p.sort()[3]);
System.out.print("\t\t" + p.howManyPairs());
in.close();
}
}