diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..dcf8596 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,37 @@ public class ArrayPractice { - public static void main(String[] args) { + public static void main(String[] args) + { // Create an array of Strings of size 4 + String[] array = new String[4]; // Set the value of the array at each index to be a different String + array[0] = "Honda"; + array[1] = "Ford"; + array[2] = "Toyota"; + array[3] = "Chevy"; + // It's OK to do this one-by-one + // Get the value of the array at index 2 + String value = array[2]; + System.out.println(value); // Get the length of the array + int arrayLength = array.length; + System.out.println(arrayLength); // 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]); + + // Iterate over the array using a for-each loop and print out each item + for(String cars : array) + { + System.out.println(cars); + } /* * Reminder! @@ -20,3 +40,5 @@ public static void main(String[] args) { */ } } +//compiling is javac src/"filename" +//running is -cp src "Name of file" \ No newline at end of file diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..b55165e 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,66 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + + + public class ListPractice { - public static void main(String[] args) { + public static void main(String[] args) + { // Create an empty ArrayList of Strings and assign it to a variable of type List + List cars = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) + cars.add("Honda"); + cars.add("Toyota"); + cars.add("Chevy"); // Print the element at index 1 + System.out.println(cars.get(1)); + // Replace the element at index 1 with a new value + cars.set(1, "Tesla"); //integer for the index and string + System.out.println(cars.get(1)); + + // (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) + cars.add(0, "BMW"); + System.out.println(cars.get(0)); // Check whether the list contains a certain string + boolean containsCars = cars.contains("BMW"); + System.out.println(containsCars); // Iterate over the list using a traditional for-loop. + for (int i = 0; i < cars.size(); i++) + System.out.println(cars.get(i)); + // Print each index and value on a separate line + for (int i = 0; i < cars.size(); i++) + System.out.println(i + cars.get(i)); // Sort the list using the Collections library + Collections.sort(cars); + System.out.println(cars); // Iterate over the list using a for-each loop + + for(String cars1: cars) + { + System.out.println(); + System.out.println(cars1); + } + + // Print each value on a second line + /* * Usage tip! * diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..fff674f 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,64 @@ - +import java.util.HashMap; +import java.util.Map; public class MapPractice { - public static void main(String[] args) { + public static void main(String[] args) + { // Create a HashMap with String keys and Integer values and + Map carMap = new HashMap<>(); + + // 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) + carMap.put("Honda", 1); + carMap.put("Ford", 2); + carMap.put("Chevy", 3); + carMap.put("Lexus", 4); // Get the value associated with a given key in the Map + int car = carMap.get("Honda"); + System.out.println(car); // Find the size (number of key/value pairs) of the Map + int car1 = carMap.size(); + System.out.println(car1); + // Replace the value associated with a given key (the size of the Map shoukld not change) + carMap.put("Honda", 3); + System.out.println(carMap.get("Honda")); // Check whether the Map contains a given key + boolean containsCar = carMap.containsKey("Honda"); + System.out.println(containsCar); // Check whether the Map contains a given value + boolean containsValue1 = carMap.containsValue(2); + System.out.println(containsValue1); + // Iterate over the keys of the Map, printing each key + for (String key : carMap.keySet()) + { + System.out.println(key); + } // Iterate over the values of the map, printing each value + for (Integer value : carMap.values()) + { + System.out.println(value); + } // Iterate over the entries in the map, printing each key and value + for(String key : carMap.keySet()) + { + System.out.println(key + " " + carMap.get(key)); + } + /* * Usage tip! diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..9f27672 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +1,37 @@ -public class NumberPractice { - public static void main(String args[]) { +public class NumberPractice +{ + public static void main(String args[]) + { // Create a float with a negative value and assign it to a variable + float negativeValue = -1.0f; + System.out.println(negativeValue); // Create an int with a positive value and assign it to a variable + int value = 2; + System.out.println(value); // Use the modulo % operator to find the remainder when the int is divided by 3 + int mod = value % 3; + System.out.println(mod); + if (value % 2 == 0) + { + System.out.println(value + "is even"); + } + else + { + System.out.println(value + "is odd"); + } + // 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. // Divide the number by another number using integer division + int number = value / 2; + System.out.println(number); + /* * Reminder! diff --git a/src/Person.java b/src/Person.java index 8ab3f95..c42d7da 100644 --- a/src/Person.java +++ b/src/Person.java @@ -3,19 +3,37 @@ * the Person class. */ -public class Person { +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 + public Person(String name, int age) //parametized constructor + { + this.name = name; + this.age = age; + } + // and assigns it to the instance variables // Create a toString method that gives the name and age of the person + public String toString() + { + return name + " " + age; + } // Implement the below public instance method "birthYear" + + + + // There should NOT be any print statement in this method. /** * birthYear returns the year the person was born. @@ -28,25 +46,37 @@ 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 player1 = new Person("Ash", 21); // Create another instance of Person with a different name and age and // assign it to a different variable + Person player2 = new Person("Brock", 22); + // Print the first person + System.out.println(player1); // Print the second person + System.out.println(player2); // Get the name of the first person and store it in a local variable + String mainCharacter = player1.name; // Using the birthYear method, get the birth year of the first person + int DOB = player1.birthYear(2025); // 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(DOB); /** * Terminology! diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..a4ec1b4 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,36 @@ +import java.util.HashSet; +import java.util.Set; + + + public class SetPractice { - public static void main(String[] args) { + public static void main(String[] args) + { // Create a HashSet of Strings and assign it to a variable of type Set + Set player = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + player.add("Ash"); + player.add("Brock"); + player.add("Misty"); // Check whether the Set contains a given String + boolean containsAsh = player.contains("Ash"); + System.out.println(containsAsh); // Remove an element from the Set + player.remove("Brock"); // Get the size of the Set + int playerSize = player.size(); + System.out.println(playerSize); // Iterate over the elements of the Set, printing each one on a separate line + for(String elements : player) + { + System.out.println(elements); + } /* * Warning! diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..bec03e9 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,53 @@ +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 cars = "Honda"; + + // Find the length of the string + int carLength = cars.length(); + System.out.println(carLength); // Concatenate (add) two strings together and reassign the result + cars = cars + "Accord"; + System.out.println(cars); // Find the value of the character at index 3 + char vehicle = cars.charAt(3); + System.out.println(vehicle); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + boolean containsCars = cars.contains("Civic"); + System.out.println(containsCars); + // Iterate over the characters of the string, printing each one on a separate line + for (int i = 0; i < cars.length(); i++) + { + System.out.println(cars.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable + List carList = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + carList.add("Lexus"); + carList.add("BMW"); + carList.add("Tesla"); // 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 carString = carList.toString(); + System.out.println(carString); // Check whether two strings are equal + boolean carEqual = cars.equals("Accord"); + System.out.println(carEqual); /* * Reminder!