diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..5f4a78e 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,25 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[] stringArray = 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 - + stringArray[0] = "apple"; + stringArray[1] = "banana"; + stringArray[2] = "date"; + stringArray[3] = "cherry"; // Get the value of the array at index 2 - + System.out.println(stringArray[2]); // Get the length of the array - + System.out.println(stringArray.length); // Iterate over the array using a traditional for loop and print out each item - + for (int i = 0; i < stringArray.length; i++) { + System.out.println(stringArray[i]); + } // Iterate over the array using a for-each loop and print out each item - + for (String element : stringArray) { + System.out.println(element); + } /* * Reminder! * diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..e3f8005 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,42 @@ +import java.util.ArrayList; +import java.util.Collections; +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 aList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + aList.add("apple"); + aList.add("orange"); + aList.add("banana"); // Print the element at index 1 - + System.out.println(aList.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) - + aList.set(1, "cumquat"); // Insert a new element at index 0 (the length of the list will change) - + aList.add(0, "grapes"); // Check whether the list contains a certain string - + if (aList.contains("avocado")) { + System.out.println("The list contains 'avocado'."); + } else { + System.out.println("The list does not contain 'avocado'."); + } // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + for (int i = 0; i < aList.size(); i++) { + System.out.println("Index: " + i + ", Value: " + aList.get(i)); + } // Sort the list using the Collections library - + Collections.sort(aList); // Iterate over the list using a for-each loop // Print each value on a second line + for (String element : aList) { + System.out.println(element); + } /* * Usage tip! diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..48d36b6 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,42 @@ - +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 mappy = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) - + mappy.put("kitties", 3); + mappy.put("doggies", 4); + mappy.put("fishies", 11); // Get the value associated with a given key in the Map - + System.out.println(mappy.get("fishies")); // Find the size (number of key/value pairs) of the Map - + System.out.println(mappy.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) - + mappy.replace("kitties", 5); // Check whether the Map contains a given key - + boolean qKey = mappy.containsKey("doggies"); + System.out.println(qKey); // Check whether the Map contains a given value - + boolean qValue = mappy.containsValue(2); + System.out.println(qValue); // Iterate over the keys of the Map, printing each key - + for (String elementKey : mappy.keySet()) { + System.out.println(elementKey); + } // Iterate over the values of the map, printing each value - + for (Integer elementValue : mappy.values()) { + System.out.println(elementValue); + } // Iterate over the entries in the map, printing each key and value - + // Map.Entry entry I had to ask for help from chatGPT on this part + for (Map.Entry entry : mappy.entrySet()) { + System.out.println("Key: " + entry.getKey() + ", Value: " + + entry.getValue()); + } /* * Usage tip! * diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..1282f6f 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +1,22 @@ public class NumberPractice { - public static void main(String args[]) { + public static void main(String[] args) { // Create a float with a negative value and assign it to a variable - + float floater = -7.77f; // Create an int with a positive value and assign it to a variable - + int winter = 12; // Use the modulo % operator to find the remainder when the int is divided by 3 - + int modRemain = winter % 3; + System.out.println("Remainder when divided by 3: " + modRemain); // 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 (winter % 2 == 0) {System.out.println("Even");} + else {System.out.println("Odd");} // Divide the number by another number using integer division + int newResult = winter / 5; // Example: divide winter by 5 + System.out.println("Integer division result: " + newResult); + /* * Reminder! @@ -23,3 +28,4 @@ public static void main(String args[]) { } } + diff --git a/src/Person.java b/src/Person.java index 8ab3f95..44132bc 100644 --- a/src/Person.java +++ b/src/Person.java @@ -5,15 +5,22 @@ public class Person { // Declare a public String instance variable for the name of the person + public String name; // Declare a private int instance variable for the age of the person - + 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 + ", Age: " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,26 +35,29 @@ public class Person { * @return The year the person was born */ // (create the instance method here) + public int birthYear(int currentYear) { + return currentYear - this.age; + } public static void main(String[] args) { // Create an instance of Person - + Person steveH = new Person("Steve Hardy", 32); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person sallieR = new Person("Sallie Ride", 49); // Print the first person - + System.out.println(steveH); // Print the second person - + System.out.println(sallieR); // Get the name of the first person and store it in a local variable - + String firstName = steveH.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 birthYearSteve = steveH.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println("Birth year of " + firstName + ": " + birthYearSteve); /** * Terminology! * diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..042481a 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,26 @@ +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 hashy = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) - + hashy.add("metal"); + hashy.add("water"); + hashy.add("air"); // Check whether the Set contains a given String - + boolean doesIt = hashy.contains("air"); + System.out.println(doesIt); // Remove an element from the Set - + hashy.remove("air"); // Get the size of the Set - + System.out.println(hashy.size()); // Iterate over the elements of the Set, printing each one on a separate line - + for (String element : hashy) { + System.out.println(element); + } /* * Warning! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..d37a63c 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,39 @@ +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 stringy = "apple"; // Find the length of the string - + System.out.println(stringy.length()); // Concatenate (add) two strings together and reassign the result - + String kitty = "cinder" + "block"; // Find the value of the character at index 3 - + System.out.println(kitty.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + boolean sub = kitty.contains("abc"); + System.out.println(sub); // Iterate over the characters of the string, printing each one on a separate line - + for (int i = 0; i < kitty.length(); i++) { + System.out.println(kitty.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable - + ArrayList listy = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + listy.add("kitty"); + listy.add("doggy"); + listy.add("birdy"); + listy.add("horsy"); + listy.add("piggy"); + listy.add("donkey"); + listy.add("cow"); // 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 joined = String.join(",", listy); // Check whether two strings are equal - + boolean equal = kitty.equals(joined); + System.out.println(equal); /* * Reminder! * diff --git a/toRefresh.md b/toRefresh.md index 163dcab..2272ee7 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,12 @@ As you work through this exercise, write down anything that you needed to look up or struggled to remember here. It can be just a word or two (e.g. "joining strings"). You can use this as a guide of what to make extra sure you're refreshed on before exams and interviews. -- \ No newline at end of file +- Taken from MapPractice.java class: +// Iterate over the entries in the map, printing each key and value + // Map.Entry entry I had to ask for help from chatGPT on this part + for (Map.Entry entry : mappy.entrySet()) { + System.out.println("Key: " + entry.getKey() + ", Value: " + + entry.getValue()); + } + +- \ No newline at end of file