From bd8a7de655a38fadbc9abca6af9b0fc0fdd415c3 Mon Sep 17 00:00:00 2001 From: Kimberly Mageary Date: Wed, 8 Jan 2025 16:07:56 -0800 Subject: [PATCH 1/7] Solved number practice --- src/NumberPractice.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..d29e3a1 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +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 newFloat = -4.545f; + System.out.println(newFloat); // Create an int with a positive value and assign it to a variable + int newInt = 36; + System.out.println(newInt); // Use the modulo % operator to find the remainder when the int is divided by 3 + float remainder = newInt % 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(newInt % 2 == 0){ + System.out.println("This number is even"); + }else{ + System.out.println("This number is odd!"); + } // Divide the number by another number using integer division + int division = newInt / 4; + System.out.println(division); /* * Reminder! * From 0a01284c97b504b74af01032515e55ac4648b3bb Mon Sep 17 00:00:00 2001 From: Kimberly Mageary Date: Wed, 8 Jan 2025 17:34:56 -0800 Subject: [PATCH 2/7] Finished ListPractice --- src/ListPractice.java | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..ab38909 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,43 @@ +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 list = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) + list.add("Hello"); + list.add("Good"); + list.add("Node"); + System.out.println("New list: " + list); // 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, "Bad"); + System.out.println("Updated List: " + list); // Insert a new element at index 0 (the length of the list will change) + list.add(0, "Food"); + System.out.println("Expected: Food, Hello, Bad, Node | " + list); // Check whether the list contains a certain string - + System.out.println(list.contains("Food")); // 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(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 item : list){ + System.out.println(item); + } /* * Usage tip! * From 0bc942078ae41ec908f6e689a75f1fb3e4e53bd4 Mon Sep 17 00:00:00 2001 From: Kimberly Mageary Date: Wed, 8 Jan 2025 18:34:51 -0800 Subject: [PATCH 3/7] Finished ArrayPractice --- src/ArrayPractice.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..e4d2ac3 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,28 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[] myArray = new String[4]; // Set the value of the array at each index to be a different String + myArray[0] = "Hello"; + myArray[1] = "Tissue"; + myArray[2] = "Phone"; + myArray[3] = "Cup"; // It's OK to do this one-by-one // Get the value of the array at index 2 + System.out.println(myArray[2]); // Get the length of the array - + System.out.println("Length of array" + myArray.length); // Iterate over the array using a traditional for loop and print out each item + for(int i =0; i Date: Wed, 8 Jan 2025 18:42:34 -0800 Subject: [PATCH 4/7] Finished String Practice --- src/StringPractice.java | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..c64958d 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,43 @@ +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 = "hello"; // Find the length of the string + System.out.println("String length: " + myString.length()); // Concatenate (add) two strings together and reassign the result + String string2 ="goodbye"; + String newString = myString + string2; + System.out.println(newString); // Find the value of the character at index 3 + System.out.println(myString.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + System.out.println(myString.contains("abc")); // Iterate over the characters of the string, printing each one on a separate line + for(int i=0; i list = new ArrayList(); // Add multiple strings to the List (OK to do one-by-one) + list.add("Hello"); + list.add("Good"); + list.add("Oui"); // 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 joinedString = String.join(", ", list); + System.out.println(joinedString); // Check whether two strings are equal - + joinedString.equals(myString); /* * Reminder! * From 75484424eec9eddb0eb1a7b7102d8d7ddfb3e2c8 Mon Sep 17 00:00:00 2001 From: Kimberly Mageary Date: Wed, 8 Jan 2025 18:47:12 -0800 Subject: [PATCH 5/7] Finished SetPractice --- src/SetPractice.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..5cb32cc 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,37 @@ +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<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + stringSet.add("Apple"); + stringSet.add("Banana"); + stringSet.add("Cherry"); // Check whether the Set contains a given String + String elementToCheck = "Banana"; + if (stringSet.contains(elementToCheck)) { + System.out.println(elementToCheck + " is in the set."); + } else { + System.out.println(elementToCheck + " is not in the set."); + } // Remove an element from the Set + stringSet.remove("Banana"); + System.out.println("Banana removed from the set."); // Get the size of the Set + System.out.println("The size: " + stringSet.size()); // Iterate over the elements of the Set, printing each one on a separate line + System.out.println("Elements in the set:"); + for (String element : stringSet) { + System.out.println(element); + } /* * Warning! From e90b64e3bbfa256bf01cc0030b244d6d2a809c32 Mon Sep 17 00:00:00 2001 From: Kimberly Mageary Date: Wed, 8 Jan 2025 18:51:35 -0800 Subject: [PATCH 6/7] Finished MapPractice --- src/MapPractice.java | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..7788fe2 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,63 @@ - +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<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + myMap.put("Alice", 25); + myMap.put("Bob", 30); + myMap.put("Charlie", 35); // Get the value associated with a given key in the Map + String keyToGet = "Bob"; + Integer value = myMap.get(keyToGet); + System.out.println("Value associated with key '" + keyToGet + "': " + value); // Find the size (number of key/value pairs) of the Map + System.out.println("Size of the map: " + myMap.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) + myMap.put("Alice", 26); // Replace the value for key "Alice" + System.out.println("Updated value for key 'Alice': " + myMap.get("Alice")); // Check whether the Map contains a given key + String keyToCheck = "Charlie"; + if (myMap.containsKey(keyToCheck)) { + System.out.println("The map contains the key '" + keyToCheck + "'."); + } else { + System.out.println("The map does not contain the key '" + keyToCheck + "'."); + } // Check whether the Map contains a given value + int valueToCheck = 30; + if (myMap.containsValue(valueToCheck)) { + System.out.println("The map contains the value " + valueToCheck + "."); + } else { + System.out.println("The map does not contain the value " + valueToCheck + "."); + } // Iterate over the keys of the Map, printing each key + System.out.println("Keys in the map:"); + for (String key : myMap.keySet()) { + System.out.println(key); + } // Iterate over the values of the map, printing each value + System.out.println("Values in the map:"); + for (Integer val : myMap.values()) { + System.out.println(val); + } // Iterate over the entries in the map, printing each key and value + System.out.println("Entries in the map:"); + for (Map.Entry entry : myMap.entrySet()) { + System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()); + } /* * Usage tip! From 431966a5167b27441ff349da4b30773084ba77f8 Mon Sep 17 00:00:00 2001 From: Kimberly Mageary Date: Wed, 8 Jan 2025 18:59:47 -0800 Subject: [PATCH 7/7] Finished Person --- src/Person.java | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..80200d5 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,14 +6,27 @@ 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 of person: " + name + " | Age: " + age; + } + public String getName(){ + return name; + } + public int getAge(){ + return age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,26 +41,33 @@ 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 example1 = new Person("Kim", 20); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person example2 = new Person("Hayden", 19); // Print the first person + System.out.println(example1); // Print the second person + System.out.println(example2); // Get the name of the first person and store it in a local variable + String name = example1.getName(); + System.out.println(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 birthYear = example1.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(birthYear); /** * Terminology! *