File size: 5,217 Bytes
d656782
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
Write a class 'Letters' that models a set of alphabetic characters (lowercase letters a...z).

Note! Since the 'Test' class is public, 
the class 'Letters' cannot be - so do not use the public attribute when defining the class!

Each letter can appear in the set at most once. 
The letters are in alphabetical order.


The class has the following public methods:
The constructor Letters(), which takes no parameters
method public void addLetter(char letter), which takes as parameter one letter to be added to the set
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
Method public String allLetters() which returns all the letters in the set as a single string.


Examples of class functionality:
Letters letters = new Letters();
letters.addLetter('b');
letters.addLetter('a');
letters.addLetter('d');
System.out.println(letters.allLetters());

// no effect, letter already in the set
letters.addLetter('b');
System.out.println(letters.allLetters());


letters.addLetters("hfsr");
System.out.println(letters.allLetters());

// Adds only those letters,
// that are not yet in the set
letters.addLetters("abxysr");
System.out.println(letters.allLetters());


Program outputs:
abd
abd
abdfhrs
abdfhrsxy





===========



import java.util.Random;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Collections;

public class Test{
    public static void main(String[] args){
        System.out.println("Testing class Letters");
        
        Letters letters = new Letters();
        
        System.out.println("Object created!");
        
        String chars = "abcxy";
        for (int i=0; i<chars.length(); i++) {
            char c = chars.charAt(i);
            System.out.println("Adding letter: " + c);
            letters.addLetter(c);
            System.out.println("Set now: " + letters.allLetters());
        }
        
        System.out.println("");
        System.out.println("Trying to add letters that are already in the set");
        
        chars = "acx";
        for (int i=0; i<chars.length(); i++) {
            char c = chars.charAt(i);
            System.out.println("Adding letter: " + c);
            letters.addLetter(c);
            System.out.println("Set now: " + letters.allLetters());
        }
        
        System.out.println("");
        System.out.println("Adding several letters at once");
        
        String[] arr = {"def", "gr", "hsz"};
        for (String s : arr) {
            System.out.println("Adding string: " + s);
            letters.addLetters(s);
            System.out.println("Set now: " + letters.allLetters());
        }
        
        System.out.println("");
        System.out.println("Adding several letters that are in the set already");
        
        arr = new String[] {"abc", "hij", "prst"};
        for (String s : arr) {
            System.out.println("Adding string: " + s);
            letters.addLetters(s);
            System.out.println("Set now: " + letters.allLetters());
        }
        
    }
}






class Letters {
    // CLASS ATTRIBUTES
    private ArrayList<String> letters;

    // CONSTRUCTOR
    public Letters() {
        this.letters = new ArrayList<String>();
    }

    public void addLetter(char letter) {
        String letterString = String.valueOf(letter);
        // new set of letters
        if (letterString.length() < 1) {
            this.letters.add(letterString);
        }
        // add letter if it is not in the set
        else if (!this.letters.contains(letterString)) {
            this.letters.add(letterString);
            // https://www.geeksforgeeks.org/java/arrays-sort-in-java/
            // 'Arrays' sort had issues
            // Arrays.sort(this.letters);
            // move to 'Collections' sort
            // https://www.baeldung.com/java-arrays-collections-sort-methods
            Collections.sort(this.letters);
        }
        // else, do not add 'letter'
    }

    public void addLetters(String letters) {
        // go through each character in the 'letters' string
        // (converted to a charcaters array)
        for (char c : letters.toCharArray()) {
            // call the single letter addition method
            this.addLetter(c);
        }
    }

    public String allLetters() {
        // convert ArrayList to a string
        // return this.letters.toString();
        return String.join("", this.letters);
    }
}









Testing class Letters
Object created!
Adding letter: a
Set now: a
Adding letter: b
Set now: ab
Adding letter: c
Set now: abc
Adding letter: x
Set now: abcx
Adding letter: y
Set now: abcxy

Trying to add letters that are already in the set
Adding letter: a
Set now: abcxy
Adding letter: c
Set now: abcxy
Adding letter: x
Set now: abcxy

Adding several letters at once
Adding string: def
Set now: abcdefxy
Adding string: gr
Set now: abcdefgrxy
Adding string: hsz
Set now: abcdefghrsxyz

Adding several letters that are in the set already
Adding string: abc
Set now: abcdefghrsxyz
Adding string: hij
Set now: abcdefghijrsxyz
Adding string: prst
Set now: abcdefghijprstxyz