diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..d707f3e 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,36 @@ +import java.util.Arrays; + public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String array[] = new String[4]; + System.out.println(Arrays.toString(array)); // 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("------------------------------------------------"); + for(int i = 0; i < array.length; i++) { + array[i] = "one" + 1; + } + System.out.println(Arrays.toString(array)); // Get the value of the array at index 2 + System.out.println("------------------------------------------------"); + System.out.println(array[2]); + System.out.println("------------------------------------------------"); // Get the length of the array - + System.out.println(array.length); + System.out.println("------------------------------------------------"); // Iterate over the array using a traditional for loop and print out each item + for(int i = 0; i < array.length; i++) { + System.out.println(array[i]); + } + System.out.println("------------------------------------------------"); // Iterate over the array using a for-each loop and print out each item - + for(String i : array) { + System.out.println(i); + } /* * Reminder! * diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..66a35f7 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,26 +1,44 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + 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 groceries = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + groceries.add("tomatoes"); + groceries.add("snacks"); + groceries.add("protein powder"); // Print the element at index 1 - + System.out.println(groceries.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) - + System.out.println(groceries.set(1, "best snacks")); + System.out.println(groceries); // Insert a new element at index 0 (the length of the list will change) - + groceries.add(0, "oranges"); + System.out.println(groceries); // Check whether the list contains a certain string - + System.out.println(groceries.contains("oranges")); // Iterate over the list using a traditional for-loop. + for(int i = 0; i < groceries.size(); i++) { + System.out.println(groceries.get(i)); + } // Print each index and value on a separate line - + for(int i = 0; i < groceries.size(); i++ ) { + System.out.println(groceries.get(i) + " is at index " + i); + } // Sort the list using the Collections library - + Collections.sort(groceries); + System.out.println(groceries); // Iterate over the list using a for-each loop + System.out.println("-------------------------------------------------------------"); + for(String i : groceries) { + System.out.println(i); + } // Print each value on a second line /* diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..88e37db 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,49 @@ - +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 + // assign it to a variable of type Map + Map phoneBook = new HashMap<>(); // Put 3 different key/value pairs in the Map - // (it's OK to do this one-by-one) + phoneBook.put("Gamma", 122); + phoneBook.put("Beta", 143); + phoneBook.put("Sylo", 176); + System.out.println(phoneBook); + System.out.println("------------------------------------------------"); // Get the value associated with a given key in the Map - + System.out.println(phoneBook.get("Gamma")); // Find the size (number of key/value pairs) of the Map - + System.out.println("------------------------------------------------"); + System.out.println(phoneBook.size()); + System.out.println("------------------------------------------------"); // Replace the value associated with a given key (the size of the Map shoukld not change) - + phoneBook.put("Jeff", 122); + System.out.println(phoneBook.get("Jeff")); + System.out.println("------------------------------------------------"); + System.out.println(phoneBook); // Check whether the Map contains a given key - + System.out.println("------------------------------------------------"); + System.out.println(phoneBook.containsKey("Alpha")); // Check whether the Map contains a given value - + System.out.println("------------------------------------------------"); + System.out.println(phoneBook.containsValue(122)); // Iterate over the keys of the Map, printing each key - + System.out.println("------------------------------------------------"); + System.out.println(phoneBook.keySet()); // Iterate over the values of the map, printing each value - + System.out.println("------------------------------------------------"); + System.out.println(phoneBook.values()); // Iterate over the entries in the map, printing each key and value + System.out.println("------------------------------------------------"); + + for(Map.Entry entry : phoneBook.entrySet()) { + System.out.println("Key: " + entry.getKey() + " value: " + entry.getValue()); + } /* * Usage tip! * diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..188e028 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,18 +1,40 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + float negativeInt = -4; + System.out.println(negativeInt); // Create an int with a positive value and assign it to a variable - + int positiveInt = 4; + System.out.println(positiveInt); // Use the modulo % operator to find the remainder when the int is divided by 3 - + double negativeRemainder = negativeInt % 3; + double positiveRemainder = positiveInt % 3; + System.out.println(negativeRemainder); + System.out.println(positiveRemainder); // 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) + + if(positiveInt % 2 == 0) { + System.out.println(positiveInt + " has no remainder when divisible by 2"); + } + + if(negativeInt % 2 == 0) { + System.out.println(negativeInt + " has no remainder when divisible by 2"); + } // Use an if-else to print "Even" if the number is even and "Odd" // if the number is odd. + int num = 10; + if(num % 2 == 0) { + System.out.println(num + " is an even number"); + } + else { + System.out.println(num + " is an odd number"); + } // Divide the number by another number using integer division + double result = (double) num / 3; + System.out.println(result); /* * Reminder! * diff --git a/src/Person.java b/src/Person.java index 8ab3f95..c255162 100644 --- a/src/Person.java +++ b/src/Person.java @@ -5,15 +5,21 @@ public class 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 - + 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 - + 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. @@ -28,26 +34,33 @@ public class Person { * @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 Alex = new Person("Alex", 22); // Create another instance of Person with a different name and age and // assign it to a different variable + Person Brittany = new Person("Brittany", 21); // Print the first person - + System.out.println(Alex); // Print the second person - + System.out.println(Brittany); // Get the name of the first person and store it in a local variable - + String firstPerson = Alex.name; + String secondPerson = Brittany.name; // Using the birthYear method, get the birth year of the first person + int birthYear = Alex.birthYear(2026); + int birthYear2 = Brittany.birthYear(2026); // 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. - + System.out.println(firstPerson + "'s " + "birth-year is " + birthYear); + System.out.println(secondPerson + "'s " + "birth-year is " + birthYear2); /** * Terminology! * diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..fa038b9 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,29 @@ +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 something = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) - + something.add("hello"); + something.add("i'm"); + something.add("alex"); // Check whether the Set contains a given String - + System.out.println(something.contains("i'm")); + System.out.println("------------------------------------------------"); // Remove an element from the Set - + something.remove("i'm"); + System.out.println(something); // Get the size of the Set - + System.out.println("------------------------------------------------"); + System.out.println(something.size()); // Iterate over the elements of the Set, printing each one on a separate line - + System.out.println("------------------------------------------------"); + for(String i : something) { + System.out.println(i); + } /* * Warning! * diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..7b252ad 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,62 @@ +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 character = "batman"; + System.out.println(character); + System.out.println("------------------------------------------------"); // Find the length of the string - + System.out.println(character.length()); + System.out.println("------------------------------------------------"); // Concatenate (add) two strings together and reassign the result + String prefix = "Mr."; - // Find the value of the character at index 3 + String person = prefix + character; + System.out.println(person); + System.out.println("------------------------------------------------"); + // Find the value of the character at index 3 + System.out.println(person.charAt(3)); + System.out.println("------------------------------------------------"); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + if(person.contains("bat")) { + System.out.println("True"); + } + else { + System.out.println("False"); + } // Iterate over the characters of the string, printing each one on a separate line - + for(int i = 0; i < person.length(); i++) { + System.out.println(person.charAt(i)); + } + System.out.println("------------------------------------------------"); // Create an ArrayList of Strings and assign it to a variable - + List dictionary = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + dictionary.add("I want"); + dictionary.add("to go to bed"); + dictionary.add("because i am tired"); + dictionary.add("of coding"); + System.out.println(dictionary); + System.out.println("------------------------------------------------"); // 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(", ", dictionary); + System.out.println(joined); // Check whether two strings are equal + String one = "how're you doing"; + String two = "how are you doing"; + System.out.println("------------------------------------------------"); + if(one.equals(two)) { + System.out.println("Equal!"); + } + else { + System.out.println("Not equal :("); + } /* * Reminder! *