diff --git a/README.md b/README.md index dda6106..2622a6e 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,8 @@ Most of these taks should be easy, but there will likely be some that ask you to Make a Pull Request (PR) against the original repository. Copy the link into Canvas to submit. +## What i can work on: + +1. For loops +2. Arrays +3. Built in methods \ No newline at end of file diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..d7892d3 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,18 +1,32 @@ +import java.util.Arrays; + public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] ray = {"one", "two", "three", "four"}; + System.out.println(Arrays.toString(ray) + '\n'); // Set the value of the array at each index to be a different String // It's OK to do this one-by-one - + ray[0]= "1"; + ray[1]= "2"; + ray[2]= "3"; + ray[3]= "4"; + System.out.println(Arrays.toString(ray) + "\n"); // Get the value of the array at index 2 + System.out.println(ray[2] + "\n"); // Get the length of the array - + System.out.println(ray.length + "\n"); // Iterate over the array using a traditional for loop and print out each item - + for(int i = 0; i < ray.length; i++){ + System.out.println(ray[i]); + } + System.out.println("\n"); // Iterate over the array using a for-each loop and print out each item - + for (String item: ray){ + System.out.println(item); + } /* * Reminder! * diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..1b55dee 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,53 @@ +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 list = new ArrayList<>(); + // Add 3 elements to the list (OK to do one-by-one) + list.add("First"); + list.add("Second"); + list.add("Third"); + System.out.println(list + "\n"); + // Print the element at index 1 + System.out.println(list.get(1) + "\n"); // Replace the element at index 1 with a new value // (Do not insert a new value. The length of the list should not change) + list.set(1,"Replaced"); + System.out.println(list + "\n"); // Insert a new element at index 0 (the length of the list will change) + list.add(0, "Added"); + System.out.println(list + "\n"); // Check whether the list contains a certain string + System.out.println(list.contains("Third") + "\n"); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for(int i = 0; i < list.size(); i++){ + System.out.println(i + ": " + list.get(i)); + } // Sort the list using the Collections library + Collections.sort(list); + System.out.println("\n" + list + "\n"); + // Iterate over the list using a for-each loop // Print each value on a second line + for(String item : list){ + System.out.println(item + "\n"); + } /* * Usage tip! diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..c3f7834 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,48 @@ - +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 + HashMap map = new HashMap<>(); + // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + map.put("Tacos", 5); + map.put("Burgers", 8); + map.put("Cheesecake", 10); + System.out.println(map + "\n"); // Get the value associated with a given key in the Map + System.out.println(map.get("Tacos") + '\n'); // Find the size (number of key/value pairs) of the Map + System.out.println(map.size() + "\n"); // Replace the value associated with a given key (the size of the Map shoukld not change) + map.replace("Tacos", 5, 7); + System.out.println(map + "\n"); // Check whether the Map contains a given key + System.out.println(map.containsKey("Tacos") + "\n"); // Check whether the Map contains a given value + System.out.println(map.containsValue(9) + "\n"); // Iterate over the keys of the Map, printing each key - + for(String key : map.keySet()){ + System.out.println(key); + } + System.out.println("\n"); // Iterate over the values of the map, printing each value + for(int value : map.values()){ + System.out.println(value); + } + System.out.println("\n"); // Iterate over the entries in the map, printing each key and value + map.forEach((key, value) -> {System.out.println(key + ", " + value);}); /* * Usage tip! diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..78a4e0f 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -21,5 +21,24 @@ public static void main(String args[]) { * 7 / 3 = 2 when performing int division */ + float neg = -7; + System.out.println(neg); + int num = 8; + System.out.println(num); + System.out.println(num / 3 % 3); + + int even = 10; + if (even % 2 == 0){ + System.out.println("This number is even: " + even); + }; + + int n = 5; + if (n % 2 == 0){ + System.out.println("This number is even: " + n); + }else{ + System.out.println("This number is odd: " + n); + }; + + System.out.println(n / 2); } } diff --git a/src/Person.java b/src/Person.java index 8ab3f95..bdf8124 100644 --- a/src/Person.java +++ b/src/Person.java @@ -6,13 +6,25 @@ 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 = "Tom"; + private int age = 32; + + // 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 "This person's name is " + name + " and their age is " + age; + } // Implement the below public instance method "birthYear" @@ -28,26 +40,31 @@ 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 tom = new Person("Tom", 32); // Create another instance of Person with a different name and age and // assign it to a different variable + Person kari = new Person("Kari", 31); // Print the first person - + System.out.println(tom); // Print the second person - + System.out.println(kari); // Get the name of the first person and store it in a local variable + String newName = tom.name; + System.out.println(newName); // 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 birthYear = tom.birthYear(2025); // In a separate statement, print the local variable holding the birth year. - + System.out.println(birthYear); /** * Terminology! * diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..53df8aa 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) { // Create a HashSet of Strings and assign it to a variable of type Set + Set hs = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + hs.add("green"); + hs.add("blue"); + hs.add("red"); + hs.add("yellow"); + hs.add("pink"); + + System.out.println(hs +"\n"); // Check whether the Set contains a given String + System.out.println(hs.contains("red") + "\n"); // Remove an element from the Set + hs.remove("pink"); + System.out.println(hs + "\n"); // Get the size of the Set + System.out.println(hs.size() + "\n"); // Iterate over the elements of the Set, printing each one on a separate line + for(String item : hs){ + System.out.println(item); + } + System.out.println("\n"); /* * Warning! diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..a3a3090 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,48 @@ +import java.util.ArrayList; +// import java.util.Arrays; + public class StringPractice { public static void main(String[] args) { // Create a string with at least 5 characters and assign it to a variable - + String word = "Connor"; + System.out.println(word + "\n"); // Find the length of the string - + System.out.println(word.length() + "\n"); // Concatenate (add) two strings together and reassign the result - + String fName = "Connor"; + String lName = "Hughes"; + String fullName = fName + " " + lName; + System.out.println(fullName + "\n"); // Find the value of the character at index 3 + System.out.println(word.charAt(3) + "\n"); // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + System.out.println(word.contains("a") + "\n"); // Iterate over the characters of the string, printing each one on a separate line - + for(int i =0; i < word.length(); i++){ + System.out.println(word.charAt(i)); + } + System.out.println("\n"); // Create an ArrayList of Strings and assign it to a variable - + ArrayList ray = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + ray.add("car"); + ray.add("dog"); + ray.add("cat"); + ray.add("red"); + ray.add("blue"); + System.out.println(ray + "\n"); // 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 concat = String.join(", ", ray); + System.out.println(concat + "\n"); // Check whether two strings are equal + String one = "racecar"; + String two = "Truck"; + System.out.println(one.equals(two)); /* * Reminder!