From e1a4be596ef40f07ea07f36ca852b6cecc16ee36 Mon Sep 17 00:00:00 2001 From: leoraggy Date: Thu, 25 Sep 2025 17:12:58 -0700 Subject: [PATCH 1/8] Completed NumberPractice --- src/NumberPractice.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..7bbabb3 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,16 +1,26 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + float myFloat = -1.2f; + System.out.println("my float: " + myFloat); // Create an int with a positive value and assign it to a variable - + int number = 20; + System.out.println("number: " + number); // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = number % 3; + System.out.println("20 % 3 = " +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(number % 2 == 0){ + System.out.println(number + " is even!"); + }else{ + System.out.println(number + " is odd!"); + } + // Divide the number by another number using integer division /* @@ -21,5 +31,8 @@ public static void main(String args[]) { * 7 / 3 = 2 when performing int division */ + int solution = number / 4; + System.out.println(number + " / 4 = " + solution); + } } From 5b1de5f54a3929567d94e7d835d087e4122dbe5c Mon Sep 17 00:00:00 2001 From: leoraggy Date: Thu, 25 Sep 2025 17:29:35 -0700 Subject: [PATCH 2/8] Completed ListPractice.java --- src/ListPractice.java | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..c420785 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,46 @@ +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 myList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + myList.add("Apple"); + myList.add("Banana"); + myList.add("Grapes"); // Print the element at index 1 + System.out.println(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,"Blueberry"); // Insert a new element at index 0 (the length of the list will change) + myList.add(0,"Raspberry"); // Check whether the list contains a certain string - + System.out.println("Does myList contain Pear? " + myList.contains("Pear")); // 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("myList index " + i + ": " + myList.get(i)); + } + // Sort the list using the Collections library + Collections.sort(myList); // Iterate over the list using a for-each loop // Print each value on a second line + for (String str : myList) { + System.out.println(str); + } + /* * Usage tip! * From 3953485b44feff31372998c1d2191d8a3d5f031a Mon Sep 17 00:00:00 2001 From: leoraggy Date: Thu, 25 Sep 2025 17:34:10 -0700 Subject: [PATCH 3/8] Completed ArrayPractice --- src/ArrayPractice.java | 18 +++++++++++++++--- toRefresh.md | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..6b0ec20 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,30 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[] shoppingList = 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 + shoppingList[0] = "Bread"; + shoppingList[1] = "Milk"; + shoppingList[2] = "Cheese"; + shoppingList[3] = "Banana"; + // Get the value of the array at index 2 + System.out.println(shoppingList[2]); // Get the length of the array - // Iterate over the array using a traditional for loop and print out each item + System.out.println("Length of array: " + shoppingList.length); + // Iterate over the array using a traditional for loop and print out each item + for(int i = 0; i < shoppingList.length; i++){ + System.out.println("shoppingList[" + i + "] = " + shoppingList[i]); + } // Iterate over the array using a for-each loop and print out each item - + for(String str: shoppingList){ + System.out.println(str); + } /* * Reminder! * diff --git a/toRefresh.md b/toRefresh.md index 163dcab..1a8b629 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 From 6a912c245aaea1ccf7d641d1781de13a2f6f7416 Mon Sep 17 00:00:00 2001 From: leoraggy Date: Thu, 25 Sep 2025 17:49:04 -0700 Subject: [PATCH 4/8] Completed String Practice --- src/StringPractice.java | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..3433267 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +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 fruit = "banana"; // Find the length of the string + int fruitLength = fruit.length(); + System.out.println(fruitLength); // Concatenate (add) two strings together and reassign the result + String drink = "milk"; + String result = fruit + drink; + System.out.println(result); // Find the value of the character at index 3 + String letter = result.substring(3,4); + System.out.println(letter); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + System.out.println("Does result have milk? " + result.contains("milk")); // Iterate over the characters of the string, printing each one on a separate line + for(char c : result.toCharArray()){ + System.out.println(c); + } // Create an ArrayList of Strings and assign it to a variable + ArrayList myList = new ArrayList(); + // Add multiple strings to the List (OK to do one-by-one) + myList.add("Leo"); +myList.add("Will"); +myList.add("Joshua"); +myList.add("Jadon"); + // 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 combinedString = String.join(",", myList); + System.out.println(combinedString); // Check whether two strings are equal - + System.err.println("Does Leo equal to Will? " + "Leo".equals("Will")); + /* * Reminder! * From f8e052bc8f6b0e1587fce26926052228314ef9d3 Mon Sep 17 00:00:00 2001 From: leoraggy Date: Thu, 25 Sep 2025 17:55:28 -0700 Subject: [PATCH 5/8] Completed Set Practice --- src/SetPractice.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..a979404 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,27 @@ +import java.util.HashSet; + public class SetPractice { public static void main(String[] args) { // Create a HashSet of Strings and assign it to a variable of type Set - + HashSet fruits = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) - + fruits.add("Kiwi"); + fruits.add("Blackberry"); + fruits.add("Tomato"); // Check whether the Set contains a given String + System.out.println("Does fruit set contain Banana?" + fruits.contains("Banana")); // Remove an element from the Set + fruits.remove("kiwi"); // Get the size of the Set - + int sizeOfSet = fruits.size(); + System.out.println(sizeOfSet); // Iterate over the elements of the Set, printing each one on a separate line + for (String fruit : fruits) { + System.out.println(fruit); + } /* * Warning! From e534c0f23ff540e5a6a9c8ed43eff8d00d8953d9 Mon Sep 17 00:00:00 2001 From: leoraggy Date: Fri, 26 Sep 2025 11:34:16 -0700 Subject: [PATCH 6/8] Completed MapPractice --- src/MapPractice.java | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..3cebbd0 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,44 @@ - +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 + HashMap myHashMap = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + myHashMap.put("Six", 3); + myHashMap.put("Three", 5); + myHashMap.put("Four", 4); // Get the value associated with a given key in the Map - + myHashMap.get("Six"); // Find the size (number of key/value pairs) of the Map - + System.out.println("Size of hashmap: " + myHashMap.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) + myHashMap.put("Six", 6); // Check whether the Map contains a given key + System.out.println("Does myHashmap containsKey Four? " + myHashMap.containsKey("Four")); // Check whether the Map contains a given value + System.out.println("Does myHashmap containsValue 9 " + myHashMap.containsValue(9)); - // Iterate over the keys of the Map, printing each key + // Iterate over the keys of the Map, printing each key + for(String s: myHashMap.keySet()){ + System.out.println("Key: " + s); + } // Iterate over the values of the map, printing each value + for(int i: myHashMap.values()){ + System.out.println("Value: " + i); + } // Iterate over the entries in the map, printing each key and value + for(String s: myHashMap.keySet()){ + System.out.println("Key: " + s + ", Value: " + myHashMap.get(s)); + } /* * Usage tip! From 57bc524dbe889fbf10778e78b3f4c30e78101494 Mon Sep 17 00:00:00 2001 From: leoraggy Date: Fri, 26 Sep 2025 13:49:45 -0700 Subject: [PATCH 7/8] Completed Person and finished all files --- src/Person.java | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..32098ee 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 - + 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,25 +35,41 @@ public class Person { */ // (create the instance method here) + public int birthYear(int currentYear){ + return currentYear - age; + } + + public String getName(){ + return name; + } + public static void main(String[] args) { // Create an instance of Person + Person person1 = new Person("Leo", 20); // Create another instance of Person with a different name and age and // assign it to a different variable + Person person2 = new Person("Joshua", 40); // Print the first person - // Print the second 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 - + String person1Name = person1.getName(); // 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 person1Age = person1.birthYear(2025); + // In a separate statement, print the local variable holding the birth year. + System.out.println(person1Age); + /** * Terminology! * From 9c4e46835f3e26e43e49486fcf6e256befdb6a48 Mon Sep 17 00:00:00 2001 From: leoraggy Date: Fri, 26 Sep 2025 13:51:32 -0700 Subject: [PATCH 8/8] Added print statement for person2Name variable --- src/Person.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Person.java b/src/Person.java index 32098ee..11230b7 100644 --- a/src/Person.java +++ b/src/Person.java @@ -59,7 +59,8 @@ public static void main(String[] args) { // Print the second person System.out.println(person2); // Get the name of the first person and store it in a local variable - String person1Name = person1.getName(); + String person2Name = person2.getName(); + System.out.println(person2Name); // 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.