-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwk4Lab.java
More file actions
398 lines (342 loc) · 14.5 KB
/
wk4Lab.java
File metadata and controls
398 lines (342 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package week.four;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class wk4Lab {
public static void main(String[] args) {
/*
* 1. Why would we use a StringBuilder instead of a String? Strings cannot be changes and are
* immutable. StringBuilder is mutable and can be added upon. Any time we have a String we know
* is going to be build up or changed multiple times, a StringBuilder makes more sense to use.
*/
/*
* a. Instantiate a new StringBuilder b. Append the characters 0 through 9 to it separated by
* dashes Note: make sure no dash appears at the end of the StringBuilder
*/
System.out.println("\nQuestion 1: Stringbuilder - DONE");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) { // loop up through less than 10
sb.append(i); // add the integer
if (i != 9) { // if integer is not 9
sb.append("-"); // include the dash
}
}
System.out.println(sb.toString());
/*
* 2. List of String: a. Create a list of Strings b. Add 5 Strings to it, each with a different
* length
*/
System.out.println();
System.out.println("\nQuestion 2: List of strings - DONE");
List<String> strings = new ArrayList<String>(); // EXAMPLE USES STRINGS FOR LIST NAME
strings.add("Tali");
strings.add("Sammy");
strings.add("Paul");
strings.add("I");
strings.add("Me and you");
for (int i = 0; i < strings.size(); i++) {
System.out.println(strings.get(i));
} // CODE TO PRINT ENTIRE MYBANDS LIST
// System.out.println();
// Same result as above can be done using Arrays.asList(); as opposed to ArrayList<>();
// System.out.println("Using Arrays.asList(enter strings here separated by commas)");
// List<String> myBands =
// Arrays.asList("FFDP", "Slipknot", "DanceGavinDance", "Godsmack", "Nonpoint");
// System.out.println(myBands);
// List<String> strings = Arrays.asList("Tom", "Sammy", "Paul", "I", "Me");
// System.out.println(strings); // prints list with an array [Tom, Sammy, Paul, I, Me]
System.out.println();
System.out.println("\nQuestion 3: findShortestString Method - DONE");
// 3. Write and test a method that takes a list of Strings and returns the shortest
// string
System.out.println(findShortestString(strings));
/*
* 4. Write and test a method that takes a list of strings and returns the list with the first
* and last element switched
*/
System.out.println();
System.out.println("\nQuestion 4: swapFirstAndLast Method - DONE");
List<String> swapped = swapFirstAndLast(strings);
for (String string : swapped) {
System.out.println(string);
} // end of for loop
/*
* 5. Write and test a method that takes a list of strings and returns a string with all the
* list elements concatenated to each other, separated by a comma
*/
System.out.println();
System.out.println("\nQuestion 5: combineStrings Method - DONE");
System.out.println(combineStrings(swapped));
/*
* 6. Write and test a method that takes a list of strings and a string and returns a new list
* with all strings from the original list containing the second string parameter (i.e. like a
* search method)
*/
System.out.println();
System.out.println("\nQuestion 6: search Method - DONE");
List<String> searchResults = search(strings, "am");
for (String result : searchResults) {
System.out.println(result);
}
/*
* 7. Write and test a method that takes a list of integers and returns a List<List<Integer>>
* with the following conditions specified for the return value: a. The first List in the
* returned value contains any number from the input list that is divisible by 2 b. The second
* List contains values from the input list that are divisible by 3 c. The third containing
* values divisible by 5, and d. The fourth all numbers from the input List not divisible by 2,
* 3, or 5
*/
System.out.println();
System.out.println("\nQuestion 7: sortDivisibleNumbers Method - DONE");
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 16, 20, 25, 30);
List<List<Integer>> sortedNumbers = sortDivisibleNumbers(numbers);
for (List<Integer> list : sortedNumbers) {
for (Integer number : list) {
System.out.println(number);
}
System.out.println("Next list ----------");
}
/*
* 8. Write and test a method that takes a list of strings and returns a list of integers that
* contains the length of each string
*/
System.out.println();
System.out.println("\nQuestion 8: calculateStringLengths Method - DONE");
List<Integer> stringsLengths = calculateStringLengths(strings);
for (Integer i : stringsLengths) {
System.out.println(i);
}
// 9. Create a set of strings and add 5 values
System.out.println();
System.out.println("\nQuestion 9: Set of Strings - DONE");
Set<String> strings2 = new HashSet<String>();
// add the String elements to the Set
strings2.add("It's late");
System.out.println(strings2); // prints only the added variables above this request
strings2.add("On a Friday");
System.out.println(strings2); // prints all added variables above this request
strings2.add("I'm tired");
System.out.println(strings2); // in no particular order
strings2.add("and want to");
System.out.println(strings2); // in no particular order
strings2.add("go to bed!");
System.out.println(strings2); // in no particular order
/*
* 10. Write and test a method that takes a set of strings and a character and returns a set of
* strings consisting of all the strings in the input set that start with the character
* parameter.
*/
System.out.println();
System.out.println("\nQuestion 10: findALLThatStartWith Method - DONE");
Set<String> startsWithI = findALLThatStartWith(strings2, 'I');
for (String word : startsWithI) {
System.out.println(word);
}
// 11. Write and test a method that takes a set of strings and returns a list of the same
// strings
System.out.println();
System.out.println("\nQuestion 11: convertSetToList Method - DONE");
List<String> resultList = convertSetToList(strings2);
for (String listString : resultList) {
System.out.println(listString);
}
/*
* 12. Write and test a method that takes a set of integers and returns a new set of integers
* containing only even numbers from the original set
*/
System.out.println();
System.out.println("\nQuestion 12: extractEvens Method - DONE");
Set<Integer> integerSet = new HashSet<Integer>();
integerSet.add(3);
integerSet.add(4);
integerSet.add(8);
integerSet.add(33);
Set<Integer> extractedEvens = extractEvens(integerSet);
for (Integer number : extractedEvens) {
System.out.println(number);
}
/*
* 13. Create a map of string and string and add 3 items to it where the key of each is a word
* and the value is the definition of the word
*/
System.out.println("\nQuestion 13: Dictionary Map - DONE");
Map<String, String> dictionary = new HashMap<String, String>();
dictionary.put("Apple", "A crunchy fruit. Usually red, green, or yellow");
dictionary.put("Banana", "A yellow fruit which you peel before eating");
dictionary.put("Java", "A programming language created in 1995 and still heavily used today");
System.out.println("prompt did not request to print");
/*
* 14. Write and test a method that takes a Map<String, String> and a string and returns the
* value for a key in the map that matches the string parameter (i.e. like a language dictionary
* lookup)
*/
System.out.println("\nQuestion 14: LookupValue Method- DONE");
String value = lookupValue(dictionary, "Apple");
System.out.println("Dictionary Result for 'Apple': " + value);
/*
* 15. Write and test a method that takes a List<String> and returns a Map<Character, Integer>
* containing a count of all the strings that start with a given character
*/
System.out.println("\nQuestion 15: countStartingLetters Method - DONE");
Map<Character, Integer> counts = countStartingLetters(resultList);
for (Character character : counts.keySet()) {
System.out.println(character + " - " + counts.get(character));
}
} // END OF MAIN METHOD
/*
* 15. Write and test a method that takes a List<String> and returns a Map<Character, Integer>
* containing a count of all the strings that start with a given character
*/
public static Map<Character, Integer> countStartingLetters(List<String> list) {
Map<Character, Integer> results = new HashMap<Character, Integer>();
for (String string : list) {
char c = string.charAt(0);
if (results.get(c) == null) {
results.put(c, 1);
} else {
results.put(c, results.get(c) + 1);
}
}
return results;
}
/*
* 14. Write and test a method that takes a Map<String, String> and a string and returns the
* value for a key in the map that matches the string parameter (i.e. like a language dictionary
* lookup)
*/
public static String lookupValue(Map<String, String> map, String string) {
for (String key : map.keySet()) {
if (key.equals(string)) {
return map.get(key);
}
}
return "";
}
/*
* 12. Write and test a method that takes a set of integers and returns a new set of integers
* containing only even numbers from the original set
*/
public static Set<Integer> extractEvens(Set<Integer> set) {
Set<Integer> results = new HashSet<Integer>();
for (Integer number : set) {
if (number % 2 == 0) {
results.add(number);
} //END OF IF STATEMENT
} // END OF FOR LOOP
return results;
} // END OF EXTRACTEVENS METHOD
// 11. Write and test a method that takes a set of strings and returns a list of the same strings
public static List<String> convertSetToList(Set<String> set) {
List<String> results = new ArrayList<String>();
for (String string : set) {
results.add(string);
}
return results;
}
/*
* 10. Write and test a method that takes a set of strings and a character and returns a set of
* strings consisting of all the strings in the input set that start with the character parameter.
*/
public static Set<String> findALLThatStartWith(Set<String> set, char c) {
Set<String> results = new HashSet<String>();
for (String string : set) {
if (string.charAt(0) == c) {
results.add(string);
}
}
return results;
}
/*
* 8. Write and test a method that takes a list of strings and returns a list of integers that
* contains the length of each string
*/
public static List<Integer> calculateStringLengths(List<String> list) {
List<Integer> lengths = new ArrayList<Integer>();
for (String string : list) {
lengths.add(string.length());
}
return lengths;
} // end of calculateStringLengths Method
/*
* 7. Write and test a method that takes a list of integers and returns a List<List<Integer>> with
* the following conditions specified for the return value: a. The first List in the returned
* value contains any number from the input list that is divisible by 2 b. The second List
* contains values from the input list that are divisible by 3 c. The third containing values
* divisible by 5, and d. The fourth all numbers from the input List not divisible by 2, 3, or 5
*/
public static List<List<Integer>> sortDivisibleNumbers(List<Integer> list) { // list of lists of
// integers
List<List<Integer>> results = new ArrayList<List<Integer>>();
results.add(new ArrayList<Integer>());
results.add(new ArrayList<Integer>());
results.add(new ArrayList<Integer>());
results.add(new ArrayList<Integer>());
for (Integer number : list) {
if (number % 2 == 0) {
results.get(0).add(number);
} // END OF DIVISIBLE BY 2 STATEMENT
if (number % 3 == 0) {
results.get(1).add(number);
} // END OF DIVISIBLE BY 3 STATEMENT
if (number % 5 == 0) {
results.get(2).add(number);
} // END OF DIVISIBLE BY 5 STATEMENT
if (number % 2 != 0 && number % 3 != 0 && number % 5 != 0) {
results.get(3).add(number);
} // END OF NOT DIVISIBLE BY 2, 3, OR 5 STATEMENT
}
return results;
} // END OF FOR LOOP
/*
* 6. Write and test a method that takes a list of strings and a string and returns a new list
* with all strings from the original list containing the second string parameter (i.e. like a
* search method)
*/
public static List<String> search(List<String> list, String query) {
List<String> results = new ArrayList<String>();
for (String string : list) {
if (string.contains(query)) {
results.add(string);
}
}
return results;
} // END OF SEARCH METHOD
/*
* 5. Write and test a method that takes a list of strings and returns a string with all the list
* elements concatenated to each other, separated by a comma
*/
public static String combineStrings(List<String> strings) {
StringBuilder result = new StringBuilder();
for (String string : strings) {
result.append(string + ", ");
}
return result.toString();
} // END OF COMBINESTRINGS METHOD
/*
* 4. Write and test a method that takes a list of strings and returns the list with the first and
* last element switched
*/
public static List<String> swapFirstAndLast(List<String> list) {
// we need a place holder variable
String temp = list.get(0); // gets first element from list and saves into temp variable
list.set(0, list.get(list.size() - 1)); // override first element with last element
list.set(list.size() - 1, temp); // set last element as first element
return list;
}
// 3. Write and test a method that takes a list of Strings and returns the shortest
// string findShortestString
// Method 3:
public static String findShortestString(List<String> list) {
String shortest = list.get(0);
for (String string : list) {
if (string.length() < shortest.length()) {
shortest = string;
}
}
return shortest;
} // END OF FINDSHORTESTSTRING METHOD
} // END OF CLASS