diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..1834adc 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,31 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] myArray = 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 + myArray[0] = "A"; + myArray[1] = "B"; + myArray[2] = "C"; + myArray[3] = "D"; + // Get the value of the array at index 2 + System.out.println(myArray[2]); // Get the length of the array + System.out.println(myArray.length); // Iterate over the array using a traditional for loop and print out each item + for(int i = 0; i > myArray.length; i++){ + System.out.println(myArray[i]); + } // Iterate over the array using a for-each loop and print out each item + for(String letter : myArray){ + System.out.println(letter); + } /* * Reminder! @@ -19,4 +33,4 @@ public static void main(String[] args) { * Arrays start at index 0 */ } -} +} \ No newline at end of file diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..bc838d2 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,37 @@ +import java.util.*; // needs to be imported to run, don't know the specifics for collections, so importing utils. + 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 list = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) - + list.add("A"); + list.add("B"); + list.add("C"); // Print the element at index 1 - + System.out.println(list.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) - + list.set(1,"D"); // Insert a new element at index 0 (the length of the list will change) - + list.add(0, "E"); // Check whether the list contains a certain string - + System.out.println("Contains E?: " + list.contains("E")); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + for(int i = 0; i < list.size(); i++){ + System.out.println("Index: " + i + ". Value: " + list.get(i)); + } // Sort the list using the Collections library - + Collections.sort(list); // Iterate over the list using a for-each loop // Print each value on a second line - + for(String listLetter : list){ + + System.out.println("Value: " + listLetter); + } /* * Usage tip! * diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..8c67f9a 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,37 @@ - +import java.util.*; 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 mapsPractice = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) - + mapsPractice.put("hello", 0); + mapsPractice.put("neutral", 1); + mapsPractice.put("goodbye", 2); // Get the value associated with a given key in the Map - + mapsPractice.get("hello"); // Find the size (number of key/value pairs) of the Map - + mapsPractice.size(); // Replace the value associated with a given key (the size of the Map shoukld not change) - + mapsPractice.replace("hello", 3); // Check whether the Map contains a given key - + mapsPractice.containsKey("goodbye"); // Check whether the Map contains a given value - + mapsPractice.containsValue(5); // Iterate over the keys of the Map, printing each key - + for(String entry : mapsPractice.keySet()){ + System.out.println(entry); + } // Iterate over the values of the map, printing each value - + for(int entry : mapsPractice.values()){ + System.out.println(entry); + } // Iterate over the entries in the map, printing each key and value - + for(String entry : mapsPractice.keySet()){ + System.out.println("Key: " + entry + ". Value: " + mapsPractice.get(entry)); + } /* * Usage tip! * diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..22de854 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,26 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + float i = -0.1f; // Create an int with a positive value and assign it to a variable - + int k = 4; // Use the modulo % operator to find the remainder when the int is divided by 3 - + int l = k%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(k % 2 == 1){ + System.out.println("Odd"); + } + else{ + System.out.println("Even"); + } // Divide the number by another number using integer division - + int a = 4; + int b = 2; + int c = a / b; /* * Reminder! * diff --git a/src/Person.java b/src/Person.java index 8ab3f95..f0d8841 100644 --- a/src/Person.java +++ b/src/Person.java @@ -3,17 +3,25 @@ * the Person class. */ +import java.util.*; + 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 = -99; // Create a constructor that takes the name and age of the person // and assigns it to the instance variables - + public Person(String x, int y){ + name = x; + age = y; + } // 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. @@ -28,26 +36,32 @@ public class Person { * @return The year the person was born */ // (create the instance method here) - + public int birthYear(int currentYear){ + return(currentYear - age); + } + // Created a method to get name easier. + public String getName(){ + return(name); + } public static void main(String[] args) { // Create an instance of Person - + Person jim = new Person("Jim", 45); // Create another instance of Person with a different name and age and // assign it to a different variable - + Person tim = new Person("Tim", 25); // Print the first person - + System.out.println(jim); // Print the second person - + System.out.println(tim); // Get the name of the first person and store it in a local variable - + String grabbedName = jim.getName(); // 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 jimBirthYear = jim.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(jimBirthYear); /** * Terminology! * diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..dd57218 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,24 @@ +import java.util.*; + public class SetPractice { public static void main(String[] args) { // Create a HashSet of Strings and assign it to a variable of type Set - + Set hashSetPractice = new HashSet(); // Add 3 elements to the set // (It's OK to do it one-by-one) - + hashSetPractice.add("good morning"); + hashSetPractice.add("good afternoon"); + hashSetPractice.add("good night"); // Check whether the Set contains a given String - + hashSetPractice.contains("good morning"); // Remove an element from the Set - + hashSetPractice.remove("good night"); // Get the size of the Set - + hashSetPractice.size(); // Iterate over the elements of the Set, printing each one on a separate line - + for(String saying : hashSetPractice){ + System.out.println(saying); + } /* * Warning! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..f1280e4 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,38 @@ 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 = "ABCDE"; // Find the length of the string - + a.length(); // Concatenate (add) two strings together and reassign the result - + String b = "FGHIJ"; + String c = a + b; // Find the value of the character at index 3 - + c.charAt(3); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + c.contains("BCD"); // Iterate over the characters of the string, printing each one on a separate line - + for(int i = 0; i < c.length(); i++){ + System.out.println(c.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable - + String[] myArray = new String[4]; // Add multiple strings to the List (OK to do one-by-one) - + myArray[0] = "A"; + myArray[1] = "B"; + myArray[2] = "C"; + myArray[3] = "D"; // 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(", ", myArray); // Check whether two strings are equal - + String d = "ABCDE"; + if(a.equals(d)){ + System.out.println("strings match up."); + } + else{ + System.out.println("strings don't match up."); + } /* * Reminder! *