-
Notifications
You must be signed in to change notification settings - Fork 94
Java Derusting #5
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
11bf9ba
3602d0f
7393237
5670765
08022ba
fb8ccb9
e9b3177
e41db81
1941847
e8a40d3
7fdb49c
ad17790
a8328be
5180ae5
852cff5
d7f6340
518f05e
add10b9
cd9d36e
99eb7a5
296b2d6
4abee5d
9902325
520b7cd
3ed5a73
b80f9ea
f5475c7
abbfdcd
2e03a12
d1b27ca
1e7c0b4
a9c1a98
5fef464
d8abcea
1ac7783
1b4e713
5f2d93d
f6a167c
51b87dc
e1e0e6d
55cd535
fd1b79f
a6232b9
967598b
ec661ab
55ded98
95364f5
f7aff83
54b66a8
9f3f58c
ea78bc0
6ec6b20
9679e1c
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,37 @@ | ||
| public class ArrayPractice { | ||
| public static void main(String[] args) { | ||
| // Create an array of Strings of size 4 | ||
| String[] arrayFour = 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 | ||
| arrayFour[0] = "ZERO"; | ||
| arrayFour[1] = "ONE"; | ||
| arrayFour[2] = "TWO"; | ||
| arrayFour[3] = "THREE"; | ||
|
|
||
| // Get the value of the array at index 2 | ||
| System.out.println(arrayFour[2]); //output: TWO | ||
|
|
||
| // Get the length of the array | ||
| System.out.println(arrayFour.length); | ||
|
|
||
| // Iterate over the array using a traditional for loop and print out each item | ||
| for (int i = 0; i < arrayFour.length; i++) { | ||
| System.out.println(arrayFour[i]); | ||
| } | ||
|
|
||
| // Iterate over the array using a for-each loop and print out each item | ||
| for (String string : arrayFour) { | ||
| System.out.println(string); | ||
| } | ||
|
|
||
| /* | ||
| * Reminder! | ||
| * | ||
| * Arrays start at index 0 | ||
| */ | ||
|
|
||
| //DONE | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +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> mappy = new HashMap<>(); | ||
|
Owner
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 (Map) |
||
| // Put 3 different key/value pairs in the Map | ||
| // (it's OK to do this one-by-one) | ||
| mappy.put("a", 1); | ||
| mappy.put("b", 2); | ||
| mappy.put("c", 3); | ||
|
|
||
| // Get the value associated with a given key in the Map | ||
| mappy.get("a"); | ||
|
|
||
| // Find the size (number of key/value pairs) of the Map | ||
| mappy.size(); | ||
|
|
||
| // Replace the value associated with a given key (the size of the Map shoukld not change) | ||
| mappy.replace("a", 0); | ||
|
|
||
| // Check whether the Map contains a given key | ||
| if (mappy.containsKey("d")) { | ||
| System.out.println("Letter d, is present within list MAPPY"); | ||
| } else { | ||
| System.out.println("There is no key present"); | ||
| } | ||
|
|
||
| // Check whether the Map contains a given value | ||
| if (mappy.containsValue(2)) { | ||
| System.out.println("Value 2, is present within list MAPPY"); | ||
| } else { | ||
| System.out.println("There is no value present"); | ||
| } | ||
|
|
||
| // Iterate over the keys of the Map, printing each key | ||
| for (String key : mappy.keySet()) { | ||
| System.out.println(key); | ||
| } | ||
|
|
||
| // Iterate over the values of the map, printing each value | ||
| for (Integer value : mappy.values()) { | ||
| System.out.println(value); | ||
| } | ||
|
|
||
| // Iterate over the entries in the map, printing each key and value | ||
| for (HashMap.Entry<String, Integer> entries : mappy.entrySet()) { | ||
| System.out.println("KEY " + entries.getKey()); | ||
| System.out.println("VALUE " + entries.getValue()); | ||
| } | ||
|
|
||
| /* | ||
| * Usage tip! | ||
|
|
@@ -39,5 +66,7 @@ public static void main(String[] args) { | |
| * Example: If you want to hold the student ID numbers of everyone in a course, | ||
| * and you don't care about any ordering. | ||
| */ | ||
|
|
||
| //DONE | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,29 @@ | ||
| 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> StringSet = new HashSet<>(); | ||
|
Owner
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) | ||
| StringSet.add("TOMATO"); | ||
| StringSet.add("POTATO"); | ||
| StringSet.add("YAM"); | ||
|
|
||
| // Check whether the Set contains a given String | ||
| StringSet.contains("CABBAGE"); | ||
|
|
||
| // Remove an element from the Set | ||
| StringSet.remove("TOMATO"); | ||
|
|
||
| // Get the size of the Set | ||
| StringSet.size(); | ||
|
|
||
| // Iterate over the elements of the Set, printing each one on a separate line | ||
| for (String veggie : StringSet) { | ||
| System.out.println(veggie); | ||
| } | ||
|
|
||
| /* | ||
| * Warning! | ||
|
|
@@ -25,5 +37,7 @@ public static void main(String[] args) { | |
| * | ||
| * Also remember that sets do NOT have duplicates. | ||
| */ | ||
|
|
||
| //DONE | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,52 @@ | ||
| 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 name = "Luigi"; | ||
| System.out.println(name); | ||
|
|
||
| // Find the length of the string | ||
| System.out.println(name.length()); | ||
| System.out.println(); | ||
|
|
||
| // Concatenate (add) two strings together and reassign the result | ||
| String nameTwo = "Mario "; | ||
| String result = nameTwo + name; | ||
| System.out.println(result); | ||
|
|
||
| // Find the value of the character at index 3 | ||
| System.out.println(name.charAt(3)); | ||
|
|
||
| // Check whether the string contains a given substring (i.e. does the string have "abc" in it?) | ||
| boolean substring = name.contains("abc"); | ||
| System.out.println(substring); | ||
|
|
||
| // Iterate over the characters of the string, printing each one on a separate line | ||
| for (char letter : name.toCharArray()) { | ||
| System.out.println(letter); | ||
| } | ||
|
|
||
| // Create an ArrayList of Strings and assign it to a variable | ||
|
|
||
| ArrayList<String> Spiders = new ArrayList<>(); | ||
|
Owner
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 (List) |
||
| // Add multiple strings to the List (OK to do one-by-one) | ||
| Spiders.add("Peter Parker"); | ||
| Spiders.add("Miguel O'Hara"); | ||
| Spiders.add("Miles Morales"); | ||
| Spiders.add("Gwen Stacy"); | ||
| Spiders.add("Peni Parker"); | ||
|
Comment on lines
+33
to
+37
Owner
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. Fun! |
||
|
|
||
| // 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 SpiderPeople = String.join(", ", Spiders); | ||
| System.out.println(SpiderPeople); | ||
|
|
||
| // Check whether two strings are equal | ||
| if (Spiders.get(3).equals(4)) { | ||
| System.out.println("Their names are equal!"); | ||
| } else { | ||
| System.out.println("Their names are not equal"); | ||
| } | ||
|
|
||
| /* | ||
| * Reminder! | ||
|
|
@@ -28,5 +55,7 @@ public static void main(String[] args) { | |
| * | ||
| * We use == when comparing primitives (e.g. int or char). | ||
| */ | ||
|
|
||
| //DONE | ||
| } | ||
| } | ||
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 (List)