From eb59e316be98bba9cd4f321f8ce8980b31aeace3 Mon Sep 17 00:00:00 2001 From: Squizard <184549450+BradyLeg@users.noreply.github.com> Date: Mon, 13 Jan 2025 21:47:20 -0800 Subject: [PATCH] Completed all derusting java files --- src/ArrayPractice.java | 13 +++++++++++++ src/ListPractice.java | 30 ++++++++++++++++++++++++++---- src/MapPractice.java | 30 ++++++++++++++++++++++++++---- src/NumberPractice.java | 15 +++++++++++++-- src/Person.java | 37 ++++++++++++++++++++++++++++--------- src/SetPractice.java | 19 +++++++++++++++++-- src/StringPractice.java | 26 +++++++++++++++++++++++--- 7 files changed, 146 insertions(+), 24 deletions(-) diff --git a/src/ArrayPractice.java b/src/ArrayPractice.java index bc58c83..740a6be 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[] names = 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 + names[0] = "Shiroko"; + names[1] = "Serika"; + names[2] = "Hoshino"; + names[3] = "Nonomi"; // Get the value of the array at index 2 + System.out.println(names[2]); // Get the length of the array + System.out.println(names.length); // Iterate over the array using a traditional for loop and print out each item + for (int i = 0; i < names.length; i++) { + System.out.println(names[i]); + } // Iterate over the array using a for-each loop and print out each item + for (String name : names) { + System.out.println(name); + } /* * Reminder! diff --git a/src/ListPractice.java b/src/ListPractice.java index f4de8e7..4caa78f 100644 --- a/src/ListPractice.java +++ b/src/ListPractice.java @@ -1,35 +1,57 @@ -public class ListPractice { +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.jar.Attributes.Name; +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 names = new ArrayList(); // Add 3 elements to the list (OK to do one-by-one) + names.add("Shiroko"); + names.add("Hoshino"); + names.add("Serika"); // Print the element at index 1 - + System.out.println(names.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) + names.set(1, "Ayane"); // Insert a new element at index 0 (the length of the list will change) + names.add(0, "Nonomi"); // Check whether the list contains a certain string + String search = "Shiroko"; + System.out.println(names.contains(search)); // Iterate over the list using a traditional for-loop. // Print each index and value on a separate line + for (int i = 0; i < names.size(); i++) { + System.out.println(i + " " + names.get(i)); + } // Sort the list using the Collections library + Collections.sort(names); // Iterate over the list using a for-each loop // Print each value on a second line + for (String name : names) { + System.out.println(); + System.out.println(name); + } /* * Usage tip! * - * Use a traditional for-loop when you need to use the index or you need to iterate in an + * Use a traditional for-loop when you need to use the index or you need to + * iterate in an * unconventional order (e.g. backwards) * - * Otherwise, if you're iterating the in the conventional order and don't need the + * Otherwise, if you're iterating the in the conventional order and don't need + * the * index values a for-each loop is cleaner. */ } diff --git a/src/MapPractice.java b/src/MapPractice.java index 7ebfeac..011cbce 100644 --- a/src/MapPractice.java +++ b/src/MapPractice.java @@ -1,40 +1,62 @@ - +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 nameMap = new HashMap<>(); // Put 3 different key/value pairs in the Map // (it's OK to do this one-by-one) + nameMap.put("Hoshino", 1); + nameMap.put("Shiroko", 2); + nameMap.put("Nonomi", 3); // Get the value associated with a given key in the Map + System.out.println(nameMap.get("Shiroko")); // Find the size (number of key/value pairs) of the Map + System.out.println(nameMap.size()); - // Replace the value associated with a given key (the size of the Map shoukld not change) + // Replace the value associated with a given key (the size of the Map shoukld + // not change) + nameMap.put("Nonomi", 4); // Check whether the Map contains a given key + System.out.println(nameMap.containsKey("Serika")); // Check whether the Map contains a given value + System.out.println(nameMap.containsValue(2)); // Iterate over the keys of the Map, printing each key + for (String name : nameMap.keySet()) { + System.out.println(name); + } // Iterate over the values of the map, printing each value + for (int num : nameMap.values()) { + System.out.println(num); + } // Iterate over the entries in the map, printing each key and value + for (String name : nameMap.keySet()) { + System.out.println(name + " " + nameMap.get(name)); + } /* * Usage tip! * * Maps are great when you want a specific key to value mapping. - * Example: The key could be a person's name, and the value could be their phone number + * Example: The key could be a person's name, and the value could be their phone + * number * * However if your keys are simple ascending 0-indexed integers with no gaps * (0, 1, 2, 3, 4...) then an array or List is likely a better choice. * Example: If you want to store the order of songs in a playlist. * - * If you're finding that you're just wanting to store unordered values and the keys + * If you're finding that you're just wanting to store unordered values and the + * keys * are unimportant, a Set may be a better choice. * Example: If you want to hold the student ID numbers of everyone in a course, * and you don't care about any ordering. diff --git a/src/NumberPractice.java b/src/NumberPractice.java index bbec2fe..6953423 100644 --- a/src/NumberPractice.java +++ b/src/NumberPractice.java @@ -1,23 +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 negativeFloat = -1.5f; // Create an int with a positive value and assign it to a variable + int positiveInt = 5; // Use the modulo % operator to find the remainder when the int is divided by 3 + int remainder = positiveInt % 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) + boolean even = (positiveInt % 2 == 0); + System.out.println(even); + // Use an if-else to print "Even" if the number is even and "Odd" // if the number is odd. - + if (positiveInt % 2 == 0) { + System.out.println("Even"); + } else { + System.out.println("Odd"); + } // Divide the number by another number using integer division + System.out.println(positiveInt / 4); /* * Reminder! * * When dividing ints, the result is rounded down. - * Example: + * Example: * 7 / 3 = 2 when performing int division */ diff --git a/src/Person.java b/src/Person.java index 8ab3f95..b56c7ac 100644 --- a/src/Person.java +++ b/src/Person.java @@ -5,15 +5,22 @@ public class Person { // Declare a public String instance variable for the name of the person + public String name; // Declare a private int instance variable for the age of the person - + 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 - + public String toString() { + String out = "Name: " + name + " Age: " + age; + return out; + } // Implement the below public instance method "birthYear" // There should NOT be any print statement in this method. @@ -27,26 +34,35 @@ public class Person { * @param currentYear an int for the current year * @return The year the person was born */ - // (create the instance method here) - + public int birthYear(int currentYear) { + int birthYear = currentYear - age; + return birthYear; + } public static void main(String[] args) { // Create an instance of Person - + Person person1 = new Person("Brady", 20); // Create another instance of Person with a different name and age and // assign it to a different variable + Person person2 = new Person("Shiroko", 19); // Print the first person + System.out.println(person1); // Print the second person + System.out.println(person2); // Get the name of the first person and store it in a local variable + String name1 = person1.name; + System.out.println(name1); // 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 birthYear1 = person1.birthYear(2025); // In a separate statement, print the local variable holding the birth year. + System.out.println(birthYear1); /** * Terminology! @@ -55,12 +71,15 @@ public static void main(String[] args) { * An instance is a specific object made according to that definition. * We use "instance" and "object" to mean the same thing. * - * For example, if there is a Person class, we can make an instance of a specific person: Auberon. + * For example, if there is a Person class, we can make an instance of a + * specific person: Auberon. * - * There can be many instances for the same class. For example: Auberon, Xinting, Baya are all + * There can be many instances for the same class. For example: Auberon, + * Xinting, Baya are all * different instances of the Person class. * - * Each instance has its own instance variables: Auberon's age can be different from Baya's age. + * 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..b9e8e90 100644 --- a/src/SetPractice.java +++ b/src/SetPractice.java @@ -1,26 +1,41 @@ +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 nameSet = new HashSet<>(); // Add 3 elements to the set // (It's OK to do it one-by-one) + nameSet.add("Shiroko"); + nameSet.add("Serika"); + nameSet.add("Hoshino"); // Check whether the Set contains a given String + System.out.println(nameSet.contains("Nonomi")); // Remove an element from the Set + nameSet.remove("Hoshino"); // Get the size of the Set + System.out.println(nameSet.size()); // Iterate over the elements of the Set, printing each one on a separate line + for (String name : nameSet) { + System.out.println(name); + } /* * Warning! * * The iteration order over the items in a HashSet is NOT GUARANTEED. * - * Even running the exact same program multiple times may give different results. + * Even running the exact same program multiple times may give different + * results. * Do not use a HashSet if order is important! You can use a TreeSet if you - * want items in sorted order, or an array or List if you want them in a specified + * want items in sorted order, or an array or List if you want them in a + * specified * order. * * Also remember that sets do NOT have duplicates. diff --git a/src/StringPractice.java b/src/StringPractice.java index 8d87617..2028c57 100644 --- a/src/StringPractice.java +++ b/src/StringPractice.java @@ -1,25 +1,45 @@ +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 word = "Swing"; // Find the length of the string + System.out.println(word.length()); // Concatenate (add) two strings together and reassign the result + String word2 = word + "Forward"; // Find the value of the character at index 3 + System.out.println(word2.charAt(3)); - // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) + // Check whether the string contains a given substring (i.e. does the string + // have "abc" in it?) + System.out.println(word2.contains("abc")); - // Iterate over the characters of the string, printing each one on a separate line + // Iterate over the characters of the string, printing each one on a separate + // line + for (int i = 0; i < word2.length(); i++) { + System.out.println(word2.charAt(i)); + } // Create an ArrayList of Strings and assign it to a variable + List wordList = new ArrayList<>(); // Add multiple strings to the List (OK to do one-by-one) + wordList.add(word); + wordList.add("Forward"); + wordList.add("Shiroko"); - // Join all of the strings in the list together into a single string separated by commas + // 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 + System.out.println(wordList.toString()); // Check whether two strings are equal + System.out.println(word.equals(word2)); /* * Reminder!