From 741155e651b260b17daf5b27a32ec40543227a56 Mon Sep 17 00:00:00 2001 From: kevinrsandoval <174657099+KevinSandoval12@users.noreply.github.com> Date: Wed, 1 Oct 2025 17:05:40 -0700 Subject: [PATCH 1/7] Finish NumberPractice java file --- src/NumberPractice.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..fb9f3dd 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,26 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + float negativeFloat = -1; + System.out.println(negativeFloat); // Create an int with a positive value and assign it to a variable - + int positiveInt = 12; + System.out.println(positiveInt); // Use the modulo % operator to find the remainder when the int is divided by 3 - + positiveInt = positiveInt % 3; + System.out.println(positiveInt); // 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 (positiveInt%2 == 0){ + System.out.println("true"); + } else { + System.out.println("false"); + } // Divide the number by another number using integer division - + positiveInt = positiveInt / 3; + System.out.println(positiveInt); /* * Reminder! * From f1651a37fb50d7d5cf307f2997ce769df1f66556 Mon Sep 17 00:00:00 2001 From: kevinrsandoval <174657099+KevinSandoval12@users.noreply.github.com> Date: Wed, 1 Oct 2025 17:24:40 -0700 Subject: [PATCH 2/7] Finished ListPractive.java --- src/ListPractice.java | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..43958e6 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,41 @@ +import java.util.ArrayList; +import java.util.List; +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 - - // Add 3 elements to the list (OK to do one-by-one) - + List list = new ArrayList<>(); + // Add 3 elements to the lisst (OK to do one-by-one) + list.add("hello"); + list.add("I'm"); + list.add("Kevin"); // Print the element at index 1 - + System.out.println(list.get(1)); // Replace the element at index 1 with a new value + list.set(1, "replaced"); + System.out.println(list); // (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) - + list.add(0,"hi"); + System.out.println(list); // Check whether the list contains a certain string - + System.out.println(list.contains("hi")); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + for (int i = 0; i Date: Wed, 1 Oct 2025 17:42:38 -0700 Subject: [PATCH 3/7] Finished ArrayPractice.java --- src/ArrayPractice.java | 22 +++++++++++++++++----- toRefresh.md | 2 +- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..9549efb 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,30 @@ +import java.lang.reflect.Array; + public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[] list = 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 + list[0] = "hola"; + list[1] = "mi"; + list[2] = "nombre"; + list[3] = "Kevin"; // Get the value of the array at index 2 - + System.out.println(list[2]); // Get the length of the array - + System.out.println(list.length); // Iterate over the array using a traditional for loop and print out each item - + for (int i = 0; i Date: Wed, 1 Oct 2025 18:00:09 -0700 Subject: [PATCH 4/7] Finished String Practice.java --- src/StringPractice.java | 31 +++++++++++++++++++++---------- toRefresh.md | 1 + 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..5107221 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,37 @@ +import java.util.ArrayList; +import java.util.List; + public class StringPractice { public static void main(String[] args) { // Create a string with at least 5 characters and assign it to a variable - + String str = "hello"; // Find the length of the string - + System.out.println(str.length()); // Concatenate (add) two strings together and reassign the result - + String newStr = str + "Kevin"; + System.out.println(newStr); // Find the value of the character at index 3 - + System.out.println(str.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + System.out.println(str.contains("abc")); // Iterate over the characters of the string, printing each one on a separate line - + for (int i = 0; i list = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) - + list.add("my"); + list.add("name"); + list.add("is"); + list.add("Kevin"); + System.out.println(list); // 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 combinedList = String.join(", ", list); + System.out.println(combinedList); // Check whether two strings are equal - + System.out.println(str.equals(combinedList)); /* * Reminder! * diff --git a/toRefresh.md b/toRefresh.md index fa32d95..889082b 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -3,3 +3,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. - how to insert at certain points in ArrayList +- .join() for string From c8bb7bc54209960f6d870b7e45d8ad2b457cab45 Mon Sep 17 00:00:00 2001 From: kevinrsandoval <174657099+KevinSandoval12@users.noreply.github.com> Date: Wed, 1 Oct 2025 18:04:16 -0700 Subject: [PATCH 5/7] Finished SetPractice.java --- src/SetPractice.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..acb13aa 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 setStr = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) - + setStr.add("hi"); + setStr.add("im"); + setStr.add("Kevin"); + System.out.println(setStr); // Check whether the Set contains a given String - + System.out.println(setStr.contains("hi")); // Remove an element from the Set - + setStr.remove("hi"); // Get the size of the Set - + System.out.println(setStr.size()); // Iterate over the elements of the Set, printing each one on a separate line - + for (String str : setStr) { + System.out.println(str); + } /* * Warning! * From 78f1602021d31e468bf2181ded3b3e200e5428bd Mon Sep 17 00:00:00 2001 From: kevinrsandoval <174657099+KevinSandoval12@users.noreply.github.com> Date: Wed, 1 Oct 2025 18:22:07 -0700 Subject: [PATCH 6/7] Finished MapPractice.java --- src/ArrayPractice.java | 1 - src/MapPractice.java | 34 +++++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index 9549efb..6763a26 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,4 +1,3 @@ -import java.lang.reflect.Array; public class ArrayPractice { public static void main(String[] args) { diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..c77fbc2 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,41 @@ - +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 - + Map mapStr = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) - + mapStr.put("one", 1); + mapStr.put("two", 2); + mapStr.put("three", 3); + System.out.println(mapStr); // Get the value associated with a given key in the Map - + System.out.println(mapStr.get("one")); // Find the size (number of key/value pairs) of the Map - + System.out.println(mapStr.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) - + mapStr.replace("one", 5); + System.out.println(mapStr); // Check whether the Map contains a given key - + System.out.println(mapStr.containsKey("one")); // Check whether the Map contains a given value - + System.out.println(mapStr.containsValue(5)); // Iterate over the keys of the Map, printing each key - + System.out.println(); + for (String key : mapStr.keySet()) { + System.out.println(key); + } // Iterate over the values of the map, printing each value - + for (Integer value : mapStr.values()) { + System.out.println(value); + } // Iterate over the entries in the map, printing each key and value - + for (Map.Entry entry : mapStr.entrySet()){ + System.out.println(entry.getKey() + " -> " + entry.getValue()); + } /* * Usage tip! * From 23a0328e073be28c2b70bf6bd9fd9e8278eb07ad Mon Sep 17 00:00:00 2001 From: kevinrsandoval <174657099+KevinSandoval12@users.noreply.github.com> Date: Wed, 1 Oct 2025 18:42:11 -0700 Subject: [PATCH 7/7] Finished Assignment --- src/Person.java | 31 +++++++++++++++++++++---------- toRefresh.md | 1 + 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..00c73cd 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,14 +6,22 @@ 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: " + name + + " Age: " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,26 +36,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("Gary", 2); // Create another instance of Person with a different name and age and // assign it to a different variable + Person person2 = new Person("Spongebob", 20); // 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 name = 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 birthYear1 = person1.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(birthYear1); /** * Terminology! * diff --git a/toRefresh.md b/toRefresh.md index 889082b..c7fdb50 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -4,3 +4,4 @@ As you work through this exercise, write down anything that you needed to look u - how to insert at certain points in ArrayList - .join() for string +- how to interate over maps and entries