From 578fab01ff19da3a0d22aead7f2274ceda12fcd2 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 18:06:00 -0700 Subject: [PATCH 01/31] Added float, int, and found 8/3 % 3 = 2. Addded print statements --- src/NumberPractice.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..a3502c8 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -21,5 +21,10 @@ public static void main(String args[]) { * 7 / 3 = 2 when performing int division */ + float neg = -7; + System.out.println(neg); + int num = 8; + System.out.println(num); + System.out.println(num / 3 % 3); } } From d4a774e63b84b2fee4aff2762a92610f93f17894 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 18:12:39 -0700 Subject: [PATCH 02/31] Added If statement to check for even number, If/Else statment for odd or even --- src/NumberPractice.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index a3502c8..4f27726 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -26,5 +26,17 @@ public static void main(String args[]) { int num = 8; System.out.println(num); System.out.println(num / 3 % 3); + + int even = 10; + if (even % 2 == 0){ + System.out.println("This number is even: " + even); + }; + + int n = 5; + if (n % 2 == 0){ + System.out.println("This number is even: " + n); + }else{ + System.out.println("This number is odd: " + n); + } } } From b4c108b434cebf88a67881a9cfe68a21717baeb9 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 18:14:16 -0700 Subject: [PATCH 03/31] Divided an int by another int --- src/NumberPractice.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index 4f27726..78a4e0f 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -37,6 +37,8 @@ public static void main(String args[]) { System.out.println("This number is even: " + n); }else{ System.out.println("This number is odd: " + n); - } + }; + + System.out.println(n / 2); } } From 4580fc41ab3810504aac7b1b1e5dc733b96d5891 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 18:20:08 -0700 Subject: [PATCH 04/31] Created new Arraylist with List variable type. Added 3 elements one by one --- src/ListPractice.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..2d29bcd 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,10 +1,18 @@ +import java.util.ArrayList; +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("First"); + list.add("Second"); + list.add("Third"); // Print the element at index 1 From 3d74a5d6a4ce9614d83f1da9fcc1a9e43187882d Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 18:26:58 -0700 Subject: [PATCH 05/31] Replaced index 1, and added new element to index 0 --- src/ListPractice.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index 2d29bcd..8dff280 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -15,11 +15,16 @@ public static void main(String[] args) { list.add("Third"); // Print the element at index 1 + System.out.println(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,"Replaced"); + System.out.println(list); // Insert a new element at index 0 (the length of the list will change) + list.add(0, "Added"); + System.out.println(list); // Check whether the list contains a certain string From dac66d784af460e174cec0f63a4d977b256f91aa Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 18:38:20 -0700 Subject: [PATCH 06/31] Checked for a certain string, and for loop to print each element with index on a new line --- src/ListPractice.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index 8dff280..910107f 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -27,9 +27,13 @@ public static void main(String[] args) { System.out.println(list); // Check whether the list contains a certain string + System.out.println(list.contains("Third")); // 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(i + ": " + list.get(i)); + } // Sort the list using the Collections library From 0675e0fb1734b8f9be5c665cf98c3aad95c67a4c Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 18:40:15 -0700 Subject: [PATCH 07/31] Added what i can brush up on --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index dda6106..3d50c2f 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,6 @@ Most of these taks should be easy, but there will likely be some that ask you to Make a Pull Request (PR) against the original repository. Copy the link into Canvas to submit. +## What i can work on: + +1. Brush up on For loops \ No newline at end of file From 9c077517fbf901ae8102e655ea3a4c9a2025f3c7 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 18:53:13 -0700 Subject: [PATCH 08/31] Sorted list, used for-each loop, and cleaned up code --- src/ListPractice.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index 910107f..1b55dee 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,4 +1,5 @@ import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class ListPractice { @@ -13,21 +14,23 @@ public static void main(String[] args) { list.add("First"); list.add("Second"); list.add("Third"); + System.out.println(list + "\n"); + // Print the element at index 1 - System.out.println(list.get(1)); + System.out.println(list.get(1) + "\n"); // 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,"Replaced"); - System.out.println(list); + System.out.println(list + "\n"); // Insert a new element at index 0 (the length of the list will change) list.add(0, "Added"); - System.out.println(list); + System.out.println(list + "\n"); // Check whether the list contains a certain string - System.out.println(list.contains("Third")); + System.out.println(list.contains("Third") + "\n"); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line @@ -36,9 +39,15 @@ public static void main(String[] args) { } // Sort the list using the Collections library + Collections.sort(list); + System.out.println("\n" + list + "\n"); + // Iterate over the list using a for-each loop // Print each value on a second line + for(String item : list){ + System.out.println(item + "\n"); + } /* * Usage tip! From cbc63dfd10810997893dc27f234263c0bae323dd Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 19:53:28 -0700 Subject: [PATCH 09/31] Added Array with 4 strings and then chnaged values --- src/ArrayPractice.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..a6f63f1 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,10 +1,18 @@ +import java.util.Arrays; + public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] ray = {"one", "two", "three", "four"}; + System.out.println(Arrays.toString(ray) + '\n'); // Set the value of the array at each index to be a different String // It's OK to do this one-by-one - + ray[0]= "1"; + ray[1]= "2"; + ray[2]= "3"; + ray[3]= "4"; + System.out.println(Arrays.toString(ray) + "\n"); // Get the value of the array at index 2 // Get the length of the array From 991e8c16bd8b077d22e2d673b31049e11eaf6afe Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 19:57:12 -0700 Subject: [PATCH 10/31] Printed index value 2 and length of array --- src/ArrayPractice.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index a6f63f1..b965c3a 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -14,9 +14,10 @@ public static void main(String[] args) { ray[3]= "4"; System.out.println(Arrays.toString(ray) + "\n"); // Get the value of the array at index 2 + System.out.println(ray[2] + "\n"); // Get the length of the array - + System.out.println(ray.length + "\n"); // 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 From 7efc01b87660c5e59e8ae7f3610395bb5b3b3de3 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 20:04:44 -0700 Subject: [PATCH 11/31] Printed tradional and for-each loop --- src/ArrayPractice.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index b965c3a..d7892d3 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -19,9 +19,14 @@ public static void main(String[] args) { // Get the length of the array System.out.println(ray.length + "\n"); // Iterate over the array using a traditional for loop and print out each item - + for(int i = 0; i < ray.length; i++){ + System.out.println(ray[i]); + } + System.out.println("\n"); // Iterate over the array using a for-each loop and print out each item - + for (String item: ray){ + System.out.println(item); + } /* * Reminder! * From 0ddbe29d48afe8487648b5e74da1966187e0ce0c Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 20:05:50 -0700 Subject: [PATCH 12/31] Added Arrays to work on list --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3d50c2f..440da9c 100644 --- a/README.md +++ b/README.md @@ -27,4 +27,5 @@ Make a Pull Request (PR) against the original repository. Copy the link into Can ## What i can work on: -1. Brush up on For loops \ No newline at end of file +1. For loops +2. Arrays From a0842cdeab87b5e4e68287dd6b02fc2033b7f025 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 20:08:01 -0700 Subject: [PATCH 13/31] Added a string with atleast 5 characters and found length --- src/StringPractice.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..2ab553b 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,9 +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 word = "Connor"; + System.out.println(word + "\n"); // Find the length of the string - + System.out.println(word.length() + "\n"); // Concatenate (add) two strings together and reassign the result // Find the value of the character at index 3 From 72736756be2b39700a157ebbce7f1ad243bfd3ea Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 20:11:32 -0700 Subject: [PATCH 14/31] Concat two strings and assign new variable and found char at index 3 --- src/StringPractice.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 2ab553b..be89420 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -6,8 +6,12 @@ public static void main(String[] args) { // Find the length of the string System.out.println(word.length() + "\n"); // Concatenate (add) two strings together and reassign the result - + String fName = "Connor"; + String lName = "Hughes"; + String fullName = fName + " " + lName; + System.out.println(fullName + "\n"); // Find the value of the character at index 3 + System.out.println(word.charAt(3) + "\n"); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) From 3631cf1bfdd6551c8a649e325cde8dea078e1022 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 20:15:40 -0700 Subject: [PATCH 15/31] Checked if string conatined a certain char and loop/ print each char --- src/StringPractice.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/StringPractice.java b/src/StringPractice.java index be89420..460f05c 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -14,8 +14,12 @@ public static void main(String[] args) { System.out.println(word.charAt(3) + "\n"); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + System.out.println(word.contains("a") + "\n"); // Iterate over the characters of the string, printing each one on a separate 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 From c4d48b3c6ac349d93c9f11f69468fe60f1974b3d Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 20:19:38 -0700 Subject: [PATCH 16/31] Created Arraylist and added 5 string elements --- src/StringPractice.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 460f05c..9b0d6f3 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,3 +1,6 @@ +import java.util.ArrayList; +import java.util.Arrays; + public class StringPractice { public static void main(String[] args) { // Create a string with at least 5 characters and assign it to a variable @@ -20,10 +23,16 @@ public static void main(String[] args) { for(int i =0; i < word.length(); i++){ System.out.println(word.charAt(i)); } - + System.out.println("\n"); // Create an ArrayList of Strings and assign it to a variable - + ArrayList ray = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + ray.add("car"); + ray.add("dog"); + ray.add("cat"); + ray.add("red"); + ray.add("blue"); + System.out.println(ray + "\n"); // 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 From 85722f3aaba151fc95dd61bcb629a296c8be93a2 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 20:33:09 -0700 Subject: [PATCH 17/31] Joined Strings together with commas and check for equal strings --- src/StringPractice.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/StringPractice.java b/src/StringPractice.java index 9b0d6f3..e42182a 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -36,8 +36,13 @@ public static void main(String[] args) { // 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 concat = String.join(", ", ray); + System.out.println(concat + "\n"); // Check whether two strings are equal + String one = "racecar"; + String two = "Truck"; + System.out.println(one.equals(two)); /* * Reminder! From a9f92ffc4255265ea7d58fe8defb055afd6cac8a Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 20:34:43 -0700 Subject: [PATCH 18/31] Added item to work on list --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 440da9c..2622a6e 100644 --- a/README.md +++ b/README.md @@ -29,3 +29,4 @@ Make a Pull Request (PR) against the original repository. Copy the link into Can 1. For loops 2. Arrays +3. Built in methods \ No newline at end of file From a6b51473501e4702afd941bc8e9f3b2a9ee0f44d Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 20:35:32 -0700 Subject: [PATCH 19/31] Commented out util --- src/StringPractice.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index e42182a..a3a3090 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,5 +1,5 @@ import java.util.ArrayList; -import java.util.Arrays; +// import java.util.Arrays; public class StringPractice { public static void main(String[] args) { From 62d885c120680282b71dc4fe0b34dab13b0c349d Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 20:41:12 -0700 Subject: [PATCH 20/31] Created new HashSet and added 5 elements --- src/SetPractice.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..eea668a 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,9 +1,20 @@ +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 hs = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + hs.add("green"); + hs.add("blue"); + hs.add("red"); + hs.add("yellow"); + hs.add("pink"); + + System.out.println(hs +"\n"); // Check whether the Set contains a given String From fb1f764e5670de8356d2fc7bdc4dd094550fddd3 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 20:42:57 -0700 Subject: [PATCH 21/31] Check if set contained a string and removed element --- src/SetPractice.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index eea668a..a5dd01d 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -17,8 +17,11 @@ public static void main(String[] args) { System.out.println(hs +"\n"); // Check whether the Set contains a given String + System.out.println(hs.contains("red") + "\n"); // Remove an element from the Set + hs.remove("pink"); + System.out.println(hs + "\n"); // Get the size of the Set From ce3fbc515e1e03649b54f6833d9de2b663c54ac3 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 20:46:31 -0700 Subject: [PATCH 22/31] Get size of Hashset and loop/ print each item --- src/SetPractice.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index a5dd01d..53df8aa 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -24,8 +24,13 @@ public static void main(String[] args) { System.out.println(hs + "\n"); // Get the size of the Set + System.out.println(hs.size() + "\n"); // Iterate over the elements of the Set, printing each one on a separate line + for(String item : hs){ + System.out.println(item); + } + System.out.println("\n"); /* * Warning! From f99beb0bf34cf7abf1af79d4929018f7b1683906 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 20:52:05 -0700 Subject: [PATCH 23/31] Created new Hashmap and added 3 keys/ values --- src/MapPractice.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..6530c10 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,13 +1,19 @@ - +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("Tacos", 5); + map.put("Burgers", 8); + map.put("Cheesecake", 10); + System.out.println(map + "\n"); + // Get the value associated with a given key in the Map // Find the size (number of key/value pairs) of the Map From 7a97a555b5c084bb94602438534e714d87c272ec Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 20:54:49 -0700 Subject: [PATCH 24/31] Got value of index 2 and size of Hashmap --- src/MapPractice.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 6530c10..b819ff7 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -13,10 +13,12 @@ public static void main(String[] args) { map.put("Burgers", 8); map.put("Cheesecake", 10); System.out.println(map + "\n"); - + // Get the value associated with a given key in the Map + System.out.println(map.get("Tacos") + '\n'); // Find the size (number of key/value pairs) of the Map + System.out.println(map.size() + "\n"); // Replace the value associated with a given key (the size of the Map shoukld not change) From db740a0845d8f407b883f9b697369a7bf3bd0f1f Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 21:03:03 -0700 Subject: [PATCH 25/31] Replaced value and checked if map contains a certain key --- src/MapPractice.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/MapPractice.java b/src/MapPractice.java index b819ff7..9870123 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -21,8 +21,11 @@ public static void main(String[] args) { System.out.println(map.size() + "\n"); // Replace the value associated with a given key (the size of the Map shoukld not change) + map.replace("Tacos", 5, 7); + System.out.println(map + "\n"); // Check whether the Map contains a given key + System.out.println(map.containsKey("Tacos") + "\n"); // Check whether the Map contains a given value From aceeaa9f25ff1ff05496f81acc05367d5ecf76c7 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 21:13:53 -0700 Subject: [PATCH 26/31] Checked for certain value. Loop key, value and key/value and print --- src/MapPractice.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 9870123..c3f7834 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -28,12 +28,21 @@ public static void main(String[] args) { System.out.println(map.containsKey("Tacos") + "\n"); // Check whether the Map contains a given value + System.out.println(map.containsValue(9) + "\n"); // Iterate over the keys of the Map, printing each key - + for(String key : map.keySet()){ + System.out.println(key); + } + System.out.println("\n"); // Iterate over the values of the map, printing each value + for(int value : map.values()){ + System.out.println(value); + } + System.out.println("\n"); // Iterate over the entries in the map, printing each key and value + map.forEach((key, value) -> {System.out.println(key + ", " + value);}); /* * Usage tip! From aa3472e4cad81ff58c09f47cb3e40b4543de20b3 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 21:22:24 -0700 Subject: [PATCH 27/31] Created public string instance, private int instance and Person constructor --- src/Person.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..a53d68d 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,10 +6,16 @@ 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 = "Tom"; + private int age = 32; // 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){ + String personName = name; + int personAge = age; + } // Create a toString method that gives the name and age of the person From 5506c9e6a2638d147bfa998df89c9f5334d5e50a Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 21:33:40 -0700 Subject: [PATCH 28/31] Created toString method and birthyear instance method --- src/Person.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Person.java b/src/Person.java index a53d68d..468fa00 100644 --- a/src/Person.java +++ b/src/Person.java @@ -8,17 +8,21 @@ public class Person { // Declare a private int instance variable for the age of the person public String name = "Tom"; private int age = 32; + // 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){ - String personName = name; - int personAge = age; + this.name = name; + this.age = age; } // Create a toString method that gives the name and age of the person + public String toString(String name, int age){ + return "This person's name is " + name + " and their age is " + age; + } // Implement the below public instance method "birthYear" @@ -34,7 +38,9 @@ public Person(String name, int age){ * @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 From 6492a75cd8a057dcaecee761791cd72bcbc70265 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 21:42:03 -0700 Subject: [PATCH 29/31] Edited constructor, created two people using constructor --- src/Person.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Person.java b/src/Person.java index 468fa00..ce44c5f 100644 --- a/src/Person.java +++ b/src/Person.java @@ -13,9 +13,9 @@ public class Person { // 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; + public Person(String name, int age){ + String pName = name; + int pAge = age; } @@ -44,9 +44,10 @@ public int birthYear( int currentyear){ public static void main(String[] args) { // Create an instance of Person - + Person tom = new Person("Tom", 32); // Create another instance of Person with a different name and age and // assign it to a different variable + Person kari = new Person("Kari", 31); // Print the first person From 17dda16f667cff253cf834df7325eecf9d9ec197 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 22:00:31 -0700 Subject: [PATCH 30/31] Fixed variables, toString and printed two different people --- src/Person.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Person.java b/src/Person.java index ce44c5f..a27c1fa 100644 --- a/src/Person.java +++ b/src/Person.java @@ -8,19 +8,21 @@ public class Person { // Declare a private int instance variable for the age of the person public String name = "Tom"; private int age = 32; + // 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){ - String pName = name; - int pAge = age; + this.name = name; + this.age = age; + } // Create a toString method that gives the name and age of the person - public String toString(String name, int age){ + public String toString(){ return "This person's name is " + name + " and their age is " + age; } @@ -44,15 +46,15 @@ public int birthYear( int currentyear){ public static void main(String[] args) { // Create an instance of Person - Person tom = new Person("Tom", 32); + Person tom = new Person("Tom", 32); // Create another instance of Person with a different name and age and // assign it to a different variable Person kari = new Person("Kari", 31); // Print the first person - + System.out.println(tom); // Print the second person - + System.out.println(kari); // 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 From a41751c3896f0571342cccde9f596db6cf8302f6 Mon Sep 17 00:00:00 2001 From: Connor Date: Wed, 1 Oct 2025 22:07:55 -0700 Subject: [PATCH 31/31] Stored newName/birthyear in variable and printed --- src/Person.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Person.java b/src/Person.java index a27c1fa..bdf8124 100644 --- a/src/Person.java +++ b/src/Person.java @@ -41,7 +41,7 @@ public String toString(){ */ // (create the instance method here) public int birthYear( int currentyear){ - return currentyear - age; + return currentyear - age; } public static void main(String[] args) { @@ -56,13 +56,15 @@ public static void main(String[] args) { // Print the second person System.out.println(kari); // Get the name of the first person and store it in a local variable + String newName = tom.name; + System.out.println(newName); // 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 = tom.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(birthYear); /** * Terminology! *