From 65a6427088c71e0c0dcf9540505037663dc3f50e Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 14:38:01 -0700 Subject: [PATCH 01/27] negative float --- src/NumberPractice.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..7c25984 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,6 +1,8 @@ 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.5f; + //System.out.println(negativeFloat); // Create an int with a positive value and assign it to a variable From 1c97eb2058904d5da539173b6843ffc89eac09c6 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 14:38:52 -0700 Subject: [PATCH 02/27] positive int --- src/NumberPractice.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index 7c25984..be52e61 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -5,6 +5,8 @@ public static void main(String args[]) { //System.out.println(negativeFloat); // Create an int with a positive value and assign it to a variable + int positiveInt = 5; + //System.out.println(positiveInt); // Use the modulo % operator to find the remainder when the int is divided by 3 From ccbf171625dd471aa155df1af23c94dcd452ca8f Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 14:39:49 -0700 Subject: [PATCH 03/27] modulo remainder --- src/NumberPractice.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index be52e61..5170ca6 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -9,6 +9,8 @@ public static void main(String args[]) { //System.out.println(positiveInt); // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = positiveInt % 3; + //System.out.println(remainder); // 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) From 7306a78f060730fe38f57149c444ac796874e81f Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 14:42:29 -0700 Subject: [PATCH 04/27] Even/Odd numbers. --- src/NumberPractice.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index 5170ca6..0aac4d9 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -16,6 +16,12 @@ 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. + int isEvenNumber = 1; + if(isEvenNumber % 2 == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + } // Divide the number by another number using integer division From 4a00923e30e0d5ff251aa1746dbf00000d8b107e Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 14:43:32 -0700 Subject: [PATCH 05/27] Dividing ints --- src/NumberPractice.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index 0aac4d9..aeb9912 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -24,6 +24,10 @@ public static void main(String args[]) { } // Divide the number by another number using integer division + int number1 = 20; + int number2 = 10; + int result = number1 / number2; + System.out.println(result); /* * Reminder! From 9ffe1cd5ef43ba8506ce3f6be9b19a64e9b74018 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 14:45:08 -0700 Subject: [PATCH 06/27] Make arraylist --- src/ListPractice.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..f78df42 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,8 +1,9 @@ +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 strings = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) From 636ebda63efe888e9ec33b128e826d26f330c78f Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 14:46:23 -0700 Subject: [PATCH 07/27] adding elements to list --- src/ListPractice.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index f78df42..6909b32 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -6,8 +6,12 @@ public static void main(String[] args) { ArrayList strings = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) + strings.add("Hey"); + strings.add("hey2"); + strings.add("whats up"); // Print the element at index 1 + System.out.println(strings.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) From 8f1efd304216643c3808d8781b16cdaca50551e0 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 14:48:07 -0700 Subject: [PATCH 08/27] setting at specific indexes --- src/ListPractice.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index 6909b32..88f51e9 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -15,8 +15,13 @@ public static void main(String[] args) { // Replace the element at index 1 with a new value // (Do not insert a new value. The length of the list should not change) + strings.set(1, "new value"); + System.out.println(strings.get(1)); // Insert a new element at index 0 (the length of the list will change) + strings.add(0, "even newer value"); + System.out.println(strings.get(0)); + // Check whether the list contains a certain string From b644faf2727942bd06e8c74a7cc7820c90eb9d63 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 14:50:19 -0700 Subject: [PATCH 09/27] Check for specific string --- src/ListPractice.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index 88f51e9..fee70cb 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -24,6 +24,11 @@ public static void main(String[] args) { // Check whether the list contains a certain string + if(strings.indexOf("new value") >= 0) { + System.out.println("Found."); + } else { + System.out.println("Not found."); + } // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line From 8e0d8ced8d09d324b824bc510244be18ba77231a Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 14:53:37 -0700 Subject: [PATCH 10/27] Iterate over list --- src/ListPractice.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index fee70cb..fa0ebce 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -32,6 +32,9 @@ public static void main(String[] args) { // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for(int i = 0; i < strings.size(); i++) { + System.out.println(i + strings.get(i)); + } // Sort the list using the Collections library From 6bbf3275c74f648280b1994f4f67f6718e981f85 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 20:54:09 -0700 Subject: [PATCH 11/27] sort & iterate foreach --- src/ListPractice.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index fa0ebce..cfe024e 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,4 +1,5 @@ import java.util.ArrayList; +import java.util.Collections; public class ListPractice { public static void main(String[] args) { @@ -37,9 +38,13 @@ public static void main(String[] args) { } // Sort the list using the Collections library + Collections.sort(strings); // Iterate over the list using a for-each loop // Print each value on a second line + for(String str : strings) { + System.out.println(str); + } /* * Usage tip! From cbdad62cb36f905239f86b4fb20f572f3f69503a Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 20:56:09 -0700 Subject: [PATCH 12/27] make new strings array --- src/ArrayPractice.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..d9be09a 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,6 +1,7 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] strings = 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 From da854224e0d66d00554c4201213e8b9ce535fca1 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 20:59:45 -0700 Subject: [PATCH 13/27] string values set and checked. --- src/ArrayPractice.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index d9be09a..71bb116 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -5,8 +5,13 @@ public static void main(String[] args) { // Set the value of the array at each index to be a different String // It's OK to do this one-by-one + strings[0] = "Hey"; + strings[1] = "Test"; + strings[2] = "Third"; + strings[3] = "Fourth"; // Get the value of the array at index 2 + System.out.println(strings[2]); // Get the length of the array From 5a6f52cce001a1b42850120a363d5c4d51f1d596 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:00:38 -0700 Subject: [PATCH 14/27] print array length --- src/ArrayPractice.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index 71bb116..b7cc525 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -14,6 +14,7 @@ public static void main(String[] args) { System.out.println(strings[2]); // Get the length of the array + System.out.println(strings.length); // Iterate over the array using a traditional for loop and print out each item From ca720b58041608b506ea3ea4895e5955bf0edb54 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:02:58 -0700 Subject: [PATCH 15/27] iterate array loops --- src/ArrayPractice.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index b7cc525..926e244 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -17,8 +17,14 @@ public static void main(String[] args) { System.out.println(strings.length); // Iterate over the array using a traditional for loop and print out each item + for(int i = 0; i < strings.length; i++) { + System.out.println(strings[i]); + } // Iterate over the array using a for-each loop and print out each item + for(String str : strings) { + System.out.println(str); + } /* * Reminder! From 3ca8cc645e22e83d64b37477738c39bfabd16bf1 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:05:26 -0700 Subject: [PATCH 16/27] string init & length --- src/StringPractice.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..00e6de9 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,8 +1,10 @@ public class StringPractice { public static void main(String[] args) { // Create a string with at least 5 characters and assign it to a variable + String fiveCharacterString = "Tests"; // Find the length of the string + System.out.println(fiveCharacterString.length()); // Concatenate (add) two strings together and reassign the result From 6ed3c5db220f5b57ec749081ad80fc1e7bf04df6 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:10:31 -0700 Subject: [PATCH 17/27] concat strings & find value of char --- src/StringPractice.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/StringPractice.java b/src/StringPractice.java index 00e6de9..6ab7f8e 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -7,8 +7,13 @@ public static void main(String[] args) { System.out.println(fiveCharacterString.length()); // Concatenate (add) two strings together and reassign the result + String stringOne = "String1"; + String stringTwo = "String2"; + stringOne = stringOne.concat(stringTwo); + System.out.println(stringOne); // Find the value of the character at index 3 + System.out.println(fiveCharacterString.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) From ecfc8db07e25df1510355e03b60a9cbef48ac081 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:16:50 -0700 Subject: [PATCH 18/27] check contains, check each char in string, new arraylist --- src/StringPractice.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/StringPractice.java b/src/StringPractice.java index 6ab7f8e..a3a8aa9 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,3 +1,5 @@ +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 @@ -16,10 +18,15 @@ public static void main(String[] args) { System.out.println(fiveCharacterString.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + System.out.println(fiveCharacterString.contains("abc")); // Iterate over the characters of the string, printing each one on a separate line + for (int i = 0; i < fiveCharacterString.length(); i++) { + System.out.print(fiveCharacterString.charAt(i) + " "); + } // Create an ArrayList of Strings and assign it to a variable + ArrayList arrayOfStrings = new ArrayList(); // Add multiple strings to the List (OK to do one-by-one) From fe71163867837fe49fdf98b869f8ca77dc426865 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:20:36 -0700 Subject: [PATCH 19/27] adding to array, tostring, checking equals. --- src/StringPractice.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index a3a8aa9..b8870af 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -22,18 +22,27 @@ public static void main(String[] args) { // Iterate over the characters of the string, printing each one on a separate line for (int i = 0; i < fiveCharacterString.length(); i++) { - System.out.print(fiveCharacterString.charAt(i) + " "); + System.out.println(fiveCharacterString.charAt(i) + " "); } // Create an ArrayList of Strings and assign it to a variable ArrayList arrayOfStrings = new ArrayList(); // Add multiple strings to the List (OK to do one-by-one) + arrayOfStrings.add("string1"); + arrayOfStrings.add("string2"); + arrayOfStrings.add("String3"); // 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 + System.out.println(arrayOfStrings.toString()); // Check whether two strings are equal + if(stringOne.equals(stringTwo)) { + System.out.println("equal"); + } else { + System.out.println("Not equal."); + } /* * Reminder! From 61bbd154d3bdac7d0be82bcbd61225b76bb43f8d Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:23:07 -0700 Subject: [PATCH 20/27] init hashset, add elements --- src/SetPractice.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..8a289b1 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,9 +1,15 @@ +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 hashStrings = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + hashStrings.add("test1"); + hashStrings.add("test2"); + hashStrings.add("test3"); // Check whether the Set contains a given String From a485bb2fb4f0b247ca87aa8e2ac5acb4be0aca56 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:28:13 -0700 Subject: [PATCH 21/27] hashset contains, remove, size --- src/SetPractice.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index 8a289b1..910ff2a 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -12,10 +12,13 @@ public static void main(String[] args) { hashStrings.add("test3"); // Check whether the Set contains a given String + System.out.println(hashStrings.contains("test1")); // Remove an element from the Set + hashStrings.remove("test3"); // Get the size of the Set + System.out.println(hashStrings.size()); // Iterate over the elements of the Set, printing each one on a separate line From edc69f42913f52c38007f4cdaec451f37e07b017 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:29:23 -0700 Subject: [PATCH 22/27] iterate over set --- src/SetPractice.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index 910ff2a..7d75e65 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -21,6 +21,9 @@ public static void main(String[] args) { System.out.println(hashStrings.size()); // Iterate over the elements of the Set, printing each one on a separate line + for(String str : hashStrings) { + System.out.println(str); + } /* * Warning! From fb033e8e74ff3b6553f9d569752a9418ae93aa23 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:31:49 -0700 Subject: [PATCH 23/27] init hashmap and fill --- src/MapPractice.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..aec247c 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,12 +1,16 @@ - +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 strIntHashMap = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + strIntHashMap.put("test1", 1); + strIntHashMap.put("test2", 2); + strIntHashMap.put("test3", 3); // Get the value associated with a given key in the Map From 2b271d5a10e9873a3f413e2f62ff87ed40457b0b Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:39:09 -0700 Subject: [PATCH 24/27] get value, size, replace, and check contains --- src/MapPractice.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/MapPractice.java b/src/MapPractice.java index aec247c..99916ec 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -13,14 +13,20 @@ public static void main(String[] args) { strIntHashMap.put("test3", 3); // Get the value associated with a given key in the Map + System.out.println(strIntHashMap.get("test2")); // Find the size (number of key/value pairs) of the Map + System.out.println(strIntHashMap.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) + strIntHashMap.replace("test2", 4); + System.out.println(strIntHashMap.size()); // Check whether the Map contains a given key + System.out.println(strIntHashMap.containsKey("test1")); // Check whether the Map contains a given value + System.out.println(strIntHashMap.containsValue(2)); // Iterate over the keys of the Map, printing each key From b59de3fe1eed2992ec6aa2d4acf2820a9501cc86 Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:45:08 -0700 Subject: [PATCH 25/27] iterating over hashmap --- src/MapPractice.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/MapPractice.java b/src/MapPractice.java index 99916ec..7afb85c 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -29,10 +29,19 @@ public static void main(String[] args) { System.out.println(strIntHashMap.containsValue(2)); // Iterate over the keys of the Map, printing each key + for(String key : strIntHashMap.keySet()) { + System.out.println(key); + } // Iterate over the values of the map, printing each value + for(Integer value : strIntHashMap.values()) { + System.out.println(value); + } // Iterate over the entries in the map, printing each key and value + for(HashMap.Entry entry : strIntHashMap.entrySet()) { + System.out.println(entry.getKey() + " - " + entry.getValue()); + } /* * Usage tip! From c44bd3bc0f8475b3a218dc35e72a7f1763adba9e Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:49:50 -0700 Subject: [PATCH 26/27] Person class impl --- src/Person.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..16a7f04 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,13 +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) { + age = _age; + name = _name; + } // Create a toString method that gives the name and age of the person + public String toString() { + return name + " - " + age; + } // Implement the below public instance method "birthYear" @@ -28,6 +37,10 @@ public class Person { * @return The year the person was born */ // (create the instance method here) + public int birthYear(int currentYear) { + int bornOn = currentYear - age; + return bornOn; + } public static void main(String[] args) { From 087de3e277278946a78c538ab6d406a5f8eee97d Mon Sep 17 00:00:00 2001 From: Darien Arthur-Gocken <233949874+DarienArthur-Gocken@users.noreply.github.com> Date: Thu, 25 Sep 2025 21:52:03 -0700 Subject: [PATCH 27/27] main impl --- src/Person.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Person.java b/src/Person.java index 16a7f04..1f6e26c 100644 --- a/src/Person.java +++ b/src/Person.java @@ -45,21 +45,28 @@ public int birthYear(int currentYear) { public static void main(String[] args) { // Create an instance of Person + Person person = new Person("Person1", 20); // Create another instance of Person with a different name and age and // assign it to a different variable + Person person2 = new Person("Person2", 25); // Print the first person + System.out.println(person); // Print the second person + System.out.println(person2); // Get the name of the first person and store it in a local variable + String firstPersonName = person.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 bornOn = person.birthYear(2025); // In a separate statement, print the local variable holding the birth year. + System.out.println(bornOn); /** * Terminology!