diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..42949ef 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,26 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[] myArr = 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 + myArr[0] = "Hello"; + myArr[1] = "Bonjour"; + myArr[2] = "Hola"; + myArr[3] = "Salam"; // Get the value of the array at index 2 - + System.out.println("Value at index 2: " + myArr[2]); // Get the length of the array - + System.out.println("Length of the array: " + myArr.length); // Iterate over the array using a traditional for loop and print out each item - + for (int i = 0; i < myArr.length; i++) { + System.out.println(myArr[i]); + } // Iterate over the array using a for-each loop and print out each item - + for (String item : myArr) { + System.out.println(item); + } /* * Reminder! * diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..a77bc51 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,41 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + 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("Apple"); + myList.add("Cherry"); + myList.add("Banana"); // Print the element at index 1 - + System.out.println("Element at index 1: " + 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, "Strawberry"); + System.out.println("Size of the List after the first insertion: " + myList.size()); // Insert a new element at index 0 (the length of the list will change) - + myList.set(0, "Kiwi"); + System.out.println("Size of the List after the second insertion: " + myList.size()); // Check whether the list contains a certain string - + System.out.println(myList.contains("Apple")); // 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("Index " + i + ": " + myList.get(i)); + } // Sort the list using the Collections library - + Collections.sort(myList); + System.out.println("Sorted List: " + myList); // Iterate over the list using a for-each loop // Print each value on a second line - + for (String fruit : myList) { + System.out.println(fruit); + } /* * Usage tip! * diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..e7637b2 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,39 @@ - +import java.util.HashMap; +import java.util.Map; 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("Alice", 75); + myMap.put("Brandon", 88); + myMap.put("Emily", 96); // Get the value associated with a given key in the Map - + System.out.println("Valued assosiated with a given key of 'Alice': " + myMap.get("Alice")); // Find the size (number of key/value pairs) of the Map - - // Replace the value associated with a given key (the size of the Map shoukld not change) - + System.out.println("Size of the Map: " + myMap.size()); + // Replace the value associated with a given key (the size of the Map should not change) + myMap.put("Alice", 76); + System.out.println("Updated value for 'Alice' key: " + myMap.get("Alice")); // Check whether the Map contains a given key - + System.out.println("Does the Map contains the key 'Edward'? " + myMap.containsKey("Edward")); // Check whether the Map contains a given value - + System.out.println("Does the Map contains the value 88? " + myMap.containsValue(88)); // 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 (Map.Entry entry : myMap.entrySet()) { + System.out.println("Key: " + entry.getKey() + " | Value: " + entry.getValue()); + } /* * Usage tip! * diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..819a9bb 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 + float floatNum = -7.7f; // Create an int with a positive value and assign it to a variable - + int intNum = 9; // Use the modulo % operator to find the remainder when the int is divided by 3 - + int remainder = intNum % 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 (intNum % 2 == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + } // Divide the number by another number using integer division - + int divResult = 6 / 2; + System.out.println("Result of the division by 2: " + divResult); /* * Reminder! * diff --git a/src/Person.java b/src/Person.java index 8ab3f95..ed039f2 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,21 +6,27 @@ 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 - - + @Override + 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. /** * birthYear returns the year the person was born. * - * The birth year is calculated by subtracting the person's age from currentYear + * The birth year is calculated by subtracting the person's age from current Year * that's passed in as an int. It assumes that the person's birthday has already * passed this year. * @@ -28,26 +34,28 @@ 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("Ali", 22); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person secondPerson = new Person("Mark", 18); // Print the first person - + System.out.println(firstPerson); // Print the second person - + System.out.println(secondPerson); // Get the name of the first person and store it in a local variable - + String firstPersonName = firstPerson.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 firstPersonBirthYear = firstPerson.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(firstPersonName + "'s Birth year: " + firstPersonBirthYear); /** * Terminology! * diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..f414985 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,25 @@ +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 mySet = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) - + mySet.add("Math"); + mySet.add("English"); + mySet.add("Music"); // Check whether the Set contains a given String - + System.out.println("Does the Set contains Math? " + mySet.contains("Math")); // Remove an element from the Set - + mySet.remove("Math"); // Get the size of the Set - + System.out.println("Size of the Set: " + mySet.size()); // Iterate over the elements of the Set, printing each one on a separate line - + for (String subject : mySet) { + System.out.println(subject); + } /* * Warning! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..bed9386 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,35 @@ +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 myWord = "Welcome"; // Find the length of the string - + System.out.println("Length of the Word: " + myWord.length()); // Concatenate (add) two strings together and reassign the result - + myWord = myWord + " Friend"; + System.out.println("Concatenated String: " + myWord); // Find the value of the character at index 3 - + System.out.println("Value of the character at index 3 is " + myWord.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + System.out.println("Does the String have 'abc' in it? " + myWord.contains("abc")); // Iterate over the characters of the string, printing each one on a separate line - + for (int i = 0; i < myWord.length(); i++) { + System.out.println(myWord.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable - + List stringList = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) - + stringList.add("Cucumber"); + stringList.add("Carrot"); + stringList.add("Pepper"); // 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 - + System.out.println("Joined String: " + String.join(", ", stringList)); // Check whether two strings are equal - + String anotherWord = "Welcome Friend"; + System.out.println("Are the Strings equal to each other? " + myWord.equals(anotherWord)); /* * Reminder! * diff --git a/toRefresh.md b/toRefresh.md index 163dcab..9a2c96f 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,5 @@ 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 +- Float Variables +- Java Classes and instance fields \ No newline at end of file