-
Notifications
You must be signed in to change notification settings - Fork 22
Derusting assignment #4
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?
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,29 +1,55 @@ | ||
|
|
||
| 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> map = 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 | ||
| // (it's OK to do this one-by-one) | ||
| map.put("Cheese", 2); | ||
| map.put("Onions",1); | ||
| map.put("Bread",5); | ||
|
|
||
| // Get the value associated with a given key in the Map | ||
|
|
||
| // Get the value associated with a given key in the Map | ||
| System.out.println("Cheese costs " + map.get("Cheese") + " dollars."); | ||
|
|
||
| // Find the size (number of key/value pairs) of the Map | ||
| System.out.println("There are " + map.size() + " pairs in the Map."); | ||
|
|
||
| // Replace the value associated with a given key (the size of the Map shoukld not change) | ||
| map.put("Cheese", 3); | ||
| System.out.println("Cheese now costs " + map.get("Cheese") + " dollars."); | ||
|
|
||
| // Check whether the Map contains a given key | ||
|
|
||
| if (map.containsKey("Onions")) { | ||
| System.out.println("Map contains onions."); | ||
| } else { | ||
| System.out.println("Map does not contain onions."); | ||
| } | ||
|
|
||
| // Check whether the Map contains a given value | ||
|
|
||
| if (map.containsValue(2)) { | ||
| System.out.println("One of the items has a value of 2."); | ||
| } else { | ||
| System.out.println("None of the items has a value of 2."); | ||
| } | ||
| // Iterate over the keys of the Map, printing each key | ||
| for (String string : map.keySet()) { | ||
| System.out.println(string); | ||
| } | ||
|
|
||
| // Iterate over the values of the map, printing each value | ||
| for (int value : map.values()) { | ||
| System.out.println(value); | ||
| } | ||
|
|
||
| // Iterate over the entries in the map, printing each key and value | ||
|
|
||
| for (String string : map.keySet()) { | ||
| System.out.println(string + "=" + map.get(string)); | ||
| } | ||
|
Comment on lines
+50
to
+52
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. This works! Can you search to find another way to iterate over the entries directly, so you you don't need to repeatedly call |
||
| /* | ||
| * Usage tip! | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,33 @@ | ||
| 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> set = 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 (Set) |
||
|
|
||
| // Add 3 elements to the set | ||
| // (It's OK to do it one-by-one) | ||
| set.add("coffee"); | ||
| set.add("tea"); | ||
| set.add("juice"); | ||
|
|
||
| // Check whether the Set contains a given String | ||
|
|
||
| if (set.contains("juice")) { | ||
| System.out.println("Set contains 'juice'."); | ||
| } else { | ||
| System.out.println("Set does not contain 'juice'"); | ||
| } | ||
| // Remove an element from the Set | ||
| set.remove("juice"); | ||
|
|
||
| // Get the size of the Set | ||
| System.out.println("The set contains " + set.size() + " items."); | ||
|
|
||
| // Iterate over the elements of the Set, printing each one on a separate line | ||
|
|
||
| for (String string : set) { | ||
| System.out.println(string); | ||
| } | ||
|
|
||
| /* | ||
| * Warning! | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,48 @@ | ||
| 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 string = "example"; | ||
|
|
||
| // Find the length of the string | ||
| int stringLength = string.length(); | ||
| System.out.println(stringLength); | ||
|
|
||
| // Concatenate (add) two strings together and reassign the result | ||
| string = string + " 1; " + string + " 2"; | ||
| System.out.println(string); | ||
|
|
||
| // Find the value of the character at index 3 | ||
| System.out.println(string.charAt(3)); | ||
|
|
||
| // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) | ||
| Boolean containsSubstring = string.contains("example"); | ||
| System.out.println("The sentence contains example?: " + containsSubstring); | ||
|
|
||
| // Iterate over the characters of the string, printing each one on a separate line | ||
|
|
||
| for (int i = 0; i < string.length(); i++) { | ||
| System.out.println(string.charAt(i)); | ||
| } | ||
| // Create an ArrayList of Strings and assign it to a variable | ||
| ArrayList<String> stringArray = new ArrayList<>(); | ||
|
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 |
||
|
|
||
| // Add multiple strings to the List (OK to do one-by-one) | ||
| stringArray.add("example 1"); | ||
| stringArray.add("example 2"); | ||
| stringArray.add("example 3"); | ||
|
|
||
| // 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 joinedString = String.join(", ", stringArray); | ||
| System.out.println(joinedString); | ||
|
|
||
| // Check whether two strings are equal | ||
| String first = "test"; | ||
| String second = "text"; | ||
|
|
||
| /* | ||
| System.out.println("String 'test' equals 'text'?: " + first.equals(second)); | ||
| /*java | ||
| * Reminder! | ||
| * | ||
| * When comparing objects in Java we typically want to use .equals, NOT ==. | ||
|
|
||
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 (Map)