From 9dce3882845e23bf9665554c1967f7f9544c2d70 Mon Sep 17 00:00:00 2001 From: Lizock Date: Mon, 6 Jan 2025 15:57:18 -0800 Subject: [PATCH 1/7] Completed NumberPractice --- src/NumberPractice.java | 14 ++++++++++++-- toRefresh.md | 2 ++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..119d05a 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +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 floatNum = -1.12f; // Create an int with a positive value and assign it to a variable + int intNum = 9; // Use the modulo % operator to find the remainder when the int is divided by 3 - + int remainder = intNum % 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. + String evenOrOdd = ""; + if (intNum % 2 == 0){ + evenOrOdd = "Number is even"; + } else { + evenOrOdd = "Number is odd"; + } // Divide the number by another number using integer division - /* * Reminder! * @@ -20,6 +28,8 @@ public static void main(String args[]) { * Example: * 7 / 3 = 2 when performing int division */ + int divided = intNum / 2; + System.out.println(floatNum + ", " + intNum + ", " + remainder + ", " + evenOrOdd + ", " + divided ); } } diff --git a/toRefresh.md b/toRefresh.md index 163dcab..53a0b66 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. +Looked up how float number is being defined. + - \ No newline at end of file From b9fef35b6b994bd679f10fcc42accb0f2ccc59e0 Mon Sep 17 00:00:00 2001 From: Lizock Date: Mon, 6 Jan 2025 16:44:05 -0800 Subject: [PATCH 2/7] Finished ListPractice --- src/ListPractice.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..eafeb7b 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,46 @@ +import java.util.*; 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 fruits = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) + fruits.add("Apple"); + fruits.add("Pear"); + fruits.add("Grape"); // Print the element at index 1 + System.out.println(fruits.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) + fruits.set(1, "Banana"); + System.out.println(fruits.get(1)); // Insert a new element at index 0 (the length of the list will change) + fruits.add(0, "Pinapple"); + System.out.println(fruits); // Check whether the list contains a certain string + System.out.println(fruits.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 < fruits.size(); i++){ + System.out.println("Index: " + i + ", Fruit: " + fruits.get(i)); + } + // Sort the list using the Collections library + Collections.sort(fruits); // Iterate over the list using a for-each loop // Print each value on a second line + for (String fruit : fruits){ + System.out.println(fruit); + } /* * Usage tip! From 8c1efb1bd5aecb957e0e6a80695b252d60febb19 Mon Sep 17 00:00:00 2001 From: Lizock Date: Mon, 6 Jan 2025 17:04:22 -0800 Subject: [PATCH 3/7] Finished ArrayPractice --- src/ArrayPractice.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..5125b83 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,30 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] pets = 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 + pets[0] = "cat"; + pets[1] = "dog"; + pets[2] = "parrot"; + pets[3] = "hamster"; // Get the value of the array at index 2 + System.out.println(pets[2]); // Get the length of the array + System.out.println(pets.length); // Iterate over the array using a traditional for loop and print out each item + for (int i = 0; i < pets.length; i++){ + System.out.println(pets[i]); + } // Iterate over the array using a for-each loop and print out each item + for (String pet : pets){ + System.out.println(pet); + } /* * Reminder! From 931631e94aa9a2d85ab3c8d874a3625dc0293413 Mon Sep 17 00:00:00 2001 From: Lizock Date: Mon, 6 Jan 2025 17:46:40 -0800 Subject: [PATCH 4/7] Finished StringPractice --- src/StringPractice.java | 21 +++++++++++++++++++++ toRefresh.md | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..058a18c 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,46 @@ +import java.util.*; public class StringPractice { public static void main(String[] args) { // Create a string with at least 5 characters and assign it to a variable + String name = "eliza"; // Find the length of the string + System.out.println(name.length()); // Concatenate (add) two strings together and reassign the result + String name2 = "beth"; + name = name + name2; + System.out.println(name); // Find the value of the character at index 3 + System.out.println(name.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + System.out.println(name.contains("abc")); // Iterate over the characters of the string, printing each one on a separate line + for(char letter : name.toCharArray()){ + System.out.println(letter); + } // Create an ArrayList of Strings and assign it to a variable + ArrayList lastName = new ArrayList(); // Add multiple strings to the List (OK to do one-by-one) + lastName.add("Potter"); + lastName.add("Granger"); + lastName.add("Weasley"); + lastName.add("Malfoy"); // 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 words = String.join(", ", lastName); + String lastNameString = String.join(", ", lastName); + String words = "Potter, Granger, Weasley, Malfoy"; + System.out.println(lastNameString); // Check whether two strings are equal + System.out.println(lastNameString.equals(words)); /* * Reminder! diff --git a/toRefresh.md b/toRefresh.md index 53a0b66..b2034fc 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -3,5 +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. Looked up how float number is being defined. - +Looked up how to iterate through a string using a for each loop. - \ No newline at end of file From 3a681c994885d819baff788eb785fb3f83e8a581 Mon Sep 17 00:00:00 2001 From: Lizock Date: Mon, 6 Jan 2025 21:47:52 -0800 Subject: [PATCH 5/7] Finished SetPractice --- src/SetPractice.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..eef7474 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,30 @@ +import java.util.*; public class SetPractice { public static void main(String[] args) { // Create a HashSet of Strings and assign it to a variable of type Set + HashSet instruments = new HashSet(); + // Add 3 elements to the set // (It's OK to do it one-by-one) + instruments.add("piano"); + instruments.add("guitar"); + instruments.add("violin"); // Check whether the Set contains a given String + System.out.println(instruments.contains("violin")); // Remove an element from the Set + instruments.remove("guitar"); + System.out.println(instruments); // Get the size of the Set + System.out.println(instruments.size()); // Iterate over the elements of the Set, printing each one on a separate line + for (String instrument : instruments){ + System.out.println(instrument); + } /* * Warning! From 5652aa08ae879138027ee2ac86f46057a582d2c7 Mon Sep 17 00:00:00 2001 From: Lizock Date: Mon, 6 Jan 2025 22:21:45 -0800 Subject: [PATCH 6/7] Finished MapPractice --- src/MapPractice.java | 21 ++++++++++++++++++++- toRefresh.md | 1 + 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..af7a28d 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,47 @@ - +import java.util.*; 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 orders = new HashMap(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + orders.put("Tea", 3); + orders.put("Coffee", 1); + orders.put("Smoothie", 1); // Get the value associated with a given key in the Map + System.out.println(orders.get("Smoothie")); // Find the size (number of key/value pairs) of the Map + System.out.println(orders.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) + orders.put("Tea", 5); + System.out.println(orders); // Check whether the Map contains a given key + System.out.println(orders.containsKey("Coffee")); // Check whether the Map contains a given value + System.out.println(orders.containsValue(3)); // Iterate over the keys of the Map, printing each key + for (int quantity : orders.values()){ + System.out.println(quantity); + } // Iterate over the values of the map, printing each value + for (String drink : orders.keySet()){ + System.out.println(drink); + } // Iterate over the entries in the map, printing each key and value + for (Map.Entry order : orders.entrySet()){ + System.out.println(order.getKey() + ": " + order.getValue()); + } /* * Usage tip! diff --git a/toRefresh.md b/toRefresh.md index b2034fc..25aa112 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -4,4 +4,5 @@ As you work through this exercise, write down anything that you needed to look u Looked up how float number is being defined. Looked up how to iterate through a string using a for each loop. +Looked up how to iterate trhoug a HashMap using a for each loop. - \ No newline at end of file From 5a10cdc19af89651a9a2c21915e49a72276deb52 Mon Sep 17 00:00:00 2001 From: Lizock Date: Mon, 6 Jan 2025 23:09:28 -0800 Subject: [PATCH 7/7] Finished Person --- src/Person.java | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..c047a82 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: " + name + ", Age: " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,25 +34,34 @@ 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", 13); // Create another instance of Person with a different name and age and // assign it to a different variable + Person person2 = new Person("Garry", 31); // 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 personName = person.name; // 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 personBirthYear = person.birthYear(2025); // In a separate statement, print the local variable holding the birth year. + System.out.println("Name: " + personName + ", Birth Year: " + personBirthYear); /** * Terminology!