From 1d7c9346813cbdb3bfed490cad46e695551a60f4 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Tue, 7 Jan 2025 11:52:36 -0800 Subject: [PATCH 1/8] Finished NumberPractice --- src/NumberPractice.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..33b3610 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +1,30 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float negNum = -12; + System.out.println(negNum); // Create an int with a positive value and assign it to a variable + int posNum = 7; + System.out.println(posNum); // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = posNum % 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) // Use an if-else to print "Even" if the number is even and "Odd" // if the number is odd. + if(posNum % 2 == 0){ + System.out.println("Even"); + }else{ + System.out.println("Odd"); + } // Divide the number by another number using integer division + int intDivision = posNum / 3; + System.out.println(intDivision); /* * Reminder! From 8a373ecb9e64632c956c4e440d2eb219707871c1 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Tue, 7 Jan 2025 12:37:47 -0800 Subject: [PATCH 2/8] Finished ListPractice --- src/ListPractice.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..b515c14 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,53 @@ +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 stringList = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) + stringList.add("Creed"); + stringList.add("Michael"); + stringList.add("Andy"); // Print the element at index 1 + System.out.println(stringList.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) + stringList.set(1, "Jim"); + System.out.println(stringList.get(1)); // Insert a new element at index 0 (the length of the list will change) + stringList.add(0, "Dwight"); + System.out.println(stringList.get(0)); // Check whether the list contains a certain string + if(stringList.contains("Kevin")){ + System.out.println(true); + }else{ + System.out.println(false); + } // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for(int i = 0; i < stringList.size(); i++){ + System.out.println(stringList.get(i)); + } // Sort the list using the Collections library + stringList.sort(null); + // Iterate over the list using a for-each loop // Print each value on a second line + for(int i = 0; i < stringList.size(); i++){ + System.out.println(stringList.get(i)); + System.out.println(); + + } /* * Usage tip! From 8e36118da245b41c1142132cab51b3c208945f0b Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Tue, 7 Jan 2025 12:47:24 -0800 Subject: [PATCH 3/8] Finished ArrayPractice --- src/ArrayPractice.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..4fded98 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,30 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] arrString = 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 + arrString[0] = "Java"; + arrString[1] = "HTML"; + arrString[2] = "CSS"; + arrString[3] = "C#"; // Get the value of the array at index 2 + System.out.println(arrString[2]); // Get the length of the array + System.out.println(arrString.length); // Iterate over the array using a traditional for loop and print out each item + for(int i = 0; i < arrString.length; i++){ + System.out.println(arrString[i]); + } // Iterate over the array using a for-each loop and print out each item - + for(String str : arrString){ + System.out.println(str); + } /* * Reminder! * From a4260e6e5c3a970528414f49584da39b5c6cecb4 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Sat, 11 Jan 2025 13:46:55 -0800 Subject: [PATCH 4/8] Finished ListPractice --- src/ListPractice.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index b515c14..6885726 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,11 +1,13 @@ 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 - ArrayList stringList = new ArrayList(); + List stringList = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) stringList.add("Creed"); @@ -38,7 +40,7 @@ public static void main(String[] args) { } // Sort the list using the Collections library - stringList.sort(null); + Collections.sort(stringList); // Iterate over the list using a for-each loop From eee37ede47ea2d0580e54d47a12a4862a749254a Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Sat, 11 Jan 2025 14:40:48 -0800 Subject: [PATCH 5/8] Finished StringPractice --- src/StringPractice.java | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..6cf81ba 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,49 @@ +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 faveChar = "Lines"; // Find the length of the string + int stringLength = faveChar.length(); + System.out.println(stringLength); // Concatenate (add) two strings together and reassign the result + faveChar = faveChar + " Strings"; + System.out.println(faveChar); // Find the value of the character at index 3 + System.out.println(faveChar.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + System.out.println(faveChar.contains("abc")); // Iterate over the characters of the string, printing each one on a separate line + for(Character ch : faveChar.toCharArray()){ + System.out.println(ch); + } // Create an ArrayList of Strings and assign it to a variable - + List stringList = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + stringList.add("BRUBRUBRU"); + stringList.add("DUHDUHDUH"); + stringList.add("BAHBAHBAH"); + stringList.add("RUHRUHRUH"); // 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 joinString = String.join(", ", stringList); + System.out.println(joinString); // Check whether two strings are equal - + if (faveChar.equals(stringList.get(2))) { + System.out.println(true); + }else{ + System.out.println(false); + } /* * Reminder! * From 52ff8142b7c4143fd63dfb55a9121bf2a0dbb5d2 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Sat, 11 Jan 2025 14:50:16 -0800 Subject: [PATCH 6/8] Finished SetPractice --- src/SetPractice.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..6789154 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,30 @@ +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 stringSet = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + stringSet.add("Coffee"); + stringSet.add("Water"); + stringSet.add("Soda"); // Check whether the Set contains a given String + System.out.println(stringSet.contains("Coffee")); // Remove an element from the Set + stringSet.remove("Coffee"); // Get the size of the Set + System.out.println(stringSet.size()); // Iterate over the elements of the Set, printing each one on a separate line + for(String str : stringSet){ + System.out.println(str); + } /* * Warning! From ad8e8b96e7e837c1168b8486300e8796df0610f8 Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Sat, 11 Jan 2025 15:11:46 -0800 Subject: [PATCH 7/8] Finished MapPractice --- src/MapPractice.java | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..22d1a0f 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,47 @@ - +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 stringMap = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + stringMap.put("Water", 100); + stringMap.put("Coffee", 45); + stringMap.put("Soda", 10); // Get the value associated with a given key in the Map + System.out.println(stringMap.get("Soda")); // Find the size (number of key/value pairs) of the Map + System.out.println(stringMap.size()); - // Replace the value associated with a given key (the size of the Map shoukld not change) + // Replace the value associated with a given key (the size of the Map should not change) + stringMap.replace("Coffee", 30); // Check whether the Map contains a given key + System.out.println(stringMap.containsKey("Water")); // Check whether the Map contains a given value + System.out.println(stringMap.containsValue(10)); // Iterate over the keys of the Map, printing each key + for(String key : stringMap.keySet()){ + System.out.println(key); + } // Iterate over the values of the map, printing each value + for(int value : stringMap.values()){ + System.out.println(value); + } // Iterate over the entries in the map, printing each key and value + for(String key : stringMap.keySet()){ + System.out.println("Key: " + key + " Value: " + stringMap.get(key)); + } /* * Usage tip! From 68383e2ec52eda31b72ab6187fc8cdcf9091c2be Mon Sep 17 00:00:00 2001 From: Bolshialex <145608606+Bolshialex@users.noreply.github.com> Date: Sat, 11 Jan 2025 15:36:56 -0800 Subject: [PATCH 8/8] Finished Person --- src/Person.java | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..bb379ec 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,14 +6,21 @@ 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 + "\nAge: " + age); + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,25 +35,34 @@ public class Person { * @return The year the person was born */ // (create the instance method here) - + public int getBirthYear(int currentYear){ + return currentYear - age; + } public static void main(String[] args) { // Create an instance of Person - + Person personA = new Person("Joe", 25); // Create another instance of Person with a different name and age and // assign it to a different variable + Person personB = new Person("Shmo", 23); // Print the first person + System.out.println(personA.toString()); // Print the second person + System.out.println(personB.toString()); // Get the name of the first person and store it in a local variable + String nameOfOne = personA.name; + System.out.println(nameOfOne); // 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 = personA.getBirthYear(2025); // In a separate statement, print the local variable holding the birth year. + System.out.println(birthYear); /** * Terminology!