From 4aacbd617cf4a04c4dbefe993d9e2e887d0d346d Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 7 Jan 2025 16:00:49 -0800 Subject: [PATCH 1/5] Fixed to my repo TDW --- src/ArrayPractice.java | 28 +++++++++++++++++++------ src/ListPractice.java | 46 +++++++++++++++++++++++++++++------------- src/MapPractice.java | 35 +++++++++++++++++--------------- 3 files changed, 73 insertions(+), 36 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..f291d6e 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,22 +1,38 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] arrayOfStrings = {"None", "None", "None", "None"}; // Set the value of the array at each index to be a different String + arrayOfStrings[0] = "I"; + arrayOfStrings[1] = "Was"; + arrayOfStrings[2] = "In"; + arrayOfStrings[3] = "Class"; + // It's OK to do this one-by-one // Get the value of the array at index 2 + System.out.println(arrayOfStrings[2]); // Get the length of the array - + int item = arrayOfStrings.length; + System.out.println(item); // Iterate over the array using a traditional for loop and print out each item + for(int i = 0; i < item; i++) + { + System.out.println(arrayOfStrings[i]); + } + // Iterate over the array using a for-each loop and print out each item + for (String Printing : arrayOfStrings) + { + System.out.println(Printing); + } /* - * Reminder! - * - * Arrays start at index 0 - */ - } + +Reminder! +Arrays start at index 0*/ } +} \ No newline at end of file diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..5609fbd 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,36 +1,54 @@ +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 EmptyList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + EmptyList.add("Hello"); + EmptyList.add("GoodBye"); + EmptyList.add("See you around"); // Print the element at index 1 - + System.out.println(EmptyList.get(2)); // Replace the element at index 1 with a new value + EmptyList.set(1, "GoodMorning"); // (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) - + EmptyList.add(0, "Bonjour"); + System.out.println(EmptyList); // Check whether the list contains a certain string - + System.out.println(EmptyList.contains("Bonjour")); // Iterate over the list using a traditional for-loop. + for(int i = 0; i < EmptyList.size(); i++) + { + System.out.println(EmptyList); + } // Print each index and value on a separate line + for(int i = 0; i < EmptyList.size(); i++) + { + System.out.println(i + " " + EmptyList.get(i)); + } // Sort the list using the Collections library + EmptyList.sort(null); + System.out.println(EmptyList); + // Iterate over the list using a for-each loop + for (String IterateOver : EmptyList) + { + System.out.println(IterateOver); + } // 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. - */ - } + +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.*/ +} } \ No newline at end of file diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..a11ccfa 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,15 +1,21 @@ - +import java.util.HashMap; +import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; 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 workingMap = new HashMap<>(); // Put 3 different key/value pairs in the Map + workingMap.put("Words", 10); + workingMap.put("fun", 20); + workingMap.put("excited", 30); // (it's OK to do this one-by-one) // Get the value associated with a given key in the Map + System.out.println(workingMap.get("fun")); // 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) @@ -25,19 +31,16 @@ public static void main(String[] args) { // 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. - */ - } + +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.*/ } +} \ No newline at end of file From 7cc1bdaa84e96ad34828eca041f2d5e5e0301483 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 7 Jan 2025 18:17:14 -0800 Subject: [PATCH 2/5] Saving for practice and just incase --- src/MapPractice.java | 22 +++++++++++++++++++-- src/NumberPractice.java | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index a11ccfa..84ee020 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -13,23 +13,41 @@ public static void main(String[] args) { workingMap.put("excited", 30); // (it's OK to do this one-by-one) + System.out.println(workingMap); + // Get the value associated with a given key in the Map System.out.println(workingMap.get("fun")); // Find the size (number of key/value pairs) of the Map + System.out.println(workingMap.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) - + workingMap.replace("fun", 15); + System.out.println(workingMap); // Check whether the Map contains a given key + System.out.println(workingMap.containsKey("fun")); // Check whether the Map contains a given value + System.out.println(workingMap.containsValue(30)); // Iterate over the keys of the Map, printing each key + for(int i = 0; i < workingMap.size(); i++) + { + System.out.println(workingMap.keySet()); + } // Iterate over the values of the map, printing each value - + + for(int i = 0; i < workingMap.size(); i++) + { + System.out.println(workingMap.values()); + } // Iterate over the entries in the map, printing each key and value + for(int i = 0; i < workingMap.size(); i++) + { + System.out.println(workingMap); + } /* Usage tip! diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..90e934e 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -2,17 +2,59 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float negativeValue = -1.0f; + + float applyNegative = negativeValue; + + System.out.println(applyNegative); + // Create an int with a positive value and assign it to a variable + int positiveValue = 10; + + int applyPostive = positiveValue; + + System.out.println(applyPostive); + // Use the modulo % operator to find the remainder when the int is divided by 3 + int equation = 7 % 2; + + System.out.println(equation); + // 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) + + int value = 100; + + if(value % 2 == 0) + { + System.out.println("Even"); + } + // Use an if-else to print "Even" if the number is even and "Odd" // if the number is odd. + for(int i = 0; i < 100; i++) + { + if(i % 2 == 0) + { + System.out.println("Even" + "" + i); + } + else if(i % 3 == 0) + { + System.out.println("Odd" + "" + i); + } + } + + // Divide the number by another number using integer division + int numberValue = 5; + int divideValue = 10; + + System.out.println(divideValue / numberValue); + /* * Reminder! * From 761a32011eab0e2be4578eb86942a01278e1de26 Mon Sep 17 00:00:00 2001 From: tim Date: Tue, 7 Jan 2025 20:43:32 -0800 Subject: [PATCH 3/5] Finished most of work one file to go --- src/SetPractice.java | 18 ++++++++++++++++++ src/StringPractice.java | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..d812612 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,35 @@ +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 hashSetValue = new HashSet(); // Add 3 elements to the set // (It's OK to do it one-by-one) + hashSetValue.add("red"); + hashSetValue.add("blue"); + hashSetValue.add("green"); // Check whether the Set contains a given String + System.out.println(hashSetValue.contains("red")); + // Remove an element from the Set + System.out.println(hashSetValue); + hashSetValue.remove("red"); + System.out.println(hashSetValue); + // Get the size of the Set + System.out.println(hashSetValue.size()); + // Iterate over the elements of the Set, printing each one on a separate line + for (String iterate : hashSetValue) + { + System.out.println(iterate); + } /* * Warning! diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..d8de4d8 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,51 @@ +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 + String letters = "Hello World"; + String AssignString = letters; + + System.out.println(AssignString); // Find the length of the string + System.out.println(AssignString.length()); + // Concatenate (add) two strings together and reassign the result + String Value1 = "hel"; + String Value2 = "lo"; + + System.out.println(Value1 + Value2); // Find the value of the character at index 3 + System.out.println(AssignString.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + System.out.println(AssignString.contains("Hell")); // Iterate over the characters of the string, printing each one on a separate line + for(int i = 0; i < AssignString.length(); i++) + { + System.out.println(AssignString.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable + ArrayList ValueList = new ArrayList(); // Add multiple strings to the List (OK to do one-by-one) + ValueList.add("Blue"); + ValueList.add("Red"); + ValueList.add("Green"); + ValueList.add("Yellow"); // 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 storeValues = String.join(",", ValueList); + System.out.println(storeValues); // Check whether two strings are equal + System.out.println(Value1.equals(Value2)); /* * Reminder! From a2a3c5794030c00fa3038a41422e66323357b4c3 Mon Sep 17 00:00:00 2001 From: tim Date: Thu, 9 Jan 2025 09:03:07 -0800 Subject: [PATCH 4/5] Finished All --- src/MapPractice.java | 1 - src/Person.java | 26 +++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 84ee020..c8d85bb 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,5 +1,4 @@ import java.util.HashMap; -import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock; public class MapPractice { public static void main(String[] args) { diff --git a/src/Person.java b/src/Person.java index 8ab3f95..4533604 100644 --- a/src/Person.java +++ b/src/Person.java @@ -5,17 +5,32 @@ public class Person { // Declare a public String instance variable for the name of the person + String name; // Declare a private int instance variable for the age of the person - + int age; // Create a constructor that takes the name and age of the person // and assigns it to the instance variables + public Person(int personAge, String personName) + { + age = personAge; + name = personName; + } // 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" + + public int birthYear(int year) + { + return year - age; + } // There should NOT be any print statement in this method. /** * birthYear returns the year the person was born. @@ -32,21 +47,30 @@ public class Person { public static void main(String[] args) { // Create an instance of Person + Person person1 = new Person(22, "Tim"); // Create another instance of Person with a different name and age and // assign it to a different variable + Person person2 = new Person(30, "Ron"); // 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 fName = person1.name; + System.out.println(fName); // 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 yearBorn = person1.birthYear(2025); + // In a separate statement, print the local variable holding the birth year. + System.out.println(yearBorn); /** * Terminology! From a2785f5488c5c12a62369f17be5b65c42163db86 Mon Sep 17 00:00:00 2001 From: tim Date: Thu, 9 Jan 2025 09:06:10 -0800 Subject: [PATCH 5/5] Had to come back to one problem --- src/ListPractice.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index 5609fbd..6006b50 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -42,6 +42,11 @@ public static void main(String[] args) { System.out.println(IterateOver); } // Print each value on a second line + for (String IterateOver : EmptyList) + { + System.out.println(IterateOver); + System.out.println(); + } /*