diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..866e043 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,28 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[] newArray = 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 + newArray[0] = "Hello"; + newArray[1] = "My"; + newArray[2] = "Name's"; + newArray[3] = "Tav"; // Get the value of the array at index 2 - + System.out.println(newArray[2]); // Get the length of the array - + System.out.println("The length of the array is: " + newArray.length); // Iterate over the array using a traditional for loop and print out each item - + for (int i = 0; i < newArray.length; i++) { + System.out.println(newArray[i]); + } // Iterate over the array using a for-each loop and print out each item - + // Breaking to show for each + System.out.println(); + for (String words:newArray) { + System.out.println(words); + } /* * Reminder! * diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..3d9a440 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +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 stringList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + stringList.add("Pulp"); + stringList.add("Round"); + stringList.add("Strange"); // 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, "Juice"); + System.out.println(stringList.get(1)); // Insert a new element at index 0 (the length of the list will change) - + stringList.add(0, "Oh"); + System.out.println(stringList.get(0)); // Check whether the list contains a certain string - + System.out.println(stringList.contains("Juice")); // 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("Index: " + i + ", Value: " +stringList.get(i)); + } // Sort the list using the Collections library - + Collections.sort(stringList); + System.out.println(stringList); // Iterate over the list using a for-each loop // Print each value on a second line - + for (String word : stringList) { + System.out.println(word); + } /* * Usage tip! * diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..fd31b5b 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,44 @@ +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 newMap = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + newMap.put("For", 1); + newMap.put("Gas", 2); + newMap.put("Port", 3); - // Get the value associated with a given key in the Map + // Get the value associated with a given key in the Map + System.out.println(newMap.get("Port")); // 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) - + System.out.println(newMap.size()); + // Replace the value associated with a given key (the size of the Map should not change) + newMap.replace("For", 9); // Check whether the Map contains a given key - + System.out.println(newMap.containsKey("Port")); // Check whether the Map contains a given value - + System.out.println(newMap.containsValue(4)); // Iterate over the keys of the Map, printing each key - + System.out.println("Keys:"); + for (String keys : newMap.keySet()) { + System.out.println(keys); + } // Iterate over the values of the map, printing each value - + System.out.println("Values:"); + for (Integer values : newMap.values()) { + System.out.println(values); + } // Iterate over the entries in the map, printing each key and value - + System.out.println("Both Keys and Values"); + for (Map.Entry set : newMap.entrySet()) { + System.out.println("Keys: " + set.getKey() + ", Values: " + set.getValue()); + } /* * Usage tip! * diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..176aa03 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,22 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + float floatingNum = -2l; // Create an int with a positive value and assign it to a variable - + int regNum = 22; // Use the modulo % operator to find the remainder when the int is divided by 3 - + System.out.println(regNum % 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 (regNum % 2 == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + } // Divide the number by another number using integer division - + System.out.println(regNum / 6); /* * Reminder! * diff --git a/src/Person.java b/src/Person.java index 8ab3f95..58f50b6 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,14 +6,20 @@ 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 - + public String toString() { + return "This is " + name + ", they are " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,26 +34,29 @@ public class Person { * @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 - + Person dude = new Person("Dante", 20); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person dudeTwo = new Person("Vergil", 21); // Print the first person - + System.out.println(dude); // Print the second person - + System.out.println(dudeTwo); // Get the name of the first person and store it in a local variable - + String fName = dude.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 fBirth = dude.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(fBirth); /** * Terminology! * diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..3b71029 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +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 newSet = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) - + newSet.add("Hello"); + newSet.add("World"); + newSet.add("Smile?"); // Check whether the Set contains a given String - + if (newSet.contains("Smile?")) { + System.out.println("True"); + } else { + System.out.println("False"); + } // Remove an element from the Set - + newSet.remove("Hello"); // Get the size of the Set - + System.out.println(newSet.size()); // Iterate over the elements of the Set, printing each one on a separate line - + for (String word : newSet) { + System.out.println(word); + } /* * Warning! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..a176911 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,46 @@ +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 word = "Fives"; // Find the length of the string - + System.out.println(word.length()); // Concatenate (add) two strings together and reassign the result - + word += " high?"; + System.out.println(word); // Find the value of the character at index 3 - + System.out.println(word.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + if (word.contains("high")) { + System.out.println("True"); + } else { + System.out.println("False"); + } // Iterate over the characters of the string, printing each one on a separate line - + for (Character letter : word.toCharArray()) { + System.out.println(letter); + } // Create an ArrayList of Strings and assign it to a variable - + List newList = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + newList.add("Apple"); + newList.add("bottom"); + newList.add("jeans"); + newList.add("boots"); + newList.add("with the?"); // 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 lyric = String.join(", ", newList); + System.out.println(lyric); // Check whether two strings are equal - + if (lyric.equals(word)) { + System.out.println("True"); + } else { + System.out.println("False"); + } /* * Reminder! * diff --git a/toRefresh.md b/toRefresh.md index 163dcab..8569634 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,5 @@ 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 +- Refresh on methods used in general +- Refresh on java constructors and fields \ No newline at end of file