From 8b48d15e9e0f6d0054cec51e84e8b0a657e17272 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:11:18 -0800 Subject: [PATCH 01/32] Created a float variable with a negative value --- src/NumberPractice.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..d815ea4 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,6 +1,7 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float floatNum = -7.7f; // Create an int with a positive value and assign it to a variable From 49125c29ff5207d54be3efd9e941230ba19e087c Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:13:02 -0800 Subject: [PATCH 02/32] Created an int variable with a positive value --- src/NumberPractice.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index d815ea4..bb37ae1 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -4,7 +4,7 @@ public static void main(String args[]) { float floatNum = -7.7f; // Create an int with a positive value and assign it to a variable - + int intNum = 9; // 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 From 3d8ef775b3d654791c140b1d51166abb2ba1738f Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:15:08 -0800 Subject: [PATCH 03/32] Created a remainder variable to store the remainder of the int variable when its divided by 3 --- src/NumberPractice.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bb37ae1..52d4e47 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -6,7 +6,7 @@ public static void main(String args[]) { // Create an int with a positive value and assign it to a variable int intNum = 9; // Use the modulo % operator to find the remainder when the int is divided by 3 - + int remainder = intNum % 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 886beea3de190bfcaba18e29b12533d5c66fde36 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:18:54 -0800 Subject: [PATCH 04/32] Wrote an if-else statements to determine whether an integer number is even or odd using the modulus operator --- src/NumberPractice.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index 52d4e47..10da221 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -11,7 +11,11 @@ public static void main(String args[]) { // (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 (intNum % 2 == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + } // Divide the number by another number using integer division /* From be0613671bd644e78e36d43ac6d4d3fbef018d86 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:22:35 -0800 Subject: [PATCH 05/32] Created a divison variable and performed an integer division, also showed the results in the console for check --- src/NumberPractice.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index 10da221..819a9bb 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -17,7 +17,8 @@ public static void main(String args[]) { System.out.println("Odd"); } // Divide the number by another number using integer division - + int divResult = 6 / 2; + System.out.println("Result of the division by 2: " + divResult); /* * Reminder! * From 076ffa1d599f2facd66f027892385b4470219704 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:28:33 -0800 Subject: [PATCH 06/32] Started working on second set of problems, created an empty ArrayList and poputed it with 3 different types of fruits --- src/ListPractice.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..b7b74d0 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,11 +1,16 @@ +import java.util.ArrayList; +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 myList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + myList.add("Apple"); + myList.add("Cherry"); + myList.add("Banana"); // Print the element at index 1 // Replace the element at index 1 with a new value From 1fb96f2b880559f00acf329e8b6be813b5819715 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:30:22 -0800 Subject: [PATCH 07/32] Printing the element at index 1 of the ArrayList --- src/ListPractice.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index b7b74d0..35d9578 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -12,10 +12,10 @@ public static void main(String[] args) { myList.add("Cherry"); myList.add("Banana"); // Print the element at index 1 - + System.out.println("Element at index 1: " + myList.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) - + // Insert a new element at index 0 (the length of the list will change) // Check whether the list contains a certain string From e725affda17b7ec27ca23a21fc4c2783e5983475 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:34:59 -0800 Subject: [PATCH 08/32] Inserted elements at certain spots without changing the original size of the ArrayList, also added print statements for check --- src/ListPractice.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index 35d9578..0426bbe 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -15,9 +15,11 @@ public static void main(String[] args) { System.out.println("Element at index 1: " + myList.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) - + myList.set(1, "Strawberry"); + System.out.println("Size of the List after the first insertion: " + myList.size()); // Insert a new element at index 0 (the length of the list will change) - + myList.set(0, "Kiwi"); + System.out.println("Size of the List after the second insertion: " + myList.size()); // Check whether the list contains a certain string // Iterate over the list using a traditional for-loop. From 1f50926499f15d0d1b0fa501134935fce81ab6ab Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:37:47 -0800 Subject: [PATCH 09/32] Checking whether the ArrayList contains a certain string in the console --- src/ListPractice.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index 0426bbe..ef94079 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -21,7 +21,7 @@ public static void main(String[] args) { myList.set(0, "Kiwi"); System.out.println("Size of the List after the second insertion: " + myList.size()); // Check whether the list contains a certain string - + System.out.println(myList.contains("Apple")); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line From 62d2bd139ff71f0383d171b23f6d7b0b442e78c0 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:41:15 -0800 Subject: [PATCH 10/32] Wrote a regular for-loop to iterate through an ArrayList and use a print statement to show each index and its value in the console --- src/ListPractice.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index ef94079..e022d32 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -24,7 +24,9 @@ public static void main(String[] args) { System.out.println(myList.contains("Apple")); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + for (int i = 0; i < myList.size(); i++) { + System.out.println("Index " + i + ": " + myList.get(i)); + } // Sort the list using the Collections library // Iterate over the list using a for-each loop From ba9c1f641af508479ae20b1cea3981c1e4bcaeb4 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:43:15 -0800 Subject: [PATCH 11/32] Sorted an ArrayList using Collections library and imported a statement in order to use it --- src/ListPractice.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index e022d32..5690d43 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,4 +1,5 @@ import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class ListPractice { @@ -28,7 +29,8 @@ public static void main(String[] args) { System.out.println("Index " + i + ": " + myList.get(i)); } // Sort the list using the Collections library - + Collections.sort(myList); + System.out.println("Sorted List: " + myList); // Iterate over the list using a for-each loop // Print each value on a second line From 31b7e993e5fd38ac2ba6d77efccaecc1f2046ed4 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:44:58 -0800 Subject: [PATCH 12/32] Wrote a for-each loop for the ArrayList and printed each item in sorted order on seperate lines --- src/ListPractice.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index 5690d43..a77bc51 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -33,7 +33,9 @@ public static void main(String[] args) { System.out.println("Sorted List: " + myList); // Iterate over the list using a for-each loop // Print each value on a second line - + for (String fruit : myList) { + System.out.println(fruit); + } /* * Usage tip! * From 7fe3cdb6d8a327086485587bf37d961eec2ccc5f Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:50:13 -0800 Subject: [PATCH 13/32] Started a third set of problems, created a basic String array and popular it with 4 values --- src/ArrayPractice.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..eca2477 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[] myArr = 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 + myArr[0] = "Hello"; + myArr[1] = "Bonjour"; + myArr[2] = "Hola"; + myArr[3] = "Salam"; // Get the value of the array at index 2 From 63f987ee77c1beafd9efa61cf4c40951e0bc5208 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:52:18 -0800 Subject: [PATCH 14/32] Console the value from the array at index 2, and showed the length of the array --- src/ArrayPractice.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index eca2477..f41de84 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -10,9 +10,9 @@ public static void main(String[] args) { myArr[3] = "Salam"; // Get the value of the array at index 2 - + System.out.println("Value at index 2: " + myArr[2]); // Get the length of the array - + System.out.println("Length of the array: " + myArr.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 308ff892242f25a74ecc5734b35cd2804e9d1286 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:54:59 -0800 Subject: [PATCH 15/32] Finished up third set of problems, and wrote traditional and for-each loops to iterate through an array of Strings --- src/ArrayPractice.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index f41de84..42949ef 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -14,9 +14,13 @@ public static void main(String[] args) { // Get the length of the array System.out.println("Length of the array: " + myArr.length); // Iterate over the array using a traditional for loop and print out each item - + for (int i = 0; i < myArr.length; i++) { + System.out.println(myArr[i]); + } // Iterate over the array using a for-each loop and print out each item - + for (String item : myArr) { + System.out.println(item); + } /* * Reminder! * From 063f4c786d5d1987488750b510ee4f3b46fd092b Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 16:59:03 -0800 Subject: [PATCH 16/32] Started fourth set of problems, and created and assigned values to variables using Strings --- src/StringPractice.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..955a9b7 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 myWord = "Welcome"; // Find the length of the string - + System.out.println("Length of the Word: " + myWord.length()); // Concatenate (add) two strings together and reassign the result - + myWord = myWord + " Friend"; + System.out.println("Concatenated String: " + myWord); // 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?) From 2ade9610a77db032a2357dca919a12c10858d230 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:02:43 -0800 Subject: [PATCH 17/32] Wrote additional print statements --- src/StringPractice.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 955a9b7..a7b2f97 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -8,9 +8,9 @@ public static void main(String[] args) { myWord = myWord + " Friend"; System.out.println("Concatenated String: " + myWord); // Find the value of the character at index 3 - + System.out.println("Value of the character at index 3 is " + myWord.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + System.out.println("Does the String have 'abc' in it? " + myWord.contains("abc")); // Iterate over the characters of the string, printing each one on a separate line // Create an ArrayList of Strings and assign it to a variable From 3f82e1ffb8996f13a8fb0281a215e6df86fc9575 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:07:05 -0800 Subject: [PATCH 18/32] Wrote a for-loop for the String to print each character on a seperate line, and created a new ArrayList and populated it with some values --- src/StringPractice.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index a7b2f97..274b723 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 @@ -12,11 +15,15 @@ public static void main(String[] args) { // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) System.out.println("Does the String have 'abc' in it? " + myWord.contains("abc")); // Iterate over the characters of the string, printing each one on a separate line - + for (int i = 0; i < myWord.length(); i++) { + System.out.println(myWord.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable - + List stringList = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) - + stringList.add("Cucumber"); + stringList.add("Carrot"); + stringList.add("Pepper"); // 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 From 8e0b53b9070a0f9de3b71214422a31d1e16a33a1 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:11:12 -0800 Subject: [PATCH 19/32] Finished up the problems in fourth set --- src/StringPractice.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 274b723..bed9386 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -26,9 +26,10 @@ public static void main(String[] args) { stringList.add("Pepper"); // 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 anotherWord = "Welcome Friend"; + System.out.println("Are the Strings equal to each other? " + myWord.equals(anotherWord)); /* * Reminder! * From 1777173e9a6cf1c1e657f3abbfd03a4fa1697c02 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:14:29 -0800 Subject: [PATCH 20/32] Started a fifth set of problems, created a HashSet and added some values to it --- src/SetPractice.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..bde016c 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,10 +1,16 @@ +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 mySet = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) - + mySet.add("Math"); + mySet.add("English"); + mySet.add("Music"); + mySet.add("PE"); // Check whether the Set contains a given String // Remove an element from the Set From 03fdca13fd644e041860e747b625fc337583a424 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:18:35 -0800 Subject: [PATCH 21/32] Practiced with HashSet and looped over the Set --- src/SetPractice.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index bde016c..ec9469b 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -12,13 +12,15 @@ public static void main(String[] args) { mySet.add("Music"); mySet.add("PE"); // Check whether the Set contains a given String - + System.out.println("Does the Set contains Math? " + mySet.contains("Math")); // Remove an element from the Set - + mySet.remove("PE"); // Get the size of the Set - + System.out.println("Size of the Set: " + mySet.size()); // Iterate over the elements of the Set, printing each one on a separate line - + for (String subject : mySet) { + System.out.println(subject); + } /* * Warning! * From c9ffd431d27b2a938259fbe0efc6744814ed03d2 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:22:03 -0800 Subject: [PATCH 22/32] Started sixth set of problems, created a HashMap and populated it with 3 key/values pairs --- src/MapPractice.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..a671fb2 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,13 +1,16 @@ - +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 myMap = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) - + myMap.put("Alice", 75); + myMap.put("Brandon", 88); + myMap.put("Emily", 96); // Get the value associated with a given key in the Map // Find the size (number of key/value pairs) of the Map From 5b669e85898fa636e37c4f6db914d7991dea8aa5 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:25:58 -0800 Subject: [PATCH 23/32] Practiced with HashMap and wrote additional print statements for check --- src/MapPractice.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index a671fb2..bfdd315 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -12,11 +12,12 @@ public static void main(String[] args) { myMap.put("Brandon", 88); myMap.put("Emily", 96); // Get the value associated with a given key in the Map - + System.out.println("Valued assosiated with a given key of 'Alice': " + myMap.get("Alice")); // 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("Size of the Map: " + myMap.size()); + // Replace the value associated with a given key (the size of the Map should not change) + myMap.put("Alice", 76); + System.out.println("Updated value for 'Alice' key: " + myMap.get("Alice")); // Check whether the Map contains a given key // Check whether the Map contains a given value From dbdf939633bfc5f8b34bbdd6be6159cbf8ecf9bf Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:32:52 -0800 Subject: [PATCH 24/32] Wrote a loop to iterate over the Keys for the HashMap and additional print statements --- src/MapPractice.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index bfdd315..a5dbae4 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -19,11 +19,13 @@ public static void main(String[] args) { myMap.put("Alice", 76); System.out.println("Updated value for 'Alice' key: " + myMap.get("Alice")); // Check whether the Map contains a given key - + System.out.println("Does the Map contains the key 'Edward'? " + myMap.containsKey("Edward")); // Check whether the Map contains a given value - + System.out.println("Does the Map contains the value 88? " + myMap.containsValue(88)); // Iterate over the keys of the Map, printing each key - + for (String key : myMap.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 548fb5861cde33eaa9e480c172250adde8d759ce Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:36:15 -0800 Subject: [PATCH 25/32] Wrote for loops for keys, values, and both --- src/MapPractice.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index a5dbae4..e7637b2 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -27,9 +27,13 @@ public static void main(String[] args) { System.out.println(key); } // Iterate over the values of the map, printing each value - + for (Integer value : myMap.values()) { + System.out.println(value); + } // Iterate over the entries in the map, printing each key and value - + for (Map.Entry entry : myMap.entrySet()) { + System.out.println("Key: " + entry.getKey() + " | Value: " + entry.getValue()); + } /* * Usage tip! * From d5f5ff749260095f65f4ae8b05c28e95a3b8d303 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:38:55 -0800 Subject: [PATCH 26/32] Started last set of problems, creating public and private instance variables and a constructor for them --- src/Person.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..7a50f88 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,11 +6,15 @@ 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 From 7b87e0e32a36ac0c1d001e7ee3ccf8c06504eb43 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:40:11 -0800 Subject: [PATCH 27/32] Wrote a toString method --- src/Person.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Person.java b/src/Person.java index 7a50f88..9990555 100644 --- a/src/Person.java +++ b/src/Person.java @@ -17,8 +17,10 @@ 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; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. /** From c6e71953d357158fc49b034f45f9d1fb7837beb0 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:43:39 -0800 Subject: [PATCH 28/32] Created an instance method for birth year --- src/Person.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Person.java b/src/Person.java index 9990555..d1157f9 100644 --- a/src/Person.java +++ b/src/Person.java @@ -26,7 +26,7 @@ public String toString() { /** * birthYear returns the year the person was born. * - * The birth year is calculated by subtracting the person's age from currentYear + * The birth year is calculated by subtracting the person's age from current Year * that's passed in as an int. It assumes that the person's birthday has already * passed this year. * @@ -34,7 +34,9 @@ public String toString() { * @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 From 203f3298d681915f5ca07ca4ea1d066b0a6075a1 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:45:32 -0800 Subject: [PATCH 29/32] Created two instances of Person class to test the code --- src/Person.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Person.java b/src/Person.java index d1157f9..ceb40b5 100644 --- a/src/Person.java +++ b/src/Person.java @@ -40,14 +40,14 @@ public int birthYear(int currentYear) { public static void main(String[] args) { // Create an instance of Person - + Person person1 = new Person("Ali", 22); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person person2 = new Person("Mark", 18); // 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 // Using the birthYear method, get the birth year of the first person From c99d377b23421b029d69ee09e482dcaafd3205aa Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:51:40 -0800 Subject: [PATCH 30/32] Finished up the problems, wrote main method --- src/Person.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Person.java b/src/Person.java index ceb40b5..ed039f2 100644 --- a/src/Person.java +++ b/src/Person.java @@ -40,22 +40,22 @@ public int birthYear(int currentYear) { public static void main(String[] args) { // Create an instance of Person - Person person1 = new Person("Ali", 22); + Person firstPerson = new Person("Ali", 22); // Create another instance of Person with a different name and age and // assign it to a different variable - Person person2 = new Person("Mark", 18); + Person secondPerson = new Person("Mark", 18); // Print the first person - System.out.println(person1); + System.out.println(firstPerson); // Print the second person - System.out.println(person2); + System.out.println(secondPerson); // Get the name of the first person and store it in a local variable - + String firstPersonName = firstPerson.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 firstPersonBirthYear = firstPerson.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(firstPersonName + "'s Birth year: " + firstPersonBirthYear); /** * Terminology! * From d7a20d6f6ae18994b29668e2f9c8671532d8c23d Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:53:48 -0800 Subject: [PATCH 31/32] Added topics to Refresh later on --- toRefresh.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/toRefresh.md b/toRefresh.md index 163dcab..9a2c96f 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. -- \ No newline at end of file +- Float Variables +- Java Classes and instance fields \ No newline at end of file From e5d9e2efa960323b77c557703975285ca8ac4726 Mon Sep 17 00:00:00 2001 From: Diana Date: Thu, 2 Jan 2025 17:56:03 -0800 Subject: [PATCH 32/32] Final Touches and double checking all the directions and files --- src/SetPractice.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index ec9469b..f414985 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -10,11 +10,10 @@ public static void main(String[] args) { mySet.add("Math"); mySet.add("English"); mySet.add("Music"); - mySet.add("PE"); // Check whether the Set contains a given String System.out.println("Does the Set contains Math? " + mySet.contains("Math")); // Remove an element from the Set - mySet.remove("PE"); + mySet.remove("Math"); // Get the size of the Set System.out.println("Size of the Set: " + mySet.size()); // Iterate over the elements of the Set, printing each one on a separate line