From 5a2a97fe8801c0f0258fb4f3f2cd647ef81079bc Mon Sep 17 00:00:00 2001 From: JesseChum Date: Fri, 3 Jan 2025 13:30:26 -0800 Subject: [PATCH 1/5] Finished numberpractice and listpractice. commiting now as I forgot to do the same for my first file --- src/ListPractice.java | 33 +++++++++++++++++++++++++-------- src/NumberPractice.java | 20 +++++++++++++++++--- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..19d4d4b 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,45 @@ +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 list = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + list.add("Beans"); + list.add("Pineapple"); + list.add("onion"); // Print the element at index 1 - + list.get(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, "Orange"); // Insert a new element at index 0 (the length of the list will change) - + list.add(0, "apple"); // Check whether the list contains a certain string - + if(list.contains("onion")) { + System.out.println("Found"); + } else { + System.out.println("Not found"); + } // 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("Index: " + i + ", Value: " + list.get(i)); + } // Sort the list using the Collections library - + Collections.sort(list); // Iterate over the list using a for-each loop // Print each value on a second line - + for (String i : list){ + System.out.println(); + System.out.println(i); + } /* * Usage tip! * diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..77d873a 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,32 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float f = -1f; + System.out.println(f); // Create an int with a positive value and assign it to a variable + int num = 5; + System.out.println(num); // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = num % 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. - + int number = 6; + if (number % 2 == 0) { + System.out.println(number + " is even"); + // if the number is odd. + } else { + System.out.println(number + "is odd"); + } + // Divide the number by another number using integer division - + int x = 8; + int y = 4; + System.out.println(x/y); /* * Reminder! * From cb2f8b625490504ee11907758535d6c17b7d6e45 Mon Sep 17 00:00:00 2001 From: JesseChum Date: Fri, 3 Jan 2025 14:57:09 -0800 Subject: [PATCH 2/5] Finished array practice --- src/ArrayPractice.java | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..4fea025 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,27 @@ + + public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[] arr = 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 - + arr[0]="Donut"; + arr[1]="Cake"; + arr[2]="Cookie"; + arr[3]="Icecream"; // Get the value of the array at index 2 - + String valueAtIndex = arr[2]; // Get the length of the array - + int[] length = new int[4]; // Iterate over the array using a traditional for loop and print out each item - + for (int i = 0; i < arr.length; i++){ + System.out.println(i + " : " + arr[i]); + } // Iterate over the array using a for-each loop and print out each item - + for (String item : arr){ + System.out.println(item); + } /* * Reminder! * From 9d21a5ecc4231c7983a7368d7094db1e30dfeb3a Mon Sep 17 00:00:00 2001 From: JesseChum Date: Fri, 3 Jan 2025 16:14:52 -0800 Subject: [PATCH 3/5] Finished StringPractice.java --- src/StringPractice.java | 42 +++++++++++++++++++++++++++++++---------- toRefresh.md | 2 +- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..14ed419 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,48 @@ +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 txt = "Hotdogs"; // Find the length of the string - + System.out.println(txt.length()); // Concatenate (add) two strings together and reassign the result - + String toppingOne = "Stuffed"; + String toppingTwo = "Crust"; + toppingOne = toppingOne + toppingTwo; + System.out.println(toppingOne); // Find the value of the character at index 3 - + String findme = "Please find me here"; + System.out.println(findme.indexOf("find")); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + String substring = "find"; + if(findme.contains(substring)) { + System.out.println(substring); + } else { + System.out.println("There are no substring in " + substring ); + } // Iterate over the characters of the string, printing each one on a separate line - + for (int i = 0; i < findme.length(); i++){ + char ch = findme.charAt(i); + System.out.println(ch); + } // Create an ArrayList of Strings and assign it to a variable - + ArrayList bedroom = new ArrayList(); + // Add multiple strings to the List (OK to do one-by-one) - + bedroom.add("bed"); + bedroom.add("table"); + bedroom.add("lamp"); // 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(",", bedroom); + System.out.println(joinedString); // Check whether two strings are equal - + String f1 ="football"; + String f2 = "soccerball"; + String f3 ="football"; + System.out.println(f1.equals(f2)); + System.out.println(f1.equals(f3)); /* * Reminder! * diff --git a/toRefresh.md b/toRefresh.md index 163dcab..7b84e78 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,4 @@ 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 +- I need to work on specfically Arrays as I had a hard time with that practice. I seem to do just fine as I find it easier to do ArrayLists. From 4c49d739f4acdad59c129b729376cf14daa09c81 Mon Sep 17 00:00:00 2001 From: JesseChum Date: Sat, 4 Jan 2025 22:50:57 -0800 Subject: [PATCH 4/5] Finished Set and Map practice --- src/MapPractice.java | 30 +++++++++++++++++++----------- src/SetPractice.java | 19 +++++++++++++------ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..6919335 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,37 @@ - +import java.util.HashMap; 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 - + HashMap people = new HashMap(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) - + people.put("John", 52); + people.put("Sarah", 43); + people.put("Charles", 23); // Get the value associated with a given key in the Map - + people.get("Charles"); // Find the size (number of key/value pairs) of the Map - + people.size(); // Replace the value associated with a given key (the size of the Map shoukld not change) - + people.replace("john", 56); // Check whether the Map contains a given key - + people.containsKey("sarah"); // Check whether the Map contains a given value - + people.containsValue("43"); // Iterate over the keys of the Map, printing each key - + for (String key : people.keySet()){ + System.out.println(key); + } // Iterate over the values of the map, printing each value - + for (Integer value: people.values()) { + System.out.println(value); + } // Iterate over the entries in the map, printing each key and value - + for (String i : people.keySet()) { + System.out.println("key: " + i + " value: " + people.get(i)); + } /* * Usage tip! * diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..386a894 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,25 @@ +import java.util.HashSet; + public class SetPractice { public static void main(String[] args) { // Create a HashSet of Strings and assign it to a variable of type Set - + HashSet helicopters = new HashSet(); // Add 3 elements to the set // (It's OK to do it one-by-one) - + helicopters.add("Huey"); + helicopters.add("Mi24"); + helicopters.add("cobra"); // Check whether the Set contains a given String - + System.out.println("Does the set contain 'Mi24' ?" + helicopters.contains("Mi24")); // Remove an element from the Set - + helicopters.remove("Huey"); // Get the size of the Set - + int size = helicopters.size(); + System.out.println("The set size is: " + size); // Iterate over the elements of the Set, printing each one on a separate line - + for (String helicopter : helicopters){ + System.out.println(helicopter); + } /* * Warning! * From 2de2f757779a04f1c1d416b247c8ad9b2a54836d Mon Sep 17 00:00:00 2001 From: JesseChum Date: Mon, 6 Jan 2025 14:19:18 -0800 Subject: [PATCH 5/5] Finished Person and Set.java all running successfull --- src/Person.java | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..656689f 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,26 +34,29 @@ 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("Qin Bao", 32); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person person2 = new Person("Yellow Yao", 24); // 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 firstPersonName = 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 firstPersonBirthYear = person.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println("First Person's Name: " + firstPersonName); + System.out.println("First Person's birthyear: " + firstPersonBirthYear); /** * Terminology! *