From 4a552e107ef08c244b840b6f9a129e09ba729d0c Mon Sep 17 00:00:00 2001 From: HumaGitGud Date: Mon, 6 Jan 2025 19:10:32 -0800 Subject: [PATCH 1/3] Completed some of the exercises --- src/ArrayPractice.java | 19 ++++++++++++++++++- src/ListPractice.java | 23 +++++++++++++++++++++-- src/NumberPractice.java | 13 +++++++++++++ src/StringPractice.java | 21 ++++++++++++++++++++- 4 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..9b53da4 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,34 @@ +import java.util.Arrays; + public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + int[] nums = new int[4]; // Set the value of the array at each index to be a different String // It's OK to do this one-by-one + for (int i = 0; i < nums.length; i++) { + nums[i] = i+1; + } + System.out.println(Arrays.toString(nums)); // Get the value of the array at index 2 + System.out.println(nums[2]); // Get the length of the array - + System.out.println(nums.length); + System.out.println(); + // Iterate over the array using a traditional for loop and print out each item + for (int i = 0; i < nums.length; i++) { + System.out.println(nums[i]); + } + System.out.println(); // Iterate over the array using a for-each loop and print out each item + for (int i : nums) { + System.out.println(i); + } /* * Reminder! diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..2390e63 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,46 @@ -public class ListPractice { - +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 myList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) + myList.add("Hello"); + myList.add("Oi"); + myList.add("Bonjour"); // Print the element at index 1 + System.out.println(myList.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) + myList.set(1, "Hola"); + System.out.println(myList); // Insert a new element at index 0 (the length of the list will change) + myList.add(0, "Konichiwa"); + System.out.println(myList); // Check whether the list contains a certain string + System.out.println(myList.contains("Bonjour")); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for (int i = 0; i < myList.size(); i++) { + System.out.println(i + " - " + myList.get(i)); + } // Sort the list using the Collections library + Collections.sort(myList); + System.out.println(myList); // Iterate over the list using a for-each loop // Print each value on a second line + for (String string : myList) { + System.out.println(string + "\n"); + } /* * Usage tip! diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..7e90645 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +1,30 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + double negative = -1.2; + System.out.println(negative); // Create an int with a positive value and assign it to a variable + int positive = 2; + System.out.println(positive); // Use the modulo % operator to find the remainder when the int is divided by 3 + double modulo = 7 % 3; + System.out.println("Remainder is " + modulo); // 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 num = 4; + if (num % 2 == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + } // Divide the number by another number using integer division + System.out.println(num / positive); /* * Reminder! diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..6dbf374 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,44 @@ +import java.util.ArrayList; +import java.util.Arrays; + 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 = "Luke"; // Find the length of the string + System.out.println(name.length()); // Concatenate (add) two strings together and reassign the result + name += " Skywalker"; + 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("Sky")); // Iterate over the characters of the string, printing each one on a separate line + for (int i = 0; i < name.length(); i++) { + System.out.println(name.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable - + ArrayList list = new ArrayList<>(Arrays.asList("Strawberry", "Banana", "Grape")); + // Add multiple strings to the List (OK to do one-by-one) + list.add("Pineapple"); // 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 result = String.join(", ", list); + System.out.println(result); // Check whether two strings are equal + String a = "Lol"; + String b = "Lolo"; + System.out.println(a == b); /* * Reminder! From bb2f722bfe9283b8f6ed09a172d3a312e987fe23 Mon Sep 17 00:00:00 2001 From: HumaGitGud Date: Mon, 6 Jan 2025 19:36:50 -0800 Subject: [PATCH 2/3] Completed the rest of the exercises --- src/MapPractice.java | 23 +++++++++++++++++++++-- src/Person.java | 27 ++++++++++++++++++++++----- src/SetPractice.java | 12 ++++++++++++ 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..7c4164d 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,47 @@ - +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 + HashMap map = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + map.put("Red", 0); + map.put("Green", 1); + map.put("Blue", 2); // Get the value associated with a given key in the Map + System.out.println(map.get("Green")); // Find the size (number of key/value pairs) of the Map + System.out.println(map.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) + map.put("Red", -1); // Check whether the Map contains a given key + System.out.println(map.containsKey("Yellow")); // Check whether the Map contains a given value + System.out.println(map.containsValue(2)); // Iterate over the keys of the Map, printing each key + for (String key : map.keySet()) { + System.out.println(key); + } // Iterate over the values of the map, printing each value + for (Integer value : map.values()) { + System.out.println(value); + } // Iterate over the entries in the map, printing each key and value + for (Map.Entry entry : map.entrySet()) { + System.out.println("key: " + entry.getKey() + " - value: " + entry.getValue()); + } /* * Usage tip! diff --git a/src/Person.java b/src/Person.java index 8ab3f95..04f1a72 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 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 - + @Override + public String toString() { + return name + " - " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,26 +35,36 @@ public class Person { * @return The year the person was born */ // (create the instance method here) - + public int birthYear(int age, int currentYear) { + return currentYear - age; + } public static void main(String[] args) { // Create an instance of Person + Person me = new Person("Huma", 22); // Create another instance of Person with a different name and age and // assign it to a different variable + Person dude = new Person("The Dude", 20); // Print the first person + System.out.println(me); // Print the second person + System.out.println(dude); // Get the name of the first person and store it in a local variable + String name = me.name; + 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 = dude.birthYear(20, 2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(birthYear); + /** * Terminology! * diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..c30c835 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,29 @@ +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 set = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + set.add("Cat"); + set.add("Dog"); + set.add("Bird"); // Check whether the Set contains a given String + System.out.println(set.contains("Cat")); // Remove an element from the Set + set.remove("Bird"); // Get the size of the Set + System.out.println(set.size()); // Iterate over the elements of the Set, printing each one on a separate line + for (String string : set) { + System.out.println(string); + } /* * Warning! From 8d933894784b8e84c35ebf1724f67beb54cb0e5d Mon Sep 17 00:00:00 2001 From: HumaGitGud Date: Mon, 6 Jan 2025 19:39:28 -0800 Subject: [PATCH 3/3] Updated toRefresh.md file --- toRefresh.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/toRefresh.md b/toRefresh.md index 163dcab..77bc0a4 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,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. -- \ No newline at end of file +- Join strings --> String result = String.join(", ", list); +- Sorting lists --> Collections.sort(myList); \ No newline at end of file