diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..43996ec 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,37 @@ -public class ArrayPractice { - public static void main(String[] args) { +public class ArrayPractice +{ + public static void main(String[] args) + { // Create an array of Strings of size 4 + String arrayItems[] = {"One", "Two", "Three", "Four"}; + System.out.println(arrayItems[0] + " " + arrayItems[1] + " " + arrayItems[2] + " " + arrayItems[3]); // Set the value of the array at each index to be a different String // It's OK to do this one-by-one + arrayItems[0] = "Java"; + arrayItems[1] = "C++"; + arrayItems[2] = "C#"; + arrayItems[3] = "Python"; + + System.out.println(arrayItems[0] + " " + arrayItems[1] + " " + arrayItems[2] + " " + arrayItems[3]); // Get the value of the array at index 2 + System.out.println(arrayItems[2]); // Get the length of the array + System.out.println(arrayItems.length); // Iterate over the array using a traditional for loop and print out each item + for (int i = 0; i < arrayItems.length; i++) + { + System.out.println(arrayItems[i]); + } // Iterate over the array using a for-each loop and print out each item + for (String items : arrayItems) + { + System.out.println(items); + } /* * Reminder! @@ -19,4 +39,5 @@ public static void main(String[] args) { * Arrays start at index 0 */ } + } diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..70ee0f2 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,60 @@ -public class ListPractice { +import java.util.ArrayList; +import java.util.List; +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 + public static void main(String[] args) + { + // Create an empty ArrayList of Strings and assign it to a variable of type List + List listContainer = new ArrayList(); + System.out.println(listContainer); + // Add 3 elements to the list (OK to do one-by-one) + listContainer.add("One"); + listContainer.add("Two"); + listContainer.add("Three"); + System.out.println(listContainer); // Print the element at index 1 + System.out.println(listContainer.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) + listContainer.set(1, "New"); + System.out.println(listContainer); // Insert a new element at index 0 (the length of the list will change) + listContainer.addFirst("Sky"); + System.out.println(listContainer); // Check whether the list contains a certain string + System.out.println(listContainer.contains("New")); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for (int i = 0; i < listContainer.size(); i++) + { + System.out.println("Index: " + listContainer.indexOf(listContainer.get(i)) + " & Value: " + listContainer.get(i)); + } // Sort the list using the Collections library + Collections.sort(listContainer); + System.out.println(listContainer); // Iterate over the list using a for-each loop // Print each value on a second line + String listItems = ""; + + for (Object item : listContainer) + { + System.out.println(item); + listItems += (item + " "); + } + + System.out.println(listItems); /* * Usage tip! @@ -33,4 +66,5 @@ 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..61a45e7 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,56 @@ +import java.util.HashMap; +import java.util.Map; - -public class MapPractice { - public static void main(String[] args) { +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 mapPractice = new HashMap(); + System.out.println(mapPractice); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + mapPractice.put("Java", 27); + mapPractice.put("C", 11); + mapPractice.put("Python", 120); + + System.out.println(mapPractice); // Get the value associated with a given key in the Map + System.out.println(mapPractice.get("C")); // Find the size (number of key/value pairs) of the Map + System.out.println(mapPractice.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) + mapPractice.put("Python", 2); + System.out.println(mapPractice); // Check whether the Map contains a given key + System.out.println(mapPractice.containsKey("Java")); // Check whether the Map contains a given value + System.out.println(mapPractice.containsValue(27)); // Iterate over the keys of the Map, printing each key + for (String keys : mapPractice.keySet()) + { + System.out.println(keys); + } // Iterate over the values of the map, printing each value + for (int values : mapPractice.values()) + { + System.out.println(values); + } // Iterate over the entries in the map, printing each key and value + for (String keys : mapPractice.keySet()) + { + System.out.println(keys + " and " + mapPractice.get(keys)); + } /* * Usage tip! @@ -40,4 +68,5 @@ public static void main(String[] args) { * and you don't care about any ordering. */ } + } diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..d6d4b5e 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +1,33 @@ -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 negNum = -5.666f; + System.out.println(negNum); // Create an int with a positive value and assign it to a variable + int posNum = 7; + System.out.println(posNum); // Use the modulo % operator to find the remainder when the int is divided by 3 + System.out.println(posNum % 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 (posNum % 2 == 0) + { + System.out.println("Even"); + } + else + { + System.out.println("Odd"); + } // Divide the number by another number using integer division + System.out.println(posNum / 2); /* * Reminder! @@ -22,4 +38,5 @@ public static void main(String args[]) { */ } + } diff --git a/src/Person.java b/src/Person.java index 8ab3f95..bf6fefa 100644 --- a/src/Person.java +++ b/src/Person.java @@ -3,16 +3,29 @@ * 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; + 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 + ": " + age; + } // Implement the below public instance method "birthYear" @@ -29,24 +42,39 @@ public class Person { */ // (create the instance method here) + public int birthyear(int currentYear) + { + return currentYear - age; + } - public static void main(String[] args) { + + public static void main(String[] args) + { // Create an instance of Person + Person personOne = new Person("Clair", 32); // Create another instance of Person with a different name and age and // assign it to a different variable + Person personTwo = new Person("Jill", 35); // Print the first person + System.out.println(personOne.toString()); // Print the second person + System.out.println(personTwo.toString()); // Get the name of the first person and store it in a local variable + String personName = personOne.name; + System.out.println(personName); // 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 born = personOne.birthyear(2025); + System.out.println(born); // In a separate statement, print the local variable holding the birth year. + System.out.println(personOne.age); /** * Terminology! @@ -63,4 +91,5 @@ public static void main(String[] args) { * Each instance has its own instance variables: Auberon's age can be different from Baya's age. */ } + } diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..7dbbad1 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,37 @@ -public class SetPractice { - public static void main(String[] args) { +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 hashItems = new HashSet(); + System.out.println(hashItems); // Add 3 elements to the set // (It's OK to do it one-by-one) + hashItems.add("One"); + hashItems.add("Two"); + hashItems.add("Three"); + + System.out.println(hashItems); // Check whether the Set contains a given String + System.out.println(hashItems.contains("Two")); // Remove an element from the Set + hashItems.remove("Two"); + System.out.println(hashItems); // Get the size of the Set + System.out.println(hashItems.size()); // Iterate over the elements of the Set, printing each one on a separate line + for (String items : hashItems) + { + System.out.println(items); + } /* * Warning! @@ -26,4 +46,5 @@ public static void main(String[] args) { * Also remember that sets do NOT have duplicates. */ } + } diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..1e6a000 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,53 @@ -public class StringPractice { - public static void main(String[] args) { +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 stringPractice = "Coding"; + System.out.println(stringPractice); // Find the length of the string + System.out.println(stringPractice.length()); // Concatenate (add) two strings together and reassign the result + stringPractice = stringPractice + " Java"; + System.out.println(stringPractice); // Find the value of the character at index 3 + System.out.println(stringPractice.charAt(3)); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + System.out.println(stringPractice.contains("Java")); // Iterate over the characters of the string, printing each one on a separate line + for (int i = 0; i < stringPractice.length(); i++) + { + System.out.println(stringPractice.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable + List listStrings = new ArrayList(); + System.out.println(listStrings); // Add multiple strings to the List (OK to do one-by-one) + listStrings.add(stringPractice); + listStrings.add("Coding C++"); + listStrings.add("Coding C#"); + listStrings.add("Coding Python"); + + System.out.println(listStrings); // 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 comp = listStrings.toString(); + System.out.println(comp); // Check whether two strings are equal + System.out.println(comp.equals(listStrings.toString())); + System.out.println(listStrings.toString().equals(comp)); /* * Reminder! @@ -29,4 +57,5 @@ public static void main(String[] args) { * We use == when comparing primitives (e.g. int or char). */ } + } diff --git a/toRefresh.md b/toRefresh.md index 163dcab..dd78f71 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -1,5 +1,11 @@ # Items to Refresh on: As you work through this exercise, write down anything that you needed to look up or struggled to remember here. It can be just a word or two (e.g. "joining strings"). You can use this as a guide of what to make extra sure you're refreshed on before exams and interviews. +- +float variable creation -- \ No newline at end of file +import list + +array creation + +class \ No newline at end of file