diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..91ad19c 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,30 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] animals = 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 + animals[0] = "dog"; + animals[1] = "cat"; + animals[2] = "fish"; + animals[3] = "bird"; // Get the value of the array at index 2 + System.out.println(animals[2]); // Get the length of the array + System.out.println(animals.length); // Iterate over the array using a traditional for loop and print out each item + for (int i = 0; i < animals.length; i++) { + System.out.println(animals[i]); + }; // Iterate over the array using a for-each loop and print out each item + for (String animal : animals) { + System.out.println(animal); + }; /* * Reminder! diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..b35352c 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,44 @@ +import java.util.ArrayList; + public class ListPractice { public static void main(String[] args) { // Create an empty ArrayList of Strings and assign it to a variable of type List + ArrayList List = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) + List.add("Milk"); + List.add("Eggs"); + List.add("Bread"); // Print the element at index 1 + System.out.println(List.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) + List.set(1, "Bananas"); // Insert a new element at index 0 (the length of the list will change) + List.add(0, "Apples"); // Check whether the list contains a certain string + boolean containsMilk = List.contains("Milk"); + System.out.println("Contains Milk: " + containsMilk); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + for (int i = 0; i < List.size(); i++) { + System.out.println(i + " " + List.get(i)); + }; // Sort the list using the Collections library + java.util.Collections.sort(List); // Iterate over the list using a for-each loop // Print each value on a second line + for (String s : List) { + System.out.println(s); + }; /* * Usage tip! diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..109e0f7 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,46 @@ - +import java.util.HashMap; 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 - + HashMap map = new HashMap(); + // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + map.put("a", 1); + map.put("b", 2); + map.put("c", 3); // Get the value associated with a given key in the Map + System.out.println(map.get("a")); // Find the size (number of key/value pairs) of the Map + System.out.println(map.size()); - // Replace the value associated with a given key (the size of the Map shoukld not change) + // Replace the value associated with a given key (the size of the Map should not change) + map.replace("a", 4); // Check whether the Map contains a given key + System.out.println(map.containsKey("a")); // Check whether the Map contains a given value + System.out.println(map.containsValue(4)); // Iterate over the keys of the Map, printing each key + for (String key : map.keySet()) { + System.out.println(key); + }; // Iterate over the values of the map, printing each value + for (Integer value : map.values()) { + System.out.println(value); + }; // Iterate over the entries in the map, printing each key and value + for (HashMap.Entry entry : map.entrySet()) { + System.out.println(entry.getKey() + " " + entry.getValue()); + }; /* * Usage tip! diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..c28fa8b 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +1,27 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float negFloat = -5.5f; // Create an int with a positive value and assign it to a variable + int posInt = 34; // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = 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 (remainder == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + }; // Divide the number by another number using integer division + int division = posInt / 3; + System.out.println(division); /* * Reminder! diff --git a/src/Person.java b/src/Person.java index 8ab3f95..182dc5d 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,13 +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 + public String toString() { + return name + " is " + age + " years old."; + } // Implement the below public instance method "birthYear" @@ -28,25 +36,35 @@ 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 tyler = new Person("Tyler", 29); // Create another instance of Person with a different name and age and // assign it to a different variable + Person brittany = new Person("Brittany", 30); // Print the first person + System.out.println(tyler); // Print the second person + System.out.println(brittany); // Get the name of the first person and store it in a local variable + String personOneName = tyler.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 = tyler.birthYear(2025); // In a separate statement, print the local variable holding the birth year. + System.out.println(birthYear); /** * Terminology! diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..903ca3c 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,32 @@ +import java.util.Set; +import java.util.HashSet; public class SetPractice { public static void main(String[] args) { // Create a HashSet of Strings and assign it to a variable of type Set + Set set = new HashSet(); // Add 3 elements to the set // (It's OK to do it one-by-one) + set.add("Tyler"); + set.add("Bonnie"); + set.add("Brodie"); // Check whether the Set contains a given String + boolean containsTyler = set.contains("Tyler"); + System.out.println("Does 'set' contain Tyler? " + containsTyler); // Remove an element from the Set + set.remove("Tyler"); + System.out.println(set); // Get the size of the Set + int size = set.size(); + System.out.println(size); // Iterate over the elements of the Set, printing each one on a separate line + for (String name : set) { + System.out.println(name); + }; /* * Warning! diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..968c99f 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 hello = "Hello, World!"; // Find the length of the string + hello.length(); // Concatenate (add) two strings together and reassign the result + String goodbye = hello + " Goodbye!"; // Find the value of the character at index 3 + char charAtIndex3 = goodbye.charAt(3); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + boolean containsWorld = goodbye.contains("World"); + System.out.println("Does the string contain 'World'? " + containsWorld); // Iterate over the characters of the string, printing each one on a separate line + for (int i = 0; i < goodbye.length(); i++) { + System.out.println(goodbye.charAt(i)); + }; // Create an ArrayList of Strings and assign it to a variable + ArrayList list = new ArrayList(); // Add multiple strings to the List (OK to do one-by-one) + list.add("Welcome"); + list.add("to"); + list.add("Green River"); // 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(", ", list); + System.out.println(joined); // Check whether two strings are equal + boolean isEqual = "Hello".equals("Hello"); + System.out.println(isEqual); /* * Reminder!