KaiquanMah's picture
Collections.sort(this.arrayList1)
d656782 verified
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