diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..c1debfc 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,28 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[] num = 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 + num[0] = "kaalid"; + num[1] = "john"; + num[2] = "tyler"; + num[3] = "liban"; // Get the value of the array at index 2 - + System.out.println(num[1].toString()); // Get the length of the array - + System.out.println(num.length); // Iterate over the array using a traditional for loop and print out each item - + System.out.println(); + for(int i = 0; i < num.length; i++) { + System.out.println(num[i]); + } // Iterate over the array using a for-each loop and print out each item - + System.out.println(); + for(String n : num) { + System.out.println(n); + } /* * Reminder! * diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..d0bdbf3 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,40 @@ +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 - + ArrayList arr = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + arr.add("john"); + arr.add("kaalid"); + arr.add("tyler"); // Print the element at index 1 - + System.out.println(arr.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) - + arr.set(0,"david"); + System.out.println(arr.get(0)); // Insert a new element at index 0 (the length of the list will change) - + arr.add(0, "liban"); + System.out.println(arr.get(0)); // Check whether the list contains a certain string - + System.out.println(arr.contains("david")); + System.out.println(); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + for(int i = 0; i < arr.size(); i++) { + System.out.println(arr.get(i)); + } // Sort the list using the Collections library - + Collections.sort(arr); // Iterate over the list using a for-each loop // Print each value on a second line - + System.out.println(); + for(int i = 0; i < arr.size(); i++) { + System.out.println(arr.get(i)); + } /* * Usage tip! * diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..0b7ef99 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,54 @@ +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 map = new HashMap<>(); // Put 3 different key/value pairs in the Map - // (it's OK to do this one-by-one) + map.put("Apples", 5); + map.put("Bananas", 10); + map.put("Oranges", 7); // Get the value associated with a given key in the Map + int applesCount = map.get("Apples"); + System.out.println("Value associated with key 'Apples': " + applesCount); // Find the size (number of key/value pairs) of the Map + int mapSize = map.size(); + System.out.println("Size of the Map: " + mapSize); - // Replace the value associated with a given key (the size of the Map shoukld not change) + // Replace the value associated with a given key (the size of the Map should not change) + map.put("Apples", 8); + System.out.println("Updated value for 'Apples': " + map.get("Apples")); // Check whether the Map contains a given key + boolean containsKey = map.containsKey("Bananas"); + System.out.println("Map contains key 'Bananas': " + containsKey); // Check whether the Map contains a given value + boolean containsValue = map.containsValue(10); + System.out.println("Map contains value 10: " + containsValue); // Iterate over the keys of the Map, printing each key + System.out.println("Keys in the Map:"); + for (String key : map.keySet()) { + System.out.println(key); + } - // Iterate over the values of the map, printing each value + // Iterate over the values of the Map, printing each value + System.out.println("Values in the Map:"); + for (int value : map.values()) { + System.out.println(value); + } - // Iterate over the entries in the map, printing each key and value + // Iterate over the entries in the Map, printing each key and value + System.out.println("Entries in the Map:"); + for (Map.Entry entry : map.entrySet()) { + System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()); + } /* * Usage tip! diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..a9da264 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +1,29 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float negativeFloat = -12.34f; + System.out.println("Negative float: " + negativeFloat); // Create an int with a positive value and assign it to a variable + int positiveInt = 25; + System.out.println("Positive int: " + positiveInt); // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainderWhenDividedBy3 = positiveInt % 3; + System.out.println("Remainder when " + positiveInt + " is divided by 3: " + remainderWhenDividedBy3); // 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 (positiveInt % 2 == 0) { + System.out.println(positiveInt + " is Even"); + } else { + System.out.println(positiveInt + " is Odd"); + } // Divide the number by another number using integer division + int divisor = 4; + int result = positiveInt / divisor; + System.out.println("Result of integer division (" + positiveInt + " / " + divisor + "): " + result); /* * Reminder! diff --git a/src/Person.java b/src/Person.java index 8ab3f95..ecee2c9 100644 --- a/src/Person.java +++ b/src/Person.java @@ -3,17 +3,30 @@ * the Person class. */ -public class Person { +/* + * 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 + 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 - + 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 - + @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. @@ -27,26 +40,34 @@ public class Person { * @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("Alice", 30); // Create another instance of Person with a different name and age and // assign it to a different variable + Person person2 = new Person("Bob", 25); // 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 person1Name = 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 person1BirthYear = person1.birthYear(2025); // In a separate statement, print the local variable holding the birth year. + System.out.println("Birth year of " + person1Name + ": " + person1BirthYear); /** * Terminology! diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..0dab036 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +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 + // Create a HashSet of Strings and assign it to a variable of type Set + HashSet stringSet = new HashSet(); - // Add 3 elements to the set - // (It's OK to do it one-by-one) + // Add 3 elements to the set + stringSet.add("Apple"); + stringSet.add("Banana"); + stringSet.add("Cherry"); - // Check whether the Set contains a given String + // Check whether the Set contains a given String + String checkElement = "Banana"; + if (stringSet.contains(checkElement)) { + System.out.println("The set contains: " + checkElement); + } else { + System.out.println("The set does not contain: " + checkElement); + } - // Remove an element from the Set + // Remove an element from the Set + stringSet.remove("Apple"); + System.out.println("After removal " + stringSet); - // Get the size of the Set + // Get the size of the Set + int setSize = stringSet.size(); + System.out.println("Size of the set: " + setSize); - // Iterate over the elements of the Set, printing each one on a separate line + // Iterate over the elements of the Set, printing each one on a separate line + System.out.println("Elements in the set:"); + for (String element : stringSet) { + System.out.println(element); + } /* * Warning! diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..d4870c7 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,55 @@ +import java.util.ArrayList; +import java.util.List; + 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 + String myString = "HelloWorld"; + + // Find the length of the string + int length = myString.length(); + System.out.println("Length of the string: " + length); + + // Concatenate (add) two strings together and reassign the result + myString = myString + " Java"; + System.out.println("Concatenated string: " + myString); + + // Find the value of the character at index 3 + char charAtIndex3 = myString.charAt(3); + System.out.println("Character at index 3: " + charAtIndex3); + + // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + String substring = "abc"; + boolean containsSubstring = myString.contains(substring); + System.out.println("Does the string contain \"" + substring + "\"? " + containsSubstring); + + // Iterate over the characters of the string, printing each one on a separate line + System.out.println("Characters in the string:"); + for (char c : myString.toCharArray()) { + System.out.println(c); + } + + // Create an ArrayList of Strings and assign it to a variable + List stringList = new ArrayList<>(); + + // Add multiple strings to the List (OK to do one-by-one) + stringList.add("Apple"); + stringList.add("Banana"); + stringList.add("Cherry"); + + // 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 joinedString = String.join(", ", stringList); + System.out.println("Joined string: " + joinedString); + + // Check whether two strings are equal + String string1 = "Hello"; + String string2 = "hello"; + boolean areEqual = string1.equals(string2); // Case-sensitive comparison + boolean areEqualIgnoreCase = string1.equalsIgnoreCase(string2); // Case-insensitive comparison + System.out.println("Are the strings equal (case-sensitive)? " + areEqual); + System.out.println("Are the strings equal (case-insensitive)? " + areEqualIgnoreCase); + /* * Reminder!