From 9afe4a0f92b3e5d71a4630e5229b5ab4f3cc059e Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Mon, 29 Sep 2025 17:16:02 -0700 Subject: [PATCH 1/9] Completed NumberPractice problems --- src/NumberPractice.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..0399645 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,24 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + float negativeFloat = -2.0f; // Create an int with a positive value and assign it to a variable - + int postiveValue = 4; // Use the modulo % operator to find the remainder when the int is divided by 3 - + System.out.println(postiveValue%3); // Use the modulo % operator to determine whether the number is even // (A number is even if it has a remainder of zero when divided by 2) // Use an if-else to print "Even" if the number is even and "Odd" // if the number is odd. + + if(postiveValue % 2 == 0){ + System.out.println("even"); + } else { + System.out.println("odd"); + } // Divide the number by another number using integer division - + System.out.println(postiveValue/3); /* * Reminder! * From a90b4070ac2609cb42a68733826ea8f1ccfffdf1 Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Mon, 29 Sep 2025 19:56:37 -0700 Subject: [PATCH 2/9] Completed half of ListPractice --- src/ListPractice.java | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..c9aecf5 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,23 +1,29 @@ +import java.util.ArrayList; + public class ListPractice { public static void main(String[] args) { // Create an empty ArrayList of Strings and assign it to a variable of type List - + ArrayList list = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + list.add("Cake"); + list.add("Ice Cream"); + list.add("Macaron"); // Print the element at index 1 - + System.out.println(list.get(0)); // Replace the element at index 1 with a new value // (Do not insert a new value. The length of the list should not change) - + list.set(1,"Bingsu"); + System.out.println(list.get(1)+ " " + list.size()); // Insert a new element at index 0 (the length of the list will change) - + list.add(0,"Cookies"); + System.out.println(list+ " " + list.size()); // Check whether the list contains a certain string - + System.out.println(list.contains("Cake") ); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + // Sort the list using the Collections library // Iterate over the list using a for-each loop From ae0fbbdb70bdae593ba4701d824fe0317b168f33 Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Mon, 29 Sep 2025 20:02:40 -0700 Subject: [PATCH 3/9] Completed ListPractice work problems --- src/ListPractice.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index c9aecf5..76bd6ed 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,4 +1,5 @@ import java.util.ArrayList; +import java.util.Collections; public class ListPractice { @@ -21,14 +22,20 @@ public static void main(String[] args) { System.out.println(list+ " " + list.size()); // Check whether the list contains a certain string System.out.println(list.contains("Cake") ); + System.out.println(); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + for(int i = 0; i < list.size(); i++){ + System.out.println(list.get(i)); + } // Sort the list using the Collections library - + Collections.sort(list); + System.out.println(); // Iterate over the list using a for-each loop // Print each value on a second line - + for(String dessert : list){ + System.out.println(dessert); + } /* * Usage tip! * From b71598101affaa07f066302e9aeef591335b3e53 Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Wed, 1 Oct 2025 12:51:33 -0700 Subject: [PATCH 4/9] Completed the problems for ArrayPractice --- src/ArrayPractice.java | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..9e4f7fa 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,29 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String arr[] = new String[4]; // Set the value of the array at each index to be a different String // It's OK to do this one-by-one + arr[0] = "cake"; + arr[1] = "cookies"; + arr[2] = "pancake"; + arr[3] = "cupcake"; // Get the value of the array at index 2 - + System.out.println(arr[2]); + System.out.println(); // Get the length of the array - + System.out.println(arr.length); + System.out.println(); // Iterate over the array using a traditional for loop and print out each item - + for(int i = 0; i < arr.length; i++){ + System.out.println(arr[i]); + } + System.out.println(); // Iterate over the array using a for-each loop and print out each item - + for(String dessert: arr){ + System.out.println(dessert); + } /* * Reminder! * From bedcf73481087e6d9874416a4f183ece7b88c08d Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Wed, 1 Oct 2025 13:23:01 -0700 Subject: [PATCH 5/9] Completed String Practice work problems --- src/StringPractice.java | 55 +++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..20ded75 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,61 @@ +import java.util.Arrays; +import java.util.ArrayList; public class StringPractice { public static void main(String[] args) { // Create a string with at least 5 characters and assign it to a variable - + String word = "Chariot"; // Find the length of the string - + System.out.println(word.length()); + System.out.println(); // Concatenate (add) two strings together and reassign the result - + String first = "Number"; + String second = "Two"; + String firstSecond = first+second; + System.out.println(firstSecond); + System.out.println(); // Find the value of the character at index 3 - + System.out.println(word.charAt(3)); + System.out.println(); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + String Alphabet = "abc"; + String noABC = "s"; + if (Alphabet.contains("abc")) { + System.out.println("Alphabet contains \"abc\""); + } + if (noABC.contains("abc")) { + System.out.println("Alphabet contains \"abc\""); + } else{ + System.out.println("noABC does not contain \"abc\""); + } + System.out.println(); // Iterate over the characters of the string, printing each one on a separate line - - // Create an ArrayList of Strings and assign it to a variable - + String dessert = "macaron"; + for (int i = 0; i < dessert.length(); i++) { + System.out.println(dessert.charAt(i)); + } + System.out.println(); + // Create an ArrayList of Strings and assign it to a variable + // ArrayList recipe = new ArrayList(Arrays.asList("cheese","sugar","flour","egg whites")); + ArrayList recipe = new ArrayList(); + System.out.println(); // Add multiple strings to the List (OK to do one-by-one) - + recipe.add("cheese"); + recipe.add("sugar"); + recipe.add("flour"); + recipe.add("egg whites"); + System.out.println(); // Join all of the strings in the list together into a single string separated by commas // Use a built-in method to achieve this instead of using a loop - + ArrayList recipeMixed = new ArrayList(Arrays.asList("cheese","sugar","flour","egg whites")); + System.out.println(recipeMixed); + System.out.println(); // Check whether two strings are equal - + if(recipeMixed.get(0).equals(recipeMixed.get(1))){ + System.out.println("Same ingredient"); + }else{ + System.out.println("Not the same"); + } /* * Reminder! * From 652886c3df4385e3ed436faf6843a7327e1f7fc4 Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Wed, 1 Oct 2025 13:31:11 -0700 Subject: [PATCH 6/9] Completed SetPractice work problems --- src/SetPractice.java | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..24289b1 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,30 @@ +import java.util.HashSet; + public class SetPractice { public static void main(String[] args) { // Create a HashSet of Strings and assign it to a variable of type Set - + HashSet hashbrown = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) - + hashbrown.add("potatoes"); + hashbrown.add("flour"); + hashbrown.add("egg"); + System.out.println(); // Check whether the Set contains a given String - + System.out.println(hashbrown.contains("potatoes")); + System.out.println(); // Remove an element from the Set - + hashbrown.remove("flour"); + System.out.println(hashbrown); + System.out.println(); // Get the size of the Set - + System.out.println(hashbrown.size()); + System.out.println(); // Iterate over the elements of the Set, printing each one on a separate line - + for(String Ingredient : hashbrown){ + System.out.println(Ingredient); + } + System.out.println(); /* * Warning! * From 66fb2c65aa43d0ef0e74be45bb501ef2f26fe414 Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Wed, 1 Oct 2025 13:33:14 -0700 Subject: [PATCH 7/9] Slight adjustment to the first question --- src/SetPractice.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index 24289b1..5c4d3bc 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,9 +1,9 @@ import java.util.HashSet; - +import java.util.Set; public class SetPractice { public static void main(String[] args) { // Create a HashSet of Strings and assign it to a variable of type Set - HashSet hashbrown = new HashSet<>(); + Set hashbrown = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) hashbrown.add("potatoes"); From ad61f49b2949d7ea7155ad5cfeec756c88e732ec Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Wed, 1 Oct 2025 13:44:49 -0700 Subject: [PATCH 8/9] Completed the work problems for MapPractice --- src/MapPractice.java | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..9bff7c1 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,48 @@ - +import java.util.HashMap; +import java.util.Map; public class MapPractice { public static void main(String[] args) { // Create a HashMap with String keys and Integer values and // assign it to a variable of type Map - + HashMap foods = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) - + foods.put("hot dog", 1); + foods.put("sushi", 2); + foods.put("pizza", 3); + foods.put("cake", 4); + System.out.println(); // Get the value associated with a given key in the Map - + System.out.println(foods.get("sushi")); + System.out.println(); // Find the size (number of key/value pairs) of the Map - + System.out.println(foods.size()); + System.out.println(); // Replace the value associated with a given key (the size of the Map shoukld not change) - + foods.put("hot dog", 2); + System.out.println(foods); + System.out.println(); // Check whether the Map contains a given key - + System.out.println(foods.containsKey("sushi")); + System.out.println(); // Check whether the Map contains a given value - + System.out.println(foods.containsValue(4)); + System.out.println(); // Iterate over the keys of the Map, printing each key - + for(String food : foods.keySet()){ + System.out.println(food); + } + System.out.println(); // Iterate over the values of the map, printing each value - + for(Integer food : foods.values()){ + System.out.println(food); + } + System.out.println(); // Iterate over the entries in the map, printing each key and value - + for(Map.Entry foodInventory : foods.entrySet()){ + System.out.println(foodInventory); + } /* * Usage tip! * From ea05028abb7288efb67bec16c0634273f298c074 Mon Sep 17 00:00:00 2001 From: Akai <161771111+Akaiokaa@users.noreply.github.com> Date: Wed, 1 Oct 2025 14:04:49 -0700 Subject: [PATCH 9/9] Completed Person work problems --- src/Person.java | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..61cd546 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,14 +6,20 @@ public class Person { // Declare a public String instance variable for the name of the person // Declare a private int instance variable for the age of the person - - + public String name = "Jerry"; + private int age = 1000; // Create a constructor that takes the name and age of the person // and assigns it to the instance variables - + public Person(String name, int age){ + this.name = name; + this.age = age; + } // Create a toString method that gives the name and age of the person - + @Override + public String toString(){ + return name + " " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,26 +34,32 @@ public class Person { * @return The year the person was born */ // (create the instance method here) - + public static int birthYear(int age){ + return 2025 - age; + } public static void main(String[] args) { // Create an instance of Person - + Person human = new Person("Timmy", 20); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person human2 = new Person("Joe", 30); // Print the first person - + System.out.println(human.toString()); + System.out.println(); // Print the second person - + System.out.println(human2.toString()); + System.out.println(); // Get the name of the first person and store it in a local variable - + String firstPersonsName = human.name; + System.out.println(firstPersonsName); + System.out.println(); // Using the birthYear method, get the birth year of the first person // and store it in a local variable. Input the actual current year (e.g. 2025) // as the argument. - + int firstPersonsBirthYear = birthYear(human.age); // In a separate statement, print the local variable holding the birth year. - + System.out.println(firstPersonsBirthYear); /** * Terminology! *