diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..44202ad 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,32 @@ 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] = "Apple"; + myArray[1] = "Banana"; + myArray[2] = "Cherry"; + myArray[3] = "Date"; // Get the value of the array at index 2 + System.out.println("Value at index 2: " + myArray[2]); // Get the length of the array + System.out.println("Length of the array: " + myArray.length); // Iterate over the array using a traditional for loop and print out each item + System.out.println("Traditional for-loop"); + 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 + System.out.println("For-each loop:"); + for (String item : myArray) { + System.out.println(item); + } /* * Reminder! diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..5895661 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,48 @@ +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 myList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) + myList.add("Apple"); + myList.add("Banana"); + myList.add("Cherry"); // Print the element at index 1 + System.out.println("Element at index 1: " + myList.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) + myList.set(1, "Blueberry"); // Insert a new element at index 0 (the length of the list will change) + myList.add(0, "Apricot"); // Check whether the list contains a certain string + System.out.println("Contains 'Cherry': " + myList.contains("Cherry")); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + System.out.println("Traditional for-loop:"); + for (int i = 0; i < myList.size(); i++) { + System.out.println("Index " + i + ": " + myList.get(i)); + } // Sort the list using the Collections library + Collections.sort(myList); // Iterate over the list using a for-each loop // Print each value on a second line + System.out.println("For-each loop after sorting:"); + for (String item : myList) { + System.out.println(item); + } /* * Usage tip! diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..f1d6bd7 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,48 @@ - +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 myMap = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + myMap.put("Alice", 25); + myMap.put("Robin", 30); + myMap.put("Kevin", 35); // Get the value associated with a given key in the Map + System.out.println(myMap.get("Robin")); // Find the size (number of key/value pairs) of the Map + System.out.println(myMap.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) + myMap.put("Robin", 32); + System.out.println(myMap.get("Robin")); // Check whether the Map contains a given key + System.out.println(myMap.containsKey("Alice")); // Check whether the Map contains a given value + System.out.println(myMap.containsValue(30)); // Iterate over the keys of the Map, printing each key + for (String key : myMap.keySet()) { + System.out.println(key); + } // Iterate over the values of the map, printing each value + for (Integer value : myMap.values()) { + System.out.println(value); + } // Iterate over the entries in the map, printing each key and value + for (Map.Entry entry : myMap.entrySet()) { + System.out.println(entry.getKey() + ": " + entry.getValue()); + } /* * Usage tip! diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..94a5940 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,31 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float negativeFloat = -5.67f; + System.out.println("Negative float: " + negativeFloat); // Create an int with a positive value and assign it to a variable + int positiveInt = 42; + System.out.println("Positive int: " + positiveInt); // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainderBy3 = positiveInt % 3; + System.out.println("Remainder when divided by 3: " + remainderBy3); // 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 (positiveInt % 2 == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + } // Divide the number by another number using integer division - + int divisionResult = positiveInt / 5; + System.out.println("Result of integer division: " + divisionResult); + /* * Reminder! * diff --git a/src/Person.java b/src/Person.java index 8ab3f95..d073dd8 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 - // Declare a private int instance variable for the age 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 - + public String toString() { + return name + " " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -27,26 +34,34 @@ public class Person { * @param currentYear an int for the current year * @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 person1 = new Person("Alston", 25); // Create another instance of Person with a different name and age and // assign it to a different variable + Person person2 = new Person("Bob", 30); // 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 name1 = person1.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 birthYear1 = person1.birthYear(2025); // In a separate statement, print the local variable holding the birth year. + System.out.println("Birth year of " + name1 + ": " + birthYear1); /** * Terminology! diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..31dbffe 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,31 @@ +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 mySet = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + mySet.add("Apple"); + mySet.add("Banana"); + mySet.add("Cherry"); // Check whether the Set contains a given String + System.out.println(mySet.contains("Banana")); // Remove an element from the Set + mySet.remove("Banana"); + System.out.println(mySet.contains("Banana")); // Get the size of the Set + System.out.println(mySet.size()); // Iterate over the elements of the Set, printing each one on a separate line + for (String item : mySet) { + System.out.print(item); + } /* * Warning! diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..2130b2b 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,45 @@ +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 myString = "HelloWorld"; // Find the length of the string + System.out.println("Length of the string: " + myString.length()); // Concatenate (add) two strings together and reassign the result + myString += " Alston"; + System.out.println("Concatenated string: " + myString); // Find the value of the character at index 3 + System.out.println("Character at index 3: " + myString.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + System.out.println("Contains 'World': " + myString.contains("World")); // Iterate over the characters of the string, printing each one on a separate line + for (char c : myString.toCharArray()) { + System.out.println(c); + } // 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("Apple"); + stringList.add("Banana"); + stringList.add("Cherry"); // 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 joinedString = String.join(", ", stringList); + System.out.println(joinedString); // Check whether two strings are equal + String anotherString = "Hello"; + System.out.println(myString.equals(anotherString)); /* * Reminder! diff --git a/toRefresh.md b/toRefresh.md index 163dcab..9f2a67f 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,64 @@ 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 +## NumberPractice + +- Float syntax (using f suffix) +- Modulo operator % for even/odd checks +- Integer division always rounds down +- Printing things with System.out.println + + +## ListPractice + +- Creating an ArrayList +- Using .add(), .get, and .set +- Checking with .contains +- Inserting with .add(index, value) +- Sorting with Collections.sort +- Ilerating with for and for-each loop + +## ArrayPractice + +- Creating an array with a fixed size +- Assigning values to array elements +- Getting array length with .length +- ilerating with a for loop +- ilerating with a for-each loop + +## StringPractice + +- String creation and length +- Concatenation with + or += +- Accessing with .charAt +- Checking with .contains +- Iterating with toChartArray +- Joining with String.join +- Comparing with .equals + +## SetPractice + +- Creating a HashSet +- Adding elements with .add +- Checking with .contains +- Remoing elements with .remove +- Getting size with .size +- Iterating with a for-each loop + +## MapPractice + +- Creating a Hashmap +- Additing with .put +- Accessing with .get +- Checking keys/values +- Updating values +- Getting sizes +- Iterating keys, values, entries + +## PersonPractice + +- Instance variables +- Constructor +- toString method +- birthYear method +- Creating and using objects \ No newline at end of file