diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..1f8aff4 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,27 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[] ofFour = 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 + ofFour[0] = "Hello"; + ofFour[1] = "goodbye"; + ofFour[2] = "Hello Again"; + ofFour[3] = "Good Night"; // Get the value of the array at index 2 + System.out.println(ofFour[2]); // Get the length of the array - + System.out.println(ofFour.length); // Iterate over the array using a traditional for loop and print out each item - + for (int i = 0; i < ofFour.length; i++) { + System.out.println(ofFour[i]); + } // Iterate over the array using a for-each loop and print out each item - + for (String string : ofFour) { + System.out.println(string); + } /* * Reminder! * diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..6aed8a5 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,40 @@ +import java.util.ArrayList; +import java.util.Collection; +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 arraylist = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) - + arraylist.add("Yo"); + arraylist.add("Yo2"); + arraylist.add("Yo3"); + // Print the element at index 1 - + System.out.println(arraylist.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) - + arraylist.set(1, "New Value"); + System.out.println(arraylist.get(1)); // Insert a new element at index 0 (the length of the list will change) - + arraylist.add(0, "New 0 index Element"); // Check whether the list contains a certain string - + arraylist.contains("String"); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + for (int i = 0; i < arraylist.size(); i++) { + System.out.println(arraylist.get(i)); + } // Sort the list using the Collections library - + Collections.sort(arraylist); // Iterate over the list using a for-each loop // Print each value on a second line - + for (Object item : arraylist) { + System.out.println(item); + } /* * Usage tip! * diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..62d7b08 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,36 @@ - +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 hmp = new HashMap(); + // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + hmp.put("Number 1", 0); + hmp.put("Number 2", 1); + hmp.put("Number 3", 2); // Get the value associated with a given key in the Map - + hmp.get("Number 1"); // Find the size (number of key/value pairs) of the Map - + System.out.println(hmp.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) - + hmp.replace("Number 1", 00000); // Check whether the Map contains a given key - + hmp.containsKey("Number 4"); // Check whether the Map contains a given value - + hmp.containsValue(3); // Iterate over the keys of the Map, printing each key + //Iterate over the values of the map, printing each value + // Iterate over the entries in the map, printing each key and value - // Iterate over the values of the map, printing each value - - // Iterate over the entries in the map, printing each key and value - + for (String key : hmp.keySet()) { + Integer value = hmp.get(key); + System.out.println("Key: " + key + ", Value: " + value); + } /* * Usage tip! * diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..95db8f3 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,22 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + double negativeValue = -4.0; // Create an int with a positive value and assign it to a variable - + int positveValue = 4; // Use the modulo % operator to find the remainder when the int is divided by 3 - + double remainder = positveValue % 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) + if (remainder % 2 == 0) { + System.out.println("the Number is even"); + } + else {System.out.println("Number is odd");} // Use an if-else to print "Even" if the number is even and "Odd" // if the number is odd. // Divide the number by another number using integer division - + int dividiedInts = 7/7; /* * Reminder! * diff --git a/src/Person.java b/src/Person.java index 8ab3f95..999ad92 100644 --- a/src/Person.java +++ b/src/Person.java @@ -7,13 +7,22 @@ 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 "This persons name is " + name + " and the age is " + age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,26 +37,32 @@ public class Person { * @return The year the person was born */ // (create the instance method here) + public int birthYear(int currentYear) { + return currentYear - this.age; +} public static void main(String[] args) { // Create an instance of Person - + Person person1 = new Person("Liban James", 44); // Create another instance of Person with a different name and age and // assign it to a different variable + Person person2 = new Person("Bial", 323); + // Print the first person + person1.toString(); // Print the second person - + person2.toString(); // Get the name of the first person and store it in a local variable - + String p1Name = person1.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 p1By = person1.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(p1By); /** * Terminology! * diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..c24519e 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,27 @@ +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 hashsetString = new HashSet<>(); + // Add 3 elements to the set // (It's OK to do it one-by-one) + hashsetString.add("Hello"); + hashsetString.add("Hello Again"); + hashsetString.add("Hello Again #2"); // Check whether the Set contains a given String + hashsetString.contains("String"); // Remove an element from the Set - + hashsetString.remove("Hello"); // Get the size of the Set - + System.out.println(hashsetString.size()); // Iterate over the elements of the Set, printing each one on a separate line - + for (String item : hashsetString) { + System.out.println(item); + } /* * Warning! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..26b2b42 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,44 @@ +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 five = "ontwo"; // Find the length of the string - + System.out.println(five.length()); // Concatenate (add) two strings together and reassign the result + String secondString = "another"; + five += secondString; // Find the value of the character at index 3 - + System.out.println(five.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + boolean check = five.contains("abc"); + System.out.println(check); // Iterate over the characters of the string, printing each one on a separate line - + for (int i = 0; i < five.length() ; i++) { + System.out.println(five.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable - + ArrayList newAList = new ArrayList<>(); + // Add multiple strings to the List (OK to do one-by-one) + newAList.add("Hello"); + newAList.add("No"); + newAList.add("Back"); + newAList.add("Afhasiuhs"); // 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 + StringBuilder newString = new StringBuilder(); + for (Object word : newAList) { + newString.append(word); + } + System.out.println(newString); // Check whether two strings are equal - + boolean checker = five.equals(secondString); + System.out.println(checker); /* * Reminder! * diff --git a/toRefresh.md b/toRefresh.md index 163dcab..44ed4fc 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,10 @@ 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 +- How to make an array list +- How to print a element in a arraylist +- How to add a new value (arrayList) +- How to reset a value (arrayList) +- How to make a hashset (also what is a hashset) +- How to iterate through hashmap +- How to make a constructor \ No newline at end of file