From 68320dcbd01574d98aa21543f78c38eface762b8 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Mon, 13 Jan 2025 09:46:25 -0800 Subject: [PATCH 01/10] array practice, just need the for each section --- src/ArrayPractice.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..e31a543 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -2,15 +2,30 @@ public class ArrayPractice { public static void main(String[] args) { // Create an array of Strings of size 4 + String[] firstArray = 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 + firstArray[0] = "one"; + firstArray[1] = "two"; + firstArray[2] = "three"; + firstArray[3] = "four"; + // Get the value of the array at index 2 + System.out.println(firstArray[2]); + // Get the length of the array + + System.out.println(firstArray.length); // Iterate over the array using a traditional for loop and print out each item + for(int i = 0; i < firstArray.length; i++){ + System.out.println(firstArray[i]); + } + // Iterate over the array using a for-each loop and print out each item /* From ac6fb217f8cd5e00a4169ba54304ec8a52e83408 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Mon, 13 Jan 2025 09:55:05 -0800 Subject: [PATCH 02/10] array done --- src/ArrayPractice.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index e31a543..29391d7 100644 --- a/src/ArrayPractice.java +++ b/src/ArrayPractice.java @@ -28,6 +28,10 @@ public static void main(String[] args) { // Iterate over the array using a for-each loop and print out each item + for(String fString: firstArray){ + System.out.println("value:" +fString); + } + /* * Reminder! * From e8b307fa0a7f48fde573a4cfd2530786e9ebcb2f Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:56:02 -0800 Subject: [PATCH 03/10] done list --- src/ListPractice.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..d0b836d 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,28 +1,57 @@ +import java.text.CollationElementIterator; +import java.util.ArrayList; +import java.util.Collection; +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 + ArrayList firstList = new ArrayList<>(); + // Add 3 elements to the list (OK to do one-by-one) + firstList.add("first"); + firstList.add("second"); + firstList.add("third"); + // Print the element at index 1 + System.out.println(firstList.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) + firstList.set(1,"four"); + // Insert a new element at index 0 (the length of the list will change) + firstList.add(0, "five"); + // Check whether the list contains a certain string + firstList.contains("first"); + // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for(int i = 0; i < firstList.size()-1; i++){ + System.out.println(firstList.get(i)); + } + // Sort the list using the Collections library + Collections.sort(firstList); + // Iterate over the list using a for-each loop // Print each value on a second line + for(String values: firstList){ + System.out.println(values); + } + /* * Usage tip! * From 423ed7ccfdcc3ce716f1486d4c145e4c1548757b Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:58:02 -0800 Subject: [PATCH 04/10] remove some libaries --- src/ListPractice.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ListPractice.java b/src/ListPractice.java index d0b836d..4957b90 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,6 +1,4 @@ -import java.text.CollationElementIterator; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; public class ListPractice { From 295e1df5ccd83fad3da4e44d2e3960880334805e Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Mon, 13 Jan 2025 17:14:07 -0800 Subject: [PATCH 05/10] break time --- src/MapPractice.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..2fb85b0 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,19 +1,32 @@ - +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 firstHashMap = new HashMap(); + // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + firstHashMap.put("first", 1); + firstHashMap.put("second", 2); + firstHashMap.put("third", 3); + // Get the value associated with a given key in the Map + firstHashMap.get("first"); + // Find the size (number of key/value pairs) of the Map + firstHashMap.size(); + // Replace the value associated with a given key (the size of the Map shoukld not change) + + // Check whether the Map contains a given key // Check whether the Map contains a given value From b56bdf82a0939a0bdf3db1c4946767744e9149c2 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Mon, 13 Jan 2025 17:30:04 -0800 Subject: [PATCH 06/10] done with maps --- src/MapPractice.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/MapPractice.java b/src/MapPractice.java index 2fb85b0..8215e61 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -25,18 +25,28 @@ public static void main(String[] args) { // Replace the value associated with a given key (the size of the Map shoukld not change) - + firstHashMap.put("first", 4); // Check whether the Map contains a given key + firstHashMap.containsKey("first"); + // Check whether the Map contains a given value + firstHashMap.containsValue(4); + // Iterate over the keys of the Map, printing each key + System.out.println(firstHashMap.keySet()); + // Iterate over the values of the map, printing each value + System.out.println(firstHashMap.values()); + // Iterate over the entries in the map, printing each key and value + System.out.println(firstHashMap.entrySet()); + /* * Usage tip! * From 48c42a8bc1792fecca7e7a373a8ec30024eecc31 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Mon, 13 Jan 2025 17:45:20 -0800 Subject: [PATCH 07/10] finish #practices --- src/NumberPractice.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..58cf8f4 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -2,17 +2,32 @@ public class NumberPractice { public static void main(String args[]) { // Create a float with a negative value and assign it to a variable + float neg = -1; + // Create an int with a positive value and assign it to a variable + int pos = 1; + // Use the modulo % operator to find the remainder when the int is divided by 3 + int rema = pos%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. + int userInput = 28; + if(0 == userInput%2){ + System.out.println("Even"); + }else{ + System.out.println("Odd"); + } + // Divide the number by another number using integer division + int div = rema/pos; + /* * Reminder! * From 32a2808c39ebfa1dc14ff5e6b4e252f8cbdfd158 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Mon, 13 Jan 2025 20:19:32 -0800 Subject: [PATCH 08/10] Person done, can't figure out the instance method without the use of sysout print statement --- src/Person.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/Person.java b/src/Person.java index 8ab3f95..5b5b760 100644 --- a/src/Person.java +++ b/src/Person.java @@ -7,13 +7,28 @@ 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; + public int age; // Create a constructor that takes the name and age of the person // and assigns it to the instance variables + Person(){ + name = "Unknown"; + age = 0; + } + + 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 "Person{name= "+ name + " , age=" + age + "}"; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -33,15 +48,25 @@ public class Person { public static void main(String[] args) { // Create an instance of Person + Person unKnown = new Person(null, 0); + // Create another instance of Person with a different name and age and // assign it to a different variable + Person dani = new Person("Daniel", 28); + // Print the first person + System.out.println(unKnown.toString()); + // Print the second person + System.out.println(dani.toString()); + // Get the name of the first person and store it in a local variable + String idName = dani.name; + // 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. From e349728f9bf0838a7d252e080d3acd6771cabc26 Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Mon, 13 Jan 2025 21:13:13 -0800 Subject: [PATCH 09/10] set done --- src/SetPractice.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/SetPractice.java b/src/SetPractice.java index d2fc1c9..58e3ce2 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,18 +1,37 @@ +import java.util.Set; +import java.util.HashSet; + public class SetPractice { public static void main(String[] args) { // Create a HashSet of Strings and assign it to a variable of type Set + Set firstHashSet = new HashSet(); + // Add 3 elements to the set // (It's OK to do it one-by-one) + firstHashSet.add("first"); + firstHashSet.add("second"); + firstHashSet.add("third"); + // Check whether the Set contains a given String + firstHashSet.contains("first"); + // Remove an element from the Set + firstHashSet.remove("first"); + // Get the size of the Set + firstHashSet.size(); + // Iterate over the elements of the Set, printing each one on a separate line + for(String i : firstHashSet){ + System.out.println(i); + } + /* * Warning! * From f86f3f1ac687fe19a1fedc9dc870ee690bfe113e Mon Sep 17 00:00:00 2001 From: Dani-DEV28 <193554832+Dani-DEV28@users.noreply.github.com> Date: Mon, 13 Jan 2025 22:07:33 -0800 Subject: [PATCH 10/10] string somewhat done --- src/StringPractice.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..e97daed 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,26 +1,54 @@ +import java.util.ArrayList; + public class StringPractice { public static void main(String[] args) { // Create a string with at least 5 characters and assign it to a variable + String checkSize = "Danny"; + // Find the length of the string + checkSize.length(); + // Concatenate (add) two strings together and reassign the result + checkSize = "Danny" + " DEV28"; + // Find the value of the character at index 3 + String loader = ""; + char[] refract = checkSize.toCharArray(); + loader += refract[3]; + // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + checkSize.contains("Danny"); + // Iterate over the characters of the string, printing each one on a separate line + for(char i : refract){ + System.out.println(i); + } + // Create an ArrayList of Strings and assign it to a variable + ArrayList strList = new ArrayList<>(); + // Add multiple strings to the List (OK to do one-by-one) + strList.add("Danny"); + strList.add("DEV28"); + strList.add("GitHub"); + // 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 bindingStr = String.join(" ", strList); + // Check whether two strings are equal + boolean rEqual = bindingStr.equals(checkSize); + /* * Reminder! *