From ab60b2e14145588c2298ee179b330753ab320d4b Mon Sep 17 00:00:00 2001 From: Abdi <89213120+abdirashidexe@users.noreply.github.com> Date: Fri, 24 Jan 2025 09:29:28 -0800 Subject: [PATCH 1/3] added period to readme 1st sentence --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dda6106..d41452c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# DO NOT DOWNLOAD THIS CODE AS A ZIP FILE +# DO NOT DOWNLOAD THIS CODE AS A ZIP FILE. # READ ME FIRST BEFORE DOWNLOADING This repository will be used to practice both your git and Java skills. DO NOT DOWNLOAD IT AS A ZIP FILE. Downloading it as a zip file will prevent the git commands from working properly. From fc7ddd7d657f169b63dcbf0a534d41952e049008 Mon Sep 17 00:00:00 2001 From: Abdi <89213120+abdirashidexe@users.noreply.github.com> Date: Fri, 24 Jan 2025 10:11:38 -0800 Subject: [PATCH 2/3] moving files to fork --- src/ArrayPractice.java | 23 +++++++++++++++++++++++ src/ListPractice.java | 27 +++++++++++++++++++++++++++ src/MapPractice.java | 40 +++++++++++++++++++++++++++++++++++++++- src/NumberPractice.java | 17 +++++++++++++++++ src/Person.java | 27 ++++++++++++++++++++++++--- src/SetPractice.java | 27 +++++++++++++++++++++++++++ src/StringPractice.java | 36 ++++++++++++++++++++++++++++++++++++ 7 files changed, 193 insertions(+), 4 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..a97b1e5 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -1,17 +1,40 @@ +import java.util.Arrays; + public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] myArray = new String[4]; + System.out.println(myArray.toString()); // Set the value of the array at each index to be a different String // It's OK to do this one-by-one + /*for (int i = 0; i < myArray.length; i++) + { + myArray[i] = "index" + i; + }*/ + myArray[0] = "index0"; + myArray[1] = "index1"; + myArray[2] = "index2"; + myArray[3] = "index3"; + System.out.println(Arrays.toString(myArray)); // Get the value of the array at index 2 + System.out.println(myArray[2]); // Get the length of the array + System.out.println(myArray.length); // Iterate over the array using a traditional for loop and print out each item + for (int i = 0; i < myArray.length; i++) + { + System.out.println("for-loop: " + myArray[i]); + } // Iterate over the array using a for-each loop and print out each item + for (String item : myArray) + { + System.out.println("for-each loop: " + item); + } /* * Reminder! diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..a93d2d3 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,27 +1,54 @@ +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("i0"); + list.add("i1"); + list.add("i2"); // Print the element at index 1 + System.out.println(list.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) + list.set(1, "replaced"); + System.out.println(list); // Insert a new element at index 0 (the length of the list will change) + list.add(0, "insertNew"); + System.out.println(list); // Check whether the list contains a certain string + if (list.contains("insertNew")) + { + System.out.println("insertNew exists in list."); + } // 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("Index: : " + i + ", Value: " + list.get(i).toString()); + } // Sort the list using the Collections library + Collections.sort(list); + System.out.println(list); // Iterate over the list using a for-each loop // Print each value on a second line + for (String item : list) + { + System.out.println("for-each loop: " + item + "\n"); + } /* * Usage tip! diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..3c49761 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,28 +1,66 @@ - +import java.util.Map; +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 + Map myMap = new HashMap(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + myMap.put("item_0", 0); + myMap.put("item_1", 1); + myMap.put("item_2", 2); + System.out.println(myMap); // Get the value associated with a given key in the Map + System.out.println(myMap.get("item_1")); // Find the size (number of key/value pairs) of the Map + System.out.println(myMap.size()); // Replace the value associated with a given key (the size of the Map shoukld not change) + myMap.put("item_1", 99); + System.out.println(myMap); // Check whether the Map contains a given key + if (myMap.containsKey("item_7")) + { + System.out.println(true); + } + else + { + System.out.println(false); + } // Check whether the Map contains a given value + if (myMap.containsValue(99)) + { + System.out.println(true); + } + else + { + System.out.println(false); + } // Iterate over the keys of the Map, printing each key + for (String key : myMap.keySet()) + { + System.out.println("for-each key: " + key); + } // Iterate over the values of the map, printing each value + for (Integer value : myMap.values()) + { + System.out.println("for-each value: " + value); + } // Iterate over the entries in the map, printing each key and value + for (String keyEntry : myMap.keySet()) + { + System.out.println("Key: " + keyEntry + " & the value: " + myMap.get(keyEntry)); + } /* * Usage tip! diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..21e0a26 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,17 +1,34 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float num = -3.44f; + System.out.println(num); // Create an int with a positive value and assign it to a variable + int x = 2; + System.out.println(x); // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = 7%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. + int numEorO = 3; + if (numEorO % 2 == 0) + { + System.out.println("This number is even: " + numEorO); + } + else + { + System.out.println("This number is odd: " + numEorO); + } // Divide the number by another number using integer division + int result = numEorO/x; + System.out.println("int division result: " + result); /* * Reminder! diff --git a/src/Person.java b/src/Person.java index 8ab3f95..68ceab6 100644 --- a/src/Person.java +++ b/src/Person.java @@ -3,17 +3,26 @@ * 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 n, int a) + { + name = n; + age = a; + } // Create a toString method that gives the name and age of the person - + public String toString() + { + return "Name: " + name.toString() + ", Age: " + String.valueOf(age); + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -28,25 +37,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 john = new Person("John", 25); // Create another instance of Person with a different name and age and // assign it to a different variable + Person mark = new Person("Mark", 30); // Print the first person + System.out.println(john.toString()); // Print the second person + System.out.println(mark.toString()); // Get the name of the first person and store it in a local variable + String johnsName = john.name; + System.out.println(johnsName); // 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 johnsBirthYear = john.birthYear(2025); // In a separate statement, print the local variable holding the birth year. + System.out.println(johnsBirthYear); /** * Terminology! diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..47233f0 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,17 +1,44 @@ +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 mySet = new HashSet(); // Add 3 elements to the set // (It's OK to do it one-by-one) + for (int i=0; i<3; i++) + { + mySet.add("element" + i); + } + System.out.println(mySet); // Check whether the Set contains a given String + if (mySet.contains("element2")) + { + System.out.println(true); + } + else + { + System.out.println(false); + } // Remove an element from the Set + mySet.remove("element2"); + System.out.println(mySet); // Get the size of the Set + System.out.println(mySet.size()); // Iterate over the elements of the Set, printing each one on a separate line + String[] myArray = new String[2]; + mySet.toArray(myArray); + + for (int i=0; i myArray = new ArrayList(); // Add multiple strings to the List (OK to do one-by-one) + for (int i=0; i < 4; i++) + { + myArray.add("String" + i); + } + System.out.println(myArray); // 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 singleString = String.join(",", myArray); + System.out.println(singleString); // Check whether two strings are equal + if (word.equals("NewWord")) + { + System.out.println(true); + } + else + { + System.out.println(false); + } /* * Reminder! From 18d143ea61bd197bbce1febfed4937ccf66b9487 Mon Sep 17 00:00:00 2001 From: Abdi <89213120+abdirashidexe@users.noreply.github.com> Date: Fri, 24 Jan 2025 10:12:20 -0800 Subject: [PATCH 3/3] moved old toRefresh to new fork one --- toRefresh.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/toRefresh.md b/toRefresh.md index 163dcab..8cf02fc 100644 --- a/toRefresh.md +++ b/toRefresh.md @@ -2,4 +2,23 @@ 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. +1. Wrote double instead of float first +2. ArrayList<> should be arraylist(String) +3. Looked up java methods to find set() +4. Looked up java collections sort +5. changed ArrayList .. = New ArrayList(); to List .. = New ArrayList(); +6. added to List .. = New ArrayList(); so it's now List .. = New ArrayList(); (had to look up) +7. had to change /n to \n +8. had to look up to how set a size for an array +9. had to look up why toString() wasnt working for myArray.toString(). turns out its Arrays.toString(myArray); +10. had to look up and make sure i was right in using .contains() to find substring and not another method i didn't think of +11. had to look up method that joins strings without using a loop and .concat +12. found out set could not print element at index so put my set items into an array then iterated. however don't know how i would convert set to a list then iterate. +13. HashMap does not have order +14. had to look up myMap.put("existingKey", newValueNum) replaces value but not key (so no duplicate keys in a map) +15. looked up info about constructor as well as toString() method. made sure to know if i include static, void +16. how to convert int to string to use a method like toString() but for ints +17. had to search up @param meaning in java to understand how to then use it in birthYear() +18. struggled a bit to remember the order of the syntax when making a new object variable (Object objectName = new Object(String s, int n)) for example +19. learned that it's still considered a local variable if you make it in the main method (i thought i had to make a method inside the main method when working on the get name problem). - \ No newline at end of file