diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..109013c 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,25 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[] a = {"Mario", "Luigi", "Wario", "Waluigi"}; // Set the value of the array at each index to be a different String // It's OK to do this one-by-one - + a[0] = "Link"; + a[1] = "Zelda"; + a[2] = "Ganon"; + a[3] = "Demise"; // Get the value of the array at index 2 - + System.out.println(a[2]); // Get the length of the array - + System.out.println(a.length); // Iterate over the array using a traditional for loop and print out each item - + for(int i = 0; i < a.length; i++) { + System.out.println(a[i]); + } // Iterate over the array using a for-each loop and print out each item - + for (String b : a) { + System.out.println(b); + } /* * Reminder! * diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..be18d34 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,41 @@ +import java.util.ArrayList; +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 a = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) - + a.add("lol"); + a.add("zoz"); + a.add("tot"); // Print the element at index 1 - + System.out.println (a.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) - + a.set(1, "viv"); // Insert a new element at index 0 (the length of the list will change) - + a.add(0, "Gog"); // Check whether the list contains a certain string - + for (int i = 0; i < a.size(); i++) { + if (a.get(i) == "viv") { + System.out.println("Found viv"); + } + } // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + for (int i = 0; i < a.size(); i++) { + System.out.println(i); + System.out.println(a.get(i)); + } // Sort the list using the Collections library - + Collections.sort(a); // Iterate over the list using a for-each loop // Print each value on a second line - + for (String x : a) { + System.out.println(x); } /* * Usage tip! * diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..a9c0bf9 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,38 @@ - +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 a = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) - + a.put("bib", 1); + a.put("viv", 2); + a.put("gig", 3); // Get the value associated with a given key in the Map - + System.out.println(a.get("gig")); // Find the size (number of key/value pairs) of the Map - + System.out.println(a.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) - + a.put("bib", 7); // Check whether the Map contains a given key - + a.containsKey("viv"); // Check whether the Map contains a given value - + a.containsValue(7); // Iterate over the keys of the Map, printing each key - + for( int i = 0; i < a.size(); i++) { + System.out.println(a.keySet(i)); + } // Iterate over the values of the map, printing each value - + for ( int i = 0; i < a.size(); i++) { + System.out.println(a.get(i)); + } // Iterate over the entries in the map, printing each key and value - + for ( int i = 0; i < a.size(); i++) { + System.out.println(a.keySet(i)); + System.out.println(a.get(i)); + } /* * Usage tip! * diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..6696bf8 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,28 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + float a = (-5); // Create an int with a positive value and assign it to a variable - + int b = 7; // Use the modulo % operator to find the remainder when the int is divided by 3 - + int c = b%3; // Use the modulo % operator to determine whether the number is even + boolean d = false; + if (c%2 == 0) { + d = true; + } // (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 (d == true) { + System.out.println("even"); //unsure why it inserted an X, and whenever I try to delete it it also deletes the quotes + } + else { + System.out.println("odd"); + } // if the number is odd. // Divide the number by another number using integer division - + int e = b/c; /* * Reminder! * diff --git a/src/Person.java b/src/Person.java index 8ab3f95..3c7a622 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,14 +6,20 @@ 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 x, Integer y) { + name = x; + age = y; + } // 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. @@ -28,26 +34,29 @@ public class Person { * @return The year the person was born */ // (create the instance method here) - + int birthYear (int x) { + return (x - age); + } public static void main(String[] args) { // Create an instance of Person - + Person fred = new Person("Fred", 15); // Create another instance of Person with a different name and age and // assign it to a different variable + Person sarah = new Person("Sarah", 27); // Print the first person - + System.out.println(fred.toString()); // Print the second person - + System.out.println(sarah.toString()); // Get the name of the first person and store it in a local variable - + String client = fred.toString(); // 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 clientbirthday = fred.birthYear(2015); // In a separate statement, print the local variable holding the birth year. - + System.out.println(clientbirthday); /** * Terminology! * diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..4651c63 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,26 @@ +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 a = new HashSet(); // Add 3 elements to the set + a.add("black"); + a.add("blue"); + a.add("red"); // (It's OK to do it one-by-one) // Check whether the Set contains a given String - + a.contains("red"); // Remove an element from the Set - + a.remove("black"); // Get the size of the Set - + System.out.println(a.size()); // Iterate over the elements of the Set, printing each one on a separate line - + for(String b : a) { + System.out.println(b); + } + } /* * Warning! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..097fe69 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,35 @@ +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 a = "fight"; // Find the length of the string - + System.out.println(a); // Concatenate (add) two strings together and reassign the result - + a = a + " me!"; // Find the value of the character at index 3 - + System.out.println(a.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + if (a.contains("ight")) { + System.out.println("Ight found!"); + } // Iterate over the characters of the string, printing each one on a separate line - + for (int b = 0; b < a.length(); b++) { + System.out.println(a.charAt(b)); + } // Create an ArrayList of Strings and assign it to a variable - + ArrayList c = new ArrayList(); // Add multiple strings to the List (OK to do one-by-one) - + c.add("VI"); + c.add("IV"); + c.add("XI"); // 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 combined = String.join(",", c); + System.out.println(combined); // Check whether two strings are equal - + System.out.println(a.equals(c)); /* * Reminder! *