diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..8aba92b 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,40 @@ -public class ArrayPractice { - public static void main(String[] args) { +public class ArrayPractice +{ + public static void main(String[] args) + { // Create an array of Strings of size 4 + String[] newArray = 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 + newArray[0] = "apple"; + newArray[1] = "banana"; + newArray[2] = "strawberry"; + newArray[3] = "pear"; // Get the value of the array at index 2 + String value = newArray[2]; + System.out.println(value); + // Get the length of the array + int length = newArray.length; + System.out.println(length); + // Iterate over the array using a traditional for loop and print out each item - // Iterate over the array using a for-each loop and print out each item + for(int i=0; i newList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) + newList.add("apple"); + newList.add("banana"); + newList.add("orange"); + // Print the element at index 1 + System.out.println(newList.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) + newList.set(1, "pear"); + // Insert a new element at index 0 (the length of the list will change) + newList.set(0, "strawberry"); + // Check whether the list contains a certain string + boolean check = newList.contains("orange"); + System.out.println(check); + // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for(int i=0; i newMap = new HashMap<>(); + // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + newMap.put("bob", 25); + newMap.put("tom", 30); + newMap.put("jim", 28); + // Get the value associated with a given key in the Map + int value = newMap.get("tom"); + System.out.println(value); + // Find the size (number of key/value pairs) of the Map + int size = newMap.size(); + System.out.println(size); + // Replace the value associated with a given key (the size of the Map shoukld not change) + newMap.put("tom", 22); + // Check whether the Map contains a given key + boolean checkKey = newMap.containsKey("tom"); + System.out.println(checkKey); + // Check whether the Map contains a given value + boolean checkValue = newMap.containsValue(22); + System.out.println(checkValue); + // Iterate over the keys of the Map, printing each key + for(String key: newMap.keySet()) + { + System.out.println(key); + } + // Iterate over the values of the map, printing each value + for(Integer values: newMap.values()) + { + System.out.println(values); + } + // Iterate over the entries in the map, printing each key and value + + for (Map.Entry entry : newMap.entrySet()) + { + + System.out.println("key: " + entry.getKey() + ", value: " + entry.getValue()); + } /* * Usage tip! diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..018e913 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -2,17 +2,38 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float num1 = -42.3f; + System.out.println(num1); + // Create an int with a positive value and assign it to a variable + int num2 = 7; + System.out.println(num2); + // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = num2 % 3; + System.out.println(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 (num2 % 2 == 0) + { + System.out.println("Even"); + } + else{ + System.out.println("Odd"); + } + // Divide the number by another number using integer division + int sum = num2 / 3; + System.out.println(sum); + + /* * Reminder! * diff --git a/src/Person.java b/src/Person.java index 8ab3f95..2f37287 100644 --- a/src/Person.java +++ b/src/Person.java @@ -7,13 +7,28 @@ 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. @@ -29,24 +44,44 @@ public class Person { */ // (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("Bob", 25); + // Create another instance of Person with a different name and age and // assign it to a different variable + Person person2 = new Person("Jim", 20); + // Print the first person + System.out.println(person1.toString()); + // Print the second person + System.out.println(person2.toString()); + // Get the name of the first person and store it in a local variable + String name = person1.name; + System.out.println(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 bYear = person1.birthYear(2025); + + // In a separate statement, print the local variable holding the birth year. + System.out.println("birth year: " + bYear); /** * Terminology! diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..64fbb5f 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,43 @@ -public class SetPractice { - public static void main(String[] args) { +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 newSet = new HashSet<>(); + // Add 3 elements to the set // (It's OK to do it one-by-one) + newSet.add("apple"); + newSet.add("banana"); + newSet.add("orange"); + // Check whether the Set contains a given String + boolean check = newSet.contains("apple"); + System.out.println(check); + // Remove an element from the Set + newSet.remove("orange"); + + // Get the size of the Set + int size = newSet.size(); + System.out.println("Size: " + size); + // Iterate over the elements of the Set, printing each one on a separate line + for(String fruit: newSet) + { + System.out.println(fruit); + } + /* * Warning! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..37bdd68 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,65 @@ +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 name = "Matthew"; + System.out.println(name); + // Find the length of the string + int length = name.length(); + System.out.println("length: " + length); + + // Concatenate (add) two strings together and reassign the result + String word1 = "hello"; + String word2 = "world"; + String word3 = word1 + word2; + System.out.println(word3); + // Find the value of the character at index 3 + char value = word3.charAt(3); + System.out.println("character at index 3: " + value); + + // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + boolean contains = word3.contains("low"); + System.out.println(contains); + // Iterate over the characters of the string, printing each one on a separate line + for (int i = 0; i < word3.length(); i++) + { + System.out.println(word3.charAt(i)); + } + // Create an ArrayList of Strings and assign it to a variable + List newList = new ArrayList<>(); + // Add multiple strings to the List (OK to do one-by-one) + newList.add("apple"); + newList.add("banana"); + newList.add("strawberry"); + newList.add("orange"); // 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(", ", newList); + System.out.println(joined); + // Check whether two strings are equal + boolean test = word1.equals(word2); + System.out.println(test); + /* * Reminder! * diff --git a/toRefresh.md b/toRefresh.md index 163dcab..10bec6e 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,7 @@ 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 +- entrySet() +- constructor syntax +- String.join() +- import statement syntax \ No newline at end of file