diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..acf180b 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,30 @@ + + public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 - + String[] str = 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 + str[0] = "Jessica"; + str[1] = "Tom"; + str[2] = "Sam"; + str[3] = "Linda"; // Get the value of the array at index 2 + System.out.println(str[2]); // Get the length of the array + System.out.println(str.length); // Iterate over the array using a traditional for loop and print out each item - + for(int i = 0 ; i < str.length; i++){ + System.out.println(str[i]); + } // Iterate over the array using a for-each loop and print out each item + for(String eachStr : str){ + System.out.println(eachStr); + } /* * Reminder! diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..144f03f 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,44 @@ + +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 newList = new ArrayList<>(); // Add 3 elements to the list (OK to do one-by-one) - + newList.add("Jessica"); + newList.add("Awesum"); + newList.add("Gizmo"); // Print the element at index 1 - + System.out.println(newList.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) - + newList.set(1, "DeAndre"); + System.out.println(newList); // Insert a new element at index 0 (the length of the list will change) - + newList.add(0, "Gianni"); + System.out.println(newList); // Check whether the list contains a certain string - + boolean hasSasha = newList.contains("Sasha"); + System.out.println(hasSasha); + // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for(int i =0; i < newList.size(); i++){ + System.out.println("index "+i+" Value "+ newList.get(i)); + } // Sort the list using the Collections library - + Collections.sort(newList); // Iterate over the list using a for-each loop // Print each value on a second line + for(String str : newList){ + System.out.println(str); + } /* * Usage tip! diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..a579102 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,29 +1,52 @@ +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 - + Map newMap = new HashMap<>(); + // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + newMap.put("Sasha", 9); + newMap.put("phone", 14); + newMap.put("gizmo", 1); + newMap.put("pink", 21); // Get the value associated with a given key in the Map + Integer val = newMap.get("gizmo"); + System.out.println(val); // Find the size (number of key/value pairs) of the Map - + Integer mapSize = newMap.size(); + System.out.println(mapSize); // Replace the value associated with a given key (the size of the Map shoukld not change) - + newMap.put("Tammie",21); // Check whether the Map contains a given key - + boolean conKey =newMap.containsKey("gizmo"); + System.out.println(conKey); // Check whether the Map contains a given value - + boolean conVal = newMap.containsValue(4); + System.out.println(conVal); // Iterate over the keys of the Map, printing each key - + for (String eachKey : newMap.keySet()){ + System.out.println(eachKey); + } + // Iterate over the values of the map, printing each value - + for(Integer eachVal : newMap.values()){ + System.out.println(eachVal); + } // Iterate over the entries in the map, printing each key and value - + for (Map.Entry eachEntry : newMap.entrySet()){ + String keyVal = eachEntry.getKey(); + Integer getKey = eachEntry.getValue(); + System.out.println(keyVal +" "+ getKey); + } /* * Usage tip! * diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..817abfb 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,16 +1,22 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable - + float negValue = -4; // Create an int with a positive value and assign it to a variable - + int posValue= 6; // Use the modulo % operator to find the remainder when the int is divided by 3 - + int remainder = posValue % 3; + System.out.println(remainder); // 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 (posValue %2 !=0){ + System.out.println( "odd"); + }else{ + System.out.println("even"); + } + // Divide the number by another number using integer division /* @@ -20,6 +26,7 @@ public static void main(String args[]) { * Example: * 7 / 3 = 2 when performing int division */ - + float division = posValue / negValue; + System.out.println(division); } } diff --git a/src/Person.java b/src/Person.java index 8ab3f95..4f9a3d2 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,14 +6,21 @@ 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 = "Jessica"; + private int age = 14; // 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.age = age; + this.name = name; + } // 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. @@ -28,25 +35,36 @@ 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 newPerson = new Person("Sasha", 9); // Create another instance of Person with a different name and age and // assign it to a different variable + Person secPerson = new Person("Gianni", 24); // Print the first person + System.out.println(newPerson); // Print the second person + System.out.println(secPerson); // Get the name of the first person and store it in a local variable + String storeName = newPerson.name; + System.out.println(storeName); // 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 sashaBirthYear = newPerson.birthYear(2025); // In a separate statement, print the local variable holding the birth year. + System.out.println(sashaBirthYear); + /** * Terminology! diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..7cc771f 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,43 @@ +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 = "Jessica"; // Find the length of the string - + System.out.println(str.length()); // Concatenate (add) two strings together and reassign the result - + String concatStr = str + "A.H."; + System.out.println(concatStr); // Find the value of the character at index 3 + char indexChr = concatStr.charAt(3); + System.out.println(indexChr); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) - + Boolean checkStr =concatStr.contains("ica"); + System.out.println(checkStr); // Iterate over the characters of the string, printing each one on a separate line - + for(char eachChar : concatStr.toCharArray()){ + System.out.println(eachChar); + } // Create an ArrayList of Strings and assign it to a variable - + List arrList = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + arrList.add("Sasha"); + arrList.add("apple"); + arrList.add("tree"); + arrList.add("car"); // 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 strJoin = String.join(",",arrList); + System.out.println(strJoin); // Check whether two strings are equal - + String onestr = "tim"; + boolean strCheck = onestr.equals("Sasha"); + System.out.println(strCheck); /* * Reminder! *