From 91de02a4534aeaa91b2f8dcefd1636e1b74303bd Mon Sep 17 00:00:00 2001 From: Augy Markham Date: Mon, 6 Jan 2025 20:55:55 -0800 Subject: [PATCH 1/7] completed NumberPractice exercises --- src/NumberPractice.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..c2a4563 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +1,31 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float negFloat = -10; + System.out.println("Negative Float: " + negFloat); // Create an int with a positive value and assign it to a variable + int posInt = 10; + System.out.println("Positive Int: " + posInt); // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = posInt % 3; + System.out.println("Remainder: " + 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 (posInt % 2 != 0){ + System.out.println("Odd"); + } + else System.out.println("Even"); + // Divide the number by another number using integer division + int divided = posInt / 7; + System.out.println(posInt + " / 7 = " + divided ); + /* * Reminder! From 2a60e6a79ab888baa39899de316208f92d2ea82d Mon Sep 17 00:00:00 2001 From: Augy Markham Date: Mon, 6 Jan 2025 21:11:31 -0800 Subject: [PATCH 2/7] compeleted ListPractice exercises & added to toRefresh page --- src/ListPractice.java | 27 ++++++++++++++++++++++++++- toRefresh.md | 4 +++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..a11cf55 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,52 @@ +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(); + System.out.println(stringList); // Add 3 elements to the list (OK to do one-by-one) + stringList.add("hello"); + stringList.add("world"); + stringList.add("!"); + System.out.println(stringList); // 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, "you"); + System.out.println(stringList); // Insert a new element at index 0 (the length of the list will change) + stringList.add(0, "why"); + System.out.println(stringList); // Check whether the list contains a certain string + System.out.println("Does the list contain \"hello\"? " + stringList.contains("hello")); + System.out.println("Does the list contain \".\"? " + stringList.contains(".")); // 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 + // Print each value on a second line --a seperate line? + for (String string : stringList) { + System.out.println(string); + } /* * Usage tip! diff --git a/toRefresh.md b/toRefresh.md index 163dcab..56e1273 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 +- ArrayList.set +- ArrayList methods +- Using Collections Library \ No newline at end of file From 04761c1d6b755110490f49e6fcbeecd80a7243c8 Mon Sep 17 00:00:00 2001 From: Augy Markham Date: Mon, 6 Jan 2025 21:29:10 -0800 Subject: [PATCH 3/7] completed ArrayPractice exercises & updated toRefresh again --- src/ArrayPractice.java | 22 +++++++++++++++++++++- toRefresh.md | 5 +++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..e12430d 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,37 @@ +import java.util.Arrays; + public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] stringList = new String[4]; + System.out.println(Arrays.toString(stringList)); // Set the value of the array at each index to be a different String // It's OK to do this one-by-one + stringList[0] = "hi"; + stringList[1] = "hello"; + stringList[2] = "howdy"; + stringList[3] = "hey"; + System.out.println(Arrays.toString(stringList)); // Get the value of the array at index 2 + String element2 = stringList[2]; + System.out.println("Element at index 2: " + element2); // Get the length of the array + int stringListLength = stringList.length; + System.out.println("Length: " + stringListLength); // Iterate over the array using a traditional for loop and print out each item - + System.out.println("--- Traditional For Loop ---"); + for (int i = 0; i < stringList.length; i++){ + System.out.println(stringList[i]); + } // Iterate over the array using a for-each loop and print out each item + System.out.println("--- For Each Loop ---"); + for (String string : stringList){ + System.out.println(string); + } /* * Reminder! diff --git a/toRefresh.md b/toRefresh.md index 56e1273..b813150 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -3,5 +3,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. - ArrayList.set -- ArrayList methods -- Using Collections Library \ No newline at end of file +- ArrayList methods in general +- Using Collections Library +- Printing content in array (toString) \ No newline at end of file From bcbccc023196ce235d8bdedfc1178d407fd9e63a Mon Sep 17 00:00:00 2001 From: Augy Markham Date: Mon, 6 Jan 2025 21:56:16 -0800 Subject: [PATCH 4/7] completed StringPractice & and updated toRefresh --- src/StringPractice.java | 27 +++++++++++++++++++++++++++ toRefresh.md | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..036a586 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,52 @@ +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 myString = "bagels"; + System.out.println(myString); // Find the length of the string + int myStringLength = myString.length(); + System.out.println("length: " + myStringLength); // Concatenate (add) two strings together and reassign the result + myString = myString + myString; + System.out.println("new string: " + myString); // Find the value of the character at index 3 + char value3 = myString.charAt(3); + System.out.println("value at index 3: " + value3); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + System.out.println("contains \"abc\"? " + myString.contains("abc")); + System.out.println("contains \"age\"? " + myString.contains("age")); // Iterate over the characters of the string, printing each one on a separate line + for (int i = 0; i < myString.length(); i++) { + System.out.println(i + ". " + myString.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable + ArrayList stringList= new ArrayList(); + System.out.println(stringList); // Add multiple strings to the List (OK to do one-by-one) + stringList.add("lipitytoo"); + stringList.add("lipitytah"); + stringList.add("lippytappy"); + stringList.add("tootah"); + System.out.println(stringList); // 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 conglomString = String.join(",", stringList); + System.out.println(conglomString); + // Check whether two strings are equal + System.out.println("Are \"this\" and \"that\" equal? " + "this".equals("that")); + System.out.println("Is \"this\" and \"this\" equal? " + "this".equals("this")); /* * Reminder! diff --git a/toRefresh.md b/toRefresh.md index b813150..a4c9ce8 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -5,4 +5,5 @@ As you work through this exercise, write down anything that you needed to look u - ArrayList.set - ArrayList methods in general - Using Collections Library -- Printing content in array (toString) \ No newline at end of file +- Printing content in array (toString) +- String methods \ No newline at end of file From 760313b41057ffb153e35ac423495ad5cdbdb9d7 Mon Sep 17 00:00:00 2001 From: Augy Markham Date: Mon, 6 Jan 2025 22:01:37 -0800 Subject: [PATCH 5/7] completed SetPractice --- src/SetPractice.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..a64bb54 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,34 @@ +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 stringSet = new HashSet(); + System.out.println(stringSet); // Add 3 elements to the set // (It's OK to do it one-by-one) + stringSet.add("ahh"); + stringSet.add("ahhhhh"); + stringSet.add("ahhhhhhhhhhhhh"); + System.out.println(stringSet); // Check whether the Set contains a given String + System.out.println("contains \"ah\"? " + stringSet.contains("ah")); + System.out.println("contains \"ahh\"? " + stringSet.contains("ahh")); // Remove an element from the Set + stringSet.remove("ahh"); + System.out.println(stringSet); // Get the size of the Set + System.out.println("size: " + stringSet.size()); // Iterate over the elements of the Set, printing each one on a separate line - + for (String element : stringSet){ + System.out.println(element); + } /* * Warning! * From c8e927c23d3ee1cb40c93b96ffac06464d999b3a Mon Sep 17 00:00:00 2001 From: Augy Markham Date: Mon, 6 Jan 2025 22:13:05 -0800 Subject: [PATCH 6/7] completed MapPractice --- src/MapPractice.java | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..86f1c20 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,58 @@ - +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(); + System.out.println(myMap); + // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + myMap.put("i", 56); + myMap.put("me", 23); + myMap.put("mine", 99); + System.out.println(myMap); // Get the value associated with a given key in the Map + System.out.println(myMap.get("me")); // Find the size (number of key/value pairs) of the Map + System.out.println("size: " + myMap.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) + myMap.replace("mine", 2); + System.out.println(myMap); // Check whether the Map contains a given key + System.out.println("Contains \"you\"? " + myMap.containsKey("you")); + System.out.println("Contains \"me\"? " + myMap.containsKey("me")); // Check whether the Map contains a given value + System.out.println("Contains \"1\"? " + myMap.containsValue(1)); + System.out.println("Contains \"2\"? " + myMap.containsValue(2)); // Iterate over the keys of the Map, printing each key + System.out.println("--- Keys ---"); + for (String key : myMap.keySet()){ + System.out.println(key); + } + System.out.println(); // Iterate over the values of the map, printing each value + System.out.println("--- Values ---"); + for (int val : myMap.values()){ + System.out.println(val); + } + System.out.println(); // Iterate over the entries in the map, printing each key and value - + System.out.println("--- Sets ---"); + for (String key : myMap.keySet()){ + System.out.println(key + " - " + myMap.get(key)); + } /* * Usage tip! * From 8b5d59e803c53b0061ed9dfef6e9ca4019a767ad Mon Sep 17 00:00:00 2001 From: Augy Markham Date: Mon, 6 Jan 2025 22:32:30 -0800 Subject: [PATCH 7/7] completed Person class & added to toRefresh - finished review! --- src/Person.java | 27 ++++++++++++++++++++++++--- toRefresh.md | 3 ++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..2c5106b 100644 --- a/src/Person.java +++ b/src/Person.java @@ -5,15 +5,24 @@ public class Person { // Declare a public String instance variable for the name of the person + public String name; + // Declare a private int instance variable for the age of the person + 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 name + " - " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,25 +37,37 @@ 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 me = new Person("Augy", 22); + // Create another instance of Person with a different name and age and // assign it to a different variable + Person sister = new Person("Alyssa", 16); + // Print the first person + System.out.println(me); // Print the second person + System.out.println(sister); // Get the name of the first person and store it in a local variable + String firstPersonName = me.name; + System.out.println(firstPersonName); // 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 firstBirthYear = me.birthYear(2025); + // In a separate statement, print the local variable holding the birth year. + System.out.println(firstBirthYear); /** * Terminology! diff --git a/toRefresh.md b/toRefresh.md index a4c9ce8..023cb5f 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -6,4 +6,5 @@ As you work through this exercise, write down anything that you needed to look u - ArrayList methods in general - Using Collections Library - Printing content in array (toString) -- String methods \ No newline at end of file +- String methods +- overriding toString