diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..2b7e4bd 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -2,15 +2,32 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] words = 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 + words[0] = "Array"; + words[1] = "Practice"; + words[2] = "For"; + words[3] = "Class"; + // Get the value of the array at index 2 + String valueAtIndex2 = words[2]; + System.out.println("Value at index 2: " + valueAtIndex2); + // Get the length of the array + int length = words.length; + System.out.println("Length of the array: " + length); + // Iterate over the array using a traditional for loop and print out each item + System.out.println("Using a for-each loop:"); + for (String word : words) { + System.out.println(word); + } // Iterate over the array using a for-each loop and print out each item /* @@ -18,5 +35,7 @@ public static void main(String[] args) { * * Arrays start at index 0 */ + + } } diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..a2d48af 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,42 @@ +import java.util.*; + 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 items = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + items.add("Number 1"); + items.add("Number 2"); + items.add("Number 3"); // Print the element at index 1 - + System.out.println("Element at index 1: " + items.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) - + items.set(1, "Practice"); + System.out.println("Updated element at index 1: " + items.get(1)); // Insert a new element at index 0 (the length of the list will change) - + items.add(0, "Start"); + System.out.println("Index 0: " + items); // Check whether the list contains a certain string - + boolean containsCode = items.contains("Code"); + System.out.println("Does this lest contain 'Code'? " + containsCode); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + System.out.println("Using a traditional for-loop:"); + for (int i = 0; i < items.size(); i++) { + System.out.println("Index " + i + ": " + items.get(i)); + } // Sort the list using the Collections library - + Collections.sort(items); + System.out.println("Sorted list: " + items); // Iterate over the list using a for-each loop // Print each value on a second line - + System.out.println("Using a for-each loop:"); + for (String item : items) { + System.out.println(item); + } /* * Usage tip! * diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..5ea4ac1 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,41 @@ - +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 pointsScored = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) - + pointsScored.put("Alex", 28); + pointsScored.put("Samuel", 30); + pointsScored.put("Jimmy", 42); // Get the value associated with a given key in the Map - + System.out.println("Score for Alex: " + pointsScored.get("Alex")); // Find the size (number of key/value pairs) of the Map - + System.out.println("Number of entries in the map: " + pointsScored.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) - + pointsScored.put("Alex", 31); + System.out.println("Updated score for Alex: " + pointsScored.get("Alex")); // Check whether the Map contains a given key - + System.out.println("Does the map contain the key 'Jimmy'? " + pointsScored.containsKey("Jimmy")); // Check whether the Map contains a given value - + System.out.println("Does the map contain the value 42? " + pointsScored.containsValue(42)); // Iterate over the keys of the Map, printing each key - + System.out.println("Keys in the map:"); + for (String key : pointsScored.keySet()) { + System.out.println(key); + } // Iterate over the values of the map, printing each value - + System.out.println("Values in the map:"); + for (Integer value : pointsScored.values()) { + System.out.println(value); + } // Iterate over the entries in the map, printing each key and value - + System.out.println("Entries in the map:"); + for (Map.Entry entry : pointsScored.entrySet()) { + System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()); + } /* * Usage tip! * diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..ecbf6d1 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +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 negativeFloat = -15.5f; + System.out.println("Negative float: " + negativeFloat); // Create an int with a positive value and assign it to a variable - + int positiveInt = 19; + System.out.println("Positive int: " + positiveInt); // Use the modulo % operator to find the remainder when the int is divided by 3 - + int remainder = positiveInt % 3; + System.out.println("Remainder when " + positiveInt + " is divided by 3: " + 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 (positiveInt % 2 == 0) { + System.out.println(positiveInt + " is Even"); + } else { + System.out.println(positiveInt + " is Odd"); + } // Divide the number by another number using integer division + int result = positiveInt / 4; // Divide by 4 + System.out.println("Result of integer division of " + positiveInt + " by 4: " + result); /* * Reminder! diff --git a/src/Person.java b/src/Person.java index 8ab3f95..3cd9079 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 + ", 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 - age; + } public static void main(String[] args) { // Create an instance of Person - + Person person1 = new Person("Jimmy", 23); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person person2 = new Person("John", 29); // 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 firstName = 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 birthYear = person1.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println("Birth year of " + firstName + ": " + birthYear); + /** * Terminology! * diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..7696eb9 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,31 @@ +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 mySet = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) - + mySet.add("Cookies"); + mySet.add("Cupcakes"); + mySet.add("Brownies"); // Check whether the Set contains a given String - + String searchElement = "Cupcakes"; + if (mySet.contains(searchElement)) { + System.out.println(searchElement + " is in the set."); + } else { + System.out.println(searchElement + " is not in the set."); + } // Remove an element from the Set - + mySet.remove("Brownies"); + System.out.println("After removing 'Brownies': " + mySet); // Get the size of the Set - + int setSize = mySet.size(); + System.out.println("Size of the set: " + setSize); // Iterate over the elements of the Set, printing each one on a separate line - + System.out.println("Elements in the set:"); + for (String element : mySet) { + System.out.println(element); + } /* * Warning! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..ff1cd45 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,41 @@ +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 = "Hello"; // Find the length of the string - + int length = myString.length(); + System.out.println("Length of the string: " + length); // Concatenate (add) two strings together and reassign the result - + String newString = myString + " World"; + System.out.println("Concatenated string: " + newString); // Find the value of the character at index 3 - + char charAtIndex3 = myString.charAt(3); + System.out.println("Character at index 3: " + charAtIndex3); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + boolean containsSubstring = myString.contains("abc"); + System.out.println("Does the string contain 'abc'? " + containsSubstring); // Iterate over the characters of the string, printing each one on a separate line - + System.out.println("Characters in the string:"); + for (int i = 0; i < myString.length(); i++) { + System.out.println(myString.charAt(i)); + } // 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("Apples"); + stringList.add("Carrots"); + stringList.add("Pomegranites"); // 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("Joined list into a single string: " + joinedString); // Check whether two strings are equal - + String anotherString = "Hello"; + boolean areStringsEqual = myString.equals(anotherString); + System.out.println("Are the two strings equal? " + areStringsEqual); /* * Reminder! * diff --git a/toRefresh.md b/toRefresh.md index 163dcab..a72db68 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,8 @@ 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 +- String Practice +- Set Practice +- List Practice +- Overall, I do need to look over all of these a little more to get them down +- Because I am a little shaky on them. \ No newline at end of file