-
Notifications
You must be signed in to change notification settings - Fork 22
TW-Test #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
TW-Test #3
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,38 @@ | ||
| public class ArrayPractice { | ||
| public static void main(String[] args) { | ||
| // Create an array of Strings of size 4 | ||
| String[] arrayOfStrings = {"None", "None", "None", "None"}; | ||
|
|
||
| // Set the value of the array at each index to be a different String | ||
| arrayOfStrings[0] = "I"; | ||
| arrayOfStrings[1] = "Was"; | ||
| arrayOfStrings[2] = "In"; | ||
| arrayOfStrings[3] = "Class"; | ||
|
|
||
| // It's OK to do this one-by-one | ||
|
|
||
| // Get the value of the array at index 2 | ||
| System.out.println(arrayOfStrings[2]); | ||
|
|
||
| // Get the length of the array | ||
|
|
||
| int item = arrayOfStrings.length; | ||
| System.out.println(item); | ||
| // Iterate over the array using a traditional for loop and print out each item | ||
|
|
||
| for(int i = 0; i < item; i++) | ||
| { | ||
| System.out.println(arrayOfStrings[i]); | ||
| } | ||
|
|
||
| // Iterate over the array using a for-each loop and print out each item | ||
| for (String Printing : arrayOfStrings) | ||
| { | ||
| System.out.println(Printing); | ||
| } | ||
|
|
||
| /* | ||
| * Reminder! | ||
| * | ||
| * Arrays start at index 0 | ||
| */ | ||
| } | ||
|
|
||
| Reminder! | ||
| Arrays start at index 0*/ | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,59 @@ | ||
| import java.util.ArrayList; | ||
| 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<String> EmptyList = new ArrayList<>(); | ||
|
|
||
| // Add 3 elements to the list (OK to do one-by-one) | ||
|
|
||
| EmptyList.add("Hello"); | ||
| EmptyList.add("GoodBye"); | ||
| EmptyList.add("See you around"); | ||
| // Print the element at index 1 | ||
|
|
||
| System.out.println(EmptyList.get(2)); | ||
| // Replace the element at index 1 with a new value | ||
| EmptyList.set(1, "GoodMorning"); | ||
| // (Do not insert a new value. The length of the list should not change) | ||
|
|
||
| // Insert a new element at index 0 (the length of the list will change) | ||
|
|
||
| EmptyList.add(0, "Bonjour"); | ||
| System.out.println(EmptyList); | ||
| // Check whether the list contains a certain string | ||
|
|
||
| System.out.println(EmptyList.contains("Bonjour")); | ||
| // Iterate over the list using a traditional for-loop. | ||
| for(int i = 0; i < EmptyList.size(); i++) | ||
| { | ||
| System.out.println(EmptyList); | ||
| } | ||
| // Print each index and value on a separate line | ||
| for(int i = 0; i < EmptyList.size(); i++) | ||
| { | ||
| System.out.println(i + " " + EmptyList.get(i)); | ||
| } | ||
|
|
||
| // Sort the list using the Collections library | ||
| EmptyList.sort(null); | ||
| System.out.println(EmptyList); | ||
|
|
||
|
|
||
| // Iterate over the list using a for-each loop | ||
| for (String IterateOver : EmptyList) | ||
| { | ||
| System.out.println(IterateOver); | ||
| } | ||
| // Print each value on a second line | ||
| for (String IterateOver : EmptyList) | ||
| { | ||
| System.out.println(IterateOver); | ||
| System.out.println(); | ||
| } | ||
|
|
||
| /* | ||
| * Usage tip! | ||
| * | ||
| * 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 | ||
| * index values a for-each loop is cleaner. | ||
| */ | ||
| } | ||
|
|
||
| Usage tip! | ||
| 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 | ||
| index values a for-each loop is cleaner.*/ | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,63 @@ | ||
|
|
||
| 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<String, Integer> workingMap = new HashMap<>(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember to use interface types where appropriate (Map) |
||
|
|
||
| // Put 3 different key/value pairs in the Map | ||
| workingMap.put("Words", 10); | ||
| workingMap.put("fun", 20); | ||
| workingMap.put("excited", 30); | ||
| // (it's OK to do this one-by-one) | ||
|
|
||
| System.out.println(workingMap); | ||
|
|
||
| // Get the value associated with a given key in the Map | ||
|
|
||
| System.out.println(workingMap.get("fun")); | ||
| // Find the size (number of key/value pairs) of the Map | ||
|
|
||
| System.out.println(workingMap.size()); | ||
| // Replace the value associated with a given key (the size of the Map shoukld not change) | ||
|
|
||
| workingMap.replace("fun", 15); | ||
| System.out.println(workingMap); | ||
| // Check whether the Map contains a given key | ||
|
|
||
| System.out.println(workingMap.containsKey("fun")); | ||
| // Check whether the Map contains a given value | ||
|
|
||
| System.out.println(workingMap.containsValue(30)); | ||
| // Iterate over the keys of the Map, printing each key | ||
|
|
||
| for(int i = 0; i < workingMap.size(); i++) | ||
| { | ||
| System.out.println(workingMap.keySet()); | ||
| } | ||
| // Iterate over the values of the map, printing each value | ||
|
|
||
|
|
||
| for(int i = 0; i < workingMap.size(); i++) | ||
| { | ||
| System.out.println(workingMap.values()); | ||
| } | ||
| // Iterate over the entries in the map, printing each key and value | ||
|
|
||
| for(int i = 0; i < workingMap.size(); i++) | ||
| { | ||
| System.out.println(workingMap); | ||
| } | ||
|
Comment on lines
+34
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These don't quite work. This goes and repeatedly prints out all of the keys, then all of the values, then the entire set. We want them one at a time. Consider using a for-each loop for these. Make sure to run your code to check whether it does what you're wanting. |
||
| /* | ||
| * 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 | ||
| * | ||
| * 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 | ||
| * 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. | ||
| */ | ||
| } | ||
|
|
||
| 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 | ||
| 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 | ||
| 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.*/ | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,17 +5,32 @@ | |
|
|
||
| public class Person { | ||
| // Declare a public String instance variable for the name of the person | ||
| String name; | ||
| // Declare a private int instance variable for the age of the person | ||
|
|
||
| int age; | ||
|
Comment on lines
+8
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to public and private access modifiers to make these have the access specified by the comments. |
||
|
|
||
| // Create a constructor that takes the name and age of the person | ||
| // and assigns it to the instance variables | ||
| public Person(int personAge, String personName) | ||
| { | ||
| age = personAge; | ||
| name = personName; | ||
| } | ||
|
|
||
|
|
||
| // Create a toString method that gives the name and age of the person | ||
| public String toString() | ||
| { | ||
| return name + " " + age; | ||
| } | ||
|
|
||
|
|
||
| // Implement the below public instance method "birthYear" | ||
|
|
||
| public int birthYear(int year) | ||
| { | ||
| return year - age; | ||
| } | ||
| // There should NOT be any print statement in this method. | ||
| /** | ||
| * birthYear returns the year the person was born. | ||
|
|
@@ -32,21 +47,30 @@ public class Person { | |
|
|
||
| public static void main(String[] args) { | ||
| // Create an instance of Person | ||
| Person person1 = new Person(22, "Tim"); | ||
|
|
||
| // Create another instance of Person with a different name and age and | ||
| // assign it to a different variable | ||
| Person person2 = new Person(30, "Ron"); | ||
|
|
||
| // 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 fName = person1.name; | ||
| System.out.println(fName); | ||
|
|
||
| // 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 yearBorn = person1.birthYear(2025); | ||
|
|
||
| // In a separate statement, print the local variable holding the birth year. | ||
| System.out.println(yearBorn); | ||
|
|
||
| /** | ||
| * Terminology! | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,35 @@ | ||
| 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 | ||
| HashSet<String> hashSetValue = new HashSet<String>(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember to use interface types where appropriate (Set) |
||
|
|
||
| // Add 3 elements to the set | ||
| // (It's OK to do it one-by-one) | ||
| hashSetValue.add("red"); | ||
| hashSetValue.add("blue"); | ||
| hashSetValue.add("green"); | ||
|
|
||
| // Check whether the Set contains a given String | ||
|
|
||
| System.out.println(hashSetValue.contains("red")); | ||
|
|
||
| // Remove an element from the Set | ||
|
|
||
| System.out.println(hashSetValue); | ||
| hashSetValue.remove("red"); | ||
| System.out.println(hashSetValue); | ||
|
|
||
| // Get the size of the Set | ||
|
|
||
| System.out.println(hashSetValue.size()); | ||
|
|
||
| // Iterate over the elements of the Set, printing each one on a separate line | ||
| for (String iterate : hashSetValue) | ||
| { | ||
| System.out.println(iterate); | ||
| } | ||
|
|
||
| /* | ||
| * Warning! | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remember to use interface types where appropriate (List)
Also, in general we have our variables and methods in Java be in camelCase, where the first letter is lowercase. We have classes and interfaces be capitalized.