diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..40d8e0d 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,22 +1,13 @@ 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 - - // Get the value of the array at index 2 - - // Get the length of the array - - // Iterate over the array using a traditional for loop and print out each item - - // Iterate over the array using a for-each loop and print out each item - - /* - * Reminder! - * - * Arrays start at index 0 - */ + String[] cueists = {"Efren","Francisco","Johann","AJ"}; + System.out.println(cueists[2]); + System.out.println(cueists.length); + for (int i=0; i < cueists.length; i++) { + System.out.println(cueists[i]); + } + for (String cueist : cueists) { + System.out.println(cueist); + } } } diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..d8775e9 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,36 +1,31 @@ +import java.util.*; + public class ListPractice { public static void main(String[] args) { - // Create an empty ArrayList of Strings and assign it to a variable of type List - - // 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! - * - * Use a traditional for-loop when you need to use the index or you need to iterate in an - * unconventional order (e.g. backwards) - * - * Otherwise, if you're iterating the in the conventional order and don't need the - * index values a for-each loop is cleaner. - */ + + ArrayList poolEquipment = new ArrayList<>(); + poolEquipment.add("Cue"); + poolEquipment.add("Pool Balls"); + poolEquipment.add("Chalk"); + System.err.println(poolEquipment); + + System.out.println(poolEquipment.get(1)); + poolEquipment.set(1,"Pool Table"); + System.err.println(poolEquipment); + poolEquipment.add(0, "Glove"); + System.err.println(poolEquipment); + + for (int i=0;i contactID = new HashMap<>(); + + contactID.put("Johann",00); + contactID.put("Efren",01); + contactID.put("Francisco",02); + + System.out.println(contactID.get("Efren")); + + System.out.println(contactID.size()); + + contactID.put("Francisco",04); + System.out.println(contactID.get("Francisco")); + + System.out.println("Does the HashMap Contain Efren? " + contactID.containsKey("Efren")); + System.out.println("Does the HashMap Contain 05? " + contactID.containsValue(05)); + + for (String i : contactID.keySet()) { + System.out.println(i); + } + for (Integer k : contactID.values()) { + System.out.println(k); + } + for (String j : contactID.keySet()) { + System.out.println("Keys: " + j + " Values: " + contactID.get(j)); + } } } diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..038cb98 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,25 +1,21 @@ + public class NumberPractice { public static void main(String args[]) { - // Create a float with a negative value and assign it to a variable - - // Create an int with a positive value and assign it to a variable - - // Use the modulo % operator to find the remainder when the int is divided by 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. + double numFloat = -1.2; + int numInt = 4; - // Divide the number by another number using integer division + System.out.println(numFloat); + System.out.println(numInt); - /* - * Reminder! - * - * When dividing ints, the result is rounded down. - * Example: - * 7 / 3 = 2 when performing int division - */ + System.out.println(numInt%2); + + if (numInt%2==0) { + System.out.println(numInt + " is even!"); + } + else { + System.out.println(numInt + " is negative!"); + } + System.out.println(numFloat/4); } } diff --git a/src/Person.java b/src/Person.java index 8ab3f95..a14d617 100644 --- a/src/Person.java +++ b/src/Person.java @@ -1,20 +1,28 @@ -/* - * 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 - - - // Create a constructor that takes the name and age of the person - // and assigns it to the instance variables + public String name; + private final int age; + + public Person(String name, int age) { + this.name = name; + this.age = age; + } + public String getName() { + return name; + } - // Create a toString method that gives the name and age of the person + public int getAge() { + return age; + } + public String string() { + return name + age; + } + public int birthyear(int currentyear) { + return currentyear - age; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. /** @@ -31,22 +39,17 @@ public class Person { 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 person1 = new Person("RJ",20); + Person person2 = new Person("Yao",19); + + System.out.println(person1.string()); + System.out.println(person2.string()); + + String person1name = person1.getName(); + System.out.println(person1name); + + int byear = person1.birthyear(2025); + System.out.println(byear); /** * Terminology! diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..3f619fe 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,13 +1,21 @@ +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 + HashSet set = new HashSet<>(); + set.add("Philippines"); + set.add("USA"); + set.add("Canada"); - // Add 3 elements to the set - // (It's OK to do it one-by-one) + System.out.println("Contains Philippines? " + set.contains("Philippines")); + + set.remove("USA"); - // Check whether the Set contains a given String + System.out.println("Size of the HashSet: " + set.size()); - // Remove an element from the Set + for (String i : set) { + System.out.println(i); + } // Get the size of the Set diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..75e3201 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,32 +1,29 @@ -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 +import java.util.Arrays; - // Find the value of the character at index 3 +public class StringPractice { + public static void main(String[] args) { + String word = "Billiards"; + System.out.println("Length of word: " + word.length()); - // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + String word2 = "Snooker"; - // Iterate over the characters of the string, printing each one on a separate line + System.out.println(word.concat(word2)); - // Create an ArrayList of Strings and assign it to a variable + System.out.println(word.charAt(3)); - // Add multiple strings to the List (OK to do one-by-one) + String subString = "abc"; + System.out.println("Does string contain: " + subString + "? " + word.contains(subString)); - // 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 + char[] charArray = word.toCharArray(); + for (int i = 0; i < charArray.length; i++) { + System.out.println(charArray[i]); + } - // Check whether two strings are equal + String[] wordList = {"Carom","Bank","Billiard","Kick","Masse"}; + String arrStr = Arrays.toString(wordList); + System.out.println(arrStr); - /* - * Reminder! - * - * When comparing objects in Java we typically want to use .equals, NOT ==. - * - * We use == when comparing primitives (e.g. int or char). - */ + System.out.println("Strings equal? " + word.equals(word2)); } }