KaiquanMah commited on
Commit
d656782
·
verified ·
1 Parent(s): 9be3a92

Collections.sort(this.arrayList1)

Browse files
Week 4: Writing classes/12B. Class Letters ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Write a class 'Letters' that models a set of alphabetic characters (lowercase letters a...z).
2
+
3
+ Note! Since the 'Test' class is public,
4
+ the class 'Letters' cannot be - so do not use the public attribute when defining the class!
5
+
6
+ Each letter can appear in the set at most once.
7
+ The letters are in alphabetical order.
8
+
9
+
10
+ The class has the following public methods:
11
+ The constructor Letters(), which takes no parameters
12
+ method public void addLetter(char letter), which takes as parameter one letter to be added to the set
13
+ Method public void addLetters(String letters), which takes as parameter a string that can contain multiple letters - all letters in the string are added to the set
14
+ Method public String allLetters() which returns all the letters in the set as a single string.
15
+
16
+
17
+ Examples of class functionality:
18
+ Letters letters = new Letters();
19
+ letters.addLetter('b');
20
+ letters.addLetter('a');
21
+ letters.addLetter('d');
22
+ System.out.println(letters.allLetters());
23
+
24
+ // no effect, letter already in the set
25
+ letters.addLetter('b');
26
+ System.out.println(letters.allLetters());
27
+
28
+
29
+ letters.addLetters("hfsr");
30
+ System.out.println(letters.allLetters());
31
+
32
+ // Adds only those letters,
33
+ // that are not yet in the set
34
+ letters.addLetters("abxysr");
35
+ System.out.println(letters.allLetters());
36
+
37
+
38
+ Program outputs:
39
+ abd
40
+ abd
41
+ abdfhrs
42
+ abdfhrsxy
43
+
44
+
45
+
46
+
47
+
48
+ ===========
49
+
50
+
51
+
52
+ import java.util.Random;
53
+ import java.util.ArrayList;
54
+ import java.util.Arrays;
55
+ import java.util.HashMap;
56
+ import java.util.Collections;
57
+
58
+ public class Test{
59
+ public static void main(String[] args){
60
+ System.out.println("Testing class Letters");
61
+
62
+ Letters letters = new Letters();
63
+
64
+ System.out.println("Object created!");
65
+
66
+ String chars = "abcxy";
67
+ for (int i=0; i<chars.length(); i++) {
68
+ char c = chars.charAt(i);
69
+ System.out.println("Adding letter: " + c);
70
+ letters.addLetter(c);
71
+ System.out.println("Set now: " + letters.allLetters());
72
+ }
73
+
74
+ System.out.println("");
75
+ System.out.println("Trying to add letters that are already in the set");
76
+
77
+ chars = "acx";
78
+ for (int i=0; i<chars.length(); i++) {
79
+ char c = chars.charAt(i);
80
+ System.out.println("Adding letter: " + c);
81
+ letters.addLetter(c);
82
+ System.out.println("Set now: " + letters.allLetters());
83
+ }
84
+
85
+ System.out.println("");
86
+ System.out.println("Adding several letters at once");
87
+
88
+ String[] arr = {"def", "gr", "hsz"};
89
+ for (String s : arr) {
90
+ System.out.println("Adding string: " + s);
91
+ letters.addLetters(s);
92
+ System.out.println("Set now: " + letters.allLetters());
93
+ }
94
+
95
+ System.out.println("");
96
+ System.out.println("Adding several letters that are in the set already");
97
+
98
+ arr = new String[] {"abc", "hij", "prst"};
99
+ for (String s : arr) {
100
+ System.out.println("Adding string: " + s);
101
+ letters.addLetters(s);
102
+ System.out.println("Set now: " + letters.allLetters());
103
+ }
104
+
105
+ }
106
+ }
107
+
108
+
109
+
110
+
111
+
112
+
113
+ class Letters {
114
+ // CLASS ATTRIBUTES
115
+ private ArrayList<String> letters;
116
+
117
+ // CONSTRUCTOR
118
+ public Letters() {
119
+ this.letters = new ArrayList<String>();
120
+ }
121
+
122
+ public void addLetter(char letter) {
123
+ String letterString = String.valueOf(letter);
124
+ // new set of letters
125
+ if (letterString.length() < 1) {
126
+ this.letters.add(letterString);
127
+ }
128
+ // add letter if it is not in the set
129
+ else if (!this.letters.contains(letterString)) {
130
+ this.letters.add(letterString);
131
+ // https://www.geeksforgeeks.org/java/arrays-sort-in-java/
132
+ // 'Arrays' sort had issues
133
+ // Arrays.sort(this.letters);
134
+ // move to 'Collections' sort
135
+ // https://www.baeldung.com/java-arrays-collections-sort-methods
136
+ Collections.sort(this.letters);
137
+ }
138
+ // else, do not add 'letter'
139
+ }
140
+
141
+ public void addLetters(String letters) {
142
+ // go through each character in the 'letters' string
143
+ // (converted to a charcaters array)
144
+ for (char c : letters.toCharArray()) {
145
+ // call the single letter addition method
146
+ this.addLetter(c);
147
+ }
148
+ }
149
+
150
+ public String allLetters() {
151
+ // convert ArrayList to a string
152
+ // return this.letters.toString();
153
+ return String.join("", this.letters);
154
+ }
155
+ }
156
+
157
+
158
+
159
+
160
+
161
+
162
+
163
+
164
+
165
+ Testing class Letters
166
+ Object created!
167
+ Adding letter: a
168
+ Set now: a
169
+ Adding letter: b
170
+ Set now: ab
171
+ Adding letter: c
172
+ Set now: abc
173
+ Adding letter: x
174
+ Set now: abcx
175
+ Adding letter: y
176
+ Set now: abcxy
177
+
178
+ Trying to add letters that are already in the set
179
+ Adding letter: a
180
+ Set now: abcxy
181
+ Adding letter: c
182
+ Set now: abcxy
183
+ Adding letter: x
184
+ Set now: abcxy
185
+
186
+ Adding several letters at once
187
+ Adding string: def
188
+ Set now: abcdefxy
189
+ Adding string: gr
190
+ Set now: abcdefgrxy
191
+ Adding string: hsz
192
+ Set now: abcdefghrsxyz
193
+
194
+ Adding several letters that are in the set already
195
+ Adding string: abc
196
+ Set now: abcdefghrsxyz
197
+ Adding string: hij
198
+ Set now: abcdefghijrsxyz
199
+ Adding string: prst
200
+ Set now: abcdefghijprstxyz
201
+
202
+