From a2584cd94eabd3e0b013d9cc6bb588d59166bd9a Mon Sep 17 00:00:00 2001 From: Niko_Butenko <174657090+Butenkite@users.noreply.github.com> Date: Thu, 9 Jan 2025 11:52:07 -0800 Subject: [PATCH 01/12] Array Practice completed. --- src/ArrayPractice.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..1834adc 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,31 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] myArray = 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 + myArray[0] = "A"; + myArray[1] = "B"; + myArray[2] = "C"; + myArray[3] = "D"; + // Get the value of the array at index 2 + System.out.println(myArray[2]); // Get the length of the array + System.out.println(myArray.length); // Iterate over the array using a traditional for loop and print out each item + for(int i = 0; i > myArray.length; i++){ + System.out.println(myArray[i]); + } // Iterate over the array using a for-each loop and print out each item + for(String letter : myArray){ + System.out.println(letter); + } /* * Reminder! @@ -19,4 +33,4 @@ public static void main(String[] args) { * Arrays start at index 0 */ } -} +} \ No newline at end of file From d86b64bc1dc455f5447fa5beff103a01a4748dde Mon Sep 17 00:00:00 2001 From: Niko_Butenko <174657090+Butenkite@users.noreply.github.com> Date: Thu, 9 Jan 2025 12:07:11 -0800 Subject: [PATCH 02/12] Part of ArrayList submitted. --- src/ListPractice.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..c9e0c2f 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,13 +1,17 @@ +import java.util.ArrayList; // needs to be imported to run + 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("A"); + list.add("B"); + list.add("C"); // 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) From 9ab5a85a1f73b8ddb32bb13b13693ddf4573205b Mon Sep 17 00:00:00 2001 From: Niko_Butenko <174657090+Butenkite@users.noreply.github.com> Date: Thu, 9 Jan 2025 13:04:34 -0800 Subject: [PATCH 03/12] Further done Listpractice. --- src/ListPractice.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index c9e0c2f..bb223f8 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -14,9 +14,9 @@ public static void main(String[] args) { 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.add(1,"D"); // Insert a new element at index 0 (the length of the list will change) - + list.add("E"); // Check whether the list contains a certain string // Iterate over the list using a traditional for-loop. From 88c4bdcca8a01cbbd9216ff3d60dd64fc127175f Mon Sep 17 00:00:00 2001 From: unknown <174657090+Butenkite@users.noreply.github.com> Date: Mon, 13 Jan 2025 14:58:30 -0800 Subject: [PATCH 04/12] Finished list practice. --- src/ListPractice.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index bb223f8..bc838d2 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,4 +1,4 @@ -import java.util.ArrayList; // needs to be imported to run +import java.util.*; // needs to be imported to run, don't know the specifics for collections, so importing utils. public class ListPractice { @@ -14,19 +14,24 @@ public static void main(String[] args) { 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.add(1,"D"); + list.set(1,"D"); // Insert a new element at index 0 (the length of the list will change) - list.add("E"); + list.add(0, "E"); // Check whether the list contains a certain string - + System.out.println("Contains E?: " + list.contains("E")); // 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 + ". Value: " + 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 listLetter : list){ + + System.out.println("Value: " + listLetter); + } /* * Usage tip! * From 2d86dd00409ab3cade8d9d93842446a478ed2898 Mon Sep 17 00:00:00 2001 From: unknown <174657090+Butenkite@users.noreply.github.com> Date: Mon, 13 Jan 2025 15:24:22 -0800 Subject: [PATCH 05/12] Finished Map Practice. --- src/MapPractice.java | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..8c67f9a 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,37 @@ - +import java.util.*; 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 mapsPractice = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) - + mapsPractice.put("hello", 0); + mapsPractice.put("neutral", 1); + mapsPractice.put("goodbye", 2); // Get the value associated with a given key in the Map - + mapsPractice.get("hello"); // Find the size (number of key/value pairs) of the Map - + mapsPractice.size(); // Replace the value associated with a given key (the size of the Map shoukld not change) - + mapsPractice.replace("hello", 3); // Check whether the Map contains a given key - + mapsPractice.containsKey("goodbye"); // Check whether the Map contains a given value - + mapsPractice.containsValue(5); // Iterate over the keys of the Map, printing each key - + for(String entry : mapsPractice.keySet()){ + System.out.println(entry); + } // Iterate over the values of the map, printing each value - + for(int entry : mapsPractice.values()){ + System.out.println(entry); + } // Iterate over the entries in the map, printing each key and value - + for(String entry : mapsPractice.keySet()){ + System.out.println("Key: " + entry + ". Value: " + mapsPractice.get(entry)); + } /* * Usage tip! * From 9d0d7ca1f44722bca28979a638f04c547ef288d1 Mon Sep 17 00:00:00 2001 From: unknown <174657090+Butenkite@users.noreply.github.com> Date: Mon, 13 Jan 2025 15:29:28 -0800 Subject: [PATCH 06/12] Complete Number Practice. --- src/NumberPractice.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..22de854 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 i = -0.1f; // Create an int with a positive value and assign it to a variable - + int k = 4; // Use the modulo % operator to find the remainder when the int is divided by 3 - + int l = k%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(k % 2 == 1){ + System.out.println("Odd"); + } + else{ + System.out.println("Even"); + } // Divide the number by another number using integer division - + int a = 4; + int b = 2; + int c = a / b; /* * Reminder! * From 133d7cae62fa69eab073fc3e142fd4b7ca87b1ec Mon Sep 17 00:00:00 2001 From: unknown <174657090+Butenkite@users.noreply.github.com> Date: Mon, 13 Jan 2025 16:01:21 -0800 Subject: [PATCH 07/12] Completed Person warmup. --- src/Person.java | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..f0d8841 100644 --- a/src/Person.java +++ b/src/Person.java @@ -3,17 +3,25 @@ * the Person class. */ +import java.util.*; + 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 = -99; // Create a constructor that takes the name and age of the person // and assigns it to the instance variables - + public Person(String x, int y){ + name = x; + age = y; + } // 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" // There should NOT be any print statement in this method. @@ -28,26 +36,32 @@ public class Person { * @return The year the person was born */ // (create the instance method here) - + public int birthYear(int currentYear){ + return(currentYear - age); + } + // Created a method to get name easier. + public String getName(){ + return(name); + } public static void main(String[] args) { // Create an instance of Person - + Person jim = new Person("Jim", 45); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person tim = new Person("Tim", 25); // Print the first person - + System.out.println(jim); // Print the second person - + System.out.println(tim); // Get the name of the first person and store it in a local variable - + String grabbedName = jim.getName(); // 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 jimBirthYear = jim.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(jimBirthYear); /** * Terminology! * From ef5f8d14108b0c074fc8e34d93f86f759cbab922 Mon Sep 17 00:00:00 2001 From: unknown <174657090+Butenkite@users.noreply.github.com> Date: Mon, 13 Jan 2025 16:04:03 -0800 Subject: [PATCH 08/12] Completed Person warmup. --- src/Person.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Person.java b/src/Person.java index f0d8841..6217e33 100644 --- a/src/Person.java +++ b/src/Person.java @@ -60,8 +60,8 @@ public static void main(String[] args) { // and store it in a local variable. Input the actual current year (e.g. 2025) // as the argument. int jimBirthYear = jim.birthYear(2025); + System.out.println(jimBirthYear); // In a separate statement, print the local variable holding the birth year. - System.out.println(jimBirthYear); /** * Terminology! * From 31e52e2ed9f15197d2ba8d224c8cac25f779e61f Mon Sep 17 00:00:00 2001 From: unknown <174657090+Butenkite@users.noreply.github.com> Date: Mon, 13 Jan 2025 16:07:40 -0800 Subject: [PATCH 09/12] completed Person practice. --- src/Person.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Person.java b/src/Person.java index 6217e33..3b1e0fa 100644 --- a/src/Person.java +++ b/src/Person.java @@ -60,7 +60,7 @@ public static void main(String[] args) { // and store it in a local variable. Input the actual current year (e.g. 2025) // as the argument. int jimBirthYear = jim.birthYear(2025); - System.out.println(jimBirthYear); + System.out.println(jimBirthYear); // In a separate statement, print the local variable holding the birth year. /** * Terminology! From c0faab15cbe75af1feac4d4fc95ae1729a9b8e61 Mon Sep 17 00:00:00 2001 From: unknown <174657090+Butenkite@users.noreply.github.com> Date: Mon, 13 Jan 2025 16:10:36 -0800 Subject: [PATCH 10/12] Completed Person practice. --- src/Person.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Person.java b/src/Person.java index 3b1e0fa..f0d8841 100644 --- a/src/Person.java +++ b/src/Person.java @@ -60,8 +60,8 @@ public static void main(String[] args) { // and store it in a local variable. Input the actual current year (e.g. 2025) // as the argument. int jimBirthYear = jim.birthYear(2025); - System.out.println(jimBirthYear); // In a separate statement, print the local variable holding the birth year. + System.out.println(jimBirthYear); /** * Terminology! * From c4a40b51bbc45f7155ca7573348a016aff2fe1ca Mon Sep 17 00:00:00 2001 From: unknown <174657090+Butenkite@users.noreply.github.com> Date: Mon, 13 Jan 2025 18:38:23 -0800 Subject: [PATCH 11/12] set practice complete. --- src/SetPractice.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..dd57218 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,24 @@ +import java.util.*; + public class SetPractice { public static void main(String[] args) { // Create a HashSet of Strings and assign it to a variable of type Set - + Set hashSetPractice = new HashSet(); // Add 3 elements to the set // (It's OK to do it one-by-one) - + hashSetPractice.add("good morning"); + hashSetPractice.add("good afternoon"); + hashSetPractice.add("good night"); // Check whether the Set contains a given String - + hashSetPractice.contains("good morning"); // Remove an element from the Set - + hashSetPractice.remove("good night"); // Get the size of the Set - + hashSetPractice.size(); // Iterate over the elements of the Set, printing each one on a separate line - + for(String saying : hashSetPractice){ + System.out.println(saying); + } /* * Warning! * From 5d2d65955c6c7f67ccbeac03b399f026a5947f6f Mon Sep 17 00:00:00 2001 From: unknown <174657090+Butenkite@users.noreply.github.com> Date: Mon, 13 Jan 2025 18:49:05 -0800 Subject: [PATCH 12/12] String Practice Finished. --- src/StringPractice.java | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..f1280e4 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,38 @@ public class StringPractice { public static void main(String[] args) { // Create a string with at least 5 characters and assign it to a variable - + String a = "ABCDE"; // Find the length of the string - + a.length(); // Concatenate (add) two strings together and reassign the result - + String b = "FGHIJ"; + String c = a + b; // Find the value of the character at index 3 - + c.charAt(3); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + c.contains("BCD"); // Iterate over the characters of the string, printing each one on a separate line - + for(int i = 0; i < c.length(); i++){ + System.out.println(c.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable - + String[] myArray = new String[4]; // Add multiple strings to the List (OK to do one-by-one) - + myArray[0] = "A"; + myArray[1] = "B"; + myArray[2] = "C"; + myArray[3] = "D"; // 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 combined = String.join(", ", myArray); // Check whether two strings are equal - + String d = "ABCDE"; + if(a.equals(d)){ + System.out.println("strings match up."); + } + else{ + System.out.println("strings don't match up."); + } /* * Reminder! *