From f0ee40983f79327908ceb955a56661b836bf41ee Mon Sep 17 00:00:00 2001 From: Firas Naber Date: Mon, 14 Jul 2025 10:09:34 -0500 Subject: [PATCH 1/4] hw0b: Completed JavaExercises.java --- hw0b/src/JavaExercises.java | 42 ++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/hw0b/src/JavaExercises.java b/hw0b/src/JavaExercises.java index b0dbaf1..d9ad68a 100644 --- a/hw0b/src/JavaExercises.java +++ b/hw0b/src/JavaExercises.java @@ -5,8 +5,11 @@ public class JavaExercises { /** Returns an array [1, 2, 3, 4, 5, 6] */ public static int[] makeDice() { - // TODO: Fill in this function. - return null; + int[] arr = new int[6]; + for (int i = 0; i < 6; i++) { + arr[i] = i + 1; + } + return arr; } /** Returns the order depending on the customer. @@ -14,15 +17,31 @@ public static int[] makeDice() { * If the customer is Erik, return ["sushi", "pasta", "avocado", "coffee"]. * In any other case, return an empty String[] of size 3. */ public static String[] takeOrder(String customer) { - // TODO: Fill in this function. - return null; + String[] arr; + if (customer.equals("Ergun")) { + arr = new String[] {"beyti", "pizza", "hamburger", "tea"}; + } else if (customer.equals("Erik")) { + arr = new String[] {"sushi", "pasta", "avocado", "coffee"}; + } else { + arr = new String[3]; + } + return arr; } /** Returns the positive difference between the maximum element and minimum element of the given array. * Assumes array is nonempty. */ public static int findMinMax(int[] array) { - // TODO: Fill in this function. - return 0; + if (array.length < 1) throw new IllegalArgumentException("There are no elements in the array!"); + + int min = array[0]; + int max = array[0]; + + for (int i = 1; i < array.length; i++) { + if (array[i] < min) min = array[i]; + if (array[i] > max) max = array[i]; + } + + return Math.abs(max - min); } /** @@ -38,8 +57,15 @@ public static List hailstone(int n) { } private static List hailstoneHelper(int x, List list) { - // TODO: Fill in this function. - return null; + list.add(x); + if (x == 1) { + return list; + } + if (x % 2 == 0) { + return hailstoneHelper(x / 2, list); + } else { + return hailstoneHelper(x * 3 + 1, list); + } } } From 601f135961f786b24e4381d22d859de77315b1b0 Mon Sep 17 00:00:00 2001 From: Firas Naber Date: Mon, 14 Jul 2025 10:16:44 -0500 Subject: [PATCH 2/4] hw0b: Completed ListExercises.java --- hw0b/src/ListExercises.java | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/hw0b/src/ListExercises.java b/hw0b/src/ListExercises.java index 1196f69..d266905 100644 --- a/hw0b/src/ListExercises.java +++ b/hw0b/src/ListExercises.java @@ -1,29 +1,48 @@ +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; public class ListExercises { /** Returns the total sum in a list of integers */ public static int sum(List L) { - // TODO: Fill in this function. - return 0; + int sum = 0; + for (int item : L) { + sum += item; + } + return sum; } /** Returns a list containing the even numbers of the given list */ public static List evens(List L) { - // TODO: Fill in this function. - return null; + List output = new ArrayList<>(); + for (int item : L) { + if (item % 2 == 0) output.add(item); + } + return output; } /** Returns a list containing the common item of the two given lists */ public static List common(List L1, List L2) { - // TODO: Fill in this function. - return null; + List output = new ArrayList<>(); + Set l2Set = new HashSet<>(L2); + for (int item : L1) { + if (l2Set.contains(item)) output.add(item); + } + return output; } /** Returns the number of occurrences of the given character in a list of strings. */ public static int countOccurrencesOfC(List words, char c) { - // TODO: Fill in this function. - return 0; + int count = 0; + for (String word : words) { + char[] wordArr = word.toCharArray(); + for (char character : wordArr) { + if (character == c) count++; + } + } + return count; } } From 92f24e6b13f87a349a75e449f8982abe1798c4b4 Mon Sep 17 00:00:00 2001 From: Firas Naber Date: Mon, 14 Jul 2025 10:18:26 -0500 Subject: [PATCH 3/4] hw0b: Completed MapExercises.java --- hw0b/src/MapExercises.java | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/hw0b/src/MapExercises.java b/hw0b/src/MapExercises.java index f41c3e4..ecf0a16 100644 --- a/hw0b/src/MapExercises.java +++ b/hw0b/src/MapExercises.java @@ -1,3 +1,4 @@ +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -6,21 +7,41 @@ public class MapExercises { * 1, 'b' is 2, 'c' is 3, ..., 'z' is 26. */ public static Map letterToNum() { - // TODO: Fill in this function. - return null; + int counter = 1; + Map map = new HashMap<>(); + + for (int i = 97; i <= 122; i++) { + map.put((char) i, counter); + counter++; + } + return map; } /** Returns a map from the integers in the list to their squares. For example, if the input list * is [1, 3, 6, 7], the returned map goes from 1 to 1, 3 to 9, 6 to 36, and 7 to 49. */ public static Map squares(List nums) { - // TODO: Fill in this function. - return null; + Map map = new HashMap<>(); + + for (int num : nums) { + map.put(num, num * num); + } + + return map; } /** Returns a map of the counts of all words that appear in a list of words. */ public static Map countWords(List words) { - // TODO: Fill in this function. - return null; + Map map = new HashMap<>(); + + for (String word : words) { + if (map.containsKey(word)) { + map.put(word, map.get(word) + 1); + } else { + map.put(word, 1); + } + } + + return map; } } From aac96b22a9193d1607330a048823f1075222abe0 Mon Sep 17 00:00:00 2001 From: Firas Naber Date: Mon, 14 Jul 2025 10:20:11 -0500 Subject: [PATCH 4/4] hw0b: Completed Dessert.java and DessertTest.java --- hw0b/src/Dessert.java | 19 +++++++++++++++++++ hw0b/tests/DessertTest.java | 4 ---- 2 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 hw0b/src/Dessert.java diff --git a/hw0b/src/Dessert.java b/hw0b/src/Dessert.java new file mode 100644 index 0000000..9f56f35 --- /dev/null +++ b/hw0b/src/Dessert.java @@ -0,0 +1,19 @@ +public class Dessert { + int flavor; + int price; + static int numDesserts = 0; + + public Dessert(int flavor, int price) { + this.flavor = flavor; + this.price = price; + numDesserts++; + } + + public void printDessert() { + System.out.println(flavor + " " + price + " " + numDesserts); + } + + public static void main(String[] args) { + System.out.println("I love dessert!"); + } +} diff --git a/hw0b/tests/DessertTest.java b/hw0b/tests/DessertTest.java index 396fb44..840ef4f 100644 --- a/hw0b/tests/DessertTest.java +++ b/hw0b/tests/DessertTest.java @@ -17,11 +17,8 @@ public class DessertTest { @Order(0) @DisplayName("Test Dessert class") public void testDessert() { - // TODO: Uncomment this test when you've created and completed Dessert.java! - // TODO: Delete lines 24 and 60 of this file to uncomment. boolean completed = false; - /* ByteArrayOutputStream outContent = new ByteArrayOutputStream(); System.setOut(new PrintStream(outContent)); @@ -57,7 +54,6 @@ public void testDessert() { .isEqualTo("I love dessert!"); completed = true; - */ // Check that assertions were run if (!completed) {