diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..264386a 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,22 +1,37 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] arrayFour = 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 + arrayFour[0] = "ZERO"; + arrayFour[1] = "ONE"; + arrayFour[2] = "TWO"; + arrayFour[3] = "THREE"; // Get the value of the array at index 2 + System.out.println(arrayFour[2]); //output: TWO // Get the length of the array + System.out.println(arrayFour.length); // Iterate over the array using a traditional for loop and print out each item + for (int i = 0; i < arrayFour.length; i++) { + System.out.println(arrayFour[i]); + } // Iterate over the array using a for-each loop and print out each item + for (String string : arrayFour) { + System.out.println(string); + } /* * Reminder! * * Arrays start at index 0 */ + + //DONE } } diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..ea6d505 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,49 @@ +import java.util.ArrayList; +import java.util.Collections; + 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 stringList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) + stringList.add("APPLE"); + stringList.add("BANANA"); + stringList.add("KIWI"); // Print the element at index 1 - + System.out.println(stringList.get(1)); //output: BANANA + // 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, "STRAWBERRY"); // Insert a new element at index 0 (the length of the list will change) + stringList.add(0, "ORANGE"); // Check whether the list contains a certain string + if (stringList.contains("AVOCADO")) { + System.out.println("We have avocado :)"); + } else { + System.out.println("There is no avocado :("); + } // 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(i + ": " + stringList.get(i)); + } // Sort the list using the Collections library + Collections.sort(stringList); // Iterate over the list using a for-each loop // Print each value on a second line + for (String fruit : stringList) { + System.out.println(fruit); + } /* * Usage tip! @@ -32,5 +54,7 @@ public static void main(String[] args) { * Otherwise, if you're iterating the in the conventional order and don't need the * index values a for-each loop is cleaner. */ + + //DONE } } \ No newline at end of file diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..9222d4e 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,55 @@ - +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 mappy = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + mappy.put("a", 1); + mappy.put("b", 2); + mappy.put("c", 3); // Get the value associated with a given key in the Map + mappy.get("a"); // Find the size (number of key/value pairs) of the Map + mappy.size(); // Replace the value associated with a given key (the size of the Map shoukld not change) + mappy.replace("a", 0); // Check whether the Map contains a given key + if (mappy.containsKey("d")) { + System.out.println("Letter d, is present within list MAPPY"); + } else { + System.out.println("There is no key present"); + } // Check whether the Map contains a given value + if (mappy.containsValue(2)) { + System.out.println("Value 2, is present within list MAPPY"); + } else { + System.out.println("There is no value present"); + } // Iterate over the keys of the Map, printing each key + for (String key : mappy.keySet()) { + System.out.println(key); + } // Iterate over the values of the map, printing each value + for (Integer value : mappy.values()) { + System.out.println(value); + } // Iterate over the entries in the map, printing each key and value + for (HashMap.Entry entries : mappy.entrySet()) { + System.out.println("KEY " + entries.getKey()); + System.out.println("VALUE " + entries.getValue()); + } /* * Usage tip! @@ -39,5 +66,7 @@ public static void main(String[] args) { * Example: If you want to hold the student ID numbers of everyone in a course, * and you don't care about any ordering. */ + + //DONE } } diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..0611474 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 negativeVal = -123f; // Create an int with a positive value and assign it to a variable + int positiveVal = 123; // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = positiveVal % 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 (positiveVal % 2 == 0) { + System.out.println("EVEN"); + } else { + System.out.println("ODD"); + } // Divide the number by another number using integer division + int division = positiveVal / 3; + System.out.println(division); /* * Reminder! @@ -20,6 +30,6 @@ public static void main(String args[]) { * Example: * 7 / 3 = 2 when performing int division */ - + //DONE } } diff --git a/src/Person.java b/src/Person.java index 8ab3f95..0ede534 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 "Name: " + name + ", Age: " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,25 +34,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 personOne = new Person("Caitlyn Kiramman", 24); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person personTwo = new Person("Mel Medarda", 33); + // Print the first person + System.out.println(personOne); // Print the second person + System.out.println(personTwo); // Get the name of the first person and store it in a local variable + String ArcaneChara1 = personOne.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 currentYear = 2025; + int birthYear1 = personOne.birthYear(currentYear); // In a separate statement, print the local variable holding the birth year. + System.out.println(personOne.name + "Birth Year: " + birthYear1); /** * Terminology! @@ -62,5 +78,7 @@ public static void main(String[] args) { * * Each instance has its own instance variables: Auberon's age can be different from Baya's age. */ + + //DONE } } diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..2e8cfae 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,29 @@ +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 + HashSet StringSet = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + StringSet.add("TOMATO"); + StringSet.add("POTATO"); + StringSet.add("YAM"); // Check whether the Set contains a given String + StringSet.contains("CABBAGE"); // Remove an element from the Set + StringSet.remove("TOMATO"); // Get the size of the Set + StringSet.size(); // Iterate over the elements of the Set, printing each one on a separate line + for (String veggie : StringSet) { + System.out.println(veggie); + } /* * Warning! @@ -25,5 +37,7 @@ public static void main(String[] args) { * * Also remember that sets do NOT have duplicates. */ + + //DONE } } diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..5f9457c 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,52 @@ +import java.util.ArrayList; + public class StringPractice { public static void main(String[] args) { // Create a string with at least 5 characters and assign it to a variable + String name = "Luigi"; + System.out.println(name); // Find the length of the string + System.out.println(name.length()); + System.out.println(); // Concatenate (add) two strings together and reassign the result + String nameTwo = "Mario "; + String result = nameTwo + name; + System.out.println(result); // Find the value of the character at index 3 + System.out.println(name.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + boolean substring = name.contains("abc"); + System.out.println(substring); // Iterate over the characters of the string, printing each one on a separate line + for (char letter : name.toCharArray()) { + System.out.println(letter); + } // Create an ArrayList of Strings and assign it to a variable - + ArrayList Spiders = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + Spiders.add("Peter Parker"); + Spiders.add("Miguel O'Hara"); + Spiders.add("Miles Morales"); + Spiders.add("Gwen Stacy"); + Spiders.add("Peni Parker"); // 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 SpiderPeople = String.join(", ", Spiders); + System.out.println(SpiderPeople); + // Check whether two strings are equal + if (Spiders.get(3).equals(4)) { + System.out.println("Their names are equal!"); + } else { + System.out.println("Their names are not equal"); + } /* * Reminder! @@ -28,5 +55,7 @@ public static void main(String[] args) { * * We use == when comparing primitives (e.g. int or char). */ + + //DONE } } diff --git a/toRefresh.md b/toRefresh.md index 163dcab..39d56f1 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,13 @@ 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 +- ArrayList +- Map Entries +- Set vs. Map +- Float +- .toCharArray() + +- Public Private +- Constructor +- toString +- Instance \ No newline at end of file