From 4e6b8817c5c5728518522446b0f15f81873be520 Mon Sep 17 00:00:00 2001 From: rjfredr Date: Tue, 14 Jan 2025 10:22:43 -0800 Subject: [PATCH 1/7] commit 01.14.25 10:22am 1/7 --- src/ArrayPractice.java | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..40d8e0d 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,22 +1,13 @@ public class ArrayPractice { public static void main(String[] args) { - // Create an array of Strings of size 4 - - // Set the value of the array at each index to be a different String - // It's OK to do this one-by-one - - // Get the value of the array at index 2 - - // Get the length of the array - - // Iterate over the array using a traditional for loop and print out each item - - // Iterate over the array using a for-each loop and print out each item - - /* - * Reminder! - * - * Arrays start at index 0 - */ + String[] cueists = {"Efren","Francisco","Johann","AJ"}; + System.out.println(cueists[2]); + System.out.println(cueists.length); + for (int i=0; i < cueists.length; i++) { + System.out.println(cueists[i]); + } + for (String cueist : cueists) { + System.out.println(cueist); + } } } From 835ea83899bec02b61162091e03862172ceaeb56 Mon Sep 17 00:00:00 2001 From: rjfredr Date: Tue, 14 Jan 2025 18:20:15 -0800 Subject: [PATCH 2/7] 01.14.25 6:20pm 2/7 --- src/ListPractice.java | 55 ++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..da0acd8 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,36 +1,31 @@ +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 - - // Add 3 elements to the list (OK to do one-by-one) - - // Print the element at index 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) - - // Insert a new element at index 0 (the length of the list will change) - - // Check whether the list contains a certain string - - // Iterate over the list using a traditional for-loop. - // Print each index and value on a separate line - - // Sort the list using the Collections library - - // Iterate over the list using a for-each loop - // Print each value on a second line - - /* - * Usage tip! - * - * Use a traditional for-loop when you need to use the index or you need to iterate in an - * unconventional order (e.g. backwards) - * - * Otherwise, if you're iterating the in the conventional order and don't need the - * index values a for-each loop is cleaner. - */ + + ArrayList poolEquipment = new ArrayList(); + poolEquipment.add("Cue"); + poolEquipment.add("Pool Balls"); + poolEquipment.add("Chalk"); + System.err.println(poolEquipment); + + System.out.println(poolEquipment.get(1)); + poolEquipment.set(1,"Pool Table"); + System.err.println(poolEquipment); + poolEquipment.add(0, "Glove"); + System.err.println(poolEquipment); + + for (int i=0;i Date: Tue, 14 Jan 2025 18:38:53 -0800 Subject: [PATCH 3/7] 01.14.2025 6:38pm 3/7 --- src/ListPractice.java | 2 +- src/MapPractice.java | 64 ++++++++++++++++++------------------------- 2 files changed, 27 insertions(+), 39 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index da0acd8..d8775e9 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -5,7 +5,7 @@ public class ListPractice { public static void main(String[] args) { - ArrayList poolEquipment = new ArrayList(); + ArrayList poolEquipment = new ArrayList<>(); poolEquipment.add("Cue"); poolEquipment.add("Pool Balls"); poolEquipment.add("Chalk"); diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..adda533 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,43 +1,31 @@ - +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 - - // Put 3 different key/value pairs in the Map - // (it's OK to do this one-by-one) - - // Get the value associated with a given key in the Map - - // Find the size (number of key/value pairs) of the Map - - // Replace the value associated with a given key (the size of the Map shoukld not change) - - // Check whether the Map contains a given key - - // Check whether the Map contains a given value - - // Iterate over the keys of the Map, printing each key - - // Iterate over the values of the map, printing each value - - // Iterate over the entries in the map, printing each key and value - - /* - * Usage tip! - * - * Maps are great when you want a specific key to value mapping. - * Example: The key could be a person's name, and the value could be their phone number - * - * However if your keys are simple ascending 0-indexed integers with no gaps - * (0, 1, 2, 3, 4...) then an array or List is likely a better choice. - * Example: If you want to store the order of songs in a playlist. - * - * If you're finding that you're just wanting to store unordered values and the keys - * are unimportant, a Set may be a better choice. - * Example: If you want to hold the student ID numbers of everyone in a course, - * and you don't care about any ordering. - */ + HashMap contactID = new HashMap<>(); + + contactID.put("Johann",00); + contactID.put("Efren",01); + contactID.put("Francisco",02); + + System.out.println(contactID.get("Efren")); + + System.out.println(contactID.size()); + + contactID.put("Francisco",04); + System.out.println(contactID.get("Francisco")); + + System.out.println("Does the HashMap Contain Efren? " + contactID.containsKey("Efren")); + System.out.println("Does the HashMap Contain 05? " + contactID.containsValue(05)); + + for (String i : contactID.keySet()) { + System.out.println(i); + } + for (Integer k : contactID.values()) { + System.out.println(k); + } + for (String j : contactID.keySet()) { + System.out.println("Keys: " + j + " Values: " + contactID.get(j)); + } } } From 2d7738c69fed96d5688afe06c8d4ef5f76dbde2d Mon Sep 17 00:00:00 2001 From: rjfredr Date: Tue, 14 Jan 2025 18:45:10 -0800 Subject: [PATCH 4/7] 01.14.2025 6:45pm 4/7 --- src/NumberPractice.java | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..038cb98 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,25 +1,21 @@ + public class NumberPractice { public static void main(String args[]) { - // Create a float with a negative value and assign it to a variable - - // Create an int with a positive value and assign it to a variable - - // Use the modulo % operator to find the remainder when the int is divided by 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. + double numFloat = -1.2; + int numInt = 4; - // Divide the number by another number using integer division + System.out.println(numFloat); + System.out.println(numInt); - /* - * Reminder! - * - * When dividing ints, the result is rounded down. - * Example: - * 7 / 3 = 2 when performing int division - */ + System.out.println(numInt%2); + + if (numInt%2==0) { + System.out.println(numInt + " is even!"); + } + else { + System.out.println(numInt + " is negative!"); + } + System.out.println(numFloat/4); } } From b7076c9c7fe3299a82f8c242c1b49665cb22e988 Mon Sep 17 00:00:00 2001 From: rjfredr Date: Wed, 15 Jan 2025 23:23:26 -0800 Subject: [PATCH 5/7] 01.15.2025 11:23pm 5/7 --- src/SetPractice.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..7c26661 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,13 +1,21 @@ +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 + HashSet set = new HashSet(); + set.add("Philippines"); + set.add("USA"); + set.add("Canada"); - // Add 3 elements to the set - // (It's OK to do it one-by-one) + System.out.println("Contains Philippines? " + set.contains("Philippines")); + + set.remove("USA"); - // Check whether the Set contains a given String + System.out.println("Size of the HashSet: " + set.size()); - // Remove an element from the Set + for (String i : set) { + System.out.println(i); + } // Get the size of the Set From 83f7169adc54ce904905f704c6a86fc69a70dcb9 Mon Sep 17 00:00:00 2001 From: rjfredr Date: Wed, 15 Jan 2025 23:42:42 -0800 Subject: [PATCH 6/7] 01.15.2025 11:42pm 5/7 --- src/SetPractice.java | 2 +- src/StringPractice.java | 39 ++++++++++++++++++--------------------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index 7c26661..3f619fe 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -2,7 +2,7 @@ public class SetPractice { public static void main(String[] args) { - HashSet set = new HashSet(); + HashSet set = new HashSet<>(); set.add("Philippines"); set.add("USA"); set.add("Canada"); diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..75e3201 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,32 +1,29 @@ -public class StringPractice { - public static void main(String[] args) { - // Create a string with at least 5 characters and assign it to a variable - - // Find the length of the string - // Concatenate (add) two strings together and reassign the result +import java.util.Arrays; - // Find the value of the character at index 3 +public class StringPractice { + public static void main(String[] args) { + String word = "Billiards"; + System.out.println("Length of word: " + word.length()); - // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + String word2 = "Snooker"; - // Iterate over the characters of the string, printing each one on a separate line + System.out.println(word.concat(word2)); - // Create an ArrayList of Strings and assign it to a variable + System.out.println(word.charAt(3)); - // Add multiple strings to the List (OK to do one-by-one) + String subString = "abc"; + System.out.println("Does string contain: " + subString + "? " + word.contains(subString)); - // 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 + char[] charArray = word.toCharArray(); + for (int i = 0; i < charArray.length; i++) { + System.out.println(charArray[i]); + } - // Check whether two strings are equal + String[] wordList = {"Carom","Bank","Billiard","Kick","Masse"}; + String arrStr = Arrays.toString(wordList); + System.out.println(arrStr); - /* - * Reminder! - * - * When comparing objects in Java we typically want to use .equals, NOT ==. - * - * We use == when comparing primitives (e.g. int or char). - */ + System.out.println("Strings equal? " + word.equals(word2)); } } From 153c11bf2b7bd11e4d94df546990eafca0ea6d2c Mon Sep 17 00:00:00 2001 From: rjfredr Date: Wed, 15 Jan 2025 23:56:33 -0800 Subject: [PATCH 7/7] 01.15.2025 11:56pm 7/7git status --- src/Person.java | 57 ++++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..a14d617 100644 --- a/src/Person.java +++ b/src/Person.java @@ -1,20 +1,28 @@ -/* - * In this file you will follow the comments' instructions to complete - * 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 - - - // Create a constructor that takes the name and age of the person - // and assigns it to the instance variables + public String name; + private final int age; + + public Person(String name, int age) { + this.name = name; + this.age = age; + } + public String getName() { + return name; + } - // Create a toString method that gives the name and age of the person + public int getAge() { + return age; + } + public String string() { + return name + age; + } + public int birthyear(int currentyear) { + return currentyear - age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. /** @@ -31,22 +39,17 @@ public class Person { public static void main(String[] args) { - // Create an instance of Person - - // Create another instance of Person with a different name and age and - // assign it to a different variable - - // Print the first person - - // Print the second person - - // Get the name of the first person and store it in a local variable - - // 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. - - // In a separate statement, print the local variable holding the birth year. + Person person1 = new Person("RJ",20); + Person person2 = new Person("Yao",19); + + System.out.println(person1.string()); + System.out.println(person2.string()); + + String person1name = person1.getName(); + System.out.println(person1name); + + int byear = person1.birthyear(2025); + System.out.println(byear); /** * Terminology!