package dk.itu.oop.testexamf2004; import java.util.ArrayList; class Test2 { public static void main(String[] a){ MutableString ms = new MutableStringArray("Kasper"); System.out.println(ms); System.out.println(ms.subString(1,4)); ms.replace(2,3,new MutableStringArray("zzbb")); System.out.println(ms); ms.replace(2,5,new MutableStringArray("")); System.out.println(ms); ms.replace(2,1,new MutableStringArray("sp")); System.out.println(ms); MutableString m2 = new MutableStringArray("Jesper"); System.out.println(m2.equals(ms)); System.out.println(m2.equals(null)); m2 = new MutableStringArray("Kasper"); System.out.println(m2.equals(ms)); ms.replace(6,10,new MutableStringArray(" Østerbye")); System.out.println(ms); } } interface MutableString { /* index of first character is 0. The last character has index size()-1 */ /* return the number of characters in this mutable string */ int size(); /* return the char at the given index */ char charAt(int index); /* return a mutable string that is a copy of the charaters at index from * to index to. Both index from and to are included. * if from>to an empty substring is returned. */ MutableString subString(int from, int to); /* replace the substring as defined above with replacement */ void replace(int from, int to, MutableString replacement); } class MutableStringArray implements MutableString { private char[] string; MutableStringArray() { string = new char[0]; } MutableStringArray(String initial){ string = new char[initial.length()]; for (int i = 0; i= size() ) to = size()-1; MutableStringArray sub = new MutableStringArray(); sub.string = new char[to-from+1]; int subindex=0; for (int i = from; i<=to; i++){ sub.string[subindex] = string[i]; subindex++; } return sub; } public void replace(int from, int to, MutableString replacement){ if (from < 0 ) from = 0; if (to >= size() ) to = size()-1; // the characters [0..from-1] are untouched // the characters [from..to] are replaced // the characters [to+1..size()-1] are untouched, but moved // replaced[0..from-1] = string[o..from-1]; // replaced[from..from+replacement.size()-1] = replacement // replaced[from+replacement.size()-1..newSize-1] = string[to+1..size()-1] int newSize = from+ replacement.size()-1 + (size()-to); char[] replaced = new char[newSize]; for (int i = 0; i