diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..beaab04 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,38 @@ +import java.util.Arrays; public class ArrayPractice { public static void main(String[] args) { - // Create an array of Strings of size 4 + // Create an array of Strings of size 4 + String[] arr = {"bahram", "Ibrahim", "Kamran", "Asif"}; + System.out.println("First Array " + Arrays.toString(arr)); // Set the value of the array at each index to be a different String - // It's OK to do this one-by-one - + // It's OK to do this one-by-one + String[] newArray = new String[4]; + + newArray[0] = "Ibrahim"; + newArray[1] = "Bahram"; + newArray[2] = "Kamran"; + newArray[3] = "Asif"; + System.out.println("Second Array" + Arrays.toString(newArray)); // Get the value of the array at index 2 + System.out.println("Second Value " + newArray[1]); // Get the length of the array + System.out.println("Length of Array " + newArray.length); // Iterate over the array using a traditional for loop and print out each item + System.out.println("After for Loop " ); + for(int i = 0; i < 4 ; i++){ + System.out.println(newArray[i]); + } // Iterate over the array using a for-each loop and print out each item + System.out.println("After for each Loop "); + for(String names: newArray){ + System.out.println(names); + } + + /* * Reminder! diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..4d4988a 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,55 @@ +import java.util.ArrayList; +import java.util.Collections; 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 namesList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + namesList.add("Bahram"); + namesList.add("Ibrahim"); + namesList.add("Kamran"); + namesList.add("Asif"); + System.out.println(namesList); // Print the element at index 1 + System.out.println(namesList.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) + namesList.set(1, "Abdul"); + System.out.println(namesList); // Insert a new element at index 0 (the length of the list will change) + namesList.add("Ikram"); + System.out.println(namesList); // Check whether the list contains a certain string + String searchName = "Bahra"; + if(namesList.contains(searchName)){ + System.out.println("The nameList have " + searchName ); + }else{ + System.out.println(searchName + " is not on the List"); + } // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for(int i = 0; i < namesList.size(); i++){ + System.out.println("Index " + i + ": " + namesList.get(i)); + } // Sort the list using the Collections library + Collections.sort(namesList); + System.out.println("After the list is sorted " + namesList); // Iterate over the list using a for-each loop // Print each value on a second line + for(String newNamesList: namesList){ + System.out.println(); + System.out.println(newNamesList); + } /* * Usage tip! diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..730d233 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,30 +1,50 @@ - +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 - - // 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 - - // Find the size (number of key/value pairs) of the Map - - // Replace the value associated with a given key (the size of the Map shoukld not change) - - // Check whether the Map contains a given key - - // Check whether the Map contains a given value - - // Iterate over the keys of the Map, printing each key - - // Iterate over the values of the map, printing each value - - // Iterate over the entries in the map, printing each key and value - - /* + // Create a HashMap with String keys and Integer values + Map map = new HashMap<>(); + + // Add 3 key/value pairs to the Map + map.put("Alice", 25); + map.put("Bob", 30); + map.put("Charlie", 35); + + // Get the value for the key "Alice" + System.out.println("Alice's age: " + map.get("Alice")); + + // Find the size of the Map + System.out.println("Number of entries in the Map: " + map.size()); + + // Replace Bob's age + map.put("Bob", 40); + System.out.println("Bob's new age: " + map.get("Bob")); + + // Check if the Map contains the key "Charlie" + System.out.println("Map contains Charlie: " + map.containsKey("Charlie")); + + // Check if the Map contains the value 40 + System.out.println("Map contains age 40: " + map.containsValue(40)); + + // Print all keys + System.out.println("Keys in the Map:"); + for (String key : map.keySet()) { + System.out.println(key); + } + + // Print all values + System.out.println("Values in the Map:"); + for (Integer value : map.values()) { + System.out.println(value); + } + + // Print all key/value pairs + System.out.println("Entries in the Map:"); + for (Map.Entry entry : map.entrySet()) { + System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()); + } + /* * Usage tip! * * Maps are great when you want a specific key to value mapping. @@ -40,4 +60,4 @@ public static void main(String[] args) { * and you don't care about any ordering. */ } -} +} \ No newline at end of file diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..d15f2b4 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +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 negativeFloat = -5.5f; // Create an int with a positive value and assign it to a variable + int positiveInt = 42; // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = positiveInt % 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 (positiveInt % 2 == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + } // Divide the number by another number using integer division + int quotient = positiveInt / 7; /* * Reminder! @@ -20,6 +29,5 @@ public static void main(String args[]) { * Example: * 7 / 3 = 2 when performing int division */ - } } diff --git a/src/Person.java b/src/Person.java index 8ab3f95..17b7d8a 100644 --- a/src/Person.java +++ b/src/Person.java @@ -2,18 +2,25 @@ * 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,27 +34,33 @@ 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("Bahram", 25); // Create another instance of Person with a different name and age and // assign it to a different variable + Person person2 = new Person("Ibrahim", 30); // 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 firstName = 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. - - // In a separate statement, print the local variable holding the birth year. + // and store it in a local variable. Input the actual current year (e.g., 2025) + int firstPersonBirthYear = person1.birthYear(2025); + // In a separate statement, print the local variable holding the birth year + System.out.println("Birth Year of " + firstName + ": " + firstPersonBirthYear); /** * Terminology! * diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..2598245 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,32 @@ +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 stringSet = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + stringSet.add("Apple"); + stringSet.add("Banana"); + stringSet.add("Cherry"); // Check whether the Set contains a given String + boolean containsBanana = stringSet.contains("Banana"); + System.out.println("Set contains 'Banana': " + containsBanana); // Remove an element from the Set + stringSet.remove("Apple"); // 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 + for (String element : stringSet) { + System.out.println(element); + } /* * Warning! diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..59cdd0b 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,51 @@ +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 + String str = "HelloWorld"; // Find the length of the string + int length = str.length(); + System.out.println("Length of the string: " + length); // Concatenate (add) two strings together and reassign the result + str = str + " Java"; + System.out.println("Concatenated string: " + str); // Find the value of the character at index 3 + char charAtIndex3 = str.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?) + // Check whether the string contains a given substring (i.e., does the string have "abc" in it?) + boolean containsSubstring = str.contains("abc"); + System.out.println("String contains 'abc': " + 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 : str.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 str1 = "Hello"; + String str2 = "Hello"; + boolean areEqual = str1.equals(str2); + System.out.println("Strings are equal: " + areEqual); /* * Reminder!