diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..4f023b8 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,36 @@ +//Shawn Nguru public class ArrayPractice { public static void main(String[] args) { - // Create an array of Strings of size 4 - // Set the value of the array at each index to be a different String - // It's OK to do this one-by-one + System.out.println("***************************************************"); + // Create an array of Strings of size 4 + String[] names = {"Kaladin", "Sam", "David", "Paul"}; - // Get the value of the array at index 2 + // Set the value of the array at each index to be a different String + // It's OK to do this one-by-one + names[0] = "Kal"; + names[1] = "Sammy"; + names[2] = "Dave"; + names[3] = "Saul"; - // Get the length of the array - // Iterate over the array using a traditional for loop and print out each item + // Get the value of the array at index 2 + System.out.println(names[2]); - // Iterate over the array using a for-each loop and print out each item + // Get the length of the array + int length = names.length; + // Iterate over the array using a traditional for loop and print out each item + for(int i = 0; i < length; i++) + { + System.out.println(names[i]); + } + + // Iterate over the array using a for-each loop and print out each item + for(String tag : names) + { + System.out.println(tag); + } /* * Reminder! * diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..4d663e5 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,52 @@ +//Shawn Nguru public class ListPractice { public static void main(String[] args) { - // Create an empty ArrayList of Strings and assign it to a variable of type List + // 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("one"); + list.add("two"); + list.add("three"); + + // 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,"dos"); + System.out.println("list: " + list); + + // Insert a new element at index 0 (the length of the list will change) + list.add(0,"zero"); + System.out.println("list: " + list); + + // Check whether the list contains a certain string + if(list.contains("three")) + { + System.out.println("It contains that value!"); + } + + // 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(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 i : list) + { + System.out.println(i); + } - // Add 3 elements to the list (OK to do one-by-one) - - // Print the element at index 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) - - // Insert a new element at index 0 (the length of the list will change) - - // Check whether the list contains a certain string - - // Iterate over the list using a traditional for-loop. - // Print each index and value on a separate line - - // Sort the list using the Collections library - - // Iterate over the list using a for-each loop - // Print each value on a second line /* * Usage tip! diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..3706427 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,55 @@ - - +//Shawn Nguru 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 - - // Put 3 different key/value pairs in the Map - // (it's OK to do this one-by-one) - - // Get the value associated with a given key in the Map + // Create a HashMap with String keys and Integer values and + // assign it to a variable of type Map + Map maps = new HashMap<>(); - // Find the size (number of key/value pairs) of the Map + // Put 3 different key/value pairs in the Map + // (it's OK to do this one-by-one) + maps.put("Kal",1); + maps.put("Dalinar",2); + maps.put("Shallan",3); - // Replace the value associated with a given key (the size of the Map shoukld not change) + // Get the value associated with a given key in the Map + System.out.println(maps.get("Dalinar")); - // Check whether the Map contains a given key + // Find the size (number of key/value pairs) of the Map + System.out.println(maps.size()); - // Check whether the Map contains a given value + // Replace the value associated with a given key (the size of the Map should not change) + maps.put("Kal",100); + System.out.println(maps); - // Iterate over the keys of the Map, printing each key + // Check whether the Map contains a given key + if(maps.containsKey("Dalinar")) + { + System.out.println("The Goat"); + } + // Check whether the Map contains a given value + if(maps.containsValue(1)) + { + System.out.println("Kal The Goat"); + } + else System.out.println("N/A"); - // Iterate over the values of the map, printing each value + // Iterate over the keys of the Map, printing each key + for(String i : maps.keySet()) + { + System.out.println(i); + } + // Iterate over the values of the map, printing each value + for(Integer i : maps.values()) + { + System.out.println(i); + } - // Iterate over the entries in the map, printing each key and value + // Iterate over the entries in the map, printing each key and value + for(String i : maps.keySet()) + { + System.out.println("Key: " + i); + System.out.println("Value: " + maps.get(i)); + } /* * Usage tip! diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..1439671 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +1,34 @@ +//Shawn Nguru public class NumberPractice { public static void main(String args[]) { - // Create a float with a negative value and assign it to a variable + // Create a float with a negative value and assign it to a variable + float negativeVal = -12; - // Create an int with a positive value and assign it to a variable + // Create an int with a positive value and assign it to a variable + int positiveVal = 3; - // Use the modulo % operator to find the remainder when the int is divided by 3 + // Use the modulo % operator to find the remainder when the int is divided by 3 + System.out.println(positiveVal%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. + // 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(negativeVal%2 ==0) + { + System.out.println("Even"); + } + System.out.println("Odd"); - // Divide the number by another number using integer division + // Divide the number by another number using integer division + float solution = positiveVal/negativeVal; + /* + * Reminder! + * + * When dividing ints, the result is rounded down. + * Example: + * 7 / 3 = 2 when performing int division + */ /* * Reminder! diff --git a/src/Person.java b/src/Person.java index 8ab3f95..f8f2a76 100644 --- a/src/Person.java +++ b/src/Person.java @@ -1,53 +1,105 @@ +//Shawn Nguru /* * In this file you will follow the comments' instructions to complete * the Person class. */ 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; + // 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 - // Create a constructor that takes the name and age of the person - // and assigns it to the instance variables + Person(String name, int age) + { + this.name = name; + this.age = age; + } - // Create a toString method that gives the name and age of the person + // Create a toString method that gives the name and age of the person + public String toString() + { + return name + " " + age; + } - // 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) + // 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 + */ + public int birthYear(int currentYear) + { + return currentYear - age; + } +} 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. - + Person dude = new Person("Shawn",22); + Person girl = new Person("Alex",23); + + // Create another instance of Person with a different name and age and + // assign it to a different variable + + // Print the first person + System.out.println(dude); + // Print the second person + System.out.println(girl); + + // Get the name of the first person and store it in a local variable + String name = dude.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. + + // In a separate statement, print the local variable holding the birth year. + int age = dude.birthYear(2025); + System.out.println(age); + System.out.println("***************************************************"); + /** + * Person + */ + + // Create a float with a negative value and assign it to a variable + float negativeVal = -12; + + // Create an int with a positive value and assign it to a variable + int positiveVal = 3; + + // Use the modulo % operator to find the remainder when the int is divided by 3 + System.out.println(positiveVal%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(negativeVal%2 ==0) + { + System.out.println("Even"); + } + System.out.println("Odd"); + + // Divide the number by another number using integer division + float solution = positiveVal/negativeVal; + /* + * Reminder! + * + * When dividing ints, the result is rounded down. + * Example: + * 7 / 3 = 2 when performing int division + */ /** * Terminology! * diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..6a178c3 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,29 +1,31 @@ +//Shawn Nguru public class SetPractice { public static void main(String[] args) { - // Create a HashSet of Strings and assign it to a variable of type Set + // 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) + // Add 3 elements to the set + // (It's OK to do it one-by-one) + set.add("Running"); + set.add("Out"); + set.add("of Ideas"); - // Check whether the Set contains a given String + // Check whether the Set contains a given String + if(set.contains("Out")) + { + System.out.println("Out"); + } + // Remove an element from the Set + set.remove("of Ideas"); - // Remove an element from the Set + // Get the size of the Set + System.out.println(set.size()); - // Get the size of the Set + // Iterate over the elements of the Set, printing each one on a separate line + for(String s : set) + { + System.out.println(s); + } - // Iterate over the elements of the Set, printing each one on a separate line - - /* - * Warning! - * - * The iteration order over the items in a HashSet is NOT GUARANTEED. - * - * Even running the exact same program multiple times may give different results. - * Do not use a HashSet if order is important! You can use a TreeSet if you - * want items in sorted order, or an array or List if you want them in a specified - * order. - * - * Also remember that sets do NOT have duplicates. - */ } } diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..c01f6f4 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,32 +1,38 @@ +//Shawn Nguru public class StringPractice { public static void main(String[] args) { - // Create a string with at least 5 characters and assign it to a variable - - // Find the length of the string - - // Concatenate (add) two strings together and reassign the result - - // Find the value of the character at index 3 - - // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - - // Iterate over the characters of the string, printing each one on a separate line - - // Create an ArrayList of Strings and assign it to a variable - - // Add multiple strings to the List (OK to do one-by-one) - - // 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 - - // Check whether two strings are equal - - /* - * Reminder! - * - * When comparing objects in Java we typically want to use .equals, NOT ==. - * - * We use == when comparing primitives (e.g. int or char). - */ + // Create a string with at least 5 characters and assign it to a variable + String five = "fifth"; + + // Find the length of the string + five.length(); + + // Concatenate (add) two strings together and reassign the result + String ten = five + "sixth"; + + // Find the value of the character at index 3 + System.out.println(ten.charAt(3)); + + // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + System.out.println(ten.contains("six")); + + // Iterate over the characters of the string, printing each one on a separate line + for (int i = 0; i < ten.length(); i++) + { + System.out.println(ten.charAt(i)); + } + // Create an ArrayList of Strings and assign it to a variable + // Add multiple strings to the List (OK to do one-by-one) + ArrayList var = new ArrayList<>(); + var.add("all"); + var.add("of"); + var.add("the"); + var.add("string"); + + // 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 joined = String.join(",", var); + // Check whether two strings are equal + System.out.println(joined); } }