From 4905431973045381f564fa4c4b657fc77cc597d0 Mon Sep 17 00:00:00 2001 From: Rebecca Riffle Date: Thu, 2 Jan 2025 15:54:55 -0800 Subject: [PATCH 1/8] Completed NumberPractice and added floats and printing to toRefresh --- src/NumberPractice.java | 9 +++++++++ toRefresh.md | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..d871c8d 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +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 negativeFloat = -0.12345f; // Create an int with a positive value and assign it to a variable + int positiveInteger = 42; // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = positiveInteger % 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" // if the number is odd. + if (remainder % 2 == 0) { + System.out.println("Even"); + } else if (remainder % 2 != 0) { + System.out.println("Odd"); + }; // Divide the number by another number using integer division + int dividend = remainder / 2; /* * Reminder! diff --git a/toRefresh.md b/toRefresh.md index 163dcab..00a3bf9 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,15 @@ 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. +## Number Practice +### Floats +Create a float using the "float" keyword and an "f" at the end of the number. +``` +float exampleFloat = 0.123f; +``` +### Print to console +To print in Java, call the println() method on System.out. +``` +System.out.println("") +``` - \ No newline at end of file From 5f675068d6a0d2743b992a25ee55724550df9c00 Mon Sep 17 00:00:00 2001 From: Rebecca Riffle Date: Fri, 3 Jan 2025 10:42:07 -0800 Subject: [PATCH 2/8] Completed list practice and added items to refresh --- src/ListPractice.java | 31 +++++++++++++++++++++++++++++++ toRefresh.md | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..87732c3 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,58 @@ +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(); // Add 3 elements to the list (OK to do one-by-one) + stringList.add("banana"); + stringList.add("mango"); + stringList.add("lemon"); // Print the element at index 1 + System.out.println(stringList.get(1)); // should print "mango" // 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, "kiwi"); + System.out.println(stringList); // should print "[banana, kiwi, lemon]" // Insert a new element at index 0 (the length of the list will change) + stringList.add(0, "strawberry"); + System.out.println(stringList); // should print "[strawberry, banana, kiwi, lemon]" // Check whether the list contains a certain string + if (stringList.contains("banana")) { + System.out.println("That's bananas!"); // expected output + } else { + System.out.println("No bananas here."); + } // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + System.out.println(); + for (int i = 0; i < stringList.size(); i++) { + String fruit = stringList.get(i); + System.out.print(i); + System.out.print(" " + fruit); + System.out.println(); + } // Sort the list using the Collections library + Collections.sort(stringList); + System.out.println(stringList); // should print "[banana, kiwi, lemon, strawberry]" + System.out.println(); // Iterate over the list using a for-each loop // Print each value on a second line + for (String fruit : stringList) { + System.out.println(fruit); + }; /* * Usage tip! diff --git a/toRefresh.md b/toRefresh.md index 00a3bf9..5488084 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -1,6 +1,6 @@ # Items to Refresh on: -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. +These are all items that I either had to search online or got wrong on the first try and had to retype. ## Number Practice ### Floats @@ -13,4 +13,42 @@ To print in Java, call the println() method on System.out. ``` System.out.println("") ``` + +## List Practice +### Imports +To use an ArrayList, you must first import the java.util package. +``` +import java.util.ArrayList; +``` +### List types +When you create a list or an array list, you should specify the type of data is contained in that list, using "<>". +``` +List stringList = new ArrayList(); +``` +### Adding to a list +Use the "add" method to add an item to a list or array list. +``` +listOfDigits.add(2); +``` +### Changing an element at an index on a list +Use the "set" method to change the item at a specific index without changing the length of the list. +``` +listOfFruit.set(0, "apple"); +``` +### Collections +To use the Collections library, you must first import the java.util package. +``` +import java.util.Collections; +``` +Use Collections.sort() to sort the contents of a list or array list. +``` +Collections.sort(listOfGrades); +``` +### For Each +The syntax for a for-each loop is as follows: "for (TYPE VARIABLE : SOURCE) { // do code};" +``` + for (String animal : zoo) { + System.out.println(animal); + }; +``` - \ No newline at end of file From 3130fae95c854e8b0c2332b5f2976b095cacb531 Mon Sep 17 00:00:00 2001 From: Rebecca Riffle Date: Fri, 3 Jan 2025 11:09:13 -0800 Subject: [PATCH 3/8] Completed ArrayPractice and added array syntax to toRefresh --- src/ArrayPractice.java | 19 +++++++++++++++++++ toRefresh.md | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..85d9353 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,36 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] strArray = {"red", "orange", "yellow", "green"}; // Set the value of the array at each index to be a different String // It's OK to do this one-by-one + strArray[0] = "blue"; + strArray[1] = "purple"; + strArray[2] = "red"; + strArray[3] = "orange"; // Get the value of the array at index 2 + System.out.println("The value at index 2 is " + strArray[2]); // expect "The value at index 2 is red" // Get the length of the array + System.out.print("The array length is "); + System.out.print(strArray.length); + // expects "The array length is 4" + System.out.println(); + // Iterate over the array using a traditional for loop and print out each item + System.out.println(); + for (int i = 0; i < strArray.length; i++) { + System.out.println(strArray[i]); + } // Iterate over the array using a for-each loop and print out each item + System.out.println(); + for (String color: strArray) { + System.out.println(color); + } /* * Reminder! diff --git a/toRefresh.md b/toRefresh.md index 5488084..36d4a49 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -51,4 +51,10 @@ The syntax for a for-each loop is as follows: "for (TYPE VARIABLE : SOURCE) { // System.out.println(animal); }; ``` +## Array Practice +### Create an Array +Create an array with the following syntax: +``` +String[] arr = {"hi", "hey", "hello}; +``` - \ No newline at end of file From 59fdf9a1c0f5fce184da4cf6490d20ad4001b0e6 Mon Sep 17 00:00:00 2001 From: Rebecca Riffle Date: Fri, 3 Jan 2025 11:17:07 -0800 Subject: [PATCH 4/8] Added print statements and converted arrays to string --- src/ArrayPractice.java | 3 +++ src/ListPractice.java | 2 ++ src/NumberPractice.java | 4 ++++ toRefresh.md | 7 +++++++ 4 files changed, 16 insertions(+) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index 85d9353..c343791 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,7 +1,9 @@ +import java.util.Arrays; public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 String[] strArray = {"red", "orange", "yellow", "green"}; + System.out.println(Arrays.toString(strArray)); // Set the value of the array at each index to be a different String // It's OK to do this one-by-one @@ -9,6 +11,7 @@ public static void main(String[] args) { strArray[1] = "purple"; strArray[2] = "red"; strArray[3] = "orange"; + System.out.println(Arrays.toString(strArray)); // Get the value of the array at index 2 System.out.println("The value at index 2 is " + strArray[2]); // expect "The value at index 2 is red" diff --git a/src/ListPractice.java b/src/ListPractice.java index 87732c3..cc28b65 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -8,11 +8,13 @@ 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("banana"); stringList.add("mango"); stringList.add("lemon"); + System.out.println(stringList); // Print the element at index 1 System.out.println(stringList.get(1)); // should print "mango" diff --git a/src/NumberPractice.java b/src/NumberPractice.java index d871c8d..7c85c12 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -2,12 +2,15 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable float negativeFloat = -0.12345f; + System.out.println(negativeFloat); // Create an int with a positive value and assign it to a variable int positiveInteger = 42; + System.out.println(positiveInteger); // Use the modulo % operator to find the remainder when the int is divided by 3 int remainder = positiveInteger % 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) @@ -21,6 +24,7 @@ public static void main(String args[]) { // Divide the number by another number using integer division int dividend = remainder / 2; + System.out.println(dividend); /* * Reminder! diff --git a/toRefresh.md b/toRefresh.md index 36d4a49..24063e4 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -57,4 +57,11 @@ Create an array with the following syntax: ``` String[] arr = {"hi", "hey", "hello}; ``` +### Print an array +To print an array, you must convert it to a String. +``` +import java.util.Arrays; +int[] numbers = {1, 2, 3} +System.out.println(Arrays.toString(numbers)) // expected output: "[1, 2, 3]" +``` - \ No newline at end of file From 3e2de83260bea826550ac27ce185ad53c5e37b53 Mon Sep 17 00:00:00 2001 From: Rebecca Riffle Date: Fri, 3 Jan 2025 11:43:56 -0800 Subject: [PATCH 5/8] Completes String Practice --- src/StringPractice.java | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..cd40611 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,66 @@ +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 greeting = "Hello"; + System.out.println(greeting); // Find the length of the string + int length = greeting.length(); + System.out.println(length); // Concatenate (add) two strings together and reassign the result + String name = "Rebecca"; + String nameGreeting = greeting + ", " + name + "!"; + System.out.println(nameGreeting); // Find the value of the character at index 3 + System.out.println(nameGreeting.charAt(3)); // expect 'l' // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + boolean hasABitOfCalifornia = nameGreeting.contains("ca"); + + if (hasABitOfCalifornia) { + System.out.println("There's a little bit of California in that! ('ca')"); // expected output + } else { + System.out.println("No California here!"); + } // Iterate over the characters of the string, printing each one on a separate line + System.out.println(); + for (int i = 0; i < nameGreeting.length(); i++) { + System.out.println(nameGreeting.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable + ArrayList zoo = new ArrayList<>(); + System.out.println(zoo); // Add multiple strings to the List (OK to do one-by-one) + zoo.add("zebra"); + zoo.add("giraffe"); + zoo.add("penguin"); + zoo.add("tiger"); + System.out.println(zoo); // 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 listOfAnimals = zoo.toString(); + listOfAnimals = listOfAnimals.substring(1, listOfAnimals.length() - 1); + System.out.println(listOfAnimals); + // Check whether two strings are equal + String string1 = "apples"; + String string2 = "apples"; + String string3 = "oranges"; + + System.out.println(compareTwo(string1, string2)); + System.out.println(compareTwo(string1, string3)); + + + /* * Reminder! @@ -29,4 +70,16 @@ public static void main(String[] args) { * We use == when comparing primitives (e.g. int or char). */ } + + public static String compareTwo (String a, String b) { + String compared = ""; + if (a.contentEquals(b)) { + compared = "Comparing apples to apples!"; + } else { + compared = "Not comparing apples to apples!"; + } + return compared; + } } + + From 39df0223469279d8648c23b56552939bbbd15f30 Mon Sep 17 00:00:00 2001 From: Rebecca Riffle Date: Fri, 3 Jan 2025 11:54:21 -0800 Subject: [PATCH 6/8] Completed Set Practice --- src/SetPractice.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..fcc22d7 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,39 @@ +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 movies = new HashSet<>(); + System.out.println(movies); // Add 3 elements to the set // (It's OK to do it one-by-one) + movies.add("Anna and the Apocalypse"); + movies.add("Wicked"); + movies.add("Lilo and Stitch"); + movies.add("Memento"); + System.out.println(movies); // Check whether the Set contains a given String + if (movies.contains("The Matrix")){ + System.out.println("INSERT QUOTE FROM THE MATRIX HERE"); + } else { + System.out.println("You should add 'The Matrix' to this movie collection!"); + } // Remove an element from the Set + movies.remove("Wicked"); + System.out.println(movies); // Get the size of the Set + System.out.print("The size of this set is "); + System.out.println(movies.size()); // Iterate over the elements of the Set, printing each one on a separate line + for (String movie : movies) { + System.out.println(movie); + } /* * Warning! From 5dc44a4be6533df71df489d1e55d96aee375d310 Mon Sep 17 00:00:00 2001 From: Rebecca Riffle Date: Fri, 3 Jan 2025 12:21:06 -0800 Subject: [PATCH 7/8] Completed Map Practice and added notes to toRefresh --- src/MapPractice.java | 36 +++++++++++++++++++++++++++++++++++- toRefresh.md | 18 ++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..fdd6426 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,62 @@ - +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 simpsons = new HashMap(); + System.out.println(simpsons); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + simpsons.put("Homer", 38); + simpsons.put("Marge", 36); + simpsons.put("Bart", 10); + simpsons.put("Lisa", 8); + simpsons.put("Maggie", 1); + System.out.println(simpsons); // Get the value associated with a given key in the Map + System.out.println(simpsons.get("Maggie")); // expect 1 // Find the size (number of key/value pairs) of the Map + System.out.print("The map size is "); + System.out.println(simpsons.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) + System.out.print("It's Lisa's birthday! Now she is: "); + simpsons.put("Lisa", simpsons.get("Lisa") + 1); + System.out.println(simpsons.get("Lisa")); // Check whether the Map contains a given key + if (simpsons.containsKey("Homer")) { + System.out.println("Homer is a Simpson."); + } else { + System.out.println("Where did Homer go??"); + } // Check whether the Map contains a given value + if (simpsons.containsValue(42)) { + System.out.println("42 is a cool age!"); + } else { + System.out.println("None of the Simpsons are 42."); + } // Iterate over the keys of the Map, printing each key + for (String name : simpsons.keySet()) { + System.out.println(name); + } // Iterate over the values of the map, printing each value + for (Integer age : simpsons.values()) { + System.out.println(age); + } // Iterate over the entries in the map, printing each key and value + for (Map.Entry person : simpsons.entrySet()) { + System.out.println(person.getKey() + " is " + person.getValue() + " years old."); + } /* * Usage tip! diff --git a/toRefresh.md b/toRefresh.md index 24063e4..b6ae11e 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -64,4 +64,22 @@ import java.util.Arrays; int[] numbers = {1, 2, 3} System.out.println(Arrays.toString(numbers)) // expected output: "[1, 2, 3]" ``` +## Map Practice +### Iterate over a map +To iterate over the keys of a map, use the keySet() method on the map object; to iterate over the values, use the values() methos on the map object. To iterate over BOTH looks a little more complicated (see example below). Iterate over Map.Entry<> types through the map.entrySet(). +``` +for (String key : map.keySet()) { + // do something with key +} + +for (Integer value : map.values()) { + // do something with values +} + +for (Map.Entry< String, Integer> entry : map.entrySet()) { + // do something with keys and values + System.out.println("Key: " + entry.getKey()); + System.out.println("Value: " + entry.getValue()); +} +``` - \ No newline at end of file From 9c5cfd4806d747074004ea5df2866cf82f563878 Mon Sep 17 00:00:00 2001 From: Rebecca Riffle Date: Fri, 3 Jan 2025 12:40:30 -0800 Subject: [PATCH 8/8] Completed Person object exercises and added notes to toRefresh --- src/Person.java | 26 +++++++++++++++++++++----- toRefresh.md | 11 +++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..7e88e46 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 - + 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 +34,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 rebecca = new Person("Rebecca", 31); // Create another instance of Person with a different name and age and // assign it to a different variable + Person ari = new Person("Ari", 1); // Print the first person + System.out.println(rebecca.toString()); // Print the second person + System.out.println(ari.toString()); // Get the name of the first person and store it in a local variable + String myFirstName = rebecca.name; + System.out.println("My first name is " + myFirstName); // 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 = rebecca.birthYear(2025); + // In a separate statement, print the local variable holding the birth year. + System.out.println("If my birthday had already happened this year, my birth year would be " + birthYear); /** * Terminology! diff --git a/toRefresh.md b/toRefresh.md index b6ae11e..1cca311 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -82,4 +82,15 @@ for (Map.Entry< String, Integer> entry : map.entrySet()) { System.out.println("Value: " + entry.getValue()); } ``` +## Person +### Class constructors +``` +public class Turtle { + String name; + + public Turtle(String name) { + this.name = name; + } +} +``` - \ No newline at end of file