View Single Post
  #2 (permalink)  
Old 12-11-2007, 06:13 PM
wsaryada wsaryada is offline
Senior Member
 
Join Date: Jun 2007
Location: Bali, ID
Posts: 102
wsaryada is on a distinguished road
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.

Code:
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'); } }
__________________
Website:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
- Blog:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Reply With Quote