From 24ad8b8e251294f5191ff1caba76933bbfbfd638 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Tue, 7 Jan 2025 12:35:15 -0800 Subject: [PATCH 01/14] completed array practice --- src/ArrayPractice.java | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..fd5130d 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,37 @@ -public class ArrayPractice { - public static void main(String[] args) { +public class ArrayPractice +{ + public static void main(String[] args) + { // Create an array of Strings of size 4 + String[] newArray = 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 + newArray[1] = "string1"; + newArray[2] = "string2"; + newArray[3] = "string3"; + newArray[4] = "string4"; // Get the value of the array at index 2 + String value = newArray[2]; + // Get the length of the array + int length = newArray.length; + // Iterate over the array using a traditional for loop and print out each item + for(int i=0; i Date: Tue, 7 Jan 2025 12:48:21 -0800 Subject: [PATCH 02/14] completed array practice --- src/ArrayPractice.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index fd5130d..fe764b0 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -32,7 +32,6 @@ public static void main(String[] args) { System.out.println(word); } - /* * Reminder! * From 673770b985c181f91385e00fec60e3afb6da762b Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Tue, 7 Jan 2025 13:00:29 -0800 Subject: [PATCH 03/14] completed array practice --- src/ArrayPractice.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index fe764b0..55279ff 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -32,6 +32,7 @@ public static void main(String[] args) { System.out.println(word); } + /* * Reminder! * From d98395fb252e45b59759eeab32df1fe9f26718b7 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Tue, 7 Jan 2025 21:52:14 -0800 Subject: [PATCH 04/14] completed list practice --- src/ListPractice.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..725f26b 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,56 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + 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 newList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) + newList.add("apple"); + newList.add("banana"); + newList.add("orange"); + // Print the element at index 1 + System.out.println(newList.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) + newList.set(1, "pear"); + // Insert a new element at index 0 (the length of the list will change) + newList.set(0, "strawberry"); + // Check whether the list contains a certain string + boolean check = newList.contains("orange"); + // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for(int i=0; i Date: Tue, 7 Jan 2025 21:57:22 -0800 Subject: [PATCH 05/14] corrected array practice --- src/ArrayPractice.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index 55279ff..00debe8 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -7,10 +7,10 @@ public static void main(String[] args) // Set the value of the array at each index to be a different String // It's OK to do this one-by-one - newArray[1] = "string1"; - newArray[2] = "string2"; - newArray[3] = "string3"; - newArray[4] = "string4"; + newArray[0] = "apple"; + newArray[1] = "banana"; + newArray[2] = "strawberry"; + newArray[3] = "pear"; // Get the value of the array at index 2 @@ -28,9 +28,9 @@ public static void main(String[] args) } // Iterate over the array using a for-each loop and print out each item - for(String word: newArray) + for(String fruit: newArray) { - System.out.println(word); + System.out.println(fruit); } /* From 03618899bf4a9eca05b1fdcedb8cf984c31a9015 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Tue, 7 Jan 2025 22:26:44 -0800 Subject: [PATCH 06/14] completed map practice --- src/MapPractice.java | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..48628a7 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,61 @@ - +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 + Map newMap = new HashMap<>(); + // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + newMap.put("bob", 25); + newMap.put("tom", 30); + newMap.put("jim", 28); + // Get the value associated with a given key in the Map + int value = newMap.get("tom"); + // Find the size (number of key/value pairs) of the Map + int size = newMap.size(); + // Replace the value associated with a given key (the size of the Map shoukld not change) + newMap.put("tom", 22); + // Check whether the Map contains a given key + boolean checkKey = newMap.containsKey("tom"); + // Check whether the Map contains a given value + boolean checkValue = newMap.containsValue(22); + // Iterate over the keys of the Map, printing each key + for(String key: newMap.keySet()) + { + System.out.println(key); + } + // Iterate over the values of the map, printing each value + for(Integer values: newMap.values()) + { + System.out.println(values); + } + // Iterate over the entries in the map, printing each key and value + + for (Map.Entry entry : newMap.entrySet()) + { + + System.out.println("key: " + entry.getKey() + ", value: " + entry.getValue()); + } /* * Usage tip! From 424c5b028d60cae0aa8632c9670a5ba0201a4256 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Tue, 7 Jan 2025 22:42:35 -0800 Subject: [PATCH 07/14] corrected array practice --- src/ArrayPractice.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index 00debe8..8aba92b 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -15,10 +15,12 @@ public static void main(String[] args) // Get the value of the array at index 2 String value = newArray[2]; + System.out.println(value); // Get the length of the array int length = newArray.length; + System.out.println(length); // Iterate over the array using a traditional for loop and print out each item From b7f20f45f2ec7e9770353fe0aa898aed41867bf3 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Tue, 7 Jan 2025 22:43:51 -0800 Subject: [PATCH 08/14] corrected list practice --- src/ListPractice.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index 725f26b..639e50b 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -31,7 +31,8 @@ public static void main(String[] args) { // Check whether the list contains a certain string - boolean check = newList.contains("orange"); + boolean check = newList.contains("orange"); + System.out.println(check); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line From 78533061054cbad79d4aa69bef032261715fbe4b Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Tue, 7 Jan 2025 22:45:41 -0800 Subject: [PATCH 09/14] corrected map practice --- src/MapPractice.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/MapPractice.java b/src/MapPractice.java index 48628a7..111ee06 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -18,10 +18,12 @@ public static void main(String[] args) { // Get the value associated with a given key in the Map int value = newMap.get("tom"); + System.out.println(value); // Find the size (number of key/value pairs) of the Map int size = newMap.size(); + System.out.println(size); // Replace the value associated with a given key (the size of the Map shoukld not change) @@ -30,10 +32,12 @@ public static void main(String[] args) { // Check whether the Map contains a given key boolean checkKey = newMap.containsKey("tom"); + System.out.println(checkKey); // Check whether the Map contains a given value boolean checkValue = newMap.containsValue(22); + System.out.println(checkValue); // Iterate over the keys of the Map, printing each key From 268a31b0e4acae1d82953b848e218b81ae34ef32 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Tue, 7 Jan 2025 22:47:44 -0800 Subject: [PATCH 10/14] completed number practice --- src/NumberPractice.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..018e913 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -2,17 +2,38 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float num1 = -42.3f; + System.out.println(num1); + // Create an int with a positive value and assign it to a variable + int num2 = 7; + System.out.println(num2); + // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = num2 % 3; + System.out.println(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 (num2 % 2 == 0) + { + System.out.println("Even"); + } + else{ + System.out.println("Odd"); + } + // Divide the number by another number using integer division + int sum = num2 / 3; + System.out.println(sum); + + /* * Reminder! * From eb6cbb4df861257849d5ee0711e4927250b64d43 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Tue, 7 Jan 2025 23:08:37 -0800 Subject: [PATCH 11/14] completed person class --- src/Person.java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..2f37287 100644 --- a/src/Person.java +++ b/src/Person.java @@ -7,13 +7,28 @@ 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; + 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.name = name; + this.age = age; + } + // Create a toString method that gives the name and age of the person + 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. @@ -29,24 +44,44 @@ public class Person { */ // (create the instance method here) + public int birthYear(int currentYear) + { + return currentYear-age; + } + public static void main(String[] args) { // Create an instance of Person + Person person1 = new Person("Bob", 25); + // Create another instance of Person with a different name and age and // assign it to a different variable + Person person2 = new Person("Jim", 20); + // Print the first person + System.out.println(person1.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 name = person1.name; + System.out.println(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. + int bYear = person1.birthYear(2025); + + // In a separate statement, print the local variable holding the birth year. + System.out.println("birth year: " + bYear); /** * Terminology! From e23604ce886949fb8051aff3926615f429323bfc Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Tue, 7 Jan 2025 23:29:40 -0800 Subject: [PATCH 12/14] completed string practice --- src/StringPractice.java | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..37bdd68 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,65 @@ +import java.util.ArrayList; +import java.util.List; + public class StringPractice { public static void main(String[] args) { // Create a string with at least 5 characters and assign it to a variable + String name = "Matthew"; + System.out.println(name); + // Find the length of the string + int length = name.length(); + System.out.println("length: " + length); + + // Concatenate (add) two strings together and reassign the result + String word1 = "hello"; + String word2 = "world"; + String word3 = word1 + word2; + System.out.println(word3); + // Find the value of the character at index 3 + char value = word3.charAt(3); + System.out.println("character at index 3: " + value); + + // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + boolean contains = word3.contains("low"); + System.out.println(contains); + // Iterate over the characters of the string, printing each one on a separate line + for (int i = 0; i < word3.length(); i++) + { + System.out.println(word3.charAt(i)); + } + // Create an ArrayList of Strings and assign it to a variable + List newList = new ArrayList<>(); + // Add multiple strings to the List (OK to do one-by-one) + newList.add("apple"); + newList.add("banana"); + newList.add("strawberry"); + newList.add("orange"); // 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 + String joined = String.join(", ", newList); + System.out.println(joined); + // Check whether two strings are equal + boolean test = word1.equals(word2); + System.out.println(test); + /* * Reminder! * From f9ce4dc79d6b716a56c39eeb53a301e300eb7132 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Tue, 7 Jan 2025 23:35:32 -0800 Subject: [PATCH 13/14] completed set practice --- src/SetPractice.java | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..64fbb5f 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,43 @@ -public class SetPractice { - public static void main(String[] args) { +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 + Set newSet = new HashSet<>(); + // Add 3 elements to the set // (It's OK to do it one-by-one) + newSet.add("apple"); + newSet.add("banana"); + newSet.add("orange"); + // Check whether the Set contains a given String + boolean check = newSet.contains("apple"); + System.out.println(check); + // Remove an element from the Set + newSet.remove("orange"); + + // Get the size of the Set + int size = newSet.size(); + System.out.println("Size: " + size); + // Iterate over the elements of the Set, printing each one on a separate line + for(String fruit: newSet) + { + System.out.println(fruit); + } + /* * Warning! * From 23e2f717e806bd787145cece2f601ea18710ebd3 Mon Sep 17 00:00:00 2001 From: mlarsen <174657206+mlarsen-source@users.noreply.github.com> Date: Tue, 7 Jan 2025 23:38:52 -0800 Subject: [PATCH 14/14] added items to refresh on --- toRefresh.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/toRefresh.md b/toRefresh.md index 163dcab..10bec6e 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,7 @@ 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 +- entrySet() +- constructor syntax +- String.join() +- import statement syntax \ No newline at end of file