Here is my incomplete code you should be able to continue it. Here is some hints: using composition you can store an instance of a string in your class. As you want that this class behave just like the string class you need to recreate all the string method in your new class. These methods then will be a wrapper and delegate all its functionality to the string class.
Below I've wrap some methods for your example, the equals(), length() and to String(). Now you can continue with the rest.
public class StringWrap {
private String message;
public StringWrap(String message) {
this.message = message;
}
public boolean equals(Object anObject) {
return this.message.equals(anObject);
}
public String toString() {
return this.message.toString();
}
public int length() {
return this.message.length();
}
//
// TODO: implements others wrapper methods here!
//
//
// Your new add methods
//
public void add(int pos, char c) {
int length = this.message.length() + 1;
for (int i = 0; i < length; i++) {
//
}
// TODO: add some more code
}
public static void main(String[] args) {
StringWrap wrap = new StringWrap("hello");
wrap.add(5, 'x');
}
}