├── 10
├── src
│ └── academy
│ │ └── pocu
│ │ └── comp3500samples
│ │ └── w10
│ │ └── knapsack
│ │ ├── Item.java
│ │ └── Program.java
└── w10.iml
├── 11
├── src
│ └── academy
│ │ └── pocu
│ │ └── comp3500samples
│ │ └── w11
│ │ └── topologicalsort
│ │ ├── Course.java
│ │ └── Program.java
└── w11.iml
├── 12
├── src
│ └── academy
│ │ └── pocu
│ │ └── comp3500samples
│ │ └── w12
│ │ └── dijkstra
│ │ ├── Candidate.java
│ │ ├── Dijkstra.java
│ │ ├── Node.java
│ │ └── Program.java
└── w12.iml
├── 13
├── src
│ └── academy
│ │ └── pocu
│ │ └── comp3500samples
│ │ └── w13
│ │ └── kruskal
│ │ ├── DisjointSet.java
│ │ ├── Edge.java
│ │ ├── Kruskal.java
│ │ └── Program.java
└── w13.iml
├── .gitignore
├── .idea
├── .gitignore
├── COMP3500CodeSamples.iml
├── misc.xml
├── modules.xml
└── vcs.xml
├── 01
├── src
│ └── academy
│ │ └── pocu
│ │ └── comp3500samples
│ │ └── w01
│ │ └── helloworld
│ │ └── HelloWorld.java
└── w01.iml
├── 02
├── src
│ └── academy
│ │ └── pocu
│ │ └── comp3500samples
│ │ └── w02
│ │ ├── searchrotatedarray
│ │ └── Program.java
│ │ ├── sumrecursive
│ │ └── Program.java
│ │ └── sumtailrecursive
│ │ └── Program.java
└── w02.iml
├── 03
├── src
│ └── academy
│ │ └── pocu
│ │ └── comp3500samples
│ │ └── w03
│ │ ├── minimumdiff
│ │ └── Program.java
│ │ └── stability
│ │ ├── Book.java
│ │ └── Program.java
└── w03.iml
├── 04
├── src
│ └── academy
│ │ └── pocu
│ │ └── comp3500samples
│ │ └── w04
│ │ ├── crc32
│ │ └── Program.java
│ │ └── passwordhashing
│ │ ├── Program.java
│ │ └── User.java
└── w04.iml
├── 05
├── src
│ └── academy
│ │ └── pocu
│ │ └── comp3500samples
│ │ └── w05
│ │ ├── aes
│ │ └── Program.java
│ │ └── rsa
│ │ └── Program.java
└── w05.iml
├── 06
├── src
│ └── academy
│ │ └── pocu
│ │ └── comp3500samples
│ │ └── w06
│ │ ├── copytree
│ │ ├── Node.java
│ │ └── Program.java
│ │ ├── insert
│ │ ├── Node.java
│ │ └── Program.java
│ │ └── preorder
│ │ ├── Node.java
│ │ └── Program.java
└── w06.iml
├── 07
├── src
│ └── academy
│ │ └── pocu
│ │ └── comp3500samples
│ │ └── w07
│ │ └── quadtree
│ │ ├── BoundingRect.java
│ │ ├── GameObject.java
│ │ ├── Point.java
│ │ ├── Program.java
│ │ └── Quadrant.java
└── w07.iml
├── 09
├── src
│ └── academy
│ │ └── pocu
│ │ └── comp3500samples
│ │ └── w09
│ │ ├── directorytree
│ │ └── Program.java
│ │ └── tictactoe
│ │ ├── Move.java
│ │ ├── Player.java
│ │ ├── Program.java
│ │ └── TicTacToe.java
└── w09.iml
├── CODEOWNERS
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.nar
17 | *.ear
18 | *.zip
19 | *.tar.gz
20 | *.rar
21 |
22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23 | hs_err_pid*
24 |
25 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
26 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
27 |
28 | # User-specific stuff
29 | **/.idea/**/workspace.xml
30 | **/.idea/**/tasks.xml
31 | **/.idea/**/usage.statistics.xml
32 | **/.idea/**/dictionaries
33 | **/.idea/**/shelf
34 |
35 | # Generated files
36 | **/.idea/**/contentModel.xml
37 |
38 | # Sensitive or high-churn files
39 | **/.idea/**/dataSources/
40 | **/.idea/**/dataSources.ids
41 | **/.idea/**/dataSources.local.xml
42 | **/.idea/**/sqlDataSources.xml
43 | **/.idea/**/dynamic.xml
44 | **/.idea/**/uiDesigner.xml
45 | **/.idea/**/dbnavigator.xml
46 |
47 | # Gradle
48 | **/.idea/**/gradle.xml
49 | **/.idea/**/libraries
50 |
51 | # Gradle and Maven with auto-import
52 | # When using Gradle or Maven with auto-import, you should exclude module files,
53 | # since they will be recreated, and may cause churn. Uncomment if using
54 | # auto-import.
55 | # **/.idea/artifacts
56 | # **/.idea/compiler.xml
57 | # **/.idea/jarRepositories.xml
58 | # **/.idea/modules.xml
59 | # **/.idea/*.iml
60 | # **/.idea/modules
61 | # *.iml
62 | # *.ipr
63 |
64 | # CMake
65 | cmake-build-*/
66 |
67 | # Mongo Explorer plugin
68 | **/.idea/**/mongoSettings.xml
69 |
70 | # File-based project format
71 | *.iws
72 |
73 | # IntelliJ
74 | out/
75 |
76 | # mpeltonen/sbt-idea plugin
77 | **/.idea_modules/
78 |
79 | # JIRA plugin
80 | atlassian-ide-plugin.xml
81 |
82 | # Cursive Clojure plugin
83 | **/.idea/replstate.xml
84 |
85 | # Crashlytics plugin (for Android Studio and IntelliJ)
86 | com_crashlytics_export_strings.xml
87 | crashlytics.properties
88 | crashlytics-build.properties
89 | fabric.properties
90 |
91 | # Editor-based Rest Client
92 | **/.idea/httpRequests
93 |
94 | # Android studio 3.1+ serialized cache file
95 | **/.idea/caches/build_file_checksums.ser
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/.idea/COMP3500CodeSamples.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/01/src/academy/pocu/comp3500samples/w01/helloworld/HelloWorld.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w01.helloworld;
2 |
3 | public class HelloWorld {
4 | public static void main(String[] args) {
5 | System.out.println("Hello World!");
6 | }
7 | }
8 |
--------------------------------------------------------------------------------
/01/w01.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/02/src/academy/pocu/comp3500samples/w02/searchrotatedarray/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w02.searchrotatedarray;
2 |
3 | public class Program {
4 |
5 | public static void main(String[] args) {
6 | int[] numbers = new int[]{20, 25, 26, 29, 33, 1, 3, 5, 6, 10, 11, 19};
7 |
8 | int index = indexOfRotatedArray(numbers, 0, numbers.length - 1, 0);
9 |
10 | System.out.println(index);
11 |
12 | index = indexOfRotatedArray(numbers, 0, numbers.length - 1, 1);
13 |
14 | System.out.println(index);
15 |
16 | index = indexOfRotatedArray(numbers, 0, numbers.length - 1, 29);
17 |
18 | System.out.println(index);
19 |
20 | index = indexOfRotatedArray(numbers, 0, numbers.length - 1, 6);
21 |
22 | System.out.println(index);
23 | }
24 |
25 | private static int indexOfRotatedArray(int[] numbers, int start, int end, int num) {
26 | if (start > end) {
27 | return -1;
28 | }
29 |
30 | int mid = (start + end) / 2;
31 | if (numbers[mid] == num) {
32 | return mid;
33 | }
34 |
35 | if (numbers[start] <= numbers[mid]) {
36 | if (numbers[start] <= num && num <= numbers[mid]) {
37 | return indexOfRotatedArray(numbers, start, mid - 1, num);
38 | }
39 |
40 | return indexOfRotatedArray(numbers, mid + 1, end, num);
41 | }
42 |
43 | if (numbers[mid] <= num && num <= numbers[end]) {
44 | return indexOfRotatedArray(numbers, mid + 1, end, num);
45 | }
46 |
47 | return indexOfRotatedArray(numbers, start, mid - 1, num);
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/02/src/academy/pocu/comp3500samples/w02/sumrecursive/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w02.sumrecursive;
2 |
3 | public class Program {
4 | public static void main(String[] args) {
5 | int sum = sumRecursive(10);
6 |
7 | System.out.println(sum); // 55
8 |
9 | sum = sumRecursive(100);
10 |
11 | System.out.println(sum); // 5050
12 |
13 | sum = sumRecursive(1000);
14 |
15 | System.out.println(sum); // 500500
16 |
17 | sum = sumRecursive(100000); // ??
18 |
19 | System.out.println(sum);
20 | }
21 |
22 | private static int sumRecursive(int n) {
23 | if (n <= 1) {
24 | return n;
25 | }
26 |
27 | return n + sumRecursive(n - 1);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/02/src/academy/pocu/comp3500samples/w02/sumtailrecursive/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w02.sumtailrecursive;
2 |
3 | public class Program {
4 | public static void main(String[] args) {
5 | int sum = sumTailRecursive(10, 0);
6 |
7 | System.out.println(sum); // 55
8 |
9 | sum = sumTailRecursive(100, 0);
10 |
11 | System.out.println(sum); // 5050
12 |
13 | sum = sumTailRecursive(1000, 0);
14 |
15 | System.out.println(sum); // 500500
16 |
17 | sum = sumTailRecursive(100000, 0); // ??
18 |
19 | System.out.println(sum);
20 | }
21 |
22 | private static int sumTailRecursive(int n, int sum) {
23 | if (n <= 0) {
24 | return sum;
25 | }
26 |
27 | return sumTailRecursive(n - 1, sum + n);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/02/w02.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/03/src/academy/pocu/comp3500samples/w03/minimumdiff/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w03.minimumdiff;
2 |
3 | import java.util.Arrays;
4 | import java.util.Random;
5 |
6 | public class Program {
7 |
8 | public static void main(String[] args) {
9 | Random random = new Random(512);
10 |
11 | int[] nums = new int[15];
12 |
13 | for (int i = 0; i < nums.length; ++i) {
14 | nums[i] = random.nextInt(1000);
15 | }
16 |
17 | printNums(nums);
18 |
19 | Arrays.sort(nums);
20 |
21 | int minDiff = Integer.MAX_VALUE;
22 | int num1 = 0;
23 | int num2 = 0;
24 |
25 | for (int i = 0; i < nums.length - 1; ++i) {
26 | int diff = Math.abs(nums[i] - nums[i + 1]);
27 |
28 | if (diff < minDiff) {
29 | minDiff = diff;
30 | num1 = nums[i];
31 | num2 = nums[i + 1];
32 | }
33 | }
34 |
35 | System.out.println(String.format("minimum difference: %d", minDiff));
36 | System.out.println(String.format("num1: %d, num2: %d", num1, num2));
37 | }
38 |
39 | private static void printNums(int[] nums) {
40 | String[] s = new String[nums.length];
41 |
42 | for (int i = 0; i < nums.length; ++i) {
43 | s[i] = String.format("%d", nums[i]);
44 | }
45 |
46 | System.out.println(String.format("[ %s ]", String.join(", ", s)));
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/03/src/academy/pocu/comp3500samples/w03/stability/Book.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w03.stability;
2 |
3 | public final class Book {
4 | private final int id;
5 | private final String title;
6 |
7 | public Book(final int id, final String title) {
8 | this.id = id;
9 | this.title = title;
10 | }
11 |
12 | public int getID() {
13 | return this.id;
14 | }
15 |
16 | public String getTitle() {
17 | return this.title;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/03/src/academy/pocu/comp3500samples/w03/stability/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w03.stability;
2 |
3 | import java.util.Arrays;
4 | import java.util.Comparator;
5 |
6 | public class Program {
7 |
8 | public static void main(String[] args) {
9 | Book[] books = new Book[10];
10 |
11 | books[0] = new Book(5, "My Book 5 copy 1");
12 | books[1] = new Book(1, "My Book 1");
13 | books[2] = new Book(5, "My Book 5");
14 | books[3] = new Book(1, "My Book 1 copy 2");
15 | books[4] = new Book(3, "My Book 3");
16 | books[5] = new Book(2, "My Book 2");
17 | books[6] = new Book(4, "My Book 4 copy 1");
18 | books[7] = new Book(4, "My Book 4");
19 | books[8] = new Book(1, "My Book 1 copy 1");
20 | books[9] = new Book(4, "My Book 4 copy 2");
21 |
22 | Book[] booksCopy = copyBooks(books);
23 |
24 | quickSort(booksCopy);
25 |
26 | printBooks(booksCopy);
27 |
28 | booksCopy = copyBooks(books);
29 |
30 | Arrays.sort(booksCopy, Comparator.comparingInt(Book::getID));
31 |
32 | printBooks(booksCopy);
33 | }
34 |
35 | private static Book[] copyBooks(Book[] books) {
36 | Book[] newBooks = new Book[10];
37 |
38 | for (int i = 0; i < books.length; ++i) {
39 | newBooks[i] = new Book(books[i].getID(), books[i].getTitle());
40 | }
41 |
42 | return newBooks;
43 | }
44 |
45 | private static void printBooks(Book[] books) {
46 | System.out.println("--------------Books--------------");
47 |
48 | for (Book book : books) {
49 | System.out.println(String.format("%d. %s", book.getID(), book.getTitle()));
50 | }
51 |
52 | System.out.println("--------------end--------------");
53 | }
54 |
55 | private static void quickSort(Book[] books) {
56 | quickSortRecursive(books, 0, books.length - 1);
57 | }
58 |
59 | private static void quickSortRecursive(Book[] books, int left, int right) {
60 | if (left >= right) {
61 | return;
62 | }
63 |
64 | int pivotPos = partition(books, left, right);
65 |
66 | quickSortRecursive(books, left, pivotPos - 1);
67 | quickSortRecursive(books, pivotPos + 1 , right);
68 | }
69 |
70 | private static int partition(Book[] books, int left, int right) {
71 | Book pivot = books[right];
72 | int i = (left - 1);
73 |
74 | for (int j = left; j < right; ++j) {
75 | if (books[j].getID() < pivot.getID()) {
76 | ++i;
77 | swap(books, i, j);
78 | }
79 | }
80 |
81 | int pivotPos = i + 1;
82 |
83 | swap(books, pivotPos, right);
84 |
85 | return pivotPos;
86 | }
87 |
88 | private static void swap(Book[] books, int pos1, int pos2) {
89 | Book temp = books[pos1];
90 | books[pos1] = books[pos2];
91 | books[pos2] = temp;
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/03/w03.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/04/src/academy/pocu/comp3500samples/w04/crc32/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w04.crc32;
2 |
3 | import java.io.ByteArrayInputStream;
4 | import java.io.IOException;
5 | import java.io.InputStream;
6 | import java.nio.charset.StandardCharsets;
7 | import java.util.zip.CRC32;
8 | import java.util.zip.CheckedInputStream;
9 |
10 | public class Program {
11 | public static void main(String[] args) {
12 | String input1 = "My text";
13 | String input2 = "My text";
14 | String input3 = "My different text";
15 |
16 | byte[] input1Bytes = input1.getBytes();
17 | byte[] input2Bytes = input2.getBytes();
18 | byte[] input3Bytes = input3.getBytes();
19 |
20 | CRC32 crc32 = new CRC32();
21 |
22 | crc32.update(input1Bytes);
23 | long checksum1 = crc32.getValue();
24 |
25 | System.out.println(
26 | String.format("input1: %d",
27 | checksum1));
28 |
29 | crc32.reset();
30 |
31 | crc32.update(input2Bytes);
32 | long checksum2 = crc32.getValue();
33 |
34 | System.out.println(
35 | String.format("input2: %d",
36 | checksum2));
37 |
38 | crc32.reset();
39 |
40 | crc32.update(input3Bytes);
41 | long checksum3 = crc32.getValue();
42 |
43 | System.out.println(
44 | String.format("input3: %d",
45 | checksum3));
46 |
47 | String input = "My long long text";
48 | byte[] inputArray = input.getBytes(StandardCharsets.UTF_8);
49 | InputStream inputStream = new ByteArrayInputStream(inputArray);
50 |
51 | CheckedInputStream checkedInputStream = new CheckedInputStream(inputStream, new CRC32());
52 | byte[] buffer = new byte[1024];
53 |
54 | try {
55 | while (checkedInputStream.read(buffer,
56 | 0,
57 | buffer.length) >= 0) {
58 | // intentionally empty
59 | }
60 |
61 | long checksum = checkedInputStream
62 | .getChecksum()
63 | .getValue();
64 |
65 | System.out.println(
66 | String.format("input: %d",
67 | checksum));
68 | } catch (IOException e) {
69 | e.printStackTrace();
70 | }
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/04/src/academy/pocu/comp3500samples/w04/passwordhashing/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w04.passwordhashing;
2 |
3 | public class Program {
4 | public static void main(String[] args) {
5 | User user = new User("myemail@email.com",
6 | "My StRoNg P@$$word");
7 |
8 | System.out.println(
9 | String.format("ID: %s",
10 | user.getId()));
11 |
12 | System.out.println(
13 | String.format("Email: %s",
14 | user.getEmail()));
15 |
16 | System.out.println(
17 | String.format("Password Hash: %s",
18 | user.getPaswordHash()));
19 |
20 | boolean isValid = user.isValidPassword("wrong password!");
21 |
22 | System.out.println(isValid);
23 |
24 | isValid = user.isValidPassword("My StRoNg P@$$word");
25 |
26 | System.out.println(isValid);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/04/src/academy/pocu/comp3500samples/w04/passwordhashing/User.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w04.passwordhashing;
2 |
3 | import javax.crypto.SecretKeyFactory;
4 | import javax.crypto.spec.PBEKeySpec;
5 | import java.security.NoSuchAlgorithmException;
6 | import java.security.SecureRandom;
7 | import java.security.spec.InvalidKeySpecException;
8 | import java.security.spec.KeySpec;
9 | import java.util.Arrays;
10 | import java.util.Base64;
11 | import java.util.UUID;
12 |
13 | public final class User {
14 | private final static int SALT_LENGTH_IN_BYTES = 16;
15 | private final static int NUM_ITERATIONS = 10000;
16 | private final static int OUTPUT_KEY_LENGTH_IN_BYTES = 256 / 8;
17 |
18 | private final UUID id = UUID.randomUUID();
19 | private String email;
20 | private String paswordHash;
21 |
22 | public User(final String email,
23 | final String password) {
24 | this.email = email;
25 | setPassword(password);
26 | }
27 |
28 | public UUID getId() {
29 | return this.id;
30 | }
31 |
32 | public String getEmail() {
33 | return this.email;
34 | }
35 |
36 | public void setEmail(String email) {
37 | this.email = email;
38 | }
39 |
40 | public String getPaswordHash() {
41 | return this.paswordHash;
42 | }
43 |
44 | public void setPassword(String password) {
45 | SecureRandom random = new SecureRandom();
46 | byte[] salt = new byte[SALT_LENGTH_IN_BYTES];
47 | random.nextBytes(salt);
48 |
49 | try {
50 | KeySpec spec = new PBEKeySpec(password.toCharArray(),
51 | salt,
52 | NUM_ITERATIONS,
53 | OUTPUT_KEY_LENGTH_IN_BYTES * 8);
54 | SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
55 |
56 | byte[] hash = factory.generateSecret(spec).getEncoded();
57 |
58 | byte[] outputBytes = new byte[SALT_LENGTH_IN_BYTES + OUTPUT_KEY_LENGTH_IN_BYTES];
59 |
60 | System.arraycopy(salt,
61 | 0,
62 | outputBytes,
63 | 0,
64 | SALT_LENGTH_IN_BYTES);
65 | System.arraycopy(hash, 0,
66 | outputBytes,
67 | SALT_LENGTH_IN_BYTES,
68 | OUTPUT_KEY_LENGTH_IN_BYTES);
69 |
70 | this.paswordHash = Base64.getEncoder()
71 | .encodeToString(outputBytes);
72 | } catch (NoSuchAlgorithmException e) {
73 | throw new RuntimeException(e);
74 | } catch (InvalidKeySpecException e) {
75 | throw new RuntimeException(e);
76 | }
77 | }
78 |
79 | public boolean isValidPassword(String password) {
80 | byte[] outputBytes = Base64.getDecoder()
81 | .decode(this.paswordHash);
82 |
83 | byte[] salt = new byte[SALT_LENGTH_IN_BYTES];
84 | byte[] expectedHash = new byte[OUTPUT_KEY_LENGTH_IN_BYTES];
85 |
86 | System.arraycopy(outputBytes,
87 | 0, salt,
88 | 0, SALT_LENGTH_IN_BYTES);
89 | System.arraycopy(outputBytes,
90 | SALT_LENGTH_IN_BYTES, expectedHash,
91 | 0, OUTPUT_KEY_LENGTH_IN_BYTES);
92 |
93 | try {
94 | KeySpec spec = new PBEKeySpec(password.toCharArray(),
95 | salt, NUM_ITERATIONS,
96 | OUTPUT_KEY_LENGTH_IN_BYTES * 8);
97 |
98 | SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
99 |
100 | byte[] actualHash = factory
101 | .generateSecret(spec)
102 | .getEncoded();
103 |
104 | return Arrays.equals(expectedHash, actualHash);
105 | } catch (NoSuchAlgorithmException e) {
106 | throw new RuntimeException(e);
107 | } catch (InvalidKeySpecException e) {
108 | throw new RuntimeException(e);
109 | }
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/04/w04.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/05/src/academy/pocu/comp3500samples/w05/aes/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w05.aes;
2 |
3 | import javax.crypto.Cipher;
4 | import javax.crypto.spec.IvParameterSpec;
5 | import javax.crypto.spec.SecretKeySpec;
6 | import java.nio.charset.StandardCharsets;
7 | import java.util.Base64;
8 |
9 | public class Program {
10 | public static void main(String[] args) {
11 | String plaintext = "My message";
12 | String longPlaintext = "My longer message";
13 |
14 | {
15 | String aes128key = "1234567890123456";
16 | byte[] keyInBytes = aes128key.getBytes(StandardCharsets.US_ASCII);
17 |
18 | String ciphertext = encrypt(plaintext, keyInBytes);
19 | String longCiphertext = encrypt(longPlaintext, keyInBytes);
20 |
21 | System.out.println(ciphertext);
22 | System.out.println(longCiphertext);
23 |
24 | String decryptedText = decrypt(ciphertext, keyInBytes);
25 | String longDecryptedText = decrypt(longCiphertext, keyInBytes);
26 |
27 | System.out.println(decryptedText);
28 | System.out.println(longDecryptedText);
29 | }
30 |
31 | {
32 | String aes192key = "123456789012345678901234";
33 | byte[] keyInBytes = aes192key.getBytes(StandardCharsets.US_ASCII);
34 |
35 | String ciphertext = encrypt(plaintext, keyInBytes);
36 | String longCiphertext = encrypt(longPlaintext, keyInBytes);
37 |
38 | System.out.println(ciphertext);
39 | System.out.println(longCiphertext);
40 |
41 | String decryptedText = decrypt(ciphertext, keyInBytes);
42 | String longDecryptedText = decrypt(longCiphertext, keyInBytes);
43 |
44 | System.out.println(decryptedText);
45 | System.out.println(longDecryptedText);
46 | }
47 |
48 | {
49 | String aes256key = "12345678901234567890123456789012";
50 | byte[] keyInBytes = aes256key.getBytes(StandardCharsets.US_ASCII);
51 |
52 | String ciphertext = encrypt(plaintext, keyInBytes);
53 | String longCiphertext = encrypt(longPlaintext, keyInBytes);
54 |
55 | System.out.println(ciphertext);
56 | System.out.println(longCiphertext);
57 |
58 | String decryptedText = decrypt(ciphertext, keyInBytes);
59 | String longDecryptedText = decrypt(longCiphertext, keyInBytes);
60 |
61 | System.out.println(decryptedText);
62 | System.out.println(longDecryptedText);
63 | }
64 | }
65 |
66 | private static String encrypt(String plaintext, byte[] key) {
67 | assert (key.length == 16 || key.length == 24 || key.length == 32);
68 |
69 | try {
70 | byte[] plaintextInBytes = plaintext.getBytes(StandardCharsets.UTF_8);
71 |
72 | SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
73 |
74 | Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
75 | cipher.init(Cipher.ENCRYPT_MODE,
76 | secretKey,
77 | new IvParameterSpec(new byte[16]));
78 |
79 | byte[] encrypted = cipher
80 | .doFinal(plaintextInBytes);
81 |
82 | return Base64.getEncoder()
83 | .encodeToString(encrypted);
84 | } catch (Exception e) {
85 | e.printStackTrace();
86 | throw new RuntimeException(e);
87 | }
88 | }
89 |
90 | private static String decrypt(String cipherText, byte[] key) {
91 | assert (key.length == 16 || key.length == 24 || key.length == 32);
92 |
93 | try {
94 | byte[] encrypted = Base64.getDecoder()
95 | .decode(cipherText);
96 |
97 | SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
98 |
99 | Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
100 | cipher.init(Cipher.DECRYPT_MODE,
101 | secretKey,
102 | new IvParameterSpec(new byte[16]));
103 |
104 | byte[] plaintext = cipher
105 | .doFinal(encrypted);
106 |
107 | return new String(plaintext, StandardCharsets.UTF_8);
108 | } catch (Exception e) {
109 | e.printStackTrace();
110 | throw new RuntimeException(e);
111 | }
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/05/src/academy/pocu/comp3500samples/w05/rsa/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w05.rsa;
2 |
3 | import javax.crypto.Cipher;
4 | import java.nio.charset.StandardCharsets;
5 | import java.security.KeyPair;
6 | import java.security.KeyPairGenerator;
7 | import java.security.NoSuchAlgorithmException;
8 | import java.security.PrivateKey;
9 | import java.security.PublicKey;
10 | import java.security.SecureRandom;
11 | import java.util.Base64;
12 |
13 | public class Program {
14 | public static void main(String[] args) {
15 | KeyPair keyPair = getKeyPair();
16 |
17 | PublicKey publicKey = keyPair.getPublic();
18 | PrivateKey privateKey = keyPair.getPrivate();
19 |
20 | System.out.println(publicKey.toString());
21 | System.out.println(privateKey.toString());
22 |
23 | String plaintext = "My love letter";
24 |
25 | String ciphertext = encrypt(plaintext, publicKey);
26 |
27 | System.out.println(ciphertext);
28 |
29 | String actualPlaintext = decrypt(ciphertext, privateKey);
30 |
31 | System.out.println(actualPlaintext);
32 |
33 | actualPlaintext = decryptWithPublicKey(ciphertext, publicKey);
34 |
35 | System.out.println(actualPlaintext);
36 | }
37 |
38 | private static KeyPair getKeyPair() {
39 | try {
40 | KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
41 |
42 | generator.initialize(2048, new SecureRandom());
43 | KeyPair pair = generator.generateKeyPair();
44 |
45 | return pair;
46 | } catch (NoSuchAlgorithmException e) {
47 | e.printStackTrace();
48 | throw new RuntimeException(e);
49 | }
50 | }
51 |
52 | private static String encrypt(String plaintext, PublicKey publicKey) {
53 | try {
54 | Cipher cipher = Cipher.getInstance("RSA");
55 | cipher.init(Cipher.ENCRYPT_MODE, publicKey);
56 |
57 | byte[] bytes = plaintext.getBytes(StandardCharsets.UTF_8);
58 |
59 | byte[] ciphertext = cipher.doFinal(bytes);
60 |
61 | return Base64.getEncoder()
62 | .encodeToString(ciphertext);
63 | } catch (Exception e) {
64 | e.printStackTrace();
65 | throw new RuntimeException(e);
66 | }
67 | }
68 |
69 | private static String decrypt(String ciphertext, PrivateKey privateKey) {
70 | try {
71 | byte[] bytes = Base64.getDecoder()
72 | .decode(ciphertext);
73 |
74 | Cipher cipher = Cipher.getInstance("RSA");
75 | cipher.init(Cipher.DECRYPT_MODE, privateKey);
76 |
77 | byte[] plaintext = cipher.doFinal(bytes);
78 |
79 | return new String(plaintext, StandardCharsets.UTF_8);
80 | } catch (Exception e) {
81 | e.printStackTrace();
82 | throw new RuntimeException(e);
83 | }
84 | }
85 |
86 | private static String decryptWithPublicKey(String encryptedMessage, PublicKey publicKey) {
87 | try {
88 | byte[] bytes = Base64.getDecoder().decode(encryptedMessage);
89 |
90 | Cipher cipher = Cipher.getInstance("RSA");
91 | cipher.init(Cipher.DECRYPT_MODE, publicKey);
92 |
93 | byte[] plaintext = cipher.doFinal(bytes);
94 |
95 | return new String(plaintext, StandardCharsets.UTF_8);
96 | } catch (Exception e) {
97 | e.printStackTrace();
98 | throw new RuntimeException(e);
99 | }
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/05/w05.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/06/src/academy/pocu/comp3500samples/w06/copytree/Node.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w06.copytree;
2 |
3 | public class Node {
4 | private int data;
5 | private Node left;
6 | private Node right;
7 |
8 | public Node(final int data) {
9 | this.data = data;
10 | }
11 |
12 | public int getData() {
13 | return this.data;
14 | }
15 |
16 | public void setData(final int data) {
17 | this.data = data;
18 | }
19 |
20 | public Node getLeft() {
21 | return this.left;
22 | }
23 |
24 | public Node getRight() {
25 | return this.right;
26 | }
27 |
28 | public static Node insertRecursive(final Node node, int data) {
29 | if (node == null) {
30 | return new Node(data);
31 | }
32 |
33 | if (data < node.data) {
34 | node.left = insertRecursive(node.left, data);
35 | } else {
36 | node.right = insertRecursive(node.right, data);
37 | }
38 |
39 | return node;
40 | }
41 |
42 | public static Node copyRecursive(final Node node) {
43 | if (node == null) {
44 | return null;
45 | }
46 |
47 | Node newNode = new Node(node.data);
48 | newNode.left = copyRecursive(node.left);
49 | newNode.right = copyRecursive(node.right);
50 |
51 | return newNode;
52 | }
53 |
54 | public static void traverseInOrderRecursive(final Node node) {
55 | if (node == null) {
56 | return;
57 | }
58 |
59 | traverseInOrderRecursive(node.left);
60 | System.out.println(node.data);
61 | traverseInOrderRecursive(node.right);
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/06/src/academy/pocu/comp3500samples/w06/copytree/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w06.copytree;
2 |
3 | public class Program {
4 | public static void main(String[] args) {
5 | Node root = new Node(50);
6 |
7 | Node.insertRecursive(root, 24);
8 | Node.insertRecursive(root, 42);
9 | Node.insertRecursive(root, 33);
10 | Node.insertRecursive(root, 22);
11 |
12 | Node.insertRecursive(root, 55);
13 | Node.insertRecursive(root, 52);
14 | Node.insertRecursive(root, 57);
15 |
16 | Node.traverseInOrderRecursive(root);
17 |
18 | System.out.println("--------------------------");
19 |
20 | Node rootCopy = Node.copyRecursive(root);
21 |
22 | Node.traverseInOrderRecursive(rootCopy);
23 |
24 | System.out.println("--------------------------");
25 |
26 | root.getLeft().setData(100);
27 | Node.traverseInOrderRecursive(root);
28 |
29 | System.out.println("--------------------------");
30 |
31 | Node.traverseInOrderRecursive(rootCopy);
32 |
33 | root.getLeft().setData(24);
34 |
35 | System.out.println("--------------------------");
36 |
37 | Node.insertRecursive(root, 15);
38 |
39 | Node.traverseInOrderRecursive(root);
40 |
41 | System.out.println("--------------------------");
42 |
43 | Node.traverseInOrderRecursive(rootCopy);
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/06/src/academy/pocu/comp3500samples/w06/insert/Node.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w06.insert;
2 |
3 | public class Node {
4 | private final int data;
5 | private Node left;
6 | private Node right;
7 |
8 | public Node(final int data) {
9 | this.data = data;
10 | }
11 |
12 | public int getData() {
13 | return this.data;
14 | }
15 |
16 | public Node getLeft() {
17 | return this.left;
18 | }
19 |
20 | public Node getRight() {
21 | return this.right;
22 | }
23 |
24 | public static Node insertRecursive(final Node node, int data) {
25 | if (node == null) {
26 | return new Node(data);
27 | }
28 |
29 | if (data < node.data) {
30 | node.left = insertRecursive(node.left,
31 | data);
32 | } else {
33 | node.right = insertRecursive(node.right, data);
34 | }
35 |
36 | return node;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/06/src/academy/pocu/comp3500samples/w06/insert/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w06.insert;
2 |
3 | public class Program {
4 | public static void main(String[] args) {
5 | Node root = new Node(50);
6 |
7 | Node.insertRecursive(root, 24);
8 | Node.insertRecursive(root, 42);
9 | Node.insertRecursive(root, 33);
10 | Node.insertRecursive(root, 22);
11 |
12 | Node.insertRecursive(root, 55);
13 | Node.insertRecursive(root, 52);
14 | Node.insertRecursive(root, 57);
15 |
16 | int num = root.getData();
17 | System.out.println(num);
18 |
19 | num = root.getLeft().getData();
20 | System.out.print(num);
21 |
22 | System.out.print(" ");
23 |
24 | num = root.getRight().getData();
25 | System.out.print(num);
26 |
27 | System.out.println();
28 |
29 | num = root.getLeft().getLeft()
30 | .getData();
31 | System.out.print(num);
32 |
33 | System.out.print(" ");
34 |
35 | num = root.getLeft().getRight()
36 | .getData();
37 | System.out.print(num);
38 |
39 | System.out.print(" ");
40 |
41 | num = root.getRight().getLeft()
42 | .getData();
43 | System.out.print(num);
44 |
45 | System.out.print(" ");
46 |
47 | num = root.getRight().getRight()
48 | .getData();
49 | System.out.print(num);
50 |
51 | System.out.println();
52 |
53 | num = root.getLeft().getRight()
54 | .getLeft().getData();
55 | System.out.print(num);
56 |
57 | System.out.println();
58 |
59 | Node.insertRecursive(root, 53);
60 |
61 | num = root.getRight().getLeft()
62 | .getRight().getData();
63 | System.out.print(num);
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/06/src/academy/pocu/comp3500samples/w06/preorder/Node.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w06.preorder;
2 |
3 | import java.util.Stack;
4 |
5 | public class Node {
6 | private final int data;
7 | private Node left;
8 | private Node right;
9 |
10 | public Node(final int data) {
11 | this.data = data;
12 | }
13 |
14 | public int getData() {
15 | return this.data;
16 | }
17 |
18 | public Node getLeft() {
19 | return this.left;
20 | }
21 |
22 | public Node getRight() {
23 | return this.right;
24 | }
25 |
26 | public static Node insertRecursive(final Node root, int data) {
27 | if (root == null) {
28 | return new Node(data);
29 | }
30 |
31 | if (data < root.data) {
32 | root.left = insertRecursive(root.left, data);
33 | } else {
34 | root.right = insertRecursive(root.right, data);
35 | }
36 |
37 | return root;
38 | }
39 |
40 | public static void traversePreOrderRecursive(final Node node) {
41 | if (node == null) {
42 | return;
43 | }
44 |
45 | System.out.println(node.data);
46 | traversePreOrderRecursive(node.left);
47 | traversePreOrderRecursive(node.right);
48 | }
49 |
50 | public static void traversePreOrder(final Node root) {
51 | if (root == null) {
52 | return;
53 | }
54 |
55 | Stack nodes = new Stack<>();
56 |
57 | nodes.push(root);
58 |
59 | while (!nodes.empty()) {
60 | Node node = nodes.pop();
61 |
62 | System.out.println(node.data);
63 |
64 | if (node.right != null) {
65 | nodes.push(node.right);
66 | }
67 |
68 | if (node.left != null) {
69 | nodes.push(node.left);
70 | }
71 | }
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/06/src/academy/pocu/comp3500samples/w06/preorder/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w06.preorder;
2 |
3 | public class Program {
4 | public static void main(String[] args) {
5 | Node root = new Node(50);
6 |
7 | Node.insertRecursive(root, 24);
8 | Node.insertRecursive(root, 42);
9 | Node.insertRecursive(root, 33);
10 | Node.insertRecursive(root, 22);
11 |
12 | Node.insertRecursive(root, 55);
13 | Node.insertRecursive(root, 52);
14 | Node.insertRecursive(root, 57);
15 |
16 | Node.traversePreOrderRecursive(root);
17 |
18 | System.out.println("------------------------");
19 |
20 | Node.traversePreOrder(root);
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/06/w06.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/07/src/academy/pocu/comp3500samples/w07/quadtree/BoundingRect.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w07.quadtree;
2 |
3 | public final class BoundingRect {
4 | private final Point topLeft;
5 | private final Point bottomRight;
6 |
7 | public BoundingRect(final Point topLeft, final int width, final int height) {
8 | assert (width >= 0);
9 | assert (height >= 0);
10 |
11 | this.topLeft = topLeft;
12 | this.bottomRight = new Point(topLeft.getX() + width,
13 | topLeft.getY() + height);
14 | }
15 |
16 | public int getWidth() {
17 | final int x1 = this.topLeft
18 | .getX();
19 | final int x2 = this.bottomRight
20 | .getX();
21 |
22 | return Math.abs(x1 - x2);
23 | }
24 |
25 | public int getHeight() {
26 | final int y1 = this.topLeft
27 | .getY();
28 | final int y2 = this.bottomRight
29 | .getY();
30 |
31 | return Math.abs(y1 - y2);
32 | }
33 |
34 | public Point getTopLeft() {
35 | return this.topLeft;
36 | }
37 |
38 | public Point getBottomRight() {
39 | return this.bottomRight;
40 | }
41 |
42 | public boolean contains(final Point point) {
43 | final int pX = point.getX();
44 | final int pY = point.getY();
45 |
46 | return pX >= this.topLeft.getX()
47 | && pX <= this.bottomRight.getX()
48 | && pY >= this.topLeft.getY()
49 | && pY <= this.bottomRight.getY();
50 | }
51 |
52 | public boolean contains(final BoundingRect other) {
53 | final int x1 = this.topLeft.getX();
54 | final int x2 = this.bottomRight.getX();
55 | final int y1 = this.topLeft.getY();
56 | final int y2 = this.bottomRight.getY();
57 |
58 | final int otherX1 = other.topLeft.getX();
59 | final int otherX2 = other.bottomRight.getX();
60 | final int otherY1 = other.topLeft.getY();
61 | final int otherY2 = other.bottomRight.getY();
62 |
63 | return x1 <= otherX1
64 | && x2 >= otherX2
65 | && y1 <= otherY1
66 | && y2 >= otherY2;
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/07/src/academy/pocu/comp3500samples/w07/quadtree/GameObject.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w07.quadtree;
2 |
3 | public class GameObject {
4 | private final Point position;
5 | private final int data;
6 |
7 | public GameObject(final Point position, int data) {
8 | this.position = position;
9 | this.data = data;
10 | }
11 |
12 | public int getData() {
13 | return this.data;
14 | }
15 |
16 | public Point getPosition() {
17 | return this.position;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/07/src/academy/pocu/comp3500samples/w07/quadtree/Point.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w07.quadtree;
2 |
3 | public final class Point {
4 | private int x;
5 | private int y;
6 |
7 | public Point(final int x, final int y) {
8 | assert (x >= 0);
9 | assert (y >= 0);
10 |
11 | this.x = x;
12 | this.y = y;
13 | }
14 |
15 | public Point(Point other) {
16 | this.x = other.x;
17 | this.y = other.y;
18 | }
19 |
20 | public int getX() {
21 | return this.x;
22 | }
23 |
24 | public int getY() {
25 | return this.y;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/07/src/academy/pocu/comp3500samples/w07/quadtree/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w07.quadtree;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class Program {
6 | public static void main(String[] args) {
7 | final Point p1 = new Point(1, 4);
8 | final GameObject gameObject1 = new GameObject(p1, 1);
9 |
10 | final Point p2 = new Point(7, 9);
11 | final GameObject gameObject2 = new GameObject(p2, 2);
12 |
13 | final Point p3 = new Point(5, 5);
14 | final GameObject gameObject3 = new GameObject(p3, 3);
15 |
16 | final Point p4 = new Point(3, 4);
17 | final GameObject gameObject4 = new GameObject(p4, 4);
18 |
19 | final Point p5 = new Point(2, 7);
20 | final GameObject gameObject5 = new GameObject(p5, 5);
21 |
22 | final Point p6 = new Point(9, 3);
23 | final GameObject gameObject6 = new GameObject(p6, 6);
24 |
25 | Point topLeft = new Point(0, 0);
26 |
27 | BoundingRect rect = new BoundingRect(topLeft, 10, 10);
28 | final Quadrant root = new Quadrant(rect);
29 |
30 | root.insert(gameObject1);
31 | root.insert(gameObject2);
32 | root.insert(gameObject3);
33 | root.insert(gameObject4);
34 | root.insert(gameObject5);
35 | root.insert(gameObject6);
36 |
37 | topLeft = new Point(0, 1);
38 | rect = new BoundingRect(topLeft, 4, 3);
39 |
40 | ArrayList gameObjects = root.getGameObjects(rect);
41 |
42 | print(gameObjects);
43 |
44 | topLeft = new Point(5, 8);
45 | rect = new BoundingRect(topLeft, 1, 1);
46 |
47 | gameObjects = root.getGameObjects(rect);
48 |
49 | print(gameObjects);
50 |
51 | topLeft = new Point(6, 3);
52 | rect = new BoundingRect(topLeft, 3, 1);
53 |
54 | gameObjects = root.getGameObjects(rect);
55 |
56 | print(gameObjects);
57 | }
58 |
59 | private static void print(ArrayList gameObjects) {
60 | System.out.println("--------------------");
61 | for (int i = 0; i < gameObjects.size(); ++i) {
62 | GameObject obj = gameObjects.get(i);
63 | System.out.println(String.format("%d. [%d] (%d, %d)",
64 | i + 1,
65 | obj.getData(),
66 | obj.getPosition().getX(),
67 | obj.getPosition().getY()));
68 | }
69 |
70 | System.out.println(String.format("Count: %d", gameObjects.size()));
71 |
72 | System.out.println("--------------------");
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/07/src/academy/pocu/comp3500samples/w07/quadtree/Quadrant.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w07.quadtree;
2 |
3 | import java.util.ArrayList;
4 |
5 | public final class Quadrant {
6 | private static final int MIN_QUAD_DIMENSION = 2;
7 |
8 | private final BoundingRect boundingRect;
9 |
10 | private Quadrant topLeft;
11 | private Quadrant topRight;
12 | private Quadrant bottomLeft;
13 | private Quadrant bottomRight;
14 |
15 | private ArrayList gameObjects = new ArrayList<>();
16 |
17 | public Quadrant(final BoundingRect boundingRect) {
18 | this.boundingRect = boundingRect;
19 |
20 | createChildren();
21 | }
22 |
23 | public boolean insert(final GameObject gameObject) {
24 | final Point position = gameObject
25 | .getPosition();
26 |
27 | if (!this.boundingRect
28 | .contains(position)) {
29 | return false;
30 | }
31 |
32 | this.gameObjects.add(gameObject);
33 |
34 | if (this.topLeft != null) {
35 | this.topLeft.insert(gameObject);
36 | this.topRight.insert(gameObject);
37 | this.bottomLeft.insert(gameObject);
38 | this.bottomRight.insert(gameObject);
39 | }
40 |
41 | return true;
42 | }
43 |
44 | public ArrayList getGameObjects(final BoundingRect rect) {
45 | if (!this.boundingRect.contains(rect)) {
46 | return new ArrayList<>();
47 | }
48 |
49 | if (this.topLeft == null) {
50 | return this.gameObjects;
51 | }
52 |
53 | if (this.topLeft.boundingRect
54 | .contains(rect)) {
55 | return this.topLeft
56 | .getGameObjects(rect);
57 | }
58 |
59 | if (this.topRight.boundingRect
60 | .contains(rect)) {
61 | return this.topRight
62 | .getGameObjects(rect);
63 | }
64 |
65 | if (this.bottomRight.boundingRect
66 | .contains(rect)) {
67 | return this.bottomRight
68 | .getGameObjects(rect);
69 | }
70 |
71 | if (this.bottomLeft.boundingRect
72 | .contains(rect)) {
73 | return this.bottomLeft
74 | .getGameObjects(rect);
75 | }
76 |
77 | return this.gameObjects;
78 | }
79 |
80 | private void createChildren() {
81 | final int width = this.boundingRect.getWidth();
82 | final int height = this.boundingRect.getHeight();
83 |
84 | if (width < 2 * MIN_QUAD_DIMENSION
85 | || height < 2 * MIN_QUAD_DIMENSION) {
86 | return;
87 | }
88 |
89 | int x1 = this.boundingRect
90 | .getTopLeft()
91 | .getX();
92 | int y1 = this.boundingRect
93 | .getTopLeft()
94 | .getY();
95 | int x2 = this.boundingRect
96 | .getBottomRight()
97 | .getX();
98 | int y2 = this.boundingRect
99 | .getBottomRight()
100 | .getY();
101 |
102 | int midX = (x1 + x2) / 2;
103 | int midY = (y1 + y2) / 2;
104 |
105 | Point p1 = new Point(x1, y1);
106 | Point p2 = new Point(midX, midY);
107 |
108 | BoundingRect rect = new BoundingRect(p1,
109 | p2.getX() - p1.getX(),
110 | p2.getY() - p1.getY());
111 |
112 | this.topLeft = new Quadrant(rect);
113 |
114 | p1 = new Point(midX, y1);
115 | p2 = new Point(x2, midY);
116 | rect = new BoundingRect(p1,
117 | p2.getX() - p1.getX(),
118 | p2.getY() - p1.getY());
119 |
120 | this.topRight = new Quadrant(rect);
121 |
122 | p1 = new Point(x1, midY);
123 | p2 = new Point(midX, y2);
124 | rect = new BoundingRect(p1,
125 | p2.getX() - p1.getX(),
126 | p2.getY() - p1.getY());
127 |
128 | this.bottomLeft = new Quadrant(rect);
129 |
130 | p1 = new Point(midX, midY);
131 | p2 = new Point(x2, y2);
132 | rect = new BoundingRect(p1,
133 | p2.getX() - p1.getX(),
134 | p2.getY() - p1.getY());
135 |
136 | this.bottomRight = new Quadrant(rect);
137 | }
138 | }
139 |
--------------------------------------------------------------------------------
/07/w07.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/09/src/academy/pocu/comp3500samples/w09/directorytree/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w09.directorytree;
2 |
3 | import java.io.File;
4 |
5 | public class Program {
6 | private static final int INDENT_LENGTH = 2;
7 |
8 | public static void main(String[] args) {
9 | if (args.length != 1) {
10 | System.err.println(String.format("Wrong number of arguments: %d", args.length));
11 | System.exit(1);
12 | }
13 |
14 | String path = args[0];
15 | File file = new File(path);
16 |
17 | printDirectoryTreeRecursive(file, 0);
18 | }
19 |
20 | private static void printDirectoryTreeRecursive(File file, int depth) {
21 | String filename = file.getName();
22 |
23 | String message = String.format("- %s",
24 | filename);
25 | message = padLeft(INDENT_LENGTH * depth,
26 | message);
27 |
28 | System.out.println(message);
29 |
30 | if (file.isDirectory()) {
31 | File[] children = file.listFiles();
32 |
33 | for (File child : children) {
34 | printDirectoryTreeRecursive(child,
35 | depth + 1);
36 | }
37 | }
38 | }
39 |
40 | private static String padLeft(int padLength, String message) {
41 | StringBuilder sb = new StringBuilder();
42 | for (int i = 0; i < padLength; i++) {
43 | sb.append(' ');
44 | }
45 |
46 | sb.append(message);
47 |
48 | return sb.toString();
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/09/src/academy/pocu/comp3500samples/w09/tictactoe/Move.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w09.tictactoe;
2 |
3 | public class Move {
4 | private int index;
5 | private int score;
6 |
7 | public Move(final int index, final int score) {
8 | this.index = index;
9 | this.score = score;
10 | }
11 |
12 | public int getIndex() {
13 | return this.index;
14 | }
15 |
16 | public int getScore() {
17 | return this.score;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/09/src/academy/pocu/comp3500samples/w09/tictactoe/Player.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w09.tictactoe;
2 |
3 | public enum Player {
4 | X,
5 | O
6 | }
7 |
--------------------------------------------------------------------------------
/09/src/academy/pocu/comp3500samples/w09/tictactoe/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w09.tictactoe;
2 |
3 | public class Program {
4 | public static void main(String[] args) {
5 | {
6 | Player[] board = new Player[TicTacToe.BOARD_SIZE];
7 |
8 | int index = TicTacToe
9 | .getBestMoveIndex(board,
10 | Player.X);
11 |
12 | System.out.println(String.format("best move index: %d", index));
13 | }
14 |
15 | {
16 | Player[] board = new Player[]{
17 | null, Player.O, Player.X,
18 | Player.X, Player.O, Player.O,
19 | null, null, Player.X};
20 |
21 | int index = TicTacToe
22 | .getBestMoveIndex(board,
23 | Player.X);
24 |
25 | System.out.println(String.format("best move index: %d", index));
26 | }
27 |
28 | {
29 | Player[] board = new Player[]{
30 | Player.O, null, Player.X,
31 | Player.X, null, Player.X,
32 | null, Player.O, Player.O};
33 |
34 | int index = TicTacToe
35 | .getBestMoveIndex(board,
36 | Player.X);
37 |
38 | System.out.println(String.format("best move index: %d", index));
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/09/src/academy/pocu/comp3500samples/w09/tictactoe/TicTacToe.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w09.tictactoe;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class TicTacToe {
6 | public static final int BOARD_SIZE = 9;
7 |
8 | private TicTacToe() {
9 | }
10 |
11 | public static int getBestMoveIndex(final Player[] board, final Player player) {
12 | assert (board.length == BOARD_SIZE);
13 |
14 | Player opponent = player == Player.O
15 | ? Player.X : Player.O;
16 |
17 | Move move = getBestMoveRecursive(board,
18 | player,
19 | opponent,
20 | player);
21 |
22 | return move.getIndex();
23 | }
24 |
25 | private static Move getBestMoveRecursive(final Player[] board, final Player player, final Player opponent, final Player turn) {
26 | assert (board.length == BOARD_SIZE);
27 |
28 | if (hasWon(board, opponent)) {
29 | return new Move(-1, -10);
30 | }
31 |
32 | if (hasWon(board, player)) {
33 | return new Move(-1, 10);
34 | }
35 |
36 | ArrayList availableIndexes = getEmptyIndexes(board);
37 | if (availableIndexes.isEmpty()) {
38 | return new Move(-1, 0);
39 | }
40 |
41 | ArrayList moves = new ArrayList<>();
42 |
43 | for (int i = 0; i < availableIndexes.size(); ++i) {
44 | int index = availableIndexes.get(i);
45 |
46 | Player[] newBoard = copyBoard(board);
47 |
48 | newBoard[index] = turn;
49 |
50 | Player nextPlayer = turn == player
51 | ? opponent : player;
52 |
53 | int score = getBestMoveRecursive(newBoard,
54 | player,
55 | opponent,
56 | nextPlayer)
57 | .getScore();
58 |
59 | Move move = new Move(index, score);
60 | moves.add(move);
61 | }
62 |
63 | if (turn == player) {
64 | return getMaxScoreMove(moves);
65 | }
66 |
67 | return getMinScoreMove(moves);
68 | }
69 |
70 | private static Move getMaxScoreMove(final ArrayList moves) {
71 | assert (!moves.isEmpty());
72 |
73 | Move bestMove = moves.get(0);
74 | for (int i = 1; i < moves.size(); ++i) {
75 | if (moves.get(i).getScore() > bestMove.getScore()) {
76 | bestMove = moves.get(i);
77 | }
78 | }
79 |
80 | return bestMove;
81 | }
82 |
83 | private static Move getMinScoreMove(final ArrayList moves) {
84 | assert (!moves.isEmpty());
85 |
86 | Move bestMove = moves.get(0);
87 | for (int i = 0; i < moves.size(); ++i) {
88 | if (moves.get(i).getScore() < bestMove.getScore()) {
89 | bestMove = moves.get(i);
90 | }
91 | }
92 |
93 | return bestMove;
94 | }
95 |
96 | private static Player[] copyBoard(final Player[] board) {
97 | assert (board.length == BOARD_SIZE);
98 |
99 | Player[] newBoard = new Player[board.length];
100 |
101 | for (int i = 0; i < board.length; ++i) {
102 | newBoard[i] = board[i];
103 | }
104 |
105 | return newBoard;
106 | }
107 |
108 | private static ArrayList getEmptyIndexes(final Player[] board) {
109 | assert (board.length == BOARD_SIZE);
110 |
111 | ArrayList indexes = new ArrayList<>();
112 |
113 | for (int i = 0; i < board.length; ++i) {
114 | if (board[i] == null) {
115 | indexes.add(i);
116 | }
117 | }
118 |
119 | return indexes;
120 | }
121 |
122 | private static boolean hasWon(final Player[] board, final Player player) {
123 | assert (board.length == BOARD_SIZE);
124 |
125 | return (board[0] == player && board[1] == player && board[2] == player)
126 | || (board[3] == player && board[4] == player && board[5] == player)
127 | || (board[6] == player && board[7] == player && board[8] == player)
128 | || (board[0] == player && board[3] == player && board[6] == player)
129 | || (board[1] == player && board[4] == player && board[7] == player)
130 | || (board[2] == player && board[5] == player && board[8] == player)
131 | || (board[0] == player && board[4] == player && board[8] == player)
132 | || (board[2] == player && board[4] == player && board[6] == player);
133 | }
134 | }
135 |
--------------------------------------------------------------------------------
/09/w09.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/10/src/academy/pocu/comp3500samples/w10/knapsack/Item.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w10.knapsack;
2 |
3 | public class Item {
4 | private int value;
5 | private int space;
6 |
7 | public Item(final int value, final int space) {
8 | this.value = value;
9 | this.space = space;
10 | }
11 |
12 | public int getValue() {
13 | return this.value;
14 | }
15 |
16 | public int getSpace() {
17 | return this.space;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/10/src/academy/pocu/comp3500samples/w10/knapsack/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w10.knapsack;
2 |
3 | public class Program {
4 | public static void main(String args[]) {
5 | Item[] items = new Item[5];
6 |
7 | items[0] = new Item(3, 5);
8 | items[1] = new Item(9, 12);
9 | items[2] = new Item(1, 2);
10 | items[3] = new Item(5, 4);
11 | items[4] = new Item(7, 9);
12 |
13 | int maxValue = getMaxValue(15, items);
14 |
15 | System.out.println(String.format("Max Value: %d", maxValue));
16 | }
17 |
18 | private static int getMaxValue(int numSpace, Item[] items) {
19 | int numItems = items.length;
20 |
21 | int cache[][] = new int[numItems][numSpace + 1];
22 |
23 | for (int s = 1; s <= numSpace; ++s) {
24 | if (items[0].getSpace() > s) {
25 | continue;
26 | }
27 |
28 | cache[0][s] = items[0].getValue();
29 | }
30 |
31 | for (int i = 1; i < numItems; ++i) {
32 | for (int s = 1; s <= numSpace; ++s) {
33 | if (items[i].getSpace() > s) {
34 | cache[i][s] = cache[i - 1][s];
35 | continue;
36 | }
37 |
38 | int remainingSpace = s - items[i].getSpace();
39 | int remainingMaxValue = cache[i - 1][remainingSpace];
40 |
41 | int choice1 = cache[i - 1][s];
42 | int choice2 = items[i].getValue()
43 | + remainingMaxValue;
44 |
45 | cache[i][s] = Math.max(choice1,
46 | choice2);
47 | }
48 | }
49 |
50 | return cache[numItems - 1][numSpace];
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/10/w10.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/11/src/academy/pocu/comp3500samples/w11/topologicalsort/Course.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w11.topologicalsort;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collections;
5 | import java.util.List;
6 |
7 | public final class Course {
8 | private final String title;
9 | private final ArrayList nextCourses = new ArrayList<>();
10 |
11 | public Course(final String title) {
12 | this.title = title;
13 | }
14 |
15 | public String getTitle() {
16 | return this.title;
17 | }
18 |
19 | public List getNextCourses() {
20 | return Collections.unmodifiableList(this.nextCourses);
21 | }
22 |
23 | public void addNext(final Course course) {
24 | this.nextCourses.add(course);
25 | }
26 | }
--------------------------------------------------------------------------------
/11/src/academy/pocu/comp3500samples/w11/topologicalsort/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w11.topologicalsort;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collections;
5 | import java.util.HashSet;
6 | import java.util.LinkedList;
7 |
8 | public class Program {
9 | public static void main(String[] args) {
10 | ArrayList courses = createCourseGraph();
11 |
12 | LinkedList sortedCourses = sortTopologically(courses);
13 |
14 | for (Course course : sortedCourses) {
15 | System.out.println(course.getTitle());
16 | }
17 |
18 | System.out.println("=======================================");
19 |
20 | Collections.shuffle(courses);
21 |
22 | sortedCourses = sortTopologically(courses);
23 |
24 | for (Course course : sortedCourses) {
25 | System.out.println(course.getTitle());
26 | }
27 | }
28 |
29 | private static LinkedList sortTopologically(ArrayList courses) {
30 | HashSet discovered = new HashSet<>();
31 | LinkedList sortedList = new LinkedList<>();
32 |
33 | for (Course course : courses) {
34 | if (discovered.contains(course)) {
35 | continue;
36 | }
37 |
38 | topologicalSortRecursive(course,
39 | discovered,
40 | sortedList);
41 | }
42 |
43 | return sortedList;
44 | }
45 |
46 | private static void topologicalSortRecursive(Course course, HashSet discovered, LinkedList linkedList) {
47 | discovered.add(course);
48 |
49 | for (Course nextCourse : course.getNextCourses()) {
50 | if (discovered.contains(nextCourse)) {
51 | continue;
52 | }
53 |
54 | topologicalSortRecursive(nextCourse,
55 | discovered,
56 | linkedList);
57 | }
58 |
59 | linkedList.addFirst(course);
60 | }
61 |
62 | private static ArrayList createCourseGraph() {
63 | final Course comp0000 = new Course("0000: Intro to Programming for Novices and Hobbyists (C#)");
64 | final Course comp1500 = new Course("1500: Intro to Professional Programming with C#");
65 | final Course comp1000 = new Course("1000: Math for Software Engineers");
66 | final Course comp1600 = new Course("1600: Visual Programming with C#");
67 | final Course comp2200 = new Course("2200: Unmanaged Programming with C");
68 | final Course comp2500 = new Course("2500: Object Oriented Programming and Design with Java");
69 | final Course comp4700 = new Course("4700: Database Programming with C#");
70 | final Course comp2300 = new Course("2300: Assembly");
71 | final Course comp3200 = new Course("3200: Unmanaged Programming with C++");
72 | final Course comp3500 = new Course("3500: Algorithm & Data Structure with Java");
73 | final Course comp3000 = new Course("3000: Computer Architecture (C or Assembly)");
74 | final Course comp4000 = new Course("4000: Operating Systems (C)");
75 | final Course comp4100 = new Course("4100: Data Comm (C or C++");
76 |
77 | comp0000.addNext(comp1500);
78 |
79 | comp1500.addNext(comp1000);
80 | comp1500.addNext(comp1600);
81 | comp1500.addNext(comp2200);
82 | comp1500.addNext(comp2500);
83 |
84 | comp1000.addNext(comp1600);
85 | comp1000.addNext(comp2200);
86 | comp1000.addNext(comp2500);
87 |
88 | comp1600.addNext(comp4700);
89 |
90 | comp2200.addNext(comp2300);
91 | comp2200.addNext(comp3200);
92 | comp2200.addNext(comp3000);
93 |
94 | comp2500.addNext(comp4700);
95 | comp2500.addNext(comp3200);
96 | comp2500.addNext(comp3500);
97 |
98 | comp2300.addNext(comp3000);
99 |
100 | comp3200.addNext(comp4000);
101 | comp3200.addNext(comp4100);
102 |
103 | comp3000.addNext(comp4000);
104 |
105 | ArrayList courses = new ArrayList<>();
106 |
107 | courses.add(comp0000);
108 | courses.add(comp1000);
109 | courses.add(comp1500);
110 | courses.add(comp1600);
111 | courses.add(comp2200);
112 | courses.add(comp2300);
113 | courses.add(comp2500);
114 | courses.add(comp3000);
115 | courses.add(comp3200);
116 | courses.add(comp3500);
117 | courses.add(comp4000);
118 | courses.add(comp4100);
119 | courses.add(comp4700);
120 |
121 | return courses;
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/11/w11.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/12/src/academy/pocu/comp3500samples/w12/dijkstra/Candidate.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w12.dijkstra;
2 |
3 | public final class Candidate implements Comparable {
4 | private final Node node;
5 | private final int distance;
6 |
7 | public Candidate(final Node node, final int distance) {
8 | this.node = node;
9 | this.distance = distance;
10 | }
11 |
12 | public Node getNode() {
13 | return this.node;
14 | }
15 |
16 | public int getDistance() {
17 | return this.distance;
18 | }
19 |
20 | @Override
21 | public int compareTo(Candidate o) {
22 | return this.distance - o.distance;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/12/src/academy/pocu/comp3500samples/w12/dijkstra/Dijkstra.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w12.dijkstra;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 | import java.util.PriorityQueue;
6 |
7 | public class Dijkstra {
8 | private Dijkstra() {
9 | }
10 |
11 | public static HashMap run(final HashMap nodes, final String from, final HashMap prevs) {
12 | HashMap minDists = new HashMap<>();
13 |
14 | final int INF = Integer.MAX_VALUE;
15 | for (var entry : nodes.entrySet()) {
16 | String name = entry.getKey();
17 |
18 | minDists.put(name, INF);
19 | }
20 |
21 | minDists.put(from, 0);
22 |
23 | prevs.put(from, null);
24 |
25 | PriorityQueue open = new PriorityQueue<>();
26 |
27 | Node s = nodes.get(from);
28 | Candidate candidate = new Candidate(s, 0);
29 |
30 | open.add(candidate);
31 |
32 | while (!open.isEmpty()) {
33 | candidate = open.poll();
34 |
35 | Node n = candidate.getNode();
36 | String nodeName = n.getName();
37 |
38 | int minDist = minDists.get(nodeName);
39 | int dist = candidate.getDistance();
40 |
41 | if (minDist < dist) {
42 | continue;
43 | }
44 |
45 | Map roads = n
46 | .getRoads();
47 |
48 | for (var e : roads.entrySet()) {
49 | Node next = e.getKey();
50 |
51 | int weight = e.getValue();
52 | int newDist = minDist + weight;
53 |
54 | String nextName = next.getName();
55 | int nextMinDist = minDists
56 | .get(nextName);
57 |
58 | if (newDist >= nextMinDist) {
59 | continue;
60 | }
61 |
62 | minDists.put(nextName, newDist);
63 | prevs.put(nextName, nodeName);
64 |
65 | Candidate newCandidate = new Candidate(next, newDist);
66 |
67 | open.add(newCandidate);
68 | }
69 | }
70 |
71 | return minDists;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/12/src/academy/pocu/comp3500samples/w12/dijkstra/Node.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w12.dijkstra;
2 |
3 | import java.util.HashMap;
4 | import java.util.Map;
5 |
6 | public final class Node {
7 | private final String name;
8 | private final HashMap roads = new HashMap<>();
9 |
10 | public Node(final String name) {
11 | this.name = name;
12 | }
13 |
14 | public String getName() {
15 | return name;
16 | }
17 |
18 | public Map getRoads() {
19 | return this.roads;
20 | }
21 |
22 | public void addRoad(final Node to,
23 | final int dist) {
24 | this.roads.put(to, dist);
25 | }
26 |
27 | public int getDistance(final Node to) {
28 | return this.roads.get(to);
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/12/src/academy/pocu/comp3500samples/w12/dijkstra/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w12.dijkstra;
2 |
3 | import java.util.HashMap;
4 | import java.util.LinkedList;
5 |
6 | public class Program {
7 | public static void main(String[] args) {
8 | HashMap nodes = createNodes();
9 |
10 | HashMap prevs = new HashMap<>();
11 |
12 | HashMap minDists = Dijkstra.run(nodes, "Home", prevs);
13 |
14 | int schoolDist = minDists.get("School");
15 | System.out.println(schoolDist);
16 |
17 | int bankDist = minDists.get("Bank");
18 | System.out.println(bankDist);
19 |
20 | int libDists = minDists.get("Library");
21 | System.out.println(libDists);
22 |
23 | LinkedList path = new LinkedList<>();
24 |
25 | String name = "School";
26 | while (name != null) {
27 | path.addFirst(name);
28 | name = prevs.get(name);
29 | }
30 |
31 | String pathString = String.join(" -> ",
32 | path);
33 |
34 | System.out.println(pathString);
35 | }
36 |
37 | private static HashMap createNodes() {
38 | Node home = new Node("Home");
39 | Node policeStation = new Node("Police Station");
40 | Node school = new Node("School");
41 | Node park = new Node("Park");
42 | Node bank = new Node("Bank");
43 | Node library = new Node("Library");
44 |
45 | home.addRoad(policeStation, 2);
46 | policeStation.addRoad(home, 2);
47 |
48 | home.addRoad(park, 3);
49 | park.addRoad(home, 3);
50 |
51 | policeStation.addRoad(bank, 1);
52 | bank.addRoad(policeStation, 1);
53 |
54 | policeStation.addRoad(school, 6);
55 | school.addRoad(policeStation, 6);
56 |
57 | bank.addRoad(library, 2);
58 | library.addRoad(bank, 2);
59 |
60 | bank.addRoad(park, 2);
61 | park.addRoad(bank, 2);
62 |
63 | school.addRoad(library, 1);
64 | library.addRoad(school, 1);
65 |
66 | HashMap nodes = new HashMap<>();
67 |
68 | nodes.put(home.getName(), home);
69 | nodes.put(policeStation.getName(), policeStation);
70 | nodes.put(school.getName(), school);
71 | nodes.put(park.getName(), park);
72 | nodes.put(bank.getName(), bank);
73 | nodes.put(library.getName(), library);
74 |
75 | return nodes;
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/12/w12.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/13/src/academy/pocu/comp3500samples/w13/kruskal/DisjointSet.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w13.kruskal;
2 |
3 | import java.util.HashMap;
4 |
5 | public final class DisjointSet {
6 | private class SetNode {
7 | private String parent;
8 | private int size;
9 |
10 | public SetNode(final String parent, final int size) {
11 | this.parent = parent;
12 | this.size = size;
13 | }
14 | }
15 |
16 | private final HashMap sets = new HashMap<>(64);
17 |
18 | public DisjointSet(final String[] nodes) {
19 | for (String s : nodes) {
20 | SetNode setNode = new SetNode(s, 1);
21 | this.sets.put(s, setNode);
22 | }
23 | }
24 |
25 | public String find(final String node) {
26 | assert (this.sets.containsKey(node));
27 |
28 | SetNode n = this.sets.get(node);
29 | String parent = n.parent;
30 | if (parent.equals(node)) {
31 | return node;
32 | }
33 |
34 | n.parent = find(n.parent);
35 |
36 | return n.parent;
37 | }
38 |
39 | public void union(final String node1, final String node2) {
40 | assert (this.sets.containsKey(node1));
41 | assert (this.sets.containsKey(node2));
42 |
43 | String root1 = find(node1);
44 | String root2 = find(node2);
45 |
46 | if (root1.equals(root2)) {
47 | return;
48 | }
49 |
50 | SetNode parent = this.sets.get(root1);
51 | SetNode child = this.sets.get(root2);
52 |
53 | if (parent.size < child.size) {
54 | SetNode temp = parent;
55 |
56 | parent = child;
57 | child = temp;
58 | }
59 |
60 | child.parent = parent.parent;
61 | parent.size = child.size + parent.size;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/13/src/academy/pocu/comp3500samples/w13/kruskal/Edge.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w13.kruskal;
2 |
3 | public final class Edge implements Comparable {
4 | private final String node1;
5 | private final String node2;
6 | private final int weight;
7 |
8 | public Edge(final String node1,
9 | final String node2,
10 | final int weight) {
11 | this.node1 = node1;
12 | this.node2 = node2;
13 | this.weight = weight;
14 | }
15 |
16 | public String getNode1() {
17 | return this.node1;
18 | }
19 |
20 | public String getNode2() {
21 | return this.node2;
22 | }
23 |
24 | public int getWeight() {
25 | return this.weight;
26 | }
27 |
28 | @Override
29 | public int compareTo(Edge e) {
30 | return this.weight - e.weight;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/13/src/academy/pocu/comp3500samples/w13/kruskal/Kruskal.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w13.kruskal;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Arrays;
5 |
6 | public final class Kruskal {
7 | private Kruskal() {
8 | }
9 |
10 | public static ArrayList run(final String[] nodes, final Edge[] edges) {
11 | DisjointSet set = new DisjointSet(nodes);
12 |
13 | ArrayList mst = new ArrayList<>(edges.length);
14 |
15 | Arrays.sort(edges);
16 |
17 | for (int i = 0; i < edges.length; ++i) {
18 | String n1 = edges[i].getNode1();
19 | String n2 = edges[i].getNode2();
20 |
21 | String root1 = set.find(n1);
22 | String root2 = set.find(n2);
23 |
24 | if (!root1.equals(root2)) {
25 | mst.add(edges[i]);
26 | set.union(n1, n2);
27 | }
28 | }
29 |
30 | return mst;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/13/src/academy/pocu/comp3500samples/w13/kruskal/Program.java:
--------------------------------------------------------------------------------
1 | package academy.pocu.comp3500samples.w13.kruskal;
2 |
3 | import java.util.ArrayList;
4 |
5 | public class Program {
6 | public static void main(String[] args) {
7 | String[] nodes = new String[]{
8 | "0",
9 | "1",
10 | "2",
11 | "3",
12 | "4",
13 | "5",
14 | "6",
15 | "7"
16 | };
17 |
18 | Edge[] edges = new Edge[]{
19 | new Edge("0", "4", 9),
20 | new Edge("0", "5", 2),
21 | new Edge("0", "2", 2),
22 | new Edge("1", "4", 6),
23 | new Edge("1", "5", 10),
24 | new Edge("2", "5", 1),
25 | new Edge("2", "7", 11),
26 | new Edge("2", "3", 5),
27 | new Edge("5", "7", 8),
28 | new Edge("5", "4", 3),
29 | new Edge("6", "7", 13)
30 | };
31 |
32 | ArrayList mst = Kruskal
33 | .run(nodes, edges);
34 |
35 | for (Edge e : mst) {
36 | String edgeString = String
37 | .format("(%s, %s)",
38 | e.getNode1(),
39 | e.getNode2());
40 |
41 | System.out.println(edgeString);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/13/w13.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/CODEOWNERS:
--------------------------------------------------------------------------------
1 | * @POCU/reviewers
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # COMP3500CodeSamples
2 |
3 | Code samples for COMP3500
--------------------------------------------------------------------------------