From af0706cc4af7fa7adbf269bfb499560b17a8e670 Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Sun, 5 Jan 2025 18:00:22 -0800 Subject: [PATCH 1/7] Followed the prompt and added code to complete this task. --- src/ArrayPractice.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..cdd53f1 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,34 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] array = 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 + array[0] = "Dog"; + array[1] = "Cat"; + array[2] = "Duck"; + array[3] = "Wombat"; // Get the value of the array at index 2 + String index2Value = array[2]; + System.out.println("Index 2 contains the word: " + index2Value); // Get the length of the array + int arrayLength = array.length; + System.out.println("Length of the array: " + arrayLength); // Iterate over the array using a traditional for loop and print out each item + System.out.println("FOR-LOOP:"); + for (int i = 0; i < array.length; i++) { + System.out.println(array[i]); + } // Iterate over the array using a for-each loop and print out each item - + System.out.println("FOR-EACH LOOP: "); + for (String word : array) { + System.out.println(word); + } /* * Reminder! * From e12ae4743aa33a83fd5558836f149a517a3a4224 Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Sun, 5 Jan 2025 18:33:38 -0800 Subject: [PATCH 2/7] Completed ListPractice and added refresher in toRefresh.md --- src/ListPractice.java | 23 +++++++++++++++++++++++ toRefresh.md | 8 +++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..ce3d04f 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,50 @@ + +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("Dog"); + list.add("Cat"); + list.add("Duck"); // Print the element at index 1 + System.out.println("Element at index 1 is: " + 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, "Wombat"); // Insert a new element at index 0 (the length of the list will change) + list.add(0, "Goat"); // Check whether the list contains a certain string + boolean containsCat = list.contains("Cat"); + System.out.println("Does the list contain the word 'Cat'? " + containsCat); //false since the word was replaced // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + System.out.println("FOR-LOOP:"); + for (int i = 0; i < list.size(); i++) { + System.out.println("Index " + i + ": " + 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 + System.out.println("FOR-EACH LOOP:"); + for (String word : list) { + System.out.println(word); + } /* * Usage tip! diff --git a/toRefresh.md b/toRefresh.md index 163dcab..859e17d 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,10 @@ 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 +ListPractice "https://www.geeksforgeeks.org/arraylist-set-method-in-java-with-examples/" +- List list = new ArrayList<>(); I always get confused how to set this up +- get() function access the element +- set() function that replaces a word in the list +- contains() https://www.geeksforgeeks.org/list-contains-method-in-java-with-examples/?ref=header_outind +- size() https://stackoverflow.com/questions/4438710/using-collection-size-in-for-loop-comparison +- Collections.sort() https://www.geeksforgeeks.org/collections-sort-java-examples/?ref=header_outind \ No newline at end of file From aa7f104574730a4ab440dbdb8909b1cf1eb521ae Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Sun, 5 Jan 2025 20:24:56 -0800 Subject: [PATCH 3/7] Completed NumberPractice and modified toRefresh.md file. --- src/NumberPractice.java | 11 +++++++++++ toRefresh.md | 6 ++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..90203f8 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +1,28 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float negativeValue = -8.2f; // Create an int with a positive value and assign it to a variable + int positiveValue = 10; // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = positiveValue % 3; + System.out.println("The remainder when divided by 3: " + remainder); // output 1 // 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 (positiveValue % 2 == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + } // Divide the number by another number using integer division + int number = positiveValue / 4; + System.out.println("Integer Division is: " + number); /* * Reminder! diff --git a/toRefresh.md b/toRefresh.md index 859e17d..50cd161 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,10 +2,12 @@ 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. -ListPractice "https://www.geeksforgeeks.org/arraylist-set-method-in-java-with-examples/" +ListPractice +"https://www.geeksforgeeks.org/arraylist-set-method-in-java-with-examples/" - List list = new ArrayList<>(); I always get confused how to set this up - get() function access the element - set() function that replaces a word in the list - contains() https://www.geeksforgeeks.org/list-contains-method-in-java-with-examples/?ref=header_outind - size() https://stackoverflow.com/questions/4438710/using-collection-size-in-for-loop-comparison -- Collections.sort() https://www.geeksforgeeks.org/collections-sort-java-examples/?ref=header_outind \ No newline at end of file +- Collections.sort() https://www.geeksforgeeks.org/collections-sort-java-examples/?ref=header_outind + From 4d8ee8199f90805dddd29109776532961b9cf746 Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Sun, 5 Jan 2025 21:39:36 -0800 Subject: [PATCH 4/7] Completed StringPractice and added resources in toRefresh.md --- src/StringPractice.java | 26 ++++++++++++++++++++++++++ toRefresh.md | 9 ++++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..d7caa8e 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,51 @@ + +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 = "Computer"; // Find the length of the string + int length = word.length(); + System.out.println("Length of this word is: " + length); // Concatenate (add) two strings together and reassign the result + String word2 = "Table"; + String addedWord = word + word2; + System.out.println("Concatenated Strings: " + addedWord); // Find the value of the character at index 3 + char findChar = word.charAt(3); + System.out.println("Character at index 3 is: " + findChar); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + boolean containsSubsting = word.contains("abc"); + System.out.println("Does it contain 'abc'? " + containsSubsting); //false // Iterate over the characters of the string, printing each one on a separate line + System.out.println("-- Prints each letter in every 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 strList = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + strList.add("Camera"); + strList.add("Pen"); + strList.add("Book"); // 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 allStrings = String.join(", ", strList); + System.out.println("Joined string: " + allStrings); // Check whether two strings are equal + String word3 = "Paper"; + boolean areEqual = word.equals(word3); + System.out.println("Are they equal? " + areEqual); /* * Reminder! diff --git a/toRefresh.md b/toRefresh.md index 50cd161..7c9c2ce 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -7,7 +7,10 @@ ListPractice - List list = new ArrayList<>(); I always get confused how to set this up - get() function access the element - set() function that replaces a word in the list -- contains() https://www.geeksforgeeks.org/list-contains-method-in-java-with-examples/?ref=header_outind -- size() https://stackoverflow.com/questions/4438710/using-collection-size-in-for-loop-comparison -- Collections.sort() https://www.geeksforgeeks.org/collections-sort-java-examples/?ref=header_outind +- contains(): https://www.geeksforgeeks.org/list-contains-method-in-java-with-examples/?ref=header_outind +- size(): https://stackoverflow.com/questions/4438710/using-collection-size-in-for-loop-comparison +- Collections.sort(): https://www.geeksforgeeks.org/collections-sort-java-examples/?ref=header_outind +StringPractice +- How to find the value of character at index by using chartAt(): "https://stackoverflow.com/questions/11229986/get-string-character-by-index" +- Joining all the strings by using join(): https://www.w3schools.com/java/ref_string_join.asp From 88cc7d18517dd0330e1f576e081bdb4b8ede1f5c Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Mon, 6 Jan 2025 08:42:31 -0800 Subject: [PATCH 5/7] Completed SetPractice and added few notes in toRefresh.md --- src/SetPractice.java | 17 +++++++++++++++++ toRefresh.md | 3 +++ 2 files changed, 20 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..a3582a8 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,34 @@ + +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 wordSet = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + wordSet.add("Good"); + wordSet.add("Morning"); + wordSet.add("Everyone"); // Check whether the Set contains a given String + String checkString = "Hello"; + boolean contains = wordSet.contains(checkString); + System.out.println("Does this set contains the word " + " '" + checkString + "'? " + contains); // Remove an element from the Set + wordSet.remove("Good"); // Get the size of the Set + int size = wordSet.size(); + System.out.println("Size of Set: " + size); // Iterate over the elements of the Set, printing each one on a separate line + System.out.println("Here are the elements in the set:"); + for (String word : wordSet) { + System.out.println(word); + } /* * Warning! diff --git a/toRefresh.md b/toRefresh.md index 7c9c2ce..9e38229 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -14,3 +14,6 @@ ListPractice StringPractice - How to find the value of character at index by using chartAt(): "https://stackoverflow.com/questions/11229986/get-string-character-by-index" - Joining all the strings by using join(): https://www.w3schools.com/java/ref_string_join.asp + +SetPractice +- remove(): https://www.w3schools.com/java/java_hashset.asp \ No newline at end of file From 676b769e75ddaf708222a20c536e5014c40a3e1c Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Mon, 6 Jan 2025 20:27:00 -0800 Subject: [PATCH 6/7] Completed MapPractice exercise and added a link that guided me to solve this problem. --- src/MapPractice.java | 30 +++++++++++++++++++++++++++++- toRefresh.md | 5 ++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..ef78377 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,56 @@ - +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 maps = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + maps.put("coin", 2); + maps.put("battery", 5); + maps.put("chair", 12); // Get the value associated with a given key in the Map + int batteryKey = maps.get("battery"); + System.out.println("Value for battery: " + batteryKey); // Find the size (number of key/value pairs) of the Map + int mapSize = maps.size(); + System.out.println("The Map size is: " + mapSize); // Replace the value associated with a given key (the size of the Map shoukld not change) + maps.put("battery", 10); // Check whether the Map contains a given key + String containsKey = "battery"; + System.out.println("Contains '" + containsKey + "' key? " + maps.containsKey(containsKey)); // Check whether the Map contains a given value + int containsValue = 4; + System.out.println("Contains '" + containsValue + "' value? " + maps.containsValue(containsValue)); // Iterate over the keys of the Map, printing each key + System.out.println("KEYS:"); + for (String key : maps.keySet()) { + System.out.println(key); + } // Iterate over the values of the map, printing each value + System.out.println("VALUES:"); + for (int value : maps.values()) { + System.out.println(value); + } // Iterate over the entries in the map, printing each key and value + System.out.println("Here are entries in the Map" + " (key and its value)"); + for (Map.Entry entry : maps.entrySet()) { + String key = entry.getKey(); + Integer value = entry.getValue(); + System.out.println(key + ": " + value); + } /* * Usage tip! diff --git a/toRefresh.md b/toRefresh.md index 9e38229..571ecda 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -16,4 +16,7 @@ StringPractice - Joining all the strings by using join(): https://www.w3schools.com/java/ref_string_join.asp SetPractice -- remove(): https://www.w3schools.com/java/java_hashset.asp \ No newline at end of file +- remove(): https://www.w3schools.com/java/java_hashset.asp + +MapPractice +- A good review for different methods - keySet, values, keys, boolean, and how to loop Map: https://www.w3schools.com/java/java_hashmap.asp \ No newline at end of file From 9486166ff6df0b43bb760e9482f15bd4be6a229c Mon Sep 17 00:00:00 2001 From: arcjun01 Date: Mon, 6 Jan 2025 20:55:12 -0800 Subject: [PATCH 7/7] Completed Person.java exercise and added a resource for review in toRefresh.md file. --- src/Person.java | 27 ++++++++++++++++++++++----- toRefresh.md | 5 ++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..57b3184 100644 --- a/src/Person.java +++ b/src/Person.java @@ -1,18 +1,25 @@ /* * In this file you will follow the comments' instructions to complete - * the Person class. + * the Person class. */ 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" @@ -20,7 +27,7 @@ public class Person { /** * birthYear returns the year the person was born. * - * The birth year is calculated by subtracting the person's age from currentYear + * The birth year is calculated by subtracting the person's age from currentYear * that's passed in as an int. It assumes that the person's birthday has already * passed this year. * @@ -28,25 +35,35 @@ 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 person1 = new Person("Bud", 13); // Create another instance of Person with a different name and age and // assign it to a different variable + Person person2 = new Person("Nelie", 45); // Print the first person + System.out.println(person1); // Print the second person + System.out.println(person2); // Get the name of the first person and store it in a local variable + String firstName = person1.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 currentYear = 2025; + int birthYear = person1.birthYear(currentYear); // In a separate statement, print the local variable holding the birth year. + System.out.println(firstName + "'s birth year is " + birthYear + "."); /** * Terminology! diff --git a/toRefresh.md b/toRefresh.md index 571ecda..6fd137e 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -19,4 +19,7 @@ SetPractice - remove(): https://www.w3schools.com/java/java_hashset.asp MapPractice -- A good review for different methods - keySet, values, keys, boolean, and how to loop Map: https://www.w3schools.com/java/java_hashmap.asp \ No newline at end of file +- A good review for different methods - keySet, values, keys, boolean, and how to loop Map: https://www.w3schools.com/java/java_hashmap.asp + +Person +- Almost forgot how to manually do this, there is also a shortcut that was shown in SDEV 217(?) before. Here is a good review: https://www.youtube.com/watch?v=VhErIOUHwuw \ No newline at end of file