From 28cc5ca9334e2e05dd7ab48aafca2fafb7953402 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Tue, 30 Sep 2025 11:54:58 -0700 Subject: [PATCH 01/13] Abcd --- src/ListPractice.java | 16 ++++++++++------ src/NumberPractice.java | 14 ++++++++++---- src/StringPractice.java | 6 ++++-- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..42985f8 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,20 +1,24 @@ +import java.util.ArrayList; 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 list = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) - +list.add("Apple"); +list.add("Banana"); +list.add("Cherry"); // Print the element at index 1 - - // Replace the element at index 1 with a new value +System.out.println(list.get(1)); + // Replace he 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, "Grapes"); // Insert a new element at index 0 (the length of the list will change) - +list.add(0, "Mango"); // Check whether the list contains a certain string - +list.contains("Pineapple"); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..874317c 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,24 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - +float negative_Float = -20; // Create an int with a positive value and assign it to a variable - +int positive_Int = 200; // Use the modulo % operator to find the remainder when the int is divided by 3 +int remainder = positive_Int % 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 (remainder == 0) { + System.out.println("Even"); +}else { + System.out.println("Odd"); +} // Divide the number by another number using integer division - +int num_Division = positive_Int / 7; +System.out.println(num_Division); /* * Reminder! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..d7727f0 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,9 +1,11 @@ public class StringPractice { public static void main(String[] args) { // Create a string with at least 5 characters and assign it to a variable - + String[] fruits = {"Apple", "Mango", "Pineapple", "Kiwi", "Watermelon"}; // Find the length of the string - + for ( String fruit : fruits) { + System.out.println("The length of " + fruit + " is " + fruit.length()); + } // Concatenate (add) two strings together and reassign the result // Find the value of the character at index 3 From e3e33f41e14dedc7be9fb2fd8cc198db4d013d55 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Wed, 1 Oct 2025 00:33:45 -0700 Subject: [PATCH 02/13] Done with List Practice --- src/ListPractice.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index 42985f8..a17d970 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,4 +1,5 @@ import java.util.ArrayList; +import java.util.Collections; public class ListPractice { @@ -21,12 +22,16 @@ public static void main(String[] args) { list.contains("Pineapple"); // 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 + ": " + 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 fruit : list) { + System.out.println(fruit); + } /* * Usage tip! * From 9c88bab27942d17bdb89e973d3d47d6f077c6e51 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Wed, 1 Oct 2025 18:30:10 -0700 Subject: [PATCH 03/13] Done with List practice, ArrayPractice, Set practice --- src/ArrayPractice.java | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..03ef391 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,25 @@ -public class ArrayPractice { +public class ArrayPractice { //DONE WITH FILE DELETE THIS PART public static void main(String[] args) { // Create an array of Strings of size 4 - + int [] numbers = 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 - + numbers[0] = 10; + numbers[1] = 20; + numbers[2] = 30; + numbers[3] = 40; // Get the value of the array at index 2 - + System.out.println("The value at index 2 is " + numbers[2]); // Get the length of the array - + System.out.println("The length of the array is " + numbers.length); // Iterate over the array using a traditional for loop and print out each item - + for (int i = 0; i < numbers.length; i++) { + System.out.println("The value at index " + i + " is " + numbers[i]); + } // Iterate over the array using a for-each loop and print out each item - + for (int i = 0; i < numbers.length; i++) { + System.out.println("The value at index " + i + " is " + numbers[i]); + } /* * Reminder! * From a095742a8f54174ea9e5f11d15ece795d87ec404 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Wed, 1 Oct 2025 18:31:30 -0700 Subject: [PATCH 04/13] Done with List practice --- src/ListPractice.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index a17d970..b3065a1 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,8 +1,9 @@ import java.util.ArrayList; import java.util.Collections; -public class ListPractice { +public class ListPractice { +//DONE WITH FILE DELETE THIS PART public static void main(String[] args) { // Create an empty ArrayList of Strings and assign it to a variable of type List ArrayList list = new ArrayList(); @@ -29,8 +30,10 @@ public static void main(String[] args) { Collections.sort(list); // Iterate over the list using a for-each loop // Print each value on a second line - for (String fruit : list) { - System.out.println(fruit); + for (int i = 0; i < list.size(); i++) { + System.out.println(list.get(i)); + } + } /* * Usage tip! @@ -42,4 +45,3 @@ public static void main(String[] args) { * index values a for-each loop is cleaner. */ } -} \ No newline at end of file From acf6e306bbfca3507b8c5eb1d6fcd4fda6d55890 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Wed, 1 Oct 2025 18:32:18 -0700 Subject: [PATCH 05/13] Done with Set practice --- src/SetPractice.java | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..ec52e52 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,33 @@ +import java.util.HashSet; +import java.util.Set; + public class SetPractice { public static void main(String[] args) { // Create a HashSet of Strings and assign it to a variable of type Set - + Set set = new HashSet(); // Add 3 elements to the set // (It's OK to do it one-by-one) + set.add("Apple"); + set.add("Banana"); + set.add("Cherry"); + // Check whether the Set contains a given String + if (set.contains("Banana")) { + System.out.println("The set contains Banana"); + } else { + System.out.println("The set does not contain Banana"); + } // Remove an element from the Set - + set.remove("Apple"); + // Get the size of the Set - + System.out.println("The size of the set is " + set.size()); // Iterate over the elements of the Set, printing each one on a separate line - + for (String item : set) { + System.out.println(item); + } /* * Warning! * From a181911030bc8cd765d1a7410c61c9c47659b8e5 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Wed, 1 Oct 2025 18:32:42 -0700 Subject: [PATCH 06/13] Done with String practice --- src/StringPractice.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index d7727f0..3827122 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -7,11 +7,20 @@ public static void main(String[] args) { System.out.println("The length of " + fruit + " is " + fruit.length()); } // Concatenate (add) two strings together and reassign the result - + String joinString = String.join(", ", fruits); + System.out.println(joinString); // Find the value of the character at index 3 - + String fruit = fruits[0]; + System.out.println("The character at indeAx 3 of " + fruit + " is " + fruit.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + for (String fruit : fruits) { + if (fruit.contains("abc")) { + System.out.println("The string contains abc"); + } else { + System.out.println("The string does not contain abc"); + } + } + // Iterate over the characters of the string, printing each one on a separate line // Create an ArrayList of Strings and assign it to a variable From 5dc3072e737b0b6abddbf55e8d0ba59f9c985395 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Wed, 1 Oct 2025 19:23:48 -0700 Subject: [PATCH 07/13] Almost done with MapPractice --- src/MapPractice.java | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..7e79304 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,23 +1,34 @@ - +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 map = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) - + map.put("Apple", 1); + map.put("Pineapple", 2); + map.put("Pear", 3); + // Get the value associated with a given key in the Map - + System.out.println("The value associated with Apple is " + map.get("Apple")); // 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) - + System.out.println("The size of the map is " + map.size()); + // Replace the value associated with a given key (the size of the Map should not change) + map.put("Apple", 5); // Check whether the Map contains a given key - + if (map.containsKey("Apple")) { + System.out.println("The map contains Apple"); + }else{ + System.out.println("The map does not contain Apple"); + } // Check whether the Map contains a given value - + if (map.containsValue(5)) { + System.out.println("The map contains the value of 5") + }else{ + System.out.println("The map does not contain the value of 5"); + } // Iterate over the keys of the Map, printing each key // Iterate over the values of the map, printing each value From 38ddc15ee37e12c0f7ad4ae04a9a93d4de7f7e86 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Wed, 1 Oct 2025 19:29:53 -0700 Subject: [PATCH 08/13] Done with MapPractice --- src/MapPractice.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7e79304..f7c74da 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -30,11 +30,17 @@ public static void main(String[] args) { System.out.println("The map does not contain the value of 5"); } // 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 (HashMap.Entry entry : map.entrySet()) { + System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()); + } /* * Usage tip! * From 619bd3bee8bc46447b32ad010746709e9c7e7d49 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Wed, 1 Oct 2025 19:54:56 -0700 Subject: [PATCH 09/13] Done with StringPractice --- src/StringPractice.java | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 3827122..c44ac98 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,3 +1,5 @@ +import java.util.ArrayList; //DONE WITH FILE DELETE THIS PART + public class StringPractice { public static void main(String[] args) { // Create a string with at least 5 characters and assign it to a variable @@ -11,7 +13,7 @@ public static void main(String[] args) { System.out.println(joinString); // Find the value of the character at index 3 String fruit = fruits[0]; - System.out.println("The character at indeAx 3 of " + fruit + " is " + fruit.charAt(3)); + System.out.println("The character at index 3 of " + fruit + " is " + fruit.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) for (String fruit : fruits) { if (fruit.contains("abc")) { @@ -22,16 +24,29 @@ public static void main(String[] args) { } // Iterate over the characters of the string, printing each one on a separate line - + for (String fruit : fruits) { + for (int i = 0; i < fruit.length(); i++) { + System.out.println(fruit.charAt(i)); + } +} // Create an ArrayList of Strings and assign it to a variable - + ArrayList List = new ArrayList(); // Add multiple strings to the List (OK to do one-by-one) - + List.add("Apple"); + List.add("Mango"); + List.add("Banana"); // 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(", ", List); + System.out.println(joinedString); // Check whether two strings are equal - + if (List.get(0).equals("Apple")) { + System.out.println("The first string is Apple"); + } else { + System.out.println("The first string is not Apple"); + } + /* * Reminder! * From bb9cf367cf08b913edf07a82e7237a7f8fbc5957 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Wed, 1 Oct 2025 20:38:03 -0700 Subject: [PATCH 10/13] Done with PersonJava --- src/Person.java | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..cfbb54b 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,16 +6,26 @@ 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" + public int birthYear(int currentYear) { + return currentYear - age; + } // There should NOT be any print statement in this method. /** * birthYear returns the year the person was born. @@ -28,26 +38,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 person1 = new Person("Alice", 30); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person person2 = new Person("Bob", 25); // 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 firstPersonName = 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 birthYear = person1.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println("Birth year of " + firstPersonName + " is " + birthYear); /** * Terminology! * From ee4a422a355c4ba5cce75b649b07394dbd9a9b2e Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Wed, 1 Oct 2025 21:45:19 -0700 Subject: [PATCH 11/13] Done with ListPractice --- src/ListPractice.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index b3065a1..2ef8c7e 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,9 +1,9 @@ -import java.util.ArrayList; +import java.util.ArrayList; //DONE WITH FILE DELETE THIS PART import java.util.Collections; public class ListPractice { -//DONE WITH FILE DELETE THIS PART + public static void main(String[] args) { // Create an empty ArrayList of Strings and assign it to a variable of type List ArrayList list = new ArrayList(); From 557ae96909b06761a23b4e945fb72715ee1723a1 Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Wed, 1 Oct 2025 21:45:55 -0700 Subject: [PATCH 12/13] Done with MapPractice --- src/MapPractice.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index f7c74da..5e24c03 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,4 +1,4 @@ -import java.util.HashMap; +import java.util.HashMap; //DONE WITH FILE DELETE THIS PART public class MapPractice { public static void main(String[] args) { From ed2ff9af2c29f56e63739c4fb546b0eb6223812f Mon Sep 17 00:00:00 2001 From: Fkarau <30944677+Karau1218@users.noreply.github.com> Date: Wed, 1 Oct 2025 23:42:57 -0700 Subject: [PATCH 13/13] All projects finalized --- src/ArrayPractice.java | 24 ++++++++++++------------ src/ListPractice.java | 2 +- src/MapPractice.java | 2 +- src/NumberPractice.java | 7 ++++--- src/Person.java | 2 +- src/SetPractice.java | 2 +- src/StringPractice.java | 2 +- 7 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index 03ef391..380ae7b 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,24 +1,24 @@ -public class ArrayPractice { //DONE WITH FILE DELETE THIS PART +public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - int [] numbers = new int[4]; + String [] wordStrings = 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 - numbers[0] = 10; - numbers[1] = 20; - numbers[2] = 30; - numbers[3] = 40; + wordStrings[0] = "10"; + wordStrings[1] = "20"; + wordStrings[2] = "30"; + wordStrings[3] = "40"; // Get the value of the array at index 2 - System.out.println("The value at index 2 is " + numbers[2]); + System.out.println("The value at index 2 is " + wordStrings[2]); // Get the length of the array - System.out.println("The length of the array is " + numbers.length); + System.out.println("The length of the array is " + wordStrings.length); // Iterate over the array using a traditional for loop and print out each item - for (int i = 0; i < numbers.length; i++) { - System.out.println("The value at index " + i + " is " + numbers[i]); + for (int i = 0; i < wordStrings.length; i++) { + System.out.println("The value at index " + i + " is " + wordStrings[i]); } // Iterate over the array using a for-each loop and print out each item - for (int i = 0; i < numbers.length; i++) { - System.out.println("The value at index " + i + " is " + numbers[i]); + for (String word : wordStrings) { + System.out.println("The value is " + word); } /* * Reminder! diff --git a/src/ListPractice.java b/src/ListPractice.java index 2ef8c7e..f5a1498 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,4 +1,4 @@ -import java.util.ArrayList; //DONE WITH FILE DELETE THIS PART +import java.util.ArrayList; import java.util.Collections; public class ListPractice { diff --git a/src/MapPractice.java b/src/MapPractice.java index 5e24c03..c34d902 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,4 +1,4 @@ -import java.util.HashMap; //DONE WITH FILE DELETE THIS PART +import java.util.HashMap; public class MapPractice { public static void main(String[] args) { diff --git a/src/NumberPractice.java b/src/NumberPractice.java index 874317c..c46a5f1 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,5 +1,5 @@ -public class NumberPractice { - public static void main(String args[]) { +public class NumberPractice { +public static void main(String args[]) { // Create a float with a negative value and assign it to a variable float negative_Float = -20; // Create an int with a positive value and assign it to a variable @@ -11,7 +11,8 @@ public static void main(String args[]) { // (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 (remainder == 0) { + int evenCheck = positive_Int % 2; +if (evenCheck == 0) { System.out.println("Even"); }else { System.out.println("Odd"); diff --git a/src/Person.java b/src/Person.java index cfbb54b..992f809 100644 --- a/src/Person.java +++ b/src/Person.java @@ -58,7 +58,7 @@ public static void main(String[] args) { // 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 = person1.birthYear(2025); + int birthYear = person1.birthYear(2025); // In a separate statement, print the local variable holding the birth year. System.out.println("Birth year of " + firstPersonName + " is " + birthYear); /** diff --git a/src/SetPractice.java b/src/SetPractice.java index ec52e52..67c3bb2 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,4 +1,4 @@ -import java.util.HashSet; +import java.util.HashSet; import java.util.Set; public class SetPractice { diff --git a/src/StringPractice.java b/src/StringPractice.java index c44ac98..7808da6 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,4 +1,4 @@ -import java.util.ArrayList; //DONE WITH FILE DELETE THIS PART +import java.util.ArrayList; public class StringPractice { public static void main(String[] args) {