diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..9562248 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,13 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "jdk", + "request": "launch", + "name": "Launch Java App" + } + ] +} \ No newline at end of file diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..d17c12e 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[] arrayOf4SizeStrings = {"Home", "Part", "Love", "Mans"}; // Set the value of the array at each index to be a different String // It's OK to do this one-by-one - + arrayOf4SizeStrings[0] = "Love"; + arrayOf4SizeStrings[1] = "Joys"; + arrayOf4SizeStrings[2] = "Freedom"; + arrayOf4SizeStrings[3] = "Labor"; // Get the value of the array at index 2 - + System.out.println(arrayOf4SizeStrings[2]); // Get the length of the array - + System.out.println(arrayOf4SizeStrings.length); // Iterate over the array using a traditional for loop and print out each item - + for (int i = 0; i < arrayOf4SizeStrings.length; i++){ + System.out.println(i); + } // Iterate over the array using a for-each loop and print out each item - + for (String n : arrayOf4SizeStrings){ + System.out.println(n); + } /* * Reminder! * diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..50dfdd6 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,43 @@ +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 theList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + theList.add("Thinga"); + theList.add("Thingb"); + theList.add("Thingc"); // Print the element at index 1 - + System.out.println("\nElement at index 1: "+ theList.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) - + theList.set(1, "Grape"); + System.out.println("After replacing index 1: " + theList); // Insert a new element at index 0 (the length of the list will change) - - // Check whether the list contains a certain string - + theList.add("Thing"); + // Check whether the list contains a certain string' + if (theList.contains("Thing")){ + System.out.println("Thing detected"); + } // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + int increment = 0; + for (String n : theList){ + System.out.println("Id: " + increment + " | " + n); + increment += 1; + } // Sort the list using the Collections library - + Collections.sort(theList); // Iterate over the list using a for-each loop // Print each value on a second line - + for (String options : theList) { + System.out.println(options); + } /* * Usage tip! * diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..fc15197 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,50 @@ - +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 userScores = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) - + userScores.put("Alice", 2); + userScores.put("Bob", 1); + userScores.put("Charlie", 3); // Get the value associated with a given key in the Map - + userScores.get("Bob"); // Find the size (number of key/value pairs) of the Map - + System.out.println(userScores.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) - + userScores.replace("Bob", 7); // Check whether the Map contains a given key - + if (userScores.containsKey("Bob")){ + System.out.println("Contains key"); + } + else { + System.out.println("Does not contain key"); + } // Check whether the Map contains a given value - + if (userScores.containsValue("7")) { + System.out.println("Does contain 7"); + } + else { + System.out.println("Does not contain 7"); + } // Iterate over the keys of the Map, printing each key - + for (String key : userScores.keySet()) { + System.out.println(key); + } // Iterate over the values of the map, printing each value - + for (Integer value : userScores.values()) { + System.out.println(value); + } // Iterate over the entries in the map, printing each key and value - + for (String k : userScores.keySet()){ + int defOfPair = userScores.get(k); + System.out.println(k + " " + defOfPair); + } /* * Usage tip! * diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..3fbaf2b 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,25 +1,33 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + float negative = -1; + System.out.println(negative); // Create an int with a positive value and assign it to a variable - + int positive= 1; + System.out.println(positive); // Use the modulo % operator to find the remainder when the int is divided by 3 - + int modulo = 1 % 3; + System.out.println(modulo); // 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 (1 % 2 == 0) { + System.out.println("Even"); + } + else{ + System.out.println("Odd"); + } // Divide the number by another number using integer division - + double divided = 1.0 / 5; + System.out.println(divided); /* * Reminder! * * When dividing ints, the result is rounded down. * Example: - * 7 / 3 = 2 when performing int division + * 7 / 3 = 2 when performing int division` */ - } } diff --git a/src/Person.java b/src/Person.java index 8ab3f95..f887d8a 100644 --- a/src/Person.java +++ b/src/Person.java @@ -4,16 +4,25 @@ */ public class Person { + + public Person(String jeff, int par) { + } // 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 NameOfPerson; + private int AgeOfPerson; // Create a constructor that takes the name and age of the person // and assigns it to the instance variables - + public void PersonConstructor(String name, int age) { + this.NameOfPerson = name; + this.AgeOfPerson = age; + } // Create a toString method that gives the name and age of the person - + public String toString() { + return NameOfPerson + ": " + AgeOfPerson; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,26 +37,29 @@ public class Person { * @return The year the person was born */ // (create the instance method here) + public int birthYear(int currentYear){ + return currentYear - AgeOfPerson; + } public static void main(String[] args) { // Create an instance of Person - + Person person1 = new Person("Jeff", 26); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person person2 = new Person("Chelsea", 36); // Print the first person - + System.out.println(person1); // Print the second person - + System.out.println(person2); // Get the name of the first person and store it in a local variable - + Person storedPerson = person1; // 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 birthdateOfPerson = person1.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(birthdateOfPerson); /** * Terminology! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..b9a37c6 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,49 @@ + +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 this5Chars = "Lovely"; // Find the length of the string - + System.out.println(this5Chars.length()); // Concatenate (add) two strings together and reassign the result + String thing1 = "Wow"; + String thing2 = "zers"; + this5Chars = (thing1 + thing2 + "!"); + System.out.println(this5Chars); // Find the value of the character at index 3 - + char val3 = this5Chars.charAt(3); + System.out.println("Val 3: " + val3); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + if (this5Chars.contains("owz")) { + System.out.println("Contains owz"); + } // Iterate over the characters of the string, printing each one on a separate line - + for (int i = 0; i < this5Chars.length(); i++) { + char character = this5Chars.charAt(i); + System.out.println(character); + } // 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("TH"); + stringList.add("LIFE LIVE LOVE"); + stringList.add("Live laugh love actually"); // 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 string1 = "Who"; + String string2 = "Who"; + if (string1 == string2){ + System.out.println("Equal"); + } + else { + System.out.println("Not equal"); + } /* * Reminder! * diff --git a/toRefresh.md b/toRefresh.md index 163dcab..2aecce1 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,6 @@ 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 +- Adding to lists +- Constructors / this.[enter] statements +- Map<> functions \ No newline at end of file