Hi all, this is a homework assignment that I'm not sure how to begin with. We are given all the code and I just need to finish a function to convert digits to words. For example, the string "1, 2, and 3." becomes "One, Two, and Three." The String "10" becomes "OneZero." We aren't allowed to use any loop constructs either ("for" or "while"). We are supposed to use the switch-case statements somehow. The missing function is the "public void append(char c)" This is what I have but I'm getting no results.
public abstract class Builder {
// Appends just one character
public abstract void append(char c);
// Force implementer to create a toString method.
public abstract String toString();
// Makes use of the append(char) method
// to append a whole string.
public void append(String s) {
for(int i=0;i<s.length();i++) {
append(s.charAt(i));
}
}
// Makes use of the fact that all Objects
// have a toString() method to format all
// other objects.
public void append(Object o) {
if(o == null)
append("null");
else
append(o.toString());
}
}
public class NumBuilder extends Builder {
StringBuffer sb = new StringBuffer();
public void append(char c) {
switch(c)
{
case 1: sb.append("One");
break;
case 2: sb.append("Two");
break;
case 3: sb.append("Three");
break;
case 4: sb.append("Four");
break;
case 5: sb.append("Five");
break;
}
}
public String toString() {
return sb.toString();
}
public static void main(String[] args) {
NumBuilder nb = new NumBuilder();
nb.append("1, 2, and 3.");
System.out.println(nb);
nb.append("4 and 5");
}
}