Skip to content

Commit

Permalink
pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardusa committed Nov 8, 2023
1 parent d17eb00 commit 5cf094c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
60 changes: 60 additions & 0 deletions Pairs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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());

}
}
2 changes: 1 addition & 1 deletion carTester.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class carTester {
public class CarTester {
public static void main(String[] args) {
Car car = new Car(50);
car.addGas(20);
Expand Down

0 comments on commit 5cf094c

Please sign in to comment.