From 8db38017f281cbf743834eb166a32fd271d01dff Mon Sep 17 00:00:00 2001 From: Felix Chen Date: Sat, 4 Jan 2025 15:48:20 -0800 Subject: [PATCH 1/8] finished NumberPracice --- src/NumberPractice.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..c8806d4 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,25 @@ 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.26988f; + System.out.println(negative); // Create an int with a positive value and assign it to a variable - + int positive = 4; // Use the modulo % operator to find the remainder when the int is divided by 3 - + int remain = positive % 3; + System.out.println(remain); // 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 (positive % 2 == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + } // Divide the number by another number using integer division - + int sum = positive / 2; + System.out.println(sum); /* * Reminder! * From ce83c448a089b5af821453f2937845abc94e13f8 Mon Sep 17 00:00:00 2001 From: f3liz Date: Sat, 4 Jan 2025 16:24:50 -0800 Subject: [PATCH 2/8] finished ListPractice --- src/ListPractice.java | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..7f203aa 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,39 @@ +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 newList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + newList.add("hello"); + newList.add("bye"); + newList.add("meow"); // Print the element at index 1 - + System.out.println(newList.get(1)); + System.out.println(); // Replace the element at index 1 with a new value // (Do not insert a new value. The length of the list should not change) - + newList.set(1, "dog"); // Insert a new element at index 0 (the length of the list will change) - + newList.add(0, "ham"); // Check whether the list contains a certain string - + newList.contains("ham"); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + for (int i = 0; i < newList.size(); i++) { + System.out.println(newList.get(i)); + } + System.out.println(); // Sort the list using the Collections library - + Collections.sort(newList); // Iterate over the list using a for-each loop // Print each value on a second line - + for (String string : newList) { + System.out.println(string); + } /* * Usage tip! * From baa9c667c8ae27ec52eab325c927b74076e7bd21 Mon Sep 17 00:00:00 2001 From: f3liz Date: Sat, 4 Jan 2025 19:30:38 -0800 Subject: [PATCH 3/8] finished ArrayPractice and added to toRefresh.md --- src/ArrayPractice.java | 23 +++++++++++++++++------ toRefresh.md | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..11017ae 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,29 @@ 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] = "bye"; + newArray[2] = "banana"; + newArray[3] = "ham"; // Get the value of the array at index 2 - + System.out.println(newArray[2]); + System.out.println(); // Get the length of the array - + System.out.println(newArray.length); + System.out.println(); // 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]); + } + System.out.println(); // Iterate over the array using a for-each loop and print out each item - + for (String string : newArray) { + System.out.println(string); + } + System.out.println(); /* * Reminder! * diff --git a/toRefresh.md b/toRefresh.md index 163dcab..3b7a0ac 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 +- ArrayList \ No newline at end of file From 9c48d9846c10acccc1430c40546c896e29237ab3 Mon Sep 17 00:00:00 2001 From: f3liz Date: Sat, 4 Jan 2025 19:53:26 -0800 Subject: [PATCH 4/8] finished StringPractice and added to toRefresh.md --- src/StringPractice.java | 31 +++++++++++++++++++++---------- toRefresh.md | 3 ++- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..432eb2e 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,37 @@ +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 five = "Hello"; // Find the length of the string - + System.out.println(five.length()); + System.out.println(); // Concatenate (add) two strings together and reassign the result - + String together = "hi" + "yellow"; + System.out.println(); // Find the value of the character at index 3 - + System.out.println(together.charAt(3)); + System.out.println(); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + System.out.println(together.contains("abc")); + System.out.println(); // Iterate over the characters of the string, printing each one on a separate line - + for (int i = 0; i < together.length(); i++) { + System.out.println(together.charAt(i)); + } + System.out.println(); // Create an ArrayList of Strings and assign it to a variable - + ArrayList list = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) - + list.add("hello"); + list.add("goodbye"); + list.add("ham"); // 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.join(",", list); // Check whether two strings are equal - + System.out.println(five.equals(together)); /* * Reminder! * diff --git a/toRefresh.md b/toRefresh.md index 3b7a0ac..fc676a6 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. -- ArrayList \ No newline at end of file +- ArrayList +- String join method \ No newline at end of file From d8d62123a2f83cc6c7de18c2f4698f8f01eef13b Mon Sep 17 00:00:00 2001 From: f3liz Date: Sat, 4 Jan 2025 20:04:49 -0800 Subject: [PATCH 5/8] finished SetPractice --- src/SetPractice.java | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..c83bbf2 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,26 @@ +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 set = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) - + set.add("Hello"); + set.add("soda"); + set.add("ham"); // Check whether the Set contains a given String - + System.out.println(set.contains("pizza")); + System.out.println(); // Remove an element from the Set - + set.remove("ham"); // Get the size of the Set - + System.out.println(set.size()); + System.out.println(); // Iterate over the elements of the Set, printing each one on a separate line - + for (String string : set) { + System.out.println(string); + } /* * Warning! * From c3b397da83d4f3ff2cc9bdd188549248bf275d08 Mon Sep 17 00:00:00 2001 From: f3liz Date: Sun, 5 Jan 2025 14:53:29 -0800 Subject: [PATCH 6/8] finished MapPractice and added to toRefresh.md --- src/MapPractice.java | 37 ++++++++++++++++++++++++++----------- toRefresh.md | 3 ++- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..18272f6 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,44 @@ - +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 - + HashMap map = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) - + map.put("hello", 1); + map.put("bye", 2); + map.put("ham", 100); // Get the value associated with a given key in the Map - + System.out.println(map.get("ham")); + System.out.println(); // Find the size (number of key/value pairs) of the Map - + System.out.println(map.size()); + System.out.println(); // Replace the value associated with a given key (the size of the Map shoukld not change) - + map.replace("hello", 20); // Check whether the Map contains a given key - + System.out.println(map.containsKey("ham")); + System.out.println(); // Check whether the Map contains a given value - + System.out.println(map.containsValue(100)); + System.out.println(); // Iterate over the keys of the Map, printing each key - + for (String string : map.keySet()) { + System.out.println(string); + } + System.out.println(); // Iterate over the values of the map, printing each value - + for (int value : map.values()) { + System.out.println(value); + } + System.out.println(); // Iterate over the entries in the map, printing each key and value - + for (Map.Entry entry : map.entrySet()) { + System.out.println("Key:" + entry.getKey() + " Value:" + entry.getValue()); + } /* * Usage tip! * diff --git a/toRefresh.md b/toRefresh.md index fc676a6..3713e39 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -3,4 +3,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. - ArrayList -- String join method \ No newline at end of file +- String join method +- Map entry function \ No newline at end of file From c1a92ede59ed25cbd435e594bf1da978c8cd4a94 Mon Sep 17 00:00:00 2001 From: f3liz Date: Mon, 6 Jan 2025 11:09:34 -0800 Subject: [PATCH 7/8] finished Person file --- src/Person.java | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..6175a64 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,14 +6,22 @@ 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 name + " " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,26 +36,32 @@ public class Person { * @return The year the person was born */ // (create the instance method here) - + public int birthYear () { + return 2025 - age; + } public static void main(String[] args) { // Create an instance of Person - + Person person1 = new Person("Felix", 25); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person person2 = new Person("Joe", 21); // Print the first person - + System.out.println(person1); + System.out.println(); // Print the second person - + System.out.println(person2); + System.out.println(); // Get the name of the first person and store it in a local variable - + String name = person1.name; + System.out.println(name); + System.out.println(); // 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 birthyear = person1.birthYear(); // In a separate statement, print the local variable holding the birth year. - + System.out.println(birthyear); /** * Terminology! * From cd73cbaef9c2292db490166cb60a3a357d2580aa Mon Sep 17 00:00:00 2001 From: f3liz Date: Mon, 6 Jan 2025 13:49:53 -0800 Subject: [PATCH 8/8] corrected work --- src/ListPractice.java | 3 ++- src/MapPractice.java | 2 +- src/Person.java | 6 +++--- src/SetPractice.java | 3 ++- src/StringPractice.java | 3 ++- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index 7f203aa..632ab72 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 newList = new ArrayList<>(); + List newList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) newList.add("hello"); newList.add("bye"); diff --git a/src/MapPractice.java b/src/MapPractice.java index 18272f6..7a4d748 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -5,7 +5,7 @@ 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 map = new HashMap<>(); + Map map = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) map.put("hello", 1); diff --git a/src/Person.java b/src/Person.java index 6175a64..4ab45e1 100644 --- a/src/Person.java +++ b/src/Person.java @@ -36,8 +36,8 @@ public String toString() { * @return The year the person was born */ // (create the instance method here) - public int birthYear () { - return 2025 - age; + public int birthYear (int currentYear) { + return currentYear - age; } public static void main(String[] args) { @@ -59,7 +59,7 @@ public static void main(String[] args) { // 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 birthyear = person1.birthYear(); + int birthyear = person1.birthYear(2025); // In a separate statement, print the local variable holding the birth year. System.out.println(birthyear); /** diff --git a/src/SetPractice.java b/src/SetPractice.java index c83bbf2..9e0e708 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,9 +1,10 @@ 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 - HashSet set = new HashSet<>(); + Set set = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) set.add("Hello"); diff --git a/src/StringPractice.java b/src/StringPractice.java index 432eb2e..7dea3b8 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) { @@ -22,7 +23,7 @@ public static void main(String[] args) { } System.out.println(); // Create an ArrayList of Strings and assign it to a variable - ArrayList list = new ArrayList<>(); + List list = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) list.add("hello"); list.add("goodbye");