From e4c47340843c769c9d962d4cc7b4b5c7a1382385 Mon Sep 17 00:00:00 2001 From: Tavparsad Singh Date: Mon, 6 Jan 2025 15:04:11 -0800 Subject: [PATCH 01/11] Completing array practice --- src/ArrayPractice.java | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..866e043 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,28 @@ 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[0] = "Hello"; + newArray[1] = "My"; + newArray[2] = "Name's"; + newArray[3] = "Tav"; // Get the value of the array at index 2 - + System.out.println(newArray[2]); // Get the length of the array - + System.out.println("The length of the array is: " + newArray.length); // Iterate over the array using a traditional for loop and print out each item - + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + } // Iterate over the array using a for-each loop and print out each item - + // Breaking to show for each + System.out.println(); + for (String words:newArray) { + System.out.println(words); + } /* * Reminder! * From e73429d1db5efe3b91b82e7fd29462073f64826a Mon Sep 17 00:00:00 2001 From: Tavparsad Singh Date: Mon, 6 Jan 2025 15:31:05 -0800 Subject: [PATCH 02/11] Finished ListPractice, I noticed there was some issues with git until I restarted. --- src/ListPractice.java | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..3d9a440 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,42 @@ +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 stringList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + stringList.add("Pulp"); + stringList.add("Round"); + stringList.add("Strange"); // Print the element at index 1 + System.out.println(stringList.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) - + stringList.set(1, "Juice"); + System.out.println(stringList.get(1)); // Insert a new element at index 0 (the length of the list will change) - + stringList.add(0, "Oh"); + System.out.println(stringList.get(0)); // Check whether the list contains a certain string - + System.out.println(stringList.contains("Juice")); // 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("Index: " + i + ", Value: " +stringList.get(i)); + } // Sort the list using the Collections library - + Collections.sort(stringList); + System.out.println(stringList); // Iterate over the list using a for-each loop // Print each value on a second line - + for (String word : stringList) { + System.out.println(word); + } /* * Usage tip! * From f7c9f7895649e149d15c90a2b6136811481c696a Mon Sep 17 00:00:00 2001 From: Tavparsad Singh Date: Mon, 6 Jan 2025 15:40:37 -0800 Subject: [PATCH 03/11] Added things I need to refresh on and working on maps --- src/MapPractice.java | 14 ++++++++++---- toRefresh.md | 2 +- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..93c6d4a 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,18 +1,24 @@ +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("For", 1); + newMap.put("Gas", 2); + newMap.put("Port", 3); - // Get the value associated with a given key in the Map + // Get the value associated with a given key in the Map + System.out.println(newMap.get("Port")); // 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) + System.out.println(newMap.size()); + // Replace the value associated with a given key (the size of the Map should not change) // Check whether the Map contains a given key diff --git a/toRefresh.md b/toRefresh.md index 163dcab..28ac268 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 +- List or methods used in general \ No newline at end of file From 6214f5c591e82e4655c3d902c20194c0bcc203c6 Mon Sep 17 00:00:00 2001 From: Tavparsad Singh Date: Mon, 6 Jan 2025 15:46:56 -0800 Subject: [PATCH 04/11] Finished printing values and printing keys --- src/MapPractice.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 93c6d4a..0e54a00 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -19,13 +19,16 @@ public static void main(String[] args) { // Find the size (number of key/value pairs) of the Map System.out.println(newMap.size()); // Replace the value associated with a given key (the size of the Map should not change) - + newMap.replace("For", 9); // Check whether the Map contains a given key - + System.out.println(newMap.containsKey("Port")); // Check whether the Map contains a given value - + System.out.println(newMap.containsValue(4)); // Iterate over the keys of the Map, printing each key - + System.out.println("Keys:"); + for (String keys : newMap.keySet()) { + System.out.println(keys); + } // Iterate over the values of the map, printing each value // Iterate over the entries in the map, printing each key and value From cad9d78f56492bbe7cc8f11ac45c064d11393704 Mon Sep 17 00:00:00 2001 From: Tavparsad Singh Date: Mon, 6 Jan 2025 15:50:52 -0800 Subject: [PATCH 05/11] Finished the rest of the Map practice --- src/MapPractice.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 0e54a00..fd31b5b 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -30,9 +30,15 @@ public static void main(String[] args) { System.out.println(keys); } // Iterate over the values of the map, printing each value - + System.out.println("Values:"); + for (Integer values : newMap.values()) { + System.out.println(values); + } // Iterate over the entries in the map, printing each key and value - + System.out.println("Both Keys and Values"); + for (Map.Entry set : newMap.entrySet()) { + System.out.println("Keys: " + set.getKey() + ", Values: " + set.getValue()); + } /* * Usage tip! * From 2842b256414d363913762f7013d4b0d1d7263250 Mon Sep 17 00:00:00 2001 From: Tavparsad Singh Date: Mon, 6 Jan 2025 15:56:40 -0800 Subject: [PATCH 06/11] Finished number practice --- src/NumberPractice.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..176aa03 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,22 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + float floatingNum = -2l; // Create an int with a positive value and assign it to a variable - + int regNum = 22; // Use the modulo % operator to find the remainder when the int is divided by 3 - + System.out.println(regNum % 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 (regNum % 2 == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + } // Divide the number by another number using integer division - + System.out.println(regNum / 6); /* * Reminder! * From f0e6de377ce4c60a4a431775cce6c244b6a004c2 Mon Sep 17 00:00:00 2001 From: Tavparsad Singh Date: Mon, 6 Jan 2025 21:45:09 -0800 Subject: [PATCH 07/11] Finished person practice. --- src/Person.java | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..58f50b6 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; + 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 "This is " + name + ", they are " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,26 +34,29 @@ public class Person { * @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 dude = new Person("Dante", 20); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person dudeTwo = new Person("Vergil", 21); // Print the first person - + System.out.println(dude); // Print the second person - + System.out.println(dudeTwo); // Get the name of the first person and store it in a local variable - + String fName = dude.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 fBirth = dude.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(fBirth); /** * Terminology! * From 38af98f265df91c704a3f55e99b010a911fd7e1a Mon Sep 17 00:00:00 2001 From: Tavparsad Singh Date: Mon, 6 Jan 2025 21:52:07 -0800 Subject: [PATCH 08/11] Finished set prectice. --- src/SetPractice.java | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..3b71029 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,30 @@ +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("Hello"); + newSet.add("World"); + newSet.add("Smile?"); // Check whether the Set contains a given String - + if (newSet.contains("Smile?")) { + System.out.println("True"); + } else { + System.out.println("False"); + } // Remove an element from the Set - + newSet.remove("Hello"); // Get the size of the Set - + System.out.println(newSet.size()); // Iterate over the elements of the Set, printing each one on a separate line - + for (String word : newSet) { + System.out.println(word); + } /* * Warning! * From 073fa1d17ec4b420dcc3454d612432eed9fbdf0b Mon Sep 17 00:00:00 2001 From: Tavparsad Singh Date: Mon, 6 Jan 2025 21:55:47 -0800 Subject: [PATCH 09/11] added more to refreshers and string practice --- src/StringPractice.java | 7 ++++--- toRefresh.md | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..425d46a 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,11 +1,12 @@ 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 = "Fives"; // Find the length of the string - + System.out.println(word.length()); // Concatenate (add) two strings together and reassign the result - + word += " high?"; + System.out.println(word); // Find the value of the character at index 3 // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) diff --git a/toRefresh.md b/toRefresh.md index 28ac268..8569634 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,5 @@ 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. -- List or methods used in general \ No newline at end of file +- Refresh on methods used in general +- Refresh on java constructors and fields \ No newline at end of file From bcee6c9535d750ca38a6fe8168c03c2e3aa72c26 Mon Sep 17 00:00:00 2001 From: Tavparsad Singh Date: Mon, 6 Jan 2025 22:00:02 -0800 Subject: [PATCH 10/11] Working on String practice. --- src/StringPractice.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 425d46a..40b2f5c 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -8,11 +8,17 @@ public static void main(String[] args) { word += " high?"; System.out.println(word); // Find the value of the character at index 3 - + System.out.println(word.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + if (word.contains("high")) { + System.out.println("True"); + } else { + System.out.println("False"); + } // Iterate over the characters of the string, printing each one on a separate line - + for (Character letter : word.toCharArray()) { + System.out.println(letter); + } // Create an ArrayList of Strings and assign it to a variable // Add multiple strings to the List (OK to do one-by-one) From b755f715761fa8fd83bb6d50c22d4e251b2d6c5e Mon Sep 17 00:00:00 2001 From: Tavparsad Singh Date: Mon, 6 Jan 2025 22:05:28 -0800 Subject: [PATCH 11/11] Completed assignment --- src/StringPractice.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 40b2f5c..a176911 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,3 +1,6 @@ +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 @@ -20,14 +23,24 @@ public static void main(String[] args) { System.out.println(letter); } // 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("bottom"); + newList.add("jeans"); + newList.add("boots"); + newList.add("with the?"); // 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 lyric = String.join(", ", newList); + System.out.println(lyric); // Check whether two strings are equal - + if (lyric.equals(word)) { + System.out.println("True"); + } else { + System.out.println("False"); + } /* * Reminder! *