From bda5abd1fe4cfb918d53f1c290aadb17101e816e Mon Sep 17 00:00:00 2001 From: Matthew Vargas Date: Wed, 1 Oct 2025 16:16:10 -0700 Subject: [PATCH 1/8] Forgot to commit until now --- .vscode/launch.json | 13 +++++++++++++ src/ArrayPractice.java | 20 ++++++++++++++------ src/ListPractice.java | 35 +++++++++++++++++++++++++---------- src/NumberPractice.java | 22 +++++++++++++++------- src/StringPractice.java | 25 +++++++++++++++++++------ 5 files changed, 86 insertions(+), 29 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..9562248 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,13 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "jdk", + "request": "launch", + "name": "Launch Java App" + } + ] +} \ No newline at end of file diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..d17c12e 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,26 @@ + public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[] arrayOf4SizeStrings = {"Home", "Part", "Love", "Mans"}; // Set the value of the array at each index to be a different String // It's OK to do this one-by-one - + arrayOf4SizeStrings[0] = "Love"; + arrayOf4SizeStrings[1] = "Joys"; + arrayOf4SizeStrings[2] = "Freedom"; + arrayOf4SizeStrings[3] = "Labor"; // Get the value of the array at index 2 - + System.out.println(arrayOf4SizeStrings[2]); // Get the length of the array - + System.out.println(arrayOf4SizeStrings.length); // Iterate over the array using a traditional for loop and print out each item - + for (int i = 0; i < arrayOf4SizeStrings.length; i++){ + System.out.println(i); + } // Iterate over the array using a for-each loop and print out each item - + for (String n : arrayOf4SizeStrings){ + System.out.println(n); + } /* * Reminder! * diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..50dfdd6 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,43 @@ +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 theList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + theList.add("Thinga"); + theList.add("Thingb"); + theList.add("Thingc"); // Print the element at index 1 - + System.out.println("\nElement at index 1: "+ theList.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) - + theList.set(1, "Grape"); + System.out.println("After replacing index 1: " + theList); // Insert a new element at index 0 (the length of the list will change) - - // Check whether the list contains a certain string - + theList.add("Thing"); + // Check whether the list contains a certain string' + if (theList.contains("Thing")){ + System.out.println("Thing detected"); + } // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + int increment = 0; + for (String n : theList){ + System.out.println("Id: " + increment + " | " + n); + increment += 1; + } // Sort the list using the Collections library - + Collections.sort(theList); // Iterate over the list using a for-each loop // Print each value on a second line - + for (String options : theList) { + System.out.println(options); + } /* * Usage tip! * diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..3fbaf2b 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,25 +1,33 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + float negative = -1; + System.out.println(negative); // Create an int with a positive value and assign it to a variable - + int positive= 1; + System.out.println(positive); // Use the modulo % operator to find the remainder when the int is divided by 3 - + int modulo = 1 % 3; + System.out.println(modulo); // 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 (1 % 2 == 0) { + System.out.println("Even"); + } + else{ + System.out.println("Odd"); + } // Divide the number by another number using integer division - + double divided = 1.0 / 5; + System.out.println(divided); /* * Reminder! * * When dividing ints, the result is rounded down. * Example: - * 7 / 3 = 2 when performing int division + * 7 / 3 = 2 when performing int division` */ - } } diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..4b85a5c 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,19 +1,32 @@ + +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 this5Chars = "Lovely"; // Find the length of the string - + System.out.println(this5Chars.length()); // Concatenate (add) two strings together and reassign the result + String thing1 = "Wow"; + String thing2 = "zers"; + this5Chars = (thing1 + thing2 + "!"); + System.out.println(this5Chars); // Find the value of the character at index 3 - + char val3 = this5Chars.charAt(3); + System.out.println("Val 3: " + val3); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + if (this5Chars.contains("owz")) { + System.out.println("Contains owz"); + } // Iterate over the characters of the string, printing each one on a separate line - + for (int i = 0; i < this5Chars.length(); i++) { + char character = this5Chars.charAt(i); + System.out.println(character); + } // Create an ArrayList of Strings and assign it to a variable - + ArrayList // Add multiple strings to the List (OK to do one-by-one) // Join all of the strings in the list together into a single string separated by commas From 8786d0b0cf8949451bcd6798b5ec3410ea7dd6cc Mon Sep 17 00:00:00 2001 From: Matthew Vargas Date: Wed, 1 Oct 2025 16:23:54 -0700 Subject: [PATCH 2/8] Updated --- src/StringPractice.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 4b85a5c..b9a37c6 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,5 +1,6 @@ import java.util.ArrayList; +import java.util.List; public class StringPractice { public static void main(String[] args) { @@ -26,14 +27,23 @@ public static void main(String[] args) { System.out.println(character); } // Create an ArrayList of Strings and assign it to a variable - ArrayList + List stringList = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) - + stringList.add("TH"); + stringList.add("LIFE LIVE LOVE"); + stringList.add("Live laugh love actually"); // 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("Joined String! " + String.join(", " + stringList)); // Check whether two strings are equal - + String string1 = "Who"; + String string2 = "Who"; + if (string1 == string2){ + System.out.println("Equal"); + } + else { + System.out.println("Not equal"); + } /* * Reminder! * From 0123d9cde607732474c2f2ea9b0f344c8498dfc3 Mon Sep 17 00:00:00 2001 From: Matthew Vargas Date: Wed, 1 Oct 2025 16:57:48 -0700 Subject: [PATCH 3/8] Thing --- src/MapPractice.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..a709fcb 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,17 +1,21 @@ - +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 userScores = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) - + userScores.put("Alice", 2); + userScores.put("Bob", 1); + userScores.put("Charlie", 3); // Get the value associated with a given key in the Map - + userScores.get("Bob"); // 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) // Check whether the Map contains a given key From 594ef221f61455849411cabf60dbebeac019287d Mon Sep 17 00:00:00 2001 From: Matthew Vargas Date: Wed, 1 Oct 2025 17:17:27 -0700 Subject: [PATCH 4/8] f --- src/MapPractice.java | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index a709fcb..0a02e7a 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -15,15 +15,27 @@ public static void main(String[] args) { // Get the value associated with a given key in the Map userScores.get("Bob"); // Find the size (number of key/value pairs) of the Map - + System.out.println(userScores.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) - + userScores.replace("Bob", 7); // Check whether the Map contains a given key - + if (userScores.containsKey("Bob")){ + System.out.println("Contains key"); + } + else { + System.out.println("Does not contain key"); + } // Check whether the Map contains a given value - + if (userScores.containsValue("7")) { + System.out.println("Does contain 7"); + } + else { + System.out.println("Does not contain 7"); + } // Iterate over the keys of the Map, printing each key - + for (String key : userScores.keySet()) { + System.out.println(key); + } // Iterate over the values of the map, printing each value // Iterate over the entries in the map, printing each key and value From 8a458c605a6abe700cf4c1ad1e6f11eca8b56a51 Mon Sep 17 00:00:00 2001 From: Matthew Vargas Date: Wed, 1 Oct 2025 17:23:14 -0700 Subject: [PATCH 5/8] Done with MapPractice --- src/MapPractice.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 0a02e7a..fc15197 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -37,9 +37,14 @@ public static void main(String[] args) { System.out.println(key); } // Iterate over the values of the map, printing each value - + for (Integer value : userScores.values()) { + System.out.println(value); + } // Iterate over the entries in the map, printing each key and value - + for (String k : userScores.keySet()){ + int defOfPair = userScores.get(k); + System.out.println(k + " " + defOfPair); + } /* * Usage tip! * From 176715d20ab2f8e2f07ed619f6c73770b4cea49d Mon Sep 17 00:00:00 2001 From: Matthew Vargas Date: Thu, 2 Oct 2025 18:41:19 -0700 Subject: [PATCH 6/8] Progress --- src/Person.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..d32c1a7 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 NameOfPerson; + private int AgeOfPerson; // Create a constructor that takes the name and age of the person // and assigns it to the instance variables - + public void PersonConstructor(String name, int age) { + NameOfPerson = name; + AgeOfPerson = age; + } // Create a toString method that gives the name and age of the person - + public String toString() { + return NameOfPerson + ": " + AgeOfPerson; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,11 +34,14 @@ public class Person { * @return The year the person was born */ // (create the instance method here) + public int birthYear(int currentYear){ + return currentYear - AgeOfPerson; + } public static void main(String[] args) { // Create an instance of Person - + // Create another instance of Person with a different name and age and // assign it to a different variable From a964c5517bf0860ea4ff436402790c4ffa18607e Mon Sep 17 00:00:00 2001 From: Matthew Vargas Date: Thu, 2 Oct 2025 19:11:35 -0700 Subject: [PATCH 7/8] Realized my mistake on understanding the person.java instructions, and changed accordingly, pure flow brought me forward from there --- src/Person.java | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Person.java b/src/Person.java index d32c1a7..f887d8a 100644 --- a/src/Person.java +++ b/src/Person.java @@ -4,6 +4,9 @@ */ public class Person { + + public Person(String jeff, int par) { + } // 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 NameOfPerson; @@ -12,8 +15,8 @@ public class Person { // Create a constructor that takes the name and age of the person // and assigns it to the instance variables public void PersonConstructor(String name, int age) { - NameOfPerson = name; - AgeOfPerson = age; + this.NameOfPerson = name; + this.AgeOfPerson = age; } // Create a toString method that gives the name and age of the person @@ -41,22 +44,22 @@ public int birthYear(int currentYear){ public static void main(String[] args) { // Create an instance of Person - + Person person1 = new Person("Jeff", 26); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person person2 = new Person("Chelsea", 36); // Print the first person - + System.out.println(person1); // Print the second person - + System.out.println(person2); // Get the name of the first person and store it in a local variable - + Person storedPerson = person1; // 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 birthdateOfPerson = person1.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(birthdateOfPerson); /** * Terminology! * From e96a8980979b7511ae5fa931da6faa6351dd8c6d Mon Sep 17 00:00:00 2001 From: Matthew Vargas Date: Thu, 2 Oct 2025 19:14:15 -0700 Subject: [PATCH 8/8] Items to refresh on filled out --- toRefresh.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/toRefresh.md b/toRefresh.md index 163dcab..2aecce1 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,6 @@ 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 +- Adding to lists +- Constructors / this.[enter] statements +- Map<> functions \ No newline at end of file