From edd94c875bd5739c2a61c895333f956bb4bd0cca Mon Sep 17 00:00:00 2001 From: Alex4386 Date: Wed, 29 May 2024 11:31:19 +0900 Subject: [PATCH] chore: implemented ex14 --- .../java/me/alex4386/gachon/sw14462/Main.java | 2 +- .../alex4386/gachon/sw14462/day25/Main.java | 20 +++ .../gachon/sw14462/day25/ex14_1/Main.java | 44 +++++++ .../gachon/sw14462/day25/ex14_1/Pet.java | 117 ++++++++++++++++++ .../sw14462/day25/ex14_11/LinkedQueue.java | 58 +++++++++ .../gachon/sw14462/day25/ex14_11/Main.java | 56 +++++++++ .../gachon/sw14462/day25/ex14_1a/Main.java | 58 +++++++++ .../gachon/sw14462/day25/ex14_1b/Main.java | 7 ++ .../sw14462/day25/ex14_1b/ScoreReader.java | 39 ++++++ .../day25/ex14_1b/ScoreReaderTest.java | 40 ++++++ .../sw14462/day25/ex14_2a/HashMapDemo.java | 35 ++++++ .../sw14462/day25/ex14_2a/HashSetDemo.java | 28 +++++ .../gachon/sw14462/day25/ex14_2a/Main.java | 8 ++ .../gachon/sw14462/day25/ex14_2b/Main.java | 48 +++++++ .../gachon/sw14462/day25/ex14_2c/Main.java | 58 +++++++++ .../gachon/sw14462/day25/ex14_5/Main.java | 26 ++++ 16 files changed, 643 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1/Pet.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_11/LinkedQueue.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_11/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1a/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1b/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1b/ScoreReader.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1b/ScoreReaderTest.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2a/HashMapDemo.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2a/HashSetDemo.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2a/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2b/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2c/Main.java create mode 100644 src/main/java/me/alex4386/gachon/sw14462/day25/ex14_5/Main.java diff --git a/src/main/java/me/alex4386/gachon/sw14462/Main.java b/src/main/java/me/alex4386/gachon/sw14462/Main.java index db42bd0..4bd4993 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 = "day24"; + public static String currentTarget = "day25"; public static boolean fallbackToLatest = true; public static Map> getAvailableTargetClassNames() { diff --git a/src/main/java/me/alex4386/gachon/sw14462/day25/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day25/Main.java new file mode 100644 index 0000000..65e6188 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/Main.java @@ -0,0 +1,20 @@ +package me.alex4386.gachon.sw14462.day25; + +import me.alex4386.gachon.sw14462.utils.Chainloader; + +public class Main { + public static String chainloadTarget = "ex14_11"; + + 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/day25/ex14_1/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1/Main.java new file mode 100644 index 0000000..1223792 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1/Main.java @@ -0,0 +1,44 @@ +package me.alex4386.gachon.sw14462.day25.ex14_1; + +import java.util.ArrayList; +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + ArrayList pets = new ArrayList<>(); + + while (true) { + System.out.print("Enter the name of the pet (type \"exit\" to exit): "); + String name = scanner.nextLine(); + if (name.equals("exit")) break; + + System.out.print("Enter the age of the pet: "); + int age = scanner.nextInt(); + scanner.nextLine(); + + System.out.print("Enter the weight of the pet: "); + double weight = scanner.nextDouble(); + + Pet pet = new Pet(name, age, weight); + pets.add(pet); + + scanner.nextLine(); + } + + // sort by its name. + for (int i = 0; i < pets.size(); i++) { + for (int j = i + 1; j < pets.size(); j++) { + if (pets.get(i).getName().compareTo(pets.get(j).getName()) > 0) { + Pet temp = pets.get(i); + pets.set(i, pets.get(j)); + pets.set(j, temp); + } + } + } + + for (Pet pet : pets) { + pet.writeOutput(); + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1/Pet.java b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1/Pet.java new file mode 100644 index 0000000..1a0fbda --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1/Pet.java @@ -0,0 +1,117 @@ +package me.alex4386.gachon.sw14462.day25.ex14_1; + +/** + Class for basic pet data: name, age, and weight. */ +public class Pet { + private String name; + private int age; // in years + private double weight; // in pounds + + public Pet() // Default constructor + { + name = "No name yet."; + age = 0; + weight = 0; + } + + public Pet(String initialName, int initialAge, + double initialWeight) { + name = initialName; + if ((initialAge < 0) || (initialWeight < 0)) { + System.out.println("Error: Negative age or weight."); + System.exit(0); + } else { + age = initialAge; + weight = initialWeight; + } + } + + public void setPet(String newName, int newAge, + double newWeight) { + name = newName; + if ((newAge < 0) || (newWeight < 0)) { + System.out.println("Error: Negative age or weight."); + System.exit(0); + } else { + age = newAge; + weight = newWeight; + } + } + + public Pet(String initialName) { + name = initialName; + age = 0; + weight = 0; + } + + public void setName(String newName) { + name = newName; //age and weight are unchanged. + } + + + + + public Pet(int initialAge) + { + name = "No name yet."; + weight = 0; + if (initialAge < 0) + { + System.out.println("Error: Negative age."); + System.exit(0); + } + else + age = initialAge; + } + public void setAge(int newAge) + { + if (newAge < 0) + { + System.out.println("Error: Negative age."); + System.exit(0); + } + else + age = newAge; + //name and weight are unchanged. + } + + public Pet(double initialWeight) + { + name = "No name yet"; + age = 0; + if (initialWeight < 0) + { + System.out.println("Error: Negative weight."); + System.exit(0); + }else weight = initialWeight; + } + + public void setWeight(double newWeight) + { + if (newWeight < 0) + { + System.out.println("Error: Negative weight."); + System.exit(0); + } + else + weight = newWeight; //name and age are unchanged. + } + + public String getName() + { + return name; + } + public int getAge() + { + return age; } + public double getWeight() + { + return weight; + } + public void writeOutput() { + System.out.println("Name: " + name); + System.out.println("Age: " + age + " years"); + System.out.println("Weight: " + weight + " pounds"); + } + +} \ No newline at end of file diff --git a/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_11/LinkedQueue.java b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_11/LinkedQueue.java new file mode 100644 index 0000000..d816584 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_11/LinkedQueue.java @@ -0,0 +1,58 @@ +package me.alex4386.gachon.sw14462.day25.ex14_11; + +import java.util.Iterator; +import java.util.LinkedList; + +public class LinkedQueue extends LinkedList { + + private int front; + private int count; + + public LinkedQueue() { + super(); + front = 0; + count = 0; + } + + public void addToQueue(T item) { + this.add(item); + this.count++; + } + + public T removeFromQueue() { + if (this.count == 0) { + return null; + } + this.count--; + return this.get(this.front++); + } + + public boolean isEmpty() { + return this.count == 0; + } + + public Iterator iterator() { + return new Iterator() { + private int index = front; + private int count = 0; + + @Override + public boolean hasNext() { + return count < LinkedQueue.this.count; + } + + @Override + public T next() { + if (!this.hasNext()) return null; + + count++; + return LinkedQueue.this.get(index++); + } + }; + } + + @Override + public int size() { + return this.count; + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_11/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_11/Main.java new file mode 100644 index 0000000..17f9448 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_11/Main.java @@ -0,0 +1,56 @@ +package me.alex4386.gachon.sw14462.day25.ex14_11; + +import java.util.Iterator; + +public class Main { + public static void main(String[] args) { + LinkedQueue queue = new LinkedQueue<>(); + + for (int i = 0; i < 10; i++) { + System.out.println("Pushing "+i+" to queue"); + queue.addToQueue(i); + + System.out.println("Current Queue Content:"); + printIterator(queue.iterator()); + System.out.println(); + } + + for (int i = 0; i < 5; i++) { + int value = queue.removeFromQueue().intValue(); + System.out.println("Popping "+value+" from queue"); + + System.out.println("Current Queue Content:"); + printIterator(queue.iterator()); + System.out.println(); + } + + for (int i = 0; i < 5; i++) { + System.out.println("Pushing "+i+" to queue"); + queue.addToQueue(i); + + System.out.println("Current Queue Content:"); + printIterator(queue.iterator()); + System.out.println(); + } + + + while (!queue.isEmpty()) { + int value = queue.removeFromQueue().intValue(); + System.out.println("Popping "+value+" from queue"); + + System.out.println("Current Queue Content:"); + printIterator(queue.iterator()); + System.out.println(); + } + } + + public static void printIterator(Iterator iter) { + boolean first = true; + while (iter.hasNext()) { + if (!first) System.out.print(", "); + first = false; + System.out.print(iter.next()); + } + System.out.println(); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1a/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1a/Main.java new file mode 100644 index 0000000..28e1a84 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1a/Main.java @@ -0,0 +1,58 @@ +package me.alex4386.gachon.sw14462.day25.ex14_1a; + +import java.util.ArrayList; + +public class Main { + public static ArrayList arrayToList(String[] array) { + ArrayList list = new ArrayList(); + for (String item : array) { + list.add(item); + } + return list; + } + + public static void removeFromArrayList(ArrayList list, String s) { + for (int i = 0; i < list.size(); i++) { + if (list.get(i).equals(s)) { + list.remove(i); + i--; + } + } + } + + public static void printArray(T[] array) { + boolean first = true; + for (Object item : array) { + if (!first) System.out.print(", "); + first = false; + System.out.print(item); + } + System.out.println(); + } + + public static > void printIterable(T iter) { + boolean first = true; + for (Object item : iter) { + if (!first) System.out.print(", "); + first = false; + System.out.print(item); + } + System.out.println(); + } + + public static void main(String[] args) { + String[] array = {"a", "b", "c", "d", "e"}; + System.out.println("Array: "); + printArray(array); + + + System.out.println("ArrayList: "); + ArrayList list = arrayToList(array); + printIterable(list); + + System.out.println("Removed \"c\" from ArrayList: "); + removeFromArrayList(list, "c"); + printIterable(list); + } + +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1b/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1b/Main.java new file mode 100644 index 0000000..6a01d0c --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1b/Main.java @@ -0,0 +1,7 @@ +package me.alex4386.gachon.sw14462.day25.ex14_1b; + +public class Main { + public static void main(String[] args) { + ScoreReaderTest.main(args); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1b/ScoreReader.java b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1b/ScoreReader.java new file mode 100644 index 0000000..e0e9296 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1b/ScoreReader.java @@ -0,0 +1,39 @@ +package me.alex4386.gachon.sw14462.day25.ex14_1b; + +import java.util.ArrayList; +import java.util.List; + +public class ScoreReader { + + private ArrayList scores = new ArrayList(); + + public ScoreReader() { + } + + public void addScore(int score) { + this.scores.add(score); + } + + public double getAverage() { + double sum = 0; + for (Integer score : this.scores) { + sum += score; + } + + return sum / this.scores.size(); + } + + public List getScoresAboveAverage() { + double average = this.getAverage(); + + List data = new ArrayList(); + for (Integer score : this.scores) { + if (score > average) { + data.add(score); + } + } + + return data; + } +} + diff --git a/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1b/ScoreReaderTest.java b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1b/ScoreReaderTest.java new file mode 100644 index 0000000..b4b48f8 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_1b/ScoreReaderTest.java @@ -0,0 +1,40 @@ +package me.alex4386.gachon.sw14462.day25.ex14_1b; + +import java.util.Iterator; +import java.util.List; +import java.util.Scanner; + +public class ScoreReaderTest { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + ScoreReader scoreReader = new ScoreReader(); + + int i = 0; + while (true) { + System.out.print("Enter the score of student " + (i + 1) + " (Enter -1 to stop) : "); + int score = scanner.nextInt(); + if (score < 0) break; + + scoreReader.addScore(score); + i++; + } + + System.out.println("Average score: " + scoreReader.getAverage()); + List scoresAboveAverage = scoreReader.getScoresAboveAverage(); + System.out.print("Scores above average: "); + printIterable(scoresAboveAverage); + + System.out.println(); + } + + public static > void printIterable(T iter) { + boolean first = true; + for (Object item : iter) { + if (!first) System.out.print(", "); + first = false; + System.out.print(item); + } + System.out.println(); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2a/HashMapDemo.java b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2a/HashMapDemo.java new file mode 100644 index 0000000..71a23c1 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2a/HashMapDemo.java @@ -0,0 +1,35 @@ +package me.alex4386.gachon.sw14462.day25.ex14_2a; + +import java.util.HashMap; + +public class HashMapDemo { + public static void main(String[] args) { + HashMap mountains = new HashMap<>(); + + mountains.put("Everest", 29029); + mountains.put("K2", 28251); + mountains.put("Kangchenjunga", 28169); + mountains.put("Denali", 20335); + + printMap(mountains); + + System.out.println("Denali in the map: " + mountains.containsKey("Denali")); + System.out.println(); + System.out.println("Changing height of Denali"); + mountains.put("Denali", 20320); + printMap(mountains); + + System.out.println("Removing Kangchenjunga"); + mountains.remove("Kangchenjunga"); + printMap(mountains); + } + + public static void printMap(HashMap map) { + System.out.println("Map contains:"); + for (String mountainName : map.keySet()) { + Integer height = map.get(mountainName); + System.out.println(mountainName + " -> " + height.intValue() + "feet."); + } + System.out.println(); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2a/HashSetDemo.java b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2a/HashSetDemo.java new file mode 100644 index 0000000..1222d49 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2a/HashSetDemo.java @@ -0,0 +1,28 @@ +package me.alex4386.gachon.sw14462.day25.ex14_2a; + +import java.util.HashSet; + +public class HashSetDemo { + public static void main(String[] args) { + HashSet intSet = new HashSet<>(); + + intSet.add(2); + intSet.add(7); + intSet.add(7); // This is ignored + // since 7 is alread in the set + intSet.add(3); + printSet(intSet); + intSet.remove(3); + printSet(intSet); + System.out.println("Set contains 2: "+intSet.contains(2)); + System.out.println("Set contains 3: "+intSet.contains(3)); + } + + public static void printSet(HashSet intSet) { + System.out.println("The set contains:"); + for (Object obj : intSet.toArray()) { + Integer num = (Integer) obj; + System.out.println(num.intValue()); + } + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2a/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2a/Main.java new file mode 100644 index 0000000..da25367 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2a/Main.java @@ -0,0 +1,8 @@ +package me.alex4386.gachon.sw14462.day25.ex14_2a; + +public class Main { + public static void main(String[] args) { + HashSetDemo.main(args); + HashMapDemo.main(args); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2b/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2b/Main.java new file mode 100644 index 0000000..e8eba48 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2b/Main.java @@ -0,0 +1,48 @@ +package me.alex4386.gachon.sw14462.day25.ex14_2b; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +public class Main { + public static HashSet arrayToHashSet(T[] array) { + HashSet set = new HashSet<>(); + for (T item : array) { + set.add(item); + } + return set; + } + + public static void removeFromHashSet(HashSet set, T key) { + set.remove(key); + } + + public static > void printIterable(T iter) { + boolean first = true; + for (Object item : iter) { + if (!first) System.out.print(", "); + first = false; + System.out.print(item); + } + System.out.println(); + } + + public static void printSet(Set set) { + printIterable(set); + } + + public static void main(String[] args) { + String[] array = {"a", "b", "c", "d", "e"}; + System.out.println("Array: "); + printIterable(Arrays.asList(array)); + + HashSet set = arrayToHashSet(array); + System.out.println("HashSet: "); + printSet(set); + + System.out.println("HashSet after removing c: "); + removeFromHashSet(set, "c"); + printSet(set); + } +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2c/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2c/Main.java new file mode 100644 index 0000000..b089113 --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_2c/Main.java @@ -0,0 +1,58 @@ +package me.alex4386.gachon.sw14462.day25.ex14_2c; + +import java.util.LinkedList; + +public class Main { + public static LinkedList arrayToLinkedList(String[] array) { + LinkedList list = new LinkedList(); + for (String item : array) { + list.add(item); + } + return list; + } + + public static void removeFromLinkedList(LinkedList list, String s) { + for (int i = 0; i < list.size(); i++) { + if (list.get(i).equals(s)) { + list.remove(i); + i--; + } + } + } + + public static void printArray(T[] array) { + boolean first = true; + for (Object item : array) { + if (!first) System.out.print(", "); + first = false; + System.out.print(item); + } + System.out.println(); + } + + public static > void printIterable(T iter) { + boolean first = true; + for (Object item : iter) { + if (!first) System.out.print(", "); + first = false; + System.out.print(item); + } + System.out.println(); + } + + public static void main(String[] args) { + String[] array = {"a", "b", "c", "d", "e"}; + System.out.println("Array: "); + printArray(array); + + + System.out.println("ArrayList: "); + LinkedList list = arrayToLinkedList(array); + printIterable(list); + + System.out.println("Removed \"c\" from ArrayList: "); + removeFromLinkedList(list, "c"); + printIterable(list); + } + +} diff --git a/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_5/Main.java b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_5/Main.java new file mode 100644 index 0000000..8327e2e --- /dev/null +++ b/src/main/java/me/alex4386/gachon/sw14462/day25/ex14_5/Main.java @@ -0,0 +1,26 @@ +package me.alex4386.gachon.sw14462.day25.ex14_5; + +import java.util.HashMap; +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + HashMap counts = new HashMap<>(); + + while(true) { + int value = scanner.nextInt(); + if (value == -1) break; + + if (counts.containsKey(value)) { + counts.put(value, counts.get(value) + 1); + } else { + counts.put(value, 1); + } + } + + for (Integer key : counts.keySet()) { + System.out.println("The number "+ key + " occurs " + counts.get(key) + " times."); + } + } +}