diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..d1a3da9 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -2,17 +2,26 @@ 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] = "hi"; + myArray[1] = "hello"; + myArray[2] = "hey"; + myArray[3] = "howdy"; // Get the value of the array at index 2 - + System.out.println(myArray[2]); // Get the length of the array - + System.out.println(myArray.length); // Iterate over the array using a traditional for loop and print out each item - + for(int i = 0; i < myArray.length; i++){ + System.out.println(i + ": " + myArray[i]); + } // Iterate over the array using a for-each loop and print out each item - + for(String string : myArray){ + System.out.println(string); + } /* * Reminder! * diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..45ef845 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,40 @@ +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 myList = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) - + myList.add("hello"); + myList.add("hi"); + myList.add("hey"); // Print the element at index 1 - + System.out.println(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, "bye"); // Insert a new element at index 0 (the length of the list will change) - + myList.add(0, "goodbye"); + System.out.println(myList); // Check whether the list contains a certain string - + System.out.println(myList.contains("hi")); + System.out.println(myList.contains("bye")); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + for(int i = 0; i < myList.size(); i++){ + System.out.println(i + ": " + myList.get(i)); + } // Sort the list using the Collections library - + myList.sort(null); + System.out.println(myList); // Iterate over the list using a for-each loop // Print each value on a second line - + for (String string : myList) { + System.out.println(); + System.out.println(string); + } /* * Usage tip! * diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..0a51afb 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,38 @@ - +import java.util.*; +import java.util.Map.Entry; 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("penny", 1); + myMap.put("quarter", 25); + myMap.put("nickel", 0); // Get the value associated with a given key in the Map - + System.out.println(myMap.get("quarter")); // 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.replace("nickel", 5); // Check whether the Map contains a given key - + System.out.println(myMap.containsKey("penny")); // Check whether the Map contains a given value - + System.out.println(myMap.containsValue(0)); // 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(Entry entry: myMap.entrySet()){ + System.out.println(entry); + } /* * Usage tip! * diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..0df5a52 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,24 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + Double negFloat = -1.5; + System.out.println(negFloat); // Create an int with a positive value and assign it to a variable - + int posInt = 6; + System.out.println(posInt); // Use the modulo % operator to find the remainder when the int is divided by 3 - + System.out.println(posInt % 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 ((posInt % 2)== 0){ + System.out.println("Even"); + } else { + System.out.println("Odd"); + } // Divide the number by another number using integer division - + System.out.println(posInt / 3); /* * Reminder! * diff --git a/src/Person.java b/src/Person.java index 8ab3f95..3d8fd86 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 - - + private int age; + String name; // Create a constructor that takes the name and age of the person // and assigns it to the instance variables - + public Person(int age, String name){ + this.age = age; + this.name = name; + } // Create a toString method that gives the name and age of the person - + @Override + public String toString(){ + return name + ", " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,26 +34,31 @@ 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 firstPerson = new Person(10, "Joey"); // Create another instance of Person with a different name and age and // assign it to a different variable + Person anotherPerson = new Person(20, "Jane"); // Print the first person - + System.out.println(firstPerson); // Print the second person - + System.out.println(anotherPerson); // Get the name of the first person and store it in a local variable - + String firstName = firstPerson.name; + System.out.println(firstName); // 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 firstBirthYear = firstPerson.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(firstBirthYear); /** * Terminology! * diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..bd924c9 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,25 @@ +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("hi"); + mySet.add("hello"); + mySet.add("howdy"); // Check whether the Set contains a given String - + System.out.println(mySet.contains("howdy")); // Remove an element from the Set - + mySet.remove("hi"); + System.out.println(mySet); // 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 string : mySet){ + System.out.println(string); + } /* * Warning! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..b6f0980 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,35 @@ +import java.util.*; + 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 = "At Least Five"; // Find the length of the string - + System.out.println(myString.length()); // Concatenate (add) two strings together and reassign the result - + String concatResult = myString + " Characters"; + System.out.println(concatResult); // Find the value of the character at index 3 - + System.out.println(myString.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + System.out.println(myString.contains("east")); // 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 myArrayList = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) - + myArrayList.add("this "); + myArrayList.add("is "); + myArrayList.add("my "); + myArrayList.add("list."); // 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 singleString = myArrayList.toString(); + System.out.println(singleString); // Check whether two strings are equal - + System.out.println(myString.equals(myArrayList.get(0))); /* * Reminder! * diff --git a/toRefresh.md b/toRefresh.md index 163dcab..88e7fec 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,6 @@ 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 +- instantiating Lists and List methods +- instantiating arrays +- iterating through maps \ No newline at end of file