From 0c6178db6874f8fae6b6a4b7fd861da42c7f0d88 Mon Sep 17 00:00:00 2001 From: mmaslov007 Date: Thu, 2 Jan 2025 19:39:18 -0800 Subject: [PATCH 01/10] Completed NumberPractice.java --- src/NumberPractice.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..d21a697 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,29 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float f = -3.6f; + System.out.println(f); // Create an int with a positive value and assign it to a variable + int i = 4; + System.out.println(i); // Use the modulo % operator to find the remainder when the int is divided by 3 + System.out.println(i % 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 (i % 2 == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + } // Divide the number by another number using integer division + System.out.println(i / 4); /* * Reminder! * From 5d173ec1dceb83493bf0d706412f1cb0c686fee1 Mon Sep 17 00:00:00 2001 From: Maxwell Maslov Date: Thu, 2 Jan 2025 20:25:13 -0800 Subject: [PATCH 02/10] Completed ListPractice. --- src/ListPractice.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..3a1989e 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,51 @@ +import java.util.ArrayList; +import java.util.Collections; + 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 stringList = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) + stringList.add("Hello"); + stringList.add("Hi"); + stringList.add("Greetings"); + System.out.println(stringList + "\n"); // Print the element at index 1 + System.out.println(stringList.get(1) + "\n"); // Replace the element at index 1 with a new value // (Do not insert a new value. The length of the list should not change) + stringList.set(1, "Salutations"); + System.out.println(stringList + "\n"); // Insert a new element at index 0 (the length of the list will change) + stringList.add(0, "Blessings"); + System.out.println(stringList + "\n"); // Check whether the list contains a certain string + if (stringList.contains("Greetings")) { + System.out.println("I greet thee as well." + "\n"); + } // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for(int i = 0; i < stringList.size(); i++) { + System.out.println(stringList.get(i) + "\n"); + } // Sort the list using the Collections library + Collections.sort(stringList); + System.out.println("List is sorted." + "\n"); // Iterate over the list using a for-each loop // Print each value on a second line + for(String word : stringList) { + System.out.println(word + "\n"); + } /* * Usage tip! From 78d863142cbd55070016f1ff5b30b1b9e6044bc2 Mon Sep 17 00:00:00 2001 From: Maxwell Maslov Date: Thu, 2 Jan 2025 20:43:18 -0800 Subject: [PATCH 03/10] Completed ArrayPractice. --- src/ArrayPractice.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..36fb1c4 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,31 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] strings = 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 + strings[0] = "Freedom"; + strings[1] = "Duty"; + strings[2] = "Loner"; + strings[3] = "Monolith"; // Get the value of the array at index 2 + System.out.println(strings[2] + "\n"); // Get the length of the array + System.out.println(strings.length + "\n"); // Iterate over the array using a traditional for loop and print out each item + for (int i = 0; i < strings.length; i++) { + System.out.println(strings[i]); + } + System.out.println("\n"); // Iterate over the array using a for-each loop and print out each item + for (String string : strings) { + System.out.println(string); + } /* * Reminder! From 4b91b75f7cbdf2a8b86a310de049ddc6a7a3a1ab Mon Sep 17 00:00:00 2001 From: Maxwell Maslov Date: Thu, 2 Jan 2025 21:09:51 -0800 Subject: [PATCH 04/10] Completed StringPractice. --- src/StringPractice.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..f3cf038 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,50 @@ +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 = "Monolith"; + System.out.println(string + "\n"); // Find the length of the string + System.out.println(string.length() + "\n"); // Concatenate (add) two strings together and reassign the result + String add = " Emissary"; + string = string + add; + System.out.println(string + "\n"); // Find the value of the character at index 3 + System.out.println(string.charAt(3) + "\n"); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + System.out.println(string.contains("Mon") + "\n"); // Iterate over the characters of the string, printing each one on a separate line + for (int i = 0; i < string.length(); i++) { + System.out.println(string.charAt(i)); + } + System.out.println("\n"); // Create an ArrayList of Strings and assign it to a variable + ArrayList stringList = new ArrayList(); // Add multiple strings to the List (OK to do one-by-one) + stringList.add("Monolith"); + stringList.add("Freedom"); + stringList.add("Fighter"); + System.out.println(stringList + "\n"); // 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(String.join(", ", stringList) + "\n"); // Check whether two strings are equal + if (string.equals(add)) { + System.out.println("The strings are equal."); + } else { + System.out.println("The strings are not equal."); + } /* * Reminder! From 4c3c2a213c512c07bcdf3834ba35a804ca459c3f Mon Sep 17 00:00:00 2001 From: Maxwell Maslov Date: Thu, 2 Jan 2025 21:15:18 -0800 Subject: [PATCH 05/10] Added to refresh section. --- toRefresh.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/toRefresh.md b/toRefresh.md index 163dcab..b7c6f24 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 +- floating integers +- instantiating ArrayLists +- for each loops +- joining strings \ No newline at end of file From 36a80cabe862ad06216c069f2956ac0e079d7902 Mon Sep 17 00:00:00 2001 From: Maxwell Maslov Date: Thu, 2 Jan 2025 21:23:37 -0800 Subject: [PATCH 06/10] Completed SetPractice. --- src/SetPractice.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..71c0b81 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,31 @@ +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 hashSet = new HashSet(); // Add 3 elements to the set // (It's OK to do it one-by-one) + hashSet.add("Monolith"); + hashSet.add("Freedom"); + hashSet.add("Duty"); + System.out.println(hashSet + "\n"); // Check whether the Set contains a given String + System.out.println(hashSet.contains("Freedom") + "\n"); // Remove an element from the Set + hashSet.remove("Freedom"); // Get the size of the Set + System.out.println(hashSet.size() + "\n"); // Iterate over the elements of the Set, printing each one on a separate line + for (String string : hashSet) { + System.out.println(string); + } /* * Warning! From 68589b4a4dca4bcd901953fa83fb3548e042792e Mon Sep 17 00:00:00 2001 From: Maxwell Maslov Date: Thu, 2 Jan 2025 21:43:48 -0800 Subject: [PATCH 07/10] Completed MapPractice. --- src/MapPractice.java | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..ec12b14 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,51 @@ - +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("Monolith", 1); + hashMap.put("Freedom", 2); + hashMap.put("Duty", 3); + System.out.println(hashMap + "\n"); + // Get the value associated with a given key in the Map + System.out.println(hashMap.get("Monolith") + "\n"); // Find the size (number of key/value pairs) of the Map + System.out.println(hashMap.size() + "\n"); - // Replace the value associated with a given key (the size of the Map shoukld not change) + // Replace the value associated with a given key (the size of the Map should not change) + hashMap.put("Monolith", 4); + System.out.println(hashMap + "\n"); // Check whether the Map contains a given key + System.out.println(hashMap.containsKey("Freedom") + "\n"); // Check whether the Map contains a given value + System.out.println(hashMap.containsValue(4) + "\n"); // Iterate over the keys of the Map, printing each key + for (String key : hashMap.keySet()) { + System.out.println(key); + } + System.out.println("\n"); // Iterate over the values of the map, printing each value + for (Integer value : hashMap.values()) { + System.out.println(value); + } + System.out.println("\n"); // Iterate over the entries in the map, printing each key and value + for (String key : hashMap.keySet()) { + System.out.println(key + ", " + hashMap.get(key)); + } /* * Usage tip! From b57b74fd805749bf4d13e919db8125c002899329 Mon Sep 17 00:00:00 2001 From: Maxwell Maslov Date: Thu, 2 Jan 2025 21:51:28 -0800 Subject: [PATCH 08/10] Updated toRefresh --- toRefresh.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/toRefresh.md b/toRefresh.md index b7c6f24..f2a423e 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -5,4 +5,6 @@ As you work through this exercise, write down anything that you needed to look u - floating integers - instantiating ArrayLists - for each loops -- joining strings \ No newline at end of file +- joining strings +- printing key along with value in a single loop +- the entire Person.java class \ No newline at end of file From 1ae97a91ef4958441b48f37faf628dd7823b24bd Mon Sep 17 00:00:00 2001 From: Maxwell Maslov Date: Thu, 2 Jan 2025 22:08:55 -0800 Subject: [PATCH 09/10] Completed Person.java --- src/Person.java | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..853b81c 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,14 +6,21 @@ 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 - + @Override + public String toString() { + return "This person is called " + name + ", they are " + age + " years old."; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -27,26 +34,35 @@ public class Person { * @param currentYear an int for the current year * @return The year the person was born */ - // (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 John = new Person("John", 22); // Create another instance of Person with a different name and age and // assign it to a different variable + Person Smith = new Person("Smith", 53); // Print the first person + System.out.println(John.toString()); // Print the second person + System.out.println(Smith.toString()); // Get the name of the first person and store it in a local variable + String firstName = John.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 firstBirth = John.birthYear(2025); // In a separate statement, print the local variable holding the birth year. + System.out.println(firstName + " was born in " + firstBirth + "."); /** * Terminology! From 17f6603fcb313b2ae64658c7eb5721efe2e2afe3 Mon Sep 17 00:00:00 2001 From: mmaslov007 Date: Sat, 4 Jan 2025 19:16:51 -0800 Subject: [PATCH 10/10] Updated list interfaces --- src/ListPractice.java | 3 ++- src/StringPractice.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index 3a1989e..12cc3a8 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,12 +1,13 @@ 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 - ArrayList stringList = new ArrayList(); + List stringList = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) stringList.add("Hello"); diff --git a/src/StringPractice.java b/src/StringPractice.java index f3cf038..fa9d963 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,4 +1,5 @@ import java.util.ArrayList; +import java.util.List; public class StringPractice { public static void main(String[] args) { @@ -27,7 +28,7 @@ public static void main(String[] args) { System.out.println("\n"); // Create an ArrayList of Strings and assign it to a variable - ArrayList stringList = new ArrayList(); + List stringList = new ArrayList(); // Add multiple strings to the List (OK to do one-by-one) stringList.add("Monolith");