diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..39ee997 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -2,17 +2,36 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] array = 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 + array[0] = "Danny"; + array[1] = "Programming"; + array[2] = "Green River College"; + array[3] = "Auburn Center"; // Get the value of the array at index 2 + String index2 = array[2]; + + System.out.println("Index 2 value is " + index2); + // Get the length of the array + int arrayLength = array.length; + // Iterate over the array using a traditional for loop and print out each item + for(int i = 0; i <= arrayLength - 1; i++){ + System.out.println(array[i]);; + } // Iterate over the array using a for-each loop and print out each item + for(String string : array){ + System.out.println(string); + } + /* * Reminder! * diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..dd6e2a0 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,52 @@ +import java.util.ArrayList; +import java.util.Collection; +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 list = new ArrayList<>(); + // Add 3 elements to the list (OK to do one-by-one) + list.add("One"); + list.add("two"); + list.add("three"); // Print the element at index 1 + System.out.println(list.get(0)); // 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(0, "four"); + // Insert a new element at index 0 (the length of the list will change) + list.add(0, "five"); // Check whether the list contains a certain string + list.contains("five"); + // 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() - 1; i++){ + System.out.println(i + " : " + 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 num : list){ + System.out.println(num); + } /* * Usage tip! diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..387cc2a 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,61 @@ +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 map = new HashMap<>(); + // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + map.put("Seattle", 1); + map.put("California", 2); + map.put("Auburn", 3); + // Get the value associated with a given key in the Map + map.get("Seattle"); + + // Find the size (number of key/value pairs) of the Map + map.size(); + // Replace the value associated with a given key (the size of the Map shoukld not change) + map.replace("Seattle", 10); + // Check whether the Map contains a given key + map.containsKey("Seattle"); + // Check whether the Map contains a given value + map.containsValue(10); // Iterate over the keys of the Map, printing each key + for(String key : map.keySet()){ + System.out.println(key); + } + // Iterate over the values of the map, printing each value + for (int value : map.values()){ + System.out.println(value); + } + // Iterate over the entries in the map, printing each key and value + for(Map.Entry entry : map.entrySet()){ + System.out.println(entry); + } + /* * Usage tip! * diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..d4e8546 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -2,16 +2,26 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float negativeFloat = -.05f; // Create an int with a positive value and assign it to a variable + int num = 6; // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = num % 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(num % 2 == 0){ + System.out.println("Even"); + } else{ + System.out.println("Odd"); + } + // Divide the number by another number using integer division + int divValue = num / 3 ; /* * Reminder! diff --git a/src/Person.java b/src/Person.java index 8ab3f95..6bf3f0c 100644 --- a/src/Person.java +++ b/src/Person.java @@ -4,63 +4,83 @@ */ 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 + // Declare a public String instance variable for the name of the person + public String name; - // Create a constructor that takes the name and age of the person - // and assigns it to the instance variables + // Declare a private int instance variable for the age of the person + 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 - - - // 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 - * that's passed in as an int. It assumes that the person's birthday has already - * passed this year. - * - * @param currentYear an int for the current year - * @return The year the person was born - */ - // (create the instance method here) - - - public static void main(String[] args) { - // Create an instance of Person - - // Create another instance of Person with a different name and age and - // assign it to a different variable - - // Print the first person - - // Print the second person - - // Get the name of the first person and store it in a local variable - - // 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. - - // In a separate statement, print the local variable holding the birth year. + // 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. /** - * Terminology! - * - * A class is the overall definition, like a blueprint. - * An instance is a specific object made according to that definition. - * We use "instance" and "object" to mean the same thing. - * - * For example, if there is a Person class, we can make an instance of a specific person: Auberon. - * - * There can be many instances for the same class. For example: Auberon, Xinting, Baya are all - * different instances of the Person class. - * - * Each instance has its own instance variables: Auberon's age can be different from Baya's age. + * birthYear returns the year the person was born. + * + * The birth year is calculated by subtracting the person's age from + * currentYear that's passed in as an int. It assumes that the person's + * birthday has already passed this year. + * + * @param currentYear an int for the current year + * @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 person1 = new Person("Danny", 21); + // Create another instance of Person with a different name and age and + // assign it to a different variable + + Person person2 = new Person("Jordan", 22); + + // 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 + String personOneName = 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 birthYear = person1.birthYear(2025); + + // In a separate statement, print the local variable holding the birth year. + System.out.println("Birth Year: " + birthYear); + /** + * Terminology! + * + * A class is the overall definition, like a blueprint. An instance is a + * specific object made according to that definition. We use "instance" + * and "object" to mean the same thing. + * + * For example, if there is a Person class, we can make an instance of a + * specific person: Auberon. + * + * There can be many instances for the same class. For example: Auberon, + * Xinting, Baya are all different instances of the Person class. + * + * Each instance has its own instance variables: Auberon's age can be + * different from Baya's age. + */ + } } diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..646581f 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,37 @@ +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 set = new HashSet<>(); + // Add 3 elements to the set // (It's OK to do it one-by-one) + set.add("String 1"); + set.add("String 2"); + set.add("String 3"); + // Check whether the Set contains a given String + set.contains("String 1"); + // Remove an element from the Set + set.remove("String 1"); + // Get the size of the Set + set.size(); + // Iterate over the elements of the Set, printing each one on a separate line + for(String string : set){ + System.out.println(string); + } + /* * Warning! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..a362636 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,61 @@ +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 string = "JavaScript"; + // Find the length of the string + int stringLen = string.length(); + // Concatenate (add) two strings together and reassign the result + String result = string.concat("Java"); + + + + // Find the value of the character at index 3 + result.charAt(3); + // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + result.contains("abc"); + // Iterate over the characters of the string, printing each one on a separate line + for(int i = 0; i < stringLen; i++){ + char ch = string.charAt(i); + System.out.println(ch); + } + + // Create an ArrayList of Strings and assign it to a variable + ArrayList list = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + list.add("First"); + list.add("Second"); + list.add("Third"); + // 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 joinResult = String.join(",", list); + System.out.println(joinResult); // Check whether two strings are equal + String string1 = "Words"; + String string2 = "Word"; + + if(string1.equals(string2)){ + System.out.println("Strings are equal"); + } else{ + System.out.println("Strings are not equal"); + } /* * Reminder!