From 91022f904b0ae090dd1b5312e2879d6de039dcb4 Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 12:31:29 -0700 Subject: [PATCH 01/23] initialize variables for NumbersPractice --- src/NumberPractice.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..3bce23f 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,11 +1,11 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + double negValue = -1.0; // Create an int with a positive value and assign it to a variable - + int posValue = 1; // Use the modulo % operator to find the remainder when the int is divided by 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" From 1286f9bcfd344c1696a86ecb9a02485180221b85 Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 12:35:07 -0700 Subject: [PATCH 02/23] if-else complete in NumberPractice --- src/NumberPractice.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index 3bce23f..1013dbd 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -3,14 +3,19 @@ public static void main(String args[]) { // Create a float with a negative value and assign it to a variable double negValue = -1.0; // Create an int with a positive value and assign it to a variable - int posValue = 1; + int posValue = 9; // Use the modulo % operator to find the remainder when the int is divided by 3 - + int remainder = posValue % 3; + System.out.println("Remainder: " + remainder); // 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(posValue % 2 == 0) { + System.out.println("Number is even."); + } else { + System.out.println("Number is odd."); + } // Divide the number by another number using integer division /* From 7284649ed5a9058e4df9a3f0012bb1fe59a8c2f0 Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 12:36:37 -0700 Subject: [PATCH 03/23] finish NumberPractice --- src/NumberPractice.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index 1013dbd..a96c921 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -17,7 +17,8 @@ public static void main(String args[]) { System.out.println("Number is odd."); } // Divide the number by another number using integer division - + int result = posValue / 3; + System.out.println(result); /* * Reminder! * From 1a291e4b9ba704a3cee754602fcac70556d87bb8 Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 12:42:48 -0700 Subject: [PATCH 04/23] import required libraries, initialize empty ArrayList --- src/ListPractice.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..cbccfea 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,11 +1,12 @@ +import java.util.List; +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 - + List strList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + // Print the element at index 1 // Replace the element at index 1 with a new value From 4b15fbeae8848a45abd5c9b28c18326af6dbd0dd Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 12:44:51 -0700 Subject: [PATCH 05/23] add elements, print at index --- src/ListPractice.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index cbccfea..40d6c8f 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -6,9 +6,11 @@ public static void main(String[] args) { // Create an empty ArrayList of Strings and assign it to a variable of type List List strList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + strList.add("John"); + strList.add("Doe"); + strList.add("Happy"); // Print the element at index 1 - + System.out.println(strList.get(1)); // Replace the element at index 1 with a new value // (Do not insert a new value. The length of the list should not change) From abd19fe2bbf8e62107c63315b94a07f19ab1bde9 Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 12:49:42 -0700 Subject: [PATCH 06/23] replace and insert elements in list, traverse list to check --- src/ListPractice.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index 40d6c8f..944e238 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -13,9 +13,12 @@ public static void main(String[] args) { System.out.println(strList.get(1)); // Replace the element at index 1 with a new value // (Do not insert a new value. The length of the list should not change) - + strList.set(1, "Sad"); // Insert a new element at index 0 (the length of the list will change) - + strList.add(0, "Angry"); + for(String s : strList) { + System.out.print(s + " "); + } // Check whether the list contains a certain string // Iterate over the list using a traditional for-loop. From 44d7fd4af694143a82f25ebe1510aea2352bc13c Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 12:55:32 -0700 Subject: [PATCH 07/23] traditional for-loop complete --- src/ListPractice.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index 944e238..80d2a37 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -19,11 +19,17 @@ public static void main(String[] args) { for(String s : strList) { System.out.print(s + " "); } + System.out.println(); // Check whether the list contains a certain string - + if(strList.contains("Sad")) { + System.out.println("True"); + } // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + for(int i = 0; i < strList.size(); i++) { + System.out.println("Index: " + i); + System.out.println(strList.get(i)); + } // Sort the list using the Collections library // Iterate over the list using a for-each loop From fdca5d2f0ba36a22d4123607a4d40232f807789a Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 14:18:08 -0700 Subject: [PATCH 08/23] finish ListPractice --- src/ListPractice.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index 80d2a37..189f0cd 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,5 +1,6 @@ import java.util.List; import java.util.ArrayList; +import java.util.Collections; public class ListPractice { public static void main(String[] args) { @@ -31,10 +32,16 @@ public static void main(String[] args) { System.out.println(strList.get(i)); } // Sort the list using the Collections library - + Collections.sort(strList); // Iterate over the list using a for-each loop + for(String s : strList) { + System.out.print(s + " "); + } + System.out.println(); // Print each value on a second line - + for(String s : strList) { + System.out.println(s); + } /* * Usage tip! * From 59a01611158de9517a605eff28a7325cd20b0c0b Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 14:20:29 -0700 Subject: [PATCH 09/23] initialize string array --- src/ArrayPractice.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..02030bc 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,9 +1,13 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[] array = 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 + array[0] = "a"; + array[1] = "b"; + array[2] = "c"; + array[3] = "d"; // Get the value of the array at index 2 From 1c10ec03befb47a7c8adbfd986f173705820947f Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 14:22:02 -0700 Subject: [PATCH 10/23] print statements to check answer --- src/ArrayPractice.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index 02030bc..62eca98 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -10,9 +10,9 @@ public static void main(String[] args) { array[3] = "d"; // Get the value of the array at index 2 - + System.out.println(array[2]); // Get the length of the array - + System.out.println(array.length); // Iterate over the array using a traditional for loop and print out each item // Iterate over the array using a for-each loop and print out each item From 33631333cda2cd3a47b0ae4c4361f1a7e8337dcf Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 14:24:34 -0700 Subject: [PATCH 11/23] for loop and finish ArrayPractice --- src/ArrayPractice.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index 62eca98..a71bb00 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -14,9 +14,14 @@ public static void main(String[] args) { // Get the length of the array System.out.println(array.length); // Iterate over the array using a traditional for loop and print out each item - + for(int i = 0; i < array.length; i++) { + System.out.print(array[i]); + } + System.out.println(); // Iterate over the array using a for-each loop and print out each item - + for(String str : array) { + System.out.print(str); + } /* * Reminder! * From fc5cfa0410f3ea1ecaaaa5e184ef78235a9e3e5e Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 14:30:39 -0700 Subject: [PATCH 12/23] start StringPractice --- src/StringPractice.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..a451cac 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,13 +1,14 @@ public class StringPractice { public static void main(String[] args) { // Create a string with at least 5 characters and assign it to a variable - + String string = "Joseph"; // Find the length of the string - + System.out.println(string.length()); // Concatenate (add) two strings together and reassign the result - + String result = string + " Jonh"; + System.out.println(result); // Find the value of the character at index 3 - + System.out.println(string.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) // Iterate over the characters of the string, printing each one on a separate line From 2abf54d6775d6a69256e9680637e1a7357701d09 Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 14:38:04 -0700 Subject: [PATCH 13/23] string array list functions --- src/StringPractice.java | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index a451cac..5ae0d1b 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,31 @@ -public class StringPractice { +import java.util.List; +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 string = "Joseph"; // Find the length of the string System.out.println(string.length()); // Concatenate (add) two strings together and reassign the result - String result = string + " Jonh"; - System.out.println(result); + string += " John"; + System.out.println(string); // Find the value of the character at index 3 System.out.println(string.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + System.out.println(string.contains("ohn")); // Iterate over the characters of the string, printing each one on a separate line - + for(char c : string.toCharArray()) { + System.out.println(c); + } // Create an ArrayList of Strings and assign it to a variable - + List list = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) - + list.add("Doe"); + list.add("Flower"); + list.add("Allen"); // 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 - + System.out.println(list.toString()); // Check whether two strings are equal /* From cdcf319b8c49258a0ca5e3ff5b1e20f1b5c77364 Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 14:39:26 -0700 Subject: [PATCH 14/23] finish StringPractice --- src/StringPractice.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 5ae0d1b..adb0f99 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -27,7 +27,7 @@ public static void main(String[] args) { // Use a built-in method to achieve this instead of using a loop System.out.println(list.toString()); // Check whether two strings are equal - + System.out.println(string.equals(list.get(0))); /* * Reminder! * From 143484715f2499e9759335e83e205c57a55a7e7c Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 14:42:05 -0700 Subject: [PATCH 15/23] start SetPractice --- src/SetPractice.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..92e1f44 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,14 +1,18 @@ +import java.util.*; public class SetPractice { public static void main(String[] args) { // Create a HashSet of Strings and assign it to a variable of type Set - + Set hashSet = new HashSet<>(); // Add 3 elements to the set + hashSet.add("John"); + hashSet.add("Doe"); + hashSet.add("Vase"); // (It's OK to do it one-by-one) // Check whether the Set contains a given String - + System.out.println(hashSet.contains("Doe")); // Remove an element from the Set - + // Get the size of the Set // Iterate over the elements of the Set, printing each one on a separate line From bb66b48bed2bca883ef0cc2619632f0f014806ba Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 14:45:06 -0700 Subject: [PATCH 16/23] finish SetPractice --- src/SetPractice.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index 92e1f44..090e931 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -12,11 +12,13 @@ public static void main(String[] args) { // Check whether the Set contains a given String System.out.println(hashSet.contains("Doe")); // Remove an element from the Set - + hashSet.remove("Doe"); // Get the size of the Set - + System.out.println(hashSet.size()); // Iterate over the elements of the Set, printing each one on a separate line - + for(String str : hashSet) { + System.out.println(str); + } /* * Warning! * From 17587511a25328172f85188fadeb2a00a3d439c3 Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 14:49:06 -0700 Subject: [PATCH 17/23] start MapPractice --- src/MapPractice.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..fefd62e 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,15 +1,19 @@ - +import java.util.Map; +import java.util.HashMap; 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 + Map hashMap = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) - + hashMap.put("John", 18); + hashMap.put("Joseph", 40); + hashMap.put("David", 50); // Get the value associated with a given key in the Map - + System.out.println(hashMap.get("John")); // Find the size (number of key/value pairs) of the Map // Replace the value associated with a given key (the size of the Map shoukld not change) From 28191c6ddb9fcb589d9e306761c8bb010ba30486 Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 22:38:59 -0700 Subject: [PATCH 18/23] MapPractice almost complete --- src/MapPractice.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index fefd62e..b096715 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -15,15 +15,16 @@ public static void main(String[] args) { // Get the value associated with a given key in the Map System.out.println(hashMap.get("John")); // Find the size (number of key/value pairs) of the Map - + System.out.println(hashMap.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) - + hashMap.replace("David", 50, 20); + System.out.println(hashMap.size()); // Check whether the Map contains a given key - + System.out.println(hashMap.containsKey("David")); // Check whether the Map contains a given value - + System.out.println(hashMap.containsValue(20)); // Iterate over the keys of the Map, printing each key - + // Iterate over the values of the map, printing each value // Iterate over the entries in the map, printing each key and value From 70f640581e303bd71d696158b83e0f15d0b1f548 Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 22:45:01 -0700 Subject: [PATCH 19/23] finish MapPractice --- src/MapPractice.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index b096715..8ae8f0e 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,5 +1,7 @@ import java.util.Map; +import java.util.Collection; import java.util.HashMap; +import java.util.Set; public class MapPractice { public static void main(String[] args) { @@ -24,11 +26,22 @@ public static void main(String[] args) { // Check whether the Map contains a given value System.out.println(hashMap.containsValue(20)); // Iterate over the keys of the Map, printing each key - + Set keys = hashMap.keySet(); + for(String key : keys) { + System.out.print(key); + } // Iterate over the values of the map, printing each value - + Collection values = hashMap.values(); + for(int value : values) { + System.out.print(value); + } + System.out.println(); // Iterate over the entries in the map, printing each key and value - + for(Map.Entry entry : hashMap.entrySet()) { + String key = entry.getKey(); + int value = entry.getValue(); + System.out.println("Key: " + key + "Value: "+ value); + } /* * Usage tip! * From 6e96abbabb4744ae6ecbe7ad1ea23e746b40c3f7 Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 22:48:46 -0700 Subject: [PATCH 20/23] start object-oriented programming practice --- src/Person.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..b9bccd8 100644 --- a/src/Person.java +++ b/src/Person.java @@ -5,15 +5,22 @@ public class Person { // Declare a public String instance variable for the name of the person + public String name; // Declare a private int instance variable for the age of the person - + private int age; // 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.age = age; + this.name = name; + } // Create a toString method that gives the name and age of the person - + @Override + public String toString() { + return "Name: " + name + " Age: " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. From e2f1d75c71d62de8b7e774018eb7825dce9f9e25 Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 22:54:20 -0700 Subject: [PATCH 21/23] halfway through Person --- src/Person.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Person.java b/src/Person.java index b9bccd8..83b7f22 100644 --- a/src/Person.java +++ b/src/Person.java @@ -19,7 +19,7 @@ public Person(String name, int age){ // Create a toString method that gives the name and age of the person @Override public String toString() { - return "Name: " + name + " Age: " + age; + return "Name: " + name + "\nAge: " + age; } // Implement the below public instance method "birthYear" @@ -35,20 +35,22 @@ public String toString() { * @return The year the person was born */ // (create the instance method here) - + public int birthYear(int currentYear) { + return age - currentYear; + } public static void main(String[] args) { // Create an instance of Person - + Person person = new Person("Allen", 99); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person person2 = new Person("Dude", 23); // Print the first person - + System.out.println(person.toString()); // Print the second person - + System.out.println(person2.toString()); // Get the name of the first person and store it in a local variable - + String firstPersonName = person.name; // 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. From 1973000b06a647e31aa32940494cc7eed2b2deef Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 22:56:55 -0700 Subject: [PATCH 22/23] fix issues and finish the assignment --- src/Person.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Person.java b/src/Person.java index 83b7f22..a438654 100644 --- a/src/Person.java +++ b/src/Person.java @@ -36,7 +36,7 @@ public String toString() { */ // (create the instance method here) public int birthYear(int currentYear) { - return age - currentYear; + return currentYear - age; } public static void main(String[] args) { @@ -54,9 +54,9 @@ public static void main(String[] args) { // 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 birthYear = person.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(birthYear); /** * Terminology! * From e46e890fd290759d0a898b3194a1dec566b9e8ee Mon Sep 17 00:00:00 2001 From: allenres Date: Tue, 30 Sep 2025 23:04:20 -0700 Subject: [PATCH 23/23] add things toRefresh --- toRefresh.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toRefresh.md b/toRefresh.md index 163dcab..131e033 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,4 @@ As you work through this exercise, write down anything that you needed to look up or struggled to remember here. It can be just a word or two (e.g. "joining strings"). You can use this as a guide of what to make extra sure you're refreshed on before exams and interviews. -- \ No newline at end of file +- Maps & OOP \ No newline at end of file