From 23ce4d1cdf934c8cb9cb71ac2cbdd26f22093b23 Mon Sep 17 00:00:00 2001 From: xavierb <194048107+xavierb117@users.noreply.github.com> Date: Mon, 13 Jan 2025 18:50:39 -0800 Subject: [PATCH 1/7] Completed Array Practice --- src/ArrayPractice.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..5430368 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,33 @@ +import java.util.*; public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] arrayExample = new String[4]; + System.out.println(Arrays.toString(arrayExample)); // Set the value of the array at each index to be a different String // It's OK to do this one-by-one + arrayExample[0] = "Hello"; + arrayExample[1] = "To"; + arrayExample[2] = "Java"; + arrayExample[3] = "Derusting"; + System.out.println(Arrays.toString(arrayExample)); // Get the value of the array at index 2 + System.out.println(arrayExample[2]); // Get the length of the array + System.out.println(arrayExample.length); // Iterate over the array using a traditional for loop and print out each item + for (int i = 0; i < arrayExample.length; i++) { + System.out.println(arrayExample[i]); + } // Iterate over the array using a for-each loop and print out each item + for (String word : arrayExample) { + System.out.println(word); + } /* * Reminder! From 7b276cbfbd8d77e8c6057218f9f96312729bed2e Mon Sep 17 00:00:00 2001 From: xavierb <194048107+xavierb117@users.noreply.github.com> Date: Mon, 13 Jan 2025 19:29:42 -0800 Subject: [PATCH 2/7] Completed List Practice --- src/ListPractice.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..98c406c 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,51 @@ +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 listExample = new ArrayList<>(); + System.out.println(listExample); // Add 3 elements to the list (OK to do one-by-one) + listExample.add("Hello"); + listExample.add("Java"); + listExample.add("Derusting"); + System.out.println(listExample); // Print the element at index 1 + System.out.println(listExample.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) + listExample.set(1, "ArrayList"); + System.out.println(listExample); // Insert a new element at index 0 (the length of the list will change) + listExample.add(0, "Assignment:"); + System.out.println(listExample); // Check whether the list contains a certain string + if (listExample.contains("Hello")) { + System.out.println("Yes! It has the element Hello"); + } // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for (int i = 0; i < listExample.size(); i++) { + System.out.println(i + "=" + listExample.get(i)); + } // Sort the list using the Collections library + Collections.sort(listExample); + System.out.println(listExample); // Iterate over the list using a for-each loop // Print each value on a second line + for (String word : listExample) { + System.out.println(word); + } /* * Usage tip! From 640d96d86daf50443402c091c9ab042480238a6d Mon Sep 17 00:00:00 2001 From: xavierb <194048107+xavierb117@users.noreply.github.com> Date: Mon, 13 Jan 2025 19:57:11 -0800 Subject: [PATCH 3/7] Completed Map Practice --- src/MapPractice.java | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..0d96fa0 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,53 @@ - +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 mapExample = new HashMap<>(); + System.out.println(mapExample); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + mapExample.put("Twix", 2); + mapExample.put("Milky Way", 3); + mapExample.put("Crunch", 4); + System.out.println(mapExample); // Get the value associated with a given key in the Map + System.out.println(mapExample.get("Twix")); // Find the size (number of key/value pairs) of the Map + System.out.println(mapExample.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) + mapExample.put("Twix", 5); + System.out.println(mapExample); // Check whether the Map contains a given key + if (mapExample.containsKey("Twix")) { + System.out.println("Yes! It contains the key Twix"); + } // Check whether the Map contains a given value + if (mapExample.containsValue(5)) { + System.out.println("Yes! It contains the value 5"); + } // Iterate over the keys of the Map, printing each key + for (String key : mapExample.keySet()) { + System.out.println(key); + } // Iterate over the values of the map, printing each value + for (Integer value : mapExample.values()) { + System.out.println(value); + } // Iterate over the entries in the map, printing each key and value + for (Map.Entry entry : mapExample.entrySet()) { + System.out.println(entry.getKey() + ":" + entry.getValue()); + } /* * Usage tip! From 2ef9330b07706cc36832a62bea77fade9af00a53 Mon Sep 17 00:00:00 2001 From: xavierb <194048107+xavierb117@users.noreply.github.com> Date: Mon, 13 Jan 2025 20:11:24 -0800 Subject: [PATCH 4/7] Completed Number Practice --- src/NumberPractice.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..e1b9ef9 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +1,29 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float floatNum = -3.5f; + System.out.println(floatNum); // Create an int with a positive value and assign it to a variable + int intNum = 3; + System.out.println(intNum); // Use the modulo % operator to find the remainder when the int is divided by 3 + System.out.println("The remainder of the int is: " + 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. + if (intNum % 2 == 0) { + System.out.println("Even"); + } + else { + System.out.println("Odd"); + } // Divide the number by another number using integer division + System.out.println(intNum / 2); /* * Reminder! From cb14d87340637eac5bac708149d2e4822d6b2597 Mon Sep 17 00:00:00 2001 From: xavierb <194048107+xavierb117@users.noreply.github.com> Date: Mon, 13 Jan 2025 20:43:23 -0800 Subject: [PATCH 5/7] Completed Person Practice --- src/Person.java | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..9c3068b 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 personName; + private int personAge; // Create a constructor that takes the name and age of the person // and assigns it to the instance variables - + public Person(String personName, int personAge) { + this.personName = personName; + this.personAge = personAge; + } // Create a toString method that gives the name and age of the person - + @Override + public String toString() { + return "Name: " + personName + " Age: " + personAge; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,25 +35,36 @@ public class Person { * @return The year the person was born */ // (create the instance method here) - + public int birthYear(int currentYear) { + int birthYear = currentYear - personAge; + return birthYear; + } public static void main(String[] args) { // Create an instance of Person + Person xavier = new Person("Xavier", 20); // Create another instance of Person with a different name and age and // assign it to a different variable + Person sera = new Person("Sera", 22); // Print the first person + System.out.println(xavier); // Print the second person + System.out.println(sera); // Get the name of the first person and store it in a local variable + String firstName = xavier.personName; + System.out.println(firstName); // 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 = xavier.birthYear(2025); // In a separate statement, print the local variable holding the birth year. + System.out.println(firstBirthYear); /** * Terminology! From 408595456a2c78fcc4039d5e27da43da2cb65b07 Mon Sep 17 00:00:00 2001 From: xavierb <194048107+xavierb117@users.noreply.github.com> Date: Mon, 13 Jan 2025 20:59:20 -0800 Subject: [PATCH 6/7] Completed Set Practice --- src/SetPractice.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..3d6191d 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,34 @@ +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 + Set setExample = new HashSet<>(); + System.out.println(setExample); // Add 3 elements to the set // (It's OK to do it one-by-one) + setExample.add("Hello"); + setExample.add("Set"); + setExample.add("Practice"); + System.out.println(setExample); // Check whether the Set contains a given String + if (setExample.contains("Hello")) { + System.out.println("Yes! It does contain Hello"); + } // Remove an element from the Set + setExample.remove("Hello"); + System.out.println(setExample); // Get the size of the Set + System.out.println(setExample.size()); // Iterate over the elements of the Set, printing each one on a separate line + for (String word : setExample) { + System.out.println(word); + } /* * Warning! From bd40fa232f14612483f5362cf7583652c6edaea0 Mon Sep 17 00:00:00 2001 From: xavierb <194048107+xavierb117@users.noreply.github.com> Date: Mon, 13 Jan 2025 21:24:02 -0800 Subject: [PATCH 7/7] Completed String Practice --- src/StringPractice.java | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..f8f9fed 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 word = "Hello"; + System.out.println(word); + // Find the length of the string + System.out.println(word.length()); // Concatenate (add) two strings together and reassign the result + word = word + " World"; + System.out.println(word); // Find the value of the character at index 3 + System.out.println(word.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + if (word.contains("Hel")) { + System.out.println("Yes! The word contains the substring"); + } // Iterate over the characters of the string, printing each one on a separate line + for (int i = 0; i < word.length(); i++) { + System.out.println(word.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("Hello"); + stringList.add("String"); + stringList.add("Practice"); + 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 + System.out.println(String.join(",", stringList)); // Check whether two strings are equal + if (stringList.get(0).equals(stringList.get(1))) { + System.out.println("Yes they are the same!"); + } + else { + System.out.println("No they are different!"); + } /* * Reminder!