In: Computer Science
Name: __________________________________________
Specifications:
public void reset()
Resets the WordMaker so that it is ready to start building a word.
public void addChar(newChar);
Adds a character to the WordMaker. If it is a letter and
WordMaker is making a word, it adds it to the end of the word. If
it is a letter and WordMaker is not making a word, it erases the
current word, and starts a new word with the letter. If it is not a
letter and WordMaker is making a word, it ends the word (unless
it is an apostrophe, treated specially as described above). If it is not
a letter and WordMaker is not making a word, it is ignored.
public boolean wordReady()
Returns true if the WordMaker has finished constructing a word,
false otherwise. After a reset, or when the WordMaker is first
constructed, it returns false. When a word has just been completed
(i.e., the first non-letter character has been added), it returns true.
Otherwise it returns false.
public String getWord()
Returns the word that the WordMaker has created when
WordReady returns true. It should not be called when WordReady
returns false.
CODE :
class WordMaker{
private String word = "";
boolean wordBuilding = false;
public void reset(){
word = "";
}
public void addChar(char c){
int asci = c;
if((asci >= 65 && asci
<= 90) || (asci >=97 && asci <= 122)){
if(wordBuilding){
word += c;
}
else{
reset();
wordBuilding = true;
word += c;
}
}
else if(c == '"'){}
else{
wordBuilding =
false;
}
}
public boolean wordReady(){
if(word.length() != 0 &&
!wordBuilding){
return
true;
}
else{
return
false;
}
}
public String getWord(){
return word;
}
}
class Main{
public static void main(String [] args){
WordMaker wm = new
WordMaker();
if(wm.wordReady()){
System.out.println(wm.getWord());
}
String test = "sfda saf
;ljljoih";
for(int
i=0;i<test.length();i++){
wm.addChar(test.charAt(i));
if(wm.wordReady()){
System.out.println(wm.getWord());
}
}
System.out.println(wm.getWord());
}
}
OUTPUT :
NOTE :
Unit testing is not implemented.
The string (test) in the class "Main" is designed as such the whole program is being covered and tested.
If still test cases are required :
1] Empty string (test = "")
2] String with only one word (test = "HELLO")
3] String with more than one word without a special character (test = "HELLO WORLD")
4] String with apostrophe (test = "HELLO \"WORLD ")
5] String with special characters (test = "HELLO ;<World")