diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..dacf0d2 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,34 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[] stringArr = 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 - + stringArr[0] = "Github"; + stringArr[1] = "Gitlab"; + stringArr[2] = "Gitea"; + stringArr[3] = "Bitbucket"; // Get the value of the array at index 2 - + String indexThree = stringArr[2]; + System.out.println(indexThree); + System.out.println(); // Get the length of the array + int arrLen = stringArr.length; + System.out.println(arrLen); + + System.out.println(); // Iterate over the array using a traditional for loop and print out each item + for (int i = 0; i < stringArr.length; i++) { + System.out.println(stringArr[i]); + + } + System.out.println(); // Iterate over the array using a for-each loop and print out each item - + for (String item : stringArr) { + System.out.println(item); + } /* * Reminder! * diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..0b59658 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,42 @@ +import java.util.List; +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 - + List stringList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + stringList.add("Singapore"); + stringList.add("Seoul"); + stringList.add("Shanghai"); // Print the element at index 1 - + stringList.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) - + stringList.set(1, "Sapporo"); // Insert a new element at index 0 (the length of the list will change) - + stringList.add(1, "Singapore"); + System.out.println(stringList); // Check whether the list contains a certain string - + Boolean containsCity = stringList.contains("Seoul"); + System.out.println(containsCity); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line - + for (int i = 0; i < stringList.size(); i++) { + System.out.println(i + " " + stringList.get(i) + " "); + } // Sort the list using the Collections library - + Collections.sort(stringList); + System.out.println(stringList); // Iterate over the list using a for-each loop // Print each value on a second line - + for (String item : stringList) { + System.out.println(item); + } + } /* * Usage tip! * @@ -33,4 +47,3 @@ public static void main(String[] args) { * index values a for-each loop is cleaner. */ } -} \ No newline at end of file diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..b641057 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,52 @@ - +import java.util.Map; +import java.util.HashMap; 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("Mercury", 1); + map.put("Venus", 2); + map.put("Mars", 3); // Get the value associated with a given key in the Map + Integer closestPlanet = map.get("Mercury"); + System.out.println(closestPlanet); // Find the size (number of key/value pairs) of the Map - + int numOfPlanets = map.size(); + System.out.println(numOfPlanets); // Replace the value associated with a given key (the size of the Map shoukld not change) + + map.replace("Mars", 4); + Integer ourPlanet = map.get("Mars"); + System.out.println(ourPlanet); + int newNumOfPlanets = map.size(); + System.out.println(newNumOfPlanets); // Check whether the Map contains a given key + boolean hasMars = map.containsKey("Mars"); + System.out.println(hasMars); // Check whether the Map contains a given value - + boolean hasFive = map.containsValue(5); + System.out.println(hasFive); // Iterate over the keys of the Map, printing each key - + for (Map.Entry entry : map.entrySet()) { + System.out.println(entry.getKey()); + } // Iterate over the values of the map, printing each value - + for (Map.Entry entry : map.entrySet()) { + System.out.println(entry.getValue()); + } // Iterate over the entries in the map, printing each key and value + 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..23d333e 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,42 @@ public class NumberPractice { - public static void main(String args[]) { - // Create a float with a negative value and assign it to a variable + public static void main(String args[]) { - // 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. + // Create a float with a negative value and assign it to a variable + float negativeFloatVal = -5.75f; + System.out.println(negativeFloatVal); + + // Create an int with a positive value and assign it to a variable + int positiveIntVal = 88; + System.out.println(positiveIntVal); + + // Use the modulo % operator to find the remainder when the int is divided by 3 + int divisibleByThree = positiveIntVal % 3; + System.out.println(divisibleByThree); + + // 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. + int[] numsArray = {1, 2, 3, 4, 5, 6}; + evenOrOdd(numsArray); + + int numOne = 9; + int numTwo = 4; + int dividedNumber = divideNumber(numOne, numTwo); + System.out.println(dividedNumber); + } + public static void evenOrOdd(int[] numsArray) { + for (int i = 0; i < numsArray.length; i++) { + if (numsArray[i] % 2 == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + } + } + } + // Divide the number by another number using integer division - /* * Reminder! * @@ -20,6 +44,11 @@ public static void main(String args[]) { * Example: * 7 / 3 = 2 when performing int division */ - - } -} + public static int divideNumber(int numOne, int numTwo) { + if (numOne == 0 || numTwo == 0) { + return 0; + } + int result = numOne / numTwo; + return result; + } +} \ No newline at end of file diff --git a/src/Person.java b/src/Person.java index 8ab3f95..c1e6a20 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,14 +6,22 @@ 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; + 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. @@ -29,24 +37,37 @@ public class Person { */ // (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 Bob = new Person("Bob", 61); // Create another instance of Person with a different name and age and // assign it to a different variable + Person Tommy = new Person("Tommy", 5); // Print the first person + System.out.println(Bob); // Print the second person + System.out.println(Tommy); // Get the name of the first person and store it in a local variable - + String name = Bob.name; + System.out.println(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 AgeOfBob = Bob.birthYear(2025); + // In a separate statement, print the local variable holding the birth year. + System.out.println(AgeOfBob); /** * Terminology! diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..78e2a82 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,27 @@ +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 fruits = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) - + fruits.add("kiwis"); + fruits.add("cherries"); + fruits.add("blueberries"); // Check whether the Set contains a given String - + boolean isAFruit = fruits.contains("blueberries"); + System.out.println(isAFruit); // Remove an element from the Set + fruits.remove("blueberries"); // Get the size of the Set - + System.out.println(fruits.size()); // Iterate over the elements of the Set, printing each one on a separate line + for (String fruit : fruits) { + System.out.println(fruit); + } /* * Warning! diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..8eddf9c 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,55 @@ +import java.util.List; +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 keyboardCharsCaps = "WASD"; // Find the length of the string + int lengthOfKeyboardChars = keyboardCharsCaps.length(); + System.out.println(lengthOfKeyboardChars); // Concatenate (add) two strings together and reassign the result + String part1 = "Postgres"; + String part2 = "SQL"; + + String complete = part1 + part2; + + System.out.println(complete); // Find the value of the character at index 3 + char ch = complete.charAt(3); + System.out.println(ch); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + boolean hasABC = complete.contains("abc"); + System.out.println(hasABC); // Iterate over the characters of the string, printing each one on a separate line + for (int i = 0; i < complete.length(); i++) { + System.out.println(complete.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable + List list = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + list.add("Jupiter"); + list.add("Saturn"); + list.add("Neptune"); + System.out.println(list); // 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 planetsList = String.join(", ", list); + System.out.println(planetsList); + // Check whether two strings are equal + String string1 = "Python"; + String string2 = "C++"; + boolean areEqual = string1.equals(string2); + System.out.println(areEqual); + /* * Reminder! *