From 39930f85a6f8cdb5a979e3f40f6fd1cb77dca3b4 Mon Sep 17 00:00:00 2001 From: Alexander Ruban Date: Fri, 3 Jan 2025 14:39:57 -0800 Subject: [PATCH 1/8] Finished NumberPractice. --- src/NumberPractice.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..0e852c1 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +1,30 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float negative = -10.93843123f; + System.out.println(negative); // Create an int with a positive value and assign it to a variable + int positive = 312; + System.out.println(positive); // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = positive % 3; + System.out.println(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 (positive % 2 == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + } // Divide the number by another number using integer division + int result = positive / 12; + System.out.println(result); /* * Reminder! From 337dd6bb2d04f6a1ceaa5d7d0a634e5b84dee0fc Mon Sep 17 00:00:00 2001 From: Alexander Ruban Date: Sat, 4 Jan 2025 12:38:45 -0800 Subject: [PATCH 2/8] Completed ListPractice. --- src/ListPractice.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..b1d5b9d 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,47 @@ +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 list = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) + list.add("BMW"); + list.add("Porsche"); + list.add("Mercedes"); // Print the element at index 1 + System.out.println(list.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) + list.set(1, "AUDI"); // Insert a new element at index 0 (the length of the list will change) + list.add(0, "VW"); // Check whether the list contains a certain string + System.out.println(list.contains("BMW")); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for (int i = 0; i < list.size(); i++) { + System.out.println("Index: " + i + " Value: " + list.get(i)); + } // Sort the list using the Collections library + Collections.sort(list); + System.out.println(list); // Iterate over the list using a for-each loop // Print each value on a second line + for (String value : list) { + System.out.println(value); + } /* * Usage tip! From 902784aa35e552ea10212a33a5b96469a6a0c573 Mon Sep 17 00:00:00 2001 From: Alexander Ruban Date: Sat, 4 Jan 2025 12:47:08 -0800 Subject: [PATCH 3/8] Completed ArrayPractice. --- src/ArrayPractice.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..fb8db5c 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,32 @@ + + public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] array = 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 + array[0] = "Apple"; + array[1] = "Kiwi"; + array[2] = "Plum"; + array[3] = "Coconut"; // Get the value of the array at index 2 + System.out.println(array[2]); // Get the length of the array + System.out.println(array.length); // Iterate over the array using a traditional for loop and print out each item + for (int i = 0; i < array.length; i++) { + System.out.println(array[i]); + } // Iterate over the array using a for-each loop and print out each item + for (String element : array) { + System.out.println(element); + } /* * Reminder! From 21a95585af66e37f54c92ec951c497374418bd1e Mon Sep 17 00:00:00 2001 From: Alexander Ruban Date: Sat, 4 Jan 2025 13:01:10 -0800 Subject: [PATCH 4/8] Finished StringPractice. --- src/StringPractice.java | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..1dd7bc7 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,49 @@ +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 + String fish = "Salmon"; // Find the length of the string + System.out.println(fish.length()); // Concatenate (add) two strings together and reassign the result - + String species = "Chinook"; + fish = species + " " + fish; + System.out.println(fish); // Find the value of the character at index 3 + System.out.println(fish.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + System.out.println(fish.contains("abc")); // Iterate over the characters of the string, printing each one on a separate line + for (int i = 0; i < fish.length(); i++) { + System.out.println(fish.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable + List list = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + list.add("Chinook"); + list.add("Coho"); + list.add("Chum"); + list.add("Pink"); + list.add("Sockeye"); // 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 types = String.join(", ", list); + System.out.println(types); // Check whether two strings are equal + String a = "Apple"; + String b = "Pear"; + + System.out.println(a.equals(b)); /* * Reminder! From 93fa58b40b66eca832175a971bb9a48d9f9e23af Mon Sep 17 00:00:00 2001 From: Alexander Ruban Date: Sat, 4 Jan 2025 13:11:54 -0800 Subject: [PATCH 5/8] Finished SetPractice. --- src/SetPractice.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..35eea4d 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +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 set = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + set.add("Blue"); + set.add("Green"); + set.add("Red"); // Check whether the Set contains a given String + System.out.println(set.contains("Red")); // Remove an element from the Set + System.out.println(set.remove("Green")); // Get the size of the Set + System.out.println(set.size()); // Iterate over the elements of the Set, printing each one on a separate line + for (String element : set) { + System.out.println(element); + } /* * Warning! From 5ce37139c22c74f656c8395524be0d8304373178 Mon Sep 17 00:00:00 2001 From: Alexander Ruban Date: Sat, 4 Jan 2025 13:21:28 -0800 Subject: [PATCH 6/8] Finished MapPractice. --- src/MapPractice.java | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..2df3b3c 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,48 @@ - +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 map = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + map.put("Salmon", 43); + map.put("Trout", 175); + map.put("Bass", 85); // Get the value associated with a given key in the Map + System.out.println(map.get("Trout")); // Find the size (number of key/value pairs) of the Map + System.out.println(map.size()); - // Replace the value associated with a given key (the size of the Map shoukld not change) + // Replace the value associated with a given key (the size of the Map should not change) + map.replace("Salmon", 53); + System.out.println(map); // Check whether the Map contains a given key + System.out.println(map.containsKey("Bass")); // Check whether the Map contains a given value + System.out.println(map.containsValue(175)); // Iterate over the keys of the Map, printing each key + for (String key : map.keySet()) { + System.out.println(key); + } // Iterate over the values of the map, printing each value + for (int value : map.values()) { + System.out.println(value); + } // 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! From 947df934cdb715c9df3b19f3b58207a7bac4d9b4 Mon Sep 17 00:00:00 2001 From: Alexander Ruban Date: Sat, 4 Jan 2025 13:47:54 -0800 Subject: [PATCH 7/8] Added topics to refresh. --- toRefresh.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/toRefresh.md b/toRefresh.md index 163dcab..56801b1 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 +- Floats +- String Concatenation +- Iterating Over EntrySet in Maps From 92156ca5c52bf84142025d05c9e384d6473ed293 Mon Sep 17 00:00:00 2001 From: Alexander Ruban Date: Sat, 4 Jan 2025 13:48:25 -0800 Subject: [PATCH 8/8] Completed Person. --- src/Person.java | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..75e0c7e 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,14 +6,21 @@ 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: " + name + " Age: " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,25 +35,35 @@ 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 person = new Person("Bob Smith", 47); // Create another instance of Person with a different name and age and // assign it to a different variable + Person person2 = new Person("Joe Smoe", 34); // Print the first person + System.out.println(person); // Print the second person + System.out.println(person2); // Get the name of the first person and store it in a local variable + String name1 = person.name; + System.out.println(name1); // 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 = person.birthYear(2025); // In a separate statement, print the local variable holding the birth year. + System.out.println(birthYear); /** * Terminology!