From b353eb839eb143e37cfc981b0a685c1439565beb Mon Sep 17 00:00:00 2001 From: Alex4386 Date: Thu, 18 Apr 2024 22:50:52 +0900 Subject: [PATCH] chore: homework --- .../java/me/alex4386/gachon/sw14462/Main.java | 2 +- .../sw14462/day11/quiz/PersonAddress.java | 58 +++++++++ .../sw14462/day11/quiz/PersonAddressTest.java | 25 ++++ .../alex4386/gachon/sw14462/day13/Main.java | 18 +++ .../day13/ex7_1/ArrayOfTemperatures2.java | 43 +++++++ .../gachon/sw14462/day13/ex7_1/Main.java | 7 ++ .../gachon/sw14462/day13/ex7_1a/Main.java | 7 ++ .../sw14462/day13/ex7_1a/ScoreReader.java | 51 ++++++++ .../sw14462/day13/ex7_1a/ScoreReaderTest.java | 32 +++++ .../day13/ex7_1b/FrequencyAnalyzer.java | 45 +++++++ .../day13/ex7_1b/FrequencyAnalyzerTest.java | 28 +++++ .../gachon/sw14462/day13/ex7_1b/Main.java | 7 ++ .../alex4386/gachon/sw14462/day14/Main.java | 18 +++ .../sw14462/day14/ex7_2a/ArraySorter.java | 47 +++++++ .../gachon/sw14462/day14/ex7_2a/Main.java | 45 +++++++ .../day14/ex7_2a/SelectionSortDemo.java | 18 +++ .../sw14462/day14/ex7_2b/ArrayUtils.java | 43 +++++++ .../gachon/sw14462/day14/ex7_2b/Main.java | 37 ++++++ .../gachon/sw14462/day14/ex7_2c/Main.java | 7 ++ .../sw14462/day14/ex7_2c/Polynomial.java | 36 ++++++ .../sw14462/day14/ex7_2c/PolynomialTest.java | 26 ++++ .../sw14462/day14/ex7_3/ArraySorter.java | 72 +++++++++++ .../gachon/sw14462/day14/ex7_3/Main.java | 45 +++++++ .../gachon/sw14462/day14/ex7_6/Main.java | 7 ++ .../gachon/sw14462/day14/ex7_6/TicTacToe.java | 117 ++++++++++++++++++ .../sw14462/day14/ex7_6/TicTacToeUI.java | 32 +++++ .../gachon/sw14462/day14/ex7_8/Main.java | 37 ++++++ .../sw14462/day14/ex7_8/PINShuffler.java | 62 ++++++++++ 28 files changed, 971 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day11/quiz/PersonAddress.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day11/quiz/PersonAddressTest.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day13/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1/ArrayOfTemperatures2.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1a/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1a/ScoreReader.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1a/ScoreReaderTest.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1b/FrequencyAnalyzer.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1b/FrequencyAnalyzerTest.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1b/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2a/ArraySorter.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2a/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2a/SelectionSortDemo.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2b/ArrayUtils.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2b/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2c/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2c/Polynomial.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2c/PolynomialTest.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_3/ArraySorter.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_3/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_6/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_6/TicTacToe.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_6/TicTacToeUI.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_8/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day14/ex7_8/PINShuffler.java diff --git a/src/main/java/me/alex4386/gachon/sw14462/Main.java b/src/main/java/me/alex4386/gachon/sw14462/Main.java index 34c44c4..ab2959e 100644 --- a/src/main/java/me/alex4386/gachon/sw14462/Main.java +++ b/src/main/java/me/alex4386/gachon/sw14462/Main.java @@ -5,7 +5,7 @@ import java.util.*; public class Main { - public static String currentTarget = "day11"; + public static String currentTarget = "day14"; public static boolean fallbackToLatest = true; public static Map> getAvailableTargetClassNames() { diff --git a/src/main/java/me/alex4386/gachon/sw14462/day11/quiz/PersonAddress.java b/src/main/java/me/alex4386/gachon/sw14462/day11/quiz/PersonAddress.java new file mode 100644 index 0000000..903e00b --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day11/quiz/PersonAddress.java @@ -0,0 +1,58 @@ +package me.alex4386.gachon.sw14462.day11.quiz; + +public class PersonAddress { + + public static String nationality = "Korea"; + + private String firstName; + private String lastName; + private String emailAddress; + + + private String phoneNumber; + + public String getFirstName() { + return this.firstName; + } + + public String getLastName() { + return this.lastName; + } + + public String getEmailAddress() { + return this.emailAddress; + } + + public String getPhoneNumber() { + return this.phoneNumber; + } + + public static String getNationality() { + return PersonAddress.nationality; + } + + public void setName(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public void setEmailAddress(String emailAddress) { + this.emailAddress = emailAddress; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public void printPersonInfo() { + System.out.println("First Name: " + this.firstName); + System.out.println("Last Name: " + this.lastName); + System.out.println("Email Address: " + this.emailAddress); + System.out.println("Phone Number: " + this.phoneNumber); + System.out.println("Nationality: " + PersonAddress.nationality); + } + + public boolean equals(PersonAddress other) { + return this.firstName.equals(other.firstName) && this.lastName.equals(other.lastName); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day11/quiz/PersonAddressTest.java b/src/main/java/me/alex4386/gachon/sw14462/day11/quiz/PersonAddressTest.java new file mode 100644 index 0000000..587c8fb --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day11/quiz/PersonAddressTest.java @@ -0,0 +1,25 @@ +package me.alex4386.gachon.sw14462.day11.quiz; + +public class PersonAddressTest { + public static void main(String[] args) { + PersonAddress addr1 = new PersonAddress(); + PersonAddress addr2 = new PersonAddress(); + + addr1.setName("Alex", "Lee"); + addr1.setEmailAddress("alex.lee@naver.com"); + addr1.setPhoneNumber("010-1234-5678"); + + addr2.setName("Alex", "Lee"); + addr2.setEmailAddress("alex@gmail.com"); + addr2.setPhoneNumber("010-5678-1234"); + + addr1.printPersonInfo(); + addr2.printPersonInfo(); + + if (addr1.equals(addr2)) { + System.out.println("Same person"); + } else { + System.out.println("Different person"); + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day13/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day13/Main.java new file mode 100644 index 0000000..5ce1355 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day13/Main.java @@ -0,0 +1,18 @@ +package me.alex4386.gachon.sw14462.day13; + +import me.alex4386.gachon.sw14462.utils.Chainloader; + +public class Main { + public static String chainloadTarget = "ex7_1b"; + + public static void main(String[] args) throws Throwable { + String packageName = Main.class.getPackage().getName(); + String chainLoadTargetClass = packageName + "." + chainloadTarget + ".Main"; + + try { + Chainloader.chainloadTarget(chainLoadTargetClass, args); + } catch (Exception e) { + throw e; + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1/ArrayOfTemperatures2.java b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1/ArrayOfTemperatures2.java new file mode 100644 index 0000000..0fd5c83 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1/ArrayOfTemperatures2.java @@ -0,0 +1,43 @@ +package me.alex4386.gachon.sw14462.day13.ex7_1; + +import java.util.Scanner; + +/** + Reads temperatures from the user and shows which are above + and which are below the average of all the temperatures. + */ +public class ArrayOfTemperatures2 +{ + public static void main(String[] args) { + Scanner keyboard = new Scanner(System.in); + System.out.println("How many temperatures do you have?"); + int size = keyboard.nextInt(); + double[] temperature = new double[size]; + // Read temperatures and compute their average: + System.out.println("Enter " + temperature.length + + " temperatures:"); + double sum = 0; + for (int index = 0; index < temperature.length; index++) { + temperature[index] = keyboard.nextDouble(); + sum = sum + temperature[index]; + } + double average = sum / temperature.length; + System.out.println("The average temperature is " + + average); +// Display each temperature and its relation to the +// average: + System.out.println("The temperatures are"); + for (int index = 0; index < temperature.length; index++) { + if (temperature[index] < average) + System.out.println(temperature[index] + + " below average"); + else if (temperature[index] > average) + System.out.println(temperature[index] + + " above average"); + else //temperature[index] == average + System.out.println(temperature[index] + + " the average"); + } + System.out.println("Have a nice week."); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1/Main.java new file mode 100644 index 0000000..57b6340 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1/Main.java @@ -0,0 +1,7 @@ +package me.alex4386.gachon.sw14462.day13.ex7_1; + +public class Main { + public static void main(String[] args) { + ArrayOfTemperatures2.main(args); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1a/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1a/Main.java new file mode 100644 index 0000000..69c0219 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1a/Main.java @@ -0,0 +1,7 @@ +package me.alex4386.gachon.sw14462.day13.ex7_1a; + +public class Main { + public static void main(String[] args) { + ScoreReaderTest.main(args); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1a/ScoreReader.java b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1a/ScoreReader.java new file mode 100644 index 0000000..93e2cbf --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1a/ScoreReader.java @@ -0,0 +1,51 @@ +package me.alex4386.gachon.sw14462.day13.ex7_1a; + +public class ScoreReader { + private int[] scores; + private int currentIdx; + + public ScoreReader(int size) { + this.scores = new int[size]; + this.currentIdx = 0; + } + + public void addScore(int score) { + if (this.currentIdx >= this.scores.length) { + System.out.println("Scores are full"); + return; + } + + this.scores[this.currentIdx] = score; + this.currentIdx++; + } + + public double getAverage() { + double sum = 0; + for (int i = 0; i < this.currentIdx; i++) { + sum += this.scores[i]; + } + + return sum / this.currentIdx; + } + + public int[] getScoresAboveAverage() { + double average = this.getAverage(); + + int size = 0; + for (int i = 0; i < this.currentIdx; i++) { + if (this.scores[i] > average) { + size++; + } + } + + int[] data = new int[size]; + for (int i = 0, j = 0; i < this.currentIdx; i++) { + if (this.scores[i] > average) { + data[j] = this.scores[i]; + j++; + } + } + + return data; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1a/ScoreReaderTest.java b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1a/ScoreReaderTest.java new file mode 100644 index 0000000..f1a4c28 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1a/ScoreReaderTest.java @@ -0,0 +1,32 @@ +package me.alex4386.gachon.sw14462.day13.ex7_1a; + +import java.util.Scanner; + +public class ScoreReaderTest { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.print("Enter the number of students: "); + int studentCount = scanner.nextInt(); + + if (studentCount <= 0) { + System.out.println("Invalid number of students"); + return; + } + + ScoreReader scoreReader = new ScoreReader(studentCount); + for (int i = 0; i < studentCount; i++) { + System.out.print("Enter the score of student " + (i + 1) + ": "); + int score = scanner.nextInt(); + scoreReader.addScore(score); + } + + System.out.println("Average score: " + scoreReader.getAverage()); + int[] scoresAboveAverage = scoreReader.getScoresAboveAverage(); + System.out.print("Scores above average: "); + for (int i = 0; i < scoresAboveAverage.length; i++) { + System.out.print(scoresAboveAverage[i] + " "); + } + System.out.println(); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1b/FrequencyAnalyzer.java b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1b/FrequencyAnalyzer.java new file mode 100644 index 0000000..f4bd91a --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1b/FrequencyAnalyzer.java @@ -0,0 +1,45 @@ +package me.alex4386.gachon.sw14462.day13.ex7_1b; + +public class FrequencyAnalyzer { + private String input; + private int[] frequency; + + public FrequencyAnalyzer(String input) { + this.input = input; + this.frequency = new int[10]; + + this.analyze(); + } + + public void setInput(String input) { + this.reset(); + this.input = input; + this.analyze(); + } + + private void reset() { + // reuse the array, but initialize to 0 + for (int i = 0; i < this.frequency.length; i++) { + this.frequency[i] = 0; + } + } + + public int[] getFrequency() { + return this.frequency; + } + + private void analyze() { + // split to each character + String[] characters = this.input.split(""); + + // count frequency + for (String character : characters) { + try { + int number = Integer.parseInt(character); + this.frequency[number]++; + } catch (NumberFormatException e) { + // ignore + } + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1b/FrequencyAnalyzerTest.java b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1b/FrequencyAnalyzerTest.java new file mode 100644 index 0000000..c1ad466 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1b/FrequencyAnalyzerTest.java @@ -0,0 +1,28 @@ +package me.alex4386.gachon.sw14462.day13.ex7_1b; + +import java.util.Scanner; + +public class FrequencyAnalyzerTest { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + FrequencyAnalyzer analyzer = null; + + while (true) { + System.out.print("Enter your phone number: "); + String phoneNumber = scanner.nextLine(); + + if (phoneNumber.equals("exit")) { + break; + } + + analyzer = analyzer == null ? new FrequencyAnalyzer(phoneNumber) : analyzer; + analyzer.setInput(phoneNumber); + + System.out.println("Frequency of each digit:"); + int[] frequency = analyzer.getFrequency(); + for (int i = 0; i < frequency.length; i++) { + System.out.println(i + ": " + frequency[i]); + } + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1b/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1b/Main.java new file mode 100644 index 0000000..32985eb --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day13/ex7_1b/Main.java @@ -0,0 +1,7 @@ +package me.alex4386.gachon.sw14462.day13.ex7_1b; + +public class Main { + public static void main(String[] args) { + FrequencyAnalyzerTest.main(args); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day14/Main.java new file mode 100644 index 0000000..2db9c5d --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/Main.java @@ -0,0 +1,18 @@ +package me.alex4386.gachon.sw14462.day14; + +import me.alex4386.gachon.sw14462.utils.Chainloader; + +public class Main { + public static String chainloadTarget = "ex7_8"; + + public static void main(String[] args) throws Throwable { + String packageName = Main.class.getPackage().getName(); + String chainLoadTargetClass = packageName + "." + chainloadTarget + ".Main"; + + try { + Chainloader.chainloadTarget(chainLoadTargetClass, args); + } catch (Exception e) { + throw e; + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2a/ArraySorter.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2a/ArraySorter.java new file mode 100644 index 0000000..146b0bd --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2a/ArraySorter.java @@ -0,0 +1,47 @@ +package me.alex4386.gachon.sw14462.day14.ex7_2a; + +/** + Class for sorting an array of base type int from smallest to largest. + */ +public class ArraySorter { + /** + * Precondition: Every element in anArray has a value. + * Action: Sorts the array into ascending order. + */ + public static void selectionSort(int[] anArray) { + for (int index = 0; index < anArray.length-1; index++){ // Place the correct value in anArray[index] + int indexOfNextSmallest = getIndexOfSmallest(index, anArray); + interchange(index, indexOfNextSmallest, anArray); //Assertion:anArray[0] <= anArray[1] <=...<= anArray[index] //and these are the smallest of the original array elements. //The remaining positions contain the rest of the original //array elements. + } + } + + /** + * Returns the index of the smallest value in the portion of the + * array that begins at the element whose index is startIndex and + * ends at the last element. + */ + private static int getIndexOfSmallest(int startIndex, int[] a) { + int min = a[startIndex]; + int indexOfMin = startIndex; + for (int index = startIndex + 1; index < a.length; index++) { + if (a[index] < min) { + //min is smallest of a[startIndex] through a[index] + min = a[index]; + indexOfMin = index; + } + } + return indexOfMin; + } + + + /** + Precondition: i and j are valid indices for the array a. + Postcondition: Values of a[i] and a[j] have been interchanged. + */ + private static void interchange(int i, int j, int[] a) + { + int temp = a[i]; + a[i] = a[j]; + a[j] = temp; //original value of a[i] + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2a/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2a/Main.java new file mode 100644 index 0000000..1fc9782 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2a/Main.java @@ -0,0 +1,45 @@ +package me.alex4386.gachon.sw14462.day14.ex7_2a; + +public class Main { + public static void main(String[] args) { + SelectionSortDemo.main(args); + + System.out.println("=== EXTRA DEMOS ==="); + + System.out.println("=== DEMO 1 ==="); + int[] testArr1 = {10, 5, 2, 7, 8, 3, 1, 6, 4, 9}; + System.out.println("Before sort:"); + showArray(testArr1); + + ArraySorter.selectionSort(testArr1); + System.out.println("After sort:"); + showArray(testArr1); + + System.out.println("=== DEMO 2 ==="); + int[] testArr2 = {174,2984,2874,16,283,3984,2983,28784,21873,27,1,256,2}; + System.out.println("Before sort:"); + showArray(testArr2); + + ArraySorter.selectionSort(testArr2); + + System.out.println("After sort:"); + showArray(testArr2); + + System.out.println("=== DEMO 3 ==="); + int[] testArr3 = {9, 16,5, 1, 2, 10, 4,3, 3, 2}; + System.out.println("Before sort:"); + showArray(testArr3); + ArraySorter.selectionSort(testArr3); + System.out.println("After sort:"); + showArray(testArr3); + + System.out.println("=== Extra demos END ==="); + } + + public static void showArray(int[] array) { + for (int i = 0; i < array.length; i++) { + System.out.print(array[i] + " "); + } + System.out.println(); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2a/SelectionSortDemo.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2a/SelectionSortDemo.java new file mode 100644 index 0000000..8347465 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2a/SelectionSortDemo.java @@ -0,0 +1,18 @@ +package me.alex4386.gachon.sw14462.day14.ex7_2a; + +public class SelectionSortDemo { + public static void main(String[] args) + { + int[] b = {7, 5, 11, 2, 16, 4, 18, 14, 12, 30}; + display(b, "before"); + ArraySorter.selectionSort(b); + display(b, "after"); + } + public static void display(int[] array, String when) + { + System.out.println("Array values " + when + " sorting:"); + for (int i = 0; i < array.length; i++) + System.out.print(array[i] + " "); + System.out.println( ); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2b/ArrayUtils.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2b/ArrayUtils.java new file mode 100644 index 0000000..81b5cf9 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2b/ArrayUtils.java @@ -0,0 +1,43 @@ +package me.alex4386.gachon.sw14462.day14.ex7_2b; + +public class ArrayUtils { + /** + * Returns the sum of elements in the indexed row of 2D array `arr` + * @param arr 2D array + * @param row row index to get sum + * @return sum of elements in the row + */ + public static double getSum(double[][] arr, int row) { + return getSum1D(arr[row]); + } + + /** + * Returns the sum of elements of 1D array `arr` + * @param arr 1D array + * @return sum of elements in the array + */ + private static double getSum1D(double[] arr) { + double sum = 0; + for (double value : arr) { + sum += value; + } + return sum; + } + + private static double getSumPerpendicular(double[][] arr, int column) { + double sum = 0; + for (int i = 0; i < arr.length; i++) { + try { + sum += arr[i][column]; + } catch (ArrayIndexOutOfBoundsException e) { + // ignore + } + } + + return sum; + } + + public static double getAverage(double[][] arr, int column) { + return getSumPerpendicular(arr, column) / arr.length; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2b/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2b/Main.java new file mode 100644 index 0000000..c560479 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2b/Main.java @@ -0,0 +1,37 @@ +package me.alex4386.gachon.sw14462.day14.ex7_2b; + +public class Main { + public static void main(String[] args) { + double[][] arr = { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + + System.out.println("double[][] arr = {"); + for (int i = 0; i < arr.length; i++) { + System.out.print(" {"); + for (int j = 0; j < arr[i].length; j++) { + System.out.print(arr[i][j]); + if (j != arr[i].length - 1) { + System.out.print(", "); + } + } + System.out.print("}"); + if (i != arr.length - 1) { + System.out.println(","); + } else { + System.out.println(); + } + } + System.out.println("};"); + + System.out.println("Sum of row 0: " + ArrayUtils.getSum(arr, 0)); + System.out.println("Sum of row 1: " + ArrayUtils.getSum(arr, 1)); + System.out.println("Sum of row 2: " + ArrayUtils.getSum(arr, 2)); + + System.out.println("Average of column 0: " + ArrayUtils.getAverage(arr, 0)); + System.out.println("Average of column 1: " + ArrayUtils.getAverage(arr, 1)); + System.out.println("Average of column 2: " + ArrayUtils.getAverage(arr, 2)); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2c/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2c/Main.java new file mode 100644 index 0000000..6be99b9 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2c/Main.java @@ -0,0 +1,7 @@ +package me.alex4386.gachon.sw14462.day14.ex7_2c; + +public class Main { + public static void main(String[] args) { + PolynomialTest.main(args); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2c/Polynomial.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2c/Polynomial.java new file mode 100644 index 0000000..8b34bd9 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2c/Polynomial.java @@ -0,0 +1,36 @@ +package me.alex4386.gachon.sw14462.day14.ex7_2c; + +public class Polynomial { + private int degree; + private double[] coefficients; + + public Polynomial(int max) { + this.degree = max; + this.coefficients = new double[max + 1]; + this.resetCoefficients(); + } + + /** + * Resets all coefficients to 0 + */ + private void resetCoefficients() { + for (int i = 0; i < this.coefficients.length; i++) { + this.coefficients[i] = 0; + } + } + + public void setConstant(int i, double value) { + if (i < 0 || i > this.coefficients.length - 1) + throw new IllegalArgumentException("Invalid degree"); + + this.coefficients[i] = value; + } + + public double evaluate(double x) { + double result = 0; + for (int i = 0; i < this.coefficients.length; i++) { + result += this.coefficients[i] * Math.pow(x, i); + } + return result; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2c/PolynomialTest.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2c/PolynomialTest.java new file mode 100644 index 0000000..fe85696 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_2c/PolynomialTest.java @@ -0,0 +1,26 @@ +package me.alex4386.gachon.sw14462.day14.ex7_2c; + +public class PolynomialTest { + public static void main(String[] args) { + System.out.println("=== Polynomial Test ==="); + + System.out.println("=== TEST 1 ==="); + Polynomial p = new Polynomial(3); + p.setConstant(0, 3); + p.setConstant(1, 5); + p.setConstant(2, 0); + p.setConstant(3, 2); + System.out.println("P(x) = 3 + 5x + 2x^3"); + System.out.println("P(2) = " + p.evaluate(2)); + + System.out.println("=== TEST 2 ==="); + Polynomial y = new Polynomial(2); + y.setConstant(0, 1); + y.setConstant(1, 2); + y.setConstant(2, 3); + System.out.println("P(x) = 1 + 2x + 3x^2"); + System.out.println("P(3) = " + y.evaluate(3)); + + System.out.println("=== End of test ==="); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_3/ArraySorter.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_3/ArraySorter.java new file mode 100644 index 0000000..1b99ed4 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_3/ArraySorter.java @@ -0,0 +1,72 @@ +package me.alex4386.gachon.sw14462.day14.ex7_3; + +/** + Class for sorting an array of base type int from smallest to largest. + */ +public class ArraySorter { + /** + * Precondition: Every element in anArray has a value. + * Action: Sorts the array into ascending order. + */ + public static void selectionSort(int[] anArray) { + for (int index = 0; index < anArray.length-1; index++){ // Place the correct value in anArray[index] + int indexOfNextSmallest = getIndexOfSmallest(index, anArray); + interchange(index, indexOfNextSmallest, anArray); //Assertion:anArray[0] <= anArray[1] <=...<= anArray[index] //and these are the smallest of the original array elements. //The remaining positions contain the rest of the original //array elements. + } + } + + /** + * Returns the index of the smallest value in the portion of the + * array that begins at the element whose index is startIndex and + * ends at the last element. + */ + private static int getIndexOfSmallest(int startIndex, int[] a) { + int min = a[startIndex]; + int indexOfMin = startIndex; + for (int index = startIndex + 1; index < a.length; index++) { + if (a[index] < min) { + //min is smallest of a[startIndex] through a[index] + min = a[index]; + indexOfMin = index; + } + } + return indexOfMin; + } + + + /** + Precondition: i and j are valid indices for the array a. + Postcondition: Values of a[i] and a[j] have been interchanged. + */ + private static void interchange(int i, int j, int[] a) + { + int temp = a[i]; + a[i] = a[j]; + a[j] = temp; //original value of a[i] + } + + + // ======== IMPLEMENTATION FOR EXERCISE 7.3 ======== + + /** + * Sorts the array into ascending order using bubble sort. + * @param a array to sort + */ + public static void bubbleSort(int[] a) { + for (int i = 0; i < a.length - 1; i++) { + if (a[i] > a[i + 1]) + interchange(i, i + 1, a); + } + + if (!isSorted(a)) + bubbleSort(a); + } + + public static boolean isSorted(int[] a) { + for (int i = 0; i < a.length - 1; i++) { + if (a[i] > a[i + 1]) + return false; + } + return true; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_3/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_3/Main.java new file mode 100644 index 0000000..2a4c1da --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_3/Main.java @@ -0,0 +1,45 @@ +package me.alex4386.gachon.sw14462.day14.ex7_3; + +public class Main { + public static void main(String[] args) { + System.out.println("=== EXTRA DEMOS ==="); + System.out.println("Contrary to Exercise 7_2a, This program demonstrates the bubble sort algorithm."); + + System.out.println("=== DEMO 1 ==="); + int[] testArr1 = {10, 5, 2, 7, 8, 3, 1, 6, 4, 9}; + System.out.println("Before sort:"); + showArray(testArr1); + + ArraySorter.bubbleSort(testArr1); + System.out.println("After sort:"); + showArray(testArr1); + + System.out.println("=== DEMO 2 ==="); + int[] testArr2 = {174,2984,2874,16,283,3984,2983,28784,21873,27,1,256,2}; + System.out.println("Before sort:"); + showArray(testArr2); + + ArraySorter.bubbleSort(testArr2); + + System.out.println("After sort:"); + showArray(testArr2); + + System.out.println("=== DEMO 3 ==="); + int[] testArr3 = {9, 16,5, 1, 2, 10, 4,3, 3, 2}; + System.out.println("Before sort:"); + showArray(testArr3); + ArraySorter.bubbleSort(testArr3); + System.out.println("After sort:"); + showArray(testArr3); + + System.out.println("=== Extra demos END ==="); + } + + + public static void showArray(int[] array) { + for (int i = 0; i < array.length; i++) { + System.out.print(array[i] + " "); + } + System.out.println(); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_6/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_6/Main.java new file mode 100644 index 0000000..831f9d3 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_6/Main.java @@ -0,0 +1,7 @@ +package me.alex4386.gachon.sw14462.day14.ex7_6; + +public class Main { + public static void main(String[] args) { + TicTacToeUI.main(args); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_6/TicTacToe.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_6/TicTacToe.java new file mode 100644 index 0000000..b81f2fb --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_6/TicTacToe.java @@ -0,0 +1,117 @@ +package me.alex4386.gachon.sw14462.day14.ex7_6; + +public class TicTacToe { + public static char[] availablePlayers = {'X', 'O'}; + private int turnIdx = 0; + private char[][] board = new char[3][3]; + private boolean ended = false; + + + public TicTacToe() { + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + board[i][j] = 0; + } + } + } + + public void addMove(int x, int y) { + if (this.ended) { + throw new IllegalStateException("Game has ended"); + } + + if (x < 0 || x >= 3 || y < 0 || y >= 3) { + throw new IllegalArgumentException("Invalid position"); + } + + if (this.board[x][y] != 0) { + throw new IllegalArgumentException("Position already taken"); + } + + this.board[x][y] = this.getCurrentPlayer(); + if (this.hasWinner()) { + this.ended = true; + return; + } + + System.out.println("[DEBUG] Increment turnIdx"); + this.turnIdx++; + } + + public char getCurrentPlayer() { + return availablePlayers[this.turnIdx % 2]; + } + + public boolean isFull() { + return this.turnIdx >= 9; + } + + public boolean hasWinner() { + // each player has at least 3 turns to win, so if user only took 2 turns, there is no winner + if (this.turnIdx < 4) { + return false; + } + + for (int i = 0; i < 3; i++) { + // - + if (this.board[i][0] != 0 && this.board[i][0] == this.board[i][1] && this.board[i][1] == this.board[i][2]) { + return true; + } + + // | + if (this.board[0][i] != 0 && this.board[0][i] == this.board[1][i] && this.board[1][i] == this.board[2][i]) { + return true; + } + } + + // \ + if (this.board[0][0] != 0 && this.board[0][0] == this.board[1][1] && this.board[1][1] == this.board[2][2]) { + return true; + } + + // / + if (this.board[0][2] != 0 && this.board[0][2] == this.board[1][1] && this.board[1][1] == this.board[2][0]) { + return true; + } + + return false; + } + + public char getWinner() { + if (this.hasWinner()) { + return this.getCurrentPlayer(); + } + return 0; + } + + public void reset() { + this.turnIdx = 0; + this.ended = false; + this.resetBoard(); + } + + private void resetBoard() { + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + board[i][j] = 0; + } + } + } + + public void displayBoard() { + for (int i = 0; i < 3; i++) { + System.out.println("-------------"); + System.out.print("| "); + for (int j = 0; j < 3; j++) { + char player = board[i][j]; + if (player == 0) { + player = ' '; + } + + System.out.print(player + " | "); + } + System.out.println(); + } + System.out.println("-------------"); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_6/TicTacToeUI.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_6/TicTacToeUI.java new file mode 100644 index 0000000..6d6e870 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_6/TicTacToeUI.java @@ -0,0 +1,32 @@ +package me.alex4386.gachon.sw14462.day14.ex7_6; + +import java.util.Scanner; + +public class TicTacToeUI { + public static void main(String[] args) { + TicTacToe game = new TicTacToe(); + Scanner stdin = new Scanner(System.in); + + while (true) { + game.displayBoard(); + if (game.hasWinner()) { + System.out.println("Player " + game.getCurrentPlayer() + " wins!"); + break; + } else if (game.isFull()) { + System.out.println("It's a draw!"); + break; + } + + System.out.print("Enter row and column (0-2) seperated by space for player " + game.getCurrentPlayer() + ": "); + + int row = stdin.nextInt(); + int col = stdin.nextInt(); + + try { + game.addMove(row, col); + } catch (Exception e) { + System.err.println(e.getMessage()); + } + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_8/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_8/Main.java new file mode 100644 index 0000000..cee74b5 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_8/Main.java @@ -0,0 +1,37 @@ +package me.alex4386.gachon.sw14462.day14.ex7_8; + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + String pin = "182655"; + + System.out.println("=== Test 1 ==="); + tryTest(scanner, pin); + + pin = "273612"; + System.out.println("=== Test 2 ==="); + tryTest(scanner, pin); + + pin = "123456"; + System.out.println("=== Test 3 ==="); + tryTest(scanner, pin); + } + + private static void tryTest(Scanner scanner, String pin) { + System.out.println("Target PIN: " + pin); + + PINShuffler shuffler = new PINShuffler(pin); + shuffler.printPinArray(); + + System.out.print("Enter the shuffled PIN: "); + String validatePin = scanner.nextLine(); + + if (validatePin.equals(shuffler.shuffle())) { + System.out.println("Correct PIN!"); + } else { + System.out.println("Incorrect PIN!"); + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_8/PINShuffler.java b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_8/PINShuffler.java new file mode 100644 index 0000000..2748047 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day14/ex7_8/PINShuffler.java @@ -0,0 +1,62 @@ +package me.alex4386.gachon.sw14462.day14.ex7_8; + +import java.security.SecureRandom; + +public class PINShuffler { + private String pin; + private char[] pinArray; + + + public PINShuffler(String pin) { + this.pin = pin; + this.pinArray = new char[10]; + this.generatePinArray(); + } + + private void generatePinArray() { + SecureRandom random = new SecureRandom(); + + for (int i = 0; i < 10; i++) { + pinArray[i] = String.format("%d", (random.nextInt() % 10) + 9).charAt(0); + } + } + + public void randomize() { + this.generatePinArray(); + } + + public char[] getPinArray() { + return this.pinArray; + } + + public String shuffle() { + StringBuilder builder = new StringBuilder(); + + for (int i = 0; i < this.pin.length(); i++) { + char c = this.pin.charAt(i); + int idx = c - '0'; + if (idx < 0 || idx > 9) { + throw new IllegalArgumentException("Invalid PIN"); + } + builder.append(this.pinArray[idx]); + } + + return builder.toString(); + } + + public void printPinArray() { + System.out.print("PIN: "); + for (int i = 0; i < 10; i++) { + System.out.print(i); + System.out.print(" "); + } + System.out.println(); + + System.out.print("NUM: "); + for (int i = 0; i < 10; i++) { + System.out.print(this.pinArray[i]); + System.out.print(" "); + } + System.out.println(); + } +}