diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..feca861 Binary files /dev/null and b/.DS_Store differ diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..6d07f6f 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,29 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] arr = 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 - + arr[0] = "A"; + arr[1] = "B"; + arr[2] = "C"; + arr[3] = "D"; // Get the value of the array at index 2 + System.out.println(arr[2]); // Get the length of the array + System.out.println(arr.length); // Iterate over the array using a traditional for loop and print out each item - + for(int i=0; i arrList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + arrList.add("a"); + arrList.add("b"); + arrList.add("c"); // Print the element at index 1 + System.out.println(arrList.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) + arrList.set(1, "new"); // Insert a new element at index 0 (the length of the list will change) + arrList.add(0,"new 2"); // Check whether the list contains a certain string + System.out.println(arrList.contains("a")); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for(int i=0; i stuff = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + stuff.put("a", 1); + stuff.put("b", 2); + stuff.put("c", 3); // Get the value associated with a given key in the Map + stuff.get("a"); // Find the size (number of key/value pairs) of the Map + stuff.size(); // Replace the value associated with a given key (the size of the Map shoukld not change) + stuff.put("a", 10); // Check whether the Map contains a given key + stuff.containsKey("a"); // Check whether the Map contains a given value + stuff.containsValue(2); // Iterate over the keys of the Map, printing each key + for (Map.Entry entry : stuff.entrySet()) { + System.out.println(entry.getKey()); + } // Iterate over the values of the map, printing each value + for (Map.Entry entry : stuff.entrySet()) { + System.out.println(entry.getValue()); + } // Iterate over the entries in the map, printing each key and value + for (Map.Entry entry : stuff.entrySet()) { + System.out.println(entry.getKey() + " " + entry.getValue()); + } /* * Usage tip! diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..f00e3cd 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +1,24 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + float neg = -1; // Create an int with a positive value and assign it to a variable - + int pos = 2; // Use the modulo % operator to find the remainder when the int is divided by 3 - + System.out.println(pos%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) + System.out.println(pos%2); // Use an if-else to print "Even" if the number is even and "Odd" // if the number is odd. + if(pos%2 == 0){ + System.out.println("Even"); + }else{ + System.out.println("Odd"); + } // Divide the number by another number using integer division + System.out.println(pos/2); /* * Reminder! diff --git a/src/Person.java b/src/Person.java index 8ab3f95..7d12955 100644 --- a/src/Person.java +++ b/src/Person.java @@ -3,17 +3,25 @@ * the Person class. */ +import javax.print.attribute.standard.JobMessageFromOperator; + public class Person { // Declare a public String instance variable for the name 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.age = age; + this.name = name; + } // 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" // There should NOT be any print statement in this method. @@ -27,26 +35,37 @@ 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){ + int date = currentYear - age; + return date; + } public static void main(String[] args) { // Create an instance of Person + Person joe = new Person("Joe", 10); // Create another instance of Person with a different name and age and + Person jill = new Person("Jill", 20); // assign it to a different variable - + Person swimmer = jill; // Print the first person + System.out.println(joe.name); // Print the second person + System.out.println(swimmer.name); // Get the name of the first person and store it in a local variable + String str = joe.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 yr = joe.birthYear(2025); + // In a separate statement, print the local variable holding the birth year. + System.out.println(yr); /** * Terminology! diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..08a941f 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,27 @@ +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 strSet = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + strSet.add("1"); + strSet.add("2"); + strSet.add("3"); // Check whether the Set contains a given String - + strSet.contains("1"); // Remove an element from the Set - + strSet.remove("2"); // Get the size of the Set - + strSet.size(); // Iterate over the elements of the Set, printing each one on a separate line - + for (String strs : strSet) { + System.out.println(strs); + } /* * Warning! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..310ccf3 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,46 @@ +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 str = "12345"; // Find the length of the string + str.length(); // Concatenate (add) two strings together and reassign the result + str = str + "hi"; // Find the value of the character at index 3 + str.charAt(3); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + str.contains("45"); // Iterate over the characters of the string, printing each one on a separate line + for(int i=0; i strList = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + strList.add("aa"); + strList.add("bb"); + strList.add("cc"); + // 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 s = String.join("", strList); - // Check whether two strings are equal + + // Check whether two strings are equal + if(str.equals(strList.get(0))){ + //do stuff + } /* * Reminder! * diff --git a/toRefresh.md b/toRefresh.md index 163dcab..0d123ba 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,20 @@ 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 +- // Divide the number by another number using integer division + + + // Sort the list using the Collections library + arrList.sort(null); + + + // Iterate over the keys of the Map, printing each key + stuff.forEach(System.out.println("stuff")); + + // Iterate over the values of the map, printing each value + //ssame trouble with syntax above + + // Iterate over the entries in the map, printing each key and value + + / 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