Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; public class ClosedLab14a { /* * A simple program used

ID: 3692462 • Letter: I

Question

import java.util.Scanner;

public class ClosedLab14a {

/*

   * A simple program used to test the SimpleStringBuilder class

   */

public static void main(String[] args) {

SimpleStringBuilder myBuilder = new SimpleStringBuilder("Hello World");

System.out.println(myBuilder.toString());

// Code below this comment will not work until you complete Exercise 1

System.out.println(myBuilder.length());

System.out.println(myBuilder.charAt(3));

myBuilder.replaceCharAt(3, 'p');

System.out.println(myBuilder.toString());

// Code below this comment will not work until you complete Exercise 2

// myBuilder.append('!');

// System.out.println(myBuilder.toString());

// myBuilder.deleteCharAt(3);

// System.out.println(myBuilder.toString());

// System.out.println(myBuilder.length());

// myBuilder.insert(3, 'z');

// System.out.println(myBuilder.toString());

// System.out.println(myBuilder.length());

}

}

How to I change the first part of the code to correctly display the length, charAt, and replace? Then for the second part how do I fix the append, deleteCharAt, and insert methods?

Explanation / Answer


  
import java.io.Serializable;

/**
* <code>StringBuilder</code> represents a changeable <code>String</code>.
45: * It provides the operations required to modify the
46: * <code>StringBuilder</code>, including insert, replace, delete, append,
47: * and reverse. It like <code>StringBuffer</code>, but is not
48: * synchronized. It is ideal for use when it is known that the
49: * object will only be used from a single thread.
50: *
51: * <p><code>StringBuilder</code>s are variable-length in nature, so even if
52: * you initialize them to a certain size, they can still grow larger than
53: * that. <em>Capacity</em> indicates the number of characters the
54: * <code>StringBuilder</code> can have in it before it has to grow (growing
55: * the char array is an expensive operation involving <code>new</code>).
56: *
57: * <p>Incidentally, compilers often implement the String operator "+"
58: * by using a <code>StringBuilder</code> operation:<br>
59: * <code>a + b</code><br>
60: * is the same as<br>
61: * <code>new StringBuilder().append(a).append(b).toString()</code>.
62: *
63: * <p>Classpath's StringBuilder is capable of sharing memory with Strings for
64: * efficiency. This will help when a StringBuilder is converted to a String
65: * and the StringBuilder is not changed after that (quite common when
66: * performing string concatenation).
67: *
68: * @author Paul Fisher
69: * @author John Keiser
70: * @author Tom Tromey
71: * @author Eric Blake (ebb9@email.byu.edu)
72: * @see String
73: * @see StringBuffer
74: *
75: * @since 1.5
76: */
public final class SimpleStringBuilder
implements Serializable, CharSequence, Appendable
{
// Implementation note: if you change this class, you usually will
// want to change StringBuffer as well.
  
/**
* For compatability with Sun's JDK
*/
private static final long serialVersionUID = 4383685877147921099L;

/**
89: * Index of next available character (and thus the size of the current
90: * string contents). Note that this has permissions set this way so that
91: * String can get the value.
92: *
93: * @serial the number of characters in the buffer
94: */
int count;

/**
98: * The buffer. Note that this has permissions set this way so that String
  
99: * can get the value.
100: *
101: * @serial the buffer
102: */
char[] value;
  
/**
: * The default capacity of a buffer.
107: */
private static final int DEFAULT_CAPACITY = 16;
  
/**
* Create a new StringBuilder with default capacity 16.
112: */
public SimpleStringBuilder()
{
this(DEFAULT_CAPACITY);
}

/**
* Create an empty <code>StringBuilder</code> with the specified initial
* capacity.
*
* @param capacity the initial capacity
* @throws NegativeArraySizeException if capacity is negative
*/
public SimpleStringBuilder(int capacity)
{
value = new char[capacity];
}
  

/**
131: * Create a new <code>StringBuilder</code> with the characters in the
132: * specified <code>String</code>. Initial capacity will be the size of the
133: * String plus 16.
134: *
135: * @param str the <code>String</code> to convert
136: * @throws NullPointerException if str is null
137: */
public SimpleStringBuilder(String str)
{
// Unfortunately, because the size is 16 larger, we cannot share.
count = str.count;
value = new char[count + DEFAULT_CAPACITY];
str.getChars(0, count, value, 0);
}

/**
147: * Create a new <code>StringBuilder</code> with the characters in the
148: * specified <code>CharSequence</code>. Initial capacity will be the
149: * length of the sequence plus 16; if the sequence reports a length
150: * less than or equal to 0, then the initial capacity will be 16.
151: *
152: * @param seq the initializing <code>CharSequence</code>
153: * @throws NullPointerException if str is null
154: */
public SimpleStringBuilder(CharSequence seq)
{
int len = seq.length();
count = len <= 0 ? 0 : len;
value = new char[count + DEFAULT_CAPACITY];
for (int i = 0; i < len; ++i)
value[i] = seq.charAt(i);
}

/**
165: * Get the length of the <code>String</code> this <code>StringBuilder</code>
166: * would create. Not to be confused with the <em>capacity</em> of the
167: * <code>StringBuilder</code>.
168: *
169: * @return the length of this <code>StringBuilder</code>
170: * @see #capacity()
171: * @see #setLength(int)
172: */
public int length()
{
return count;
}

/**
179: * Get the total number of characters this <code>StringBuilder</code> can
180: * support before it must be grown. Not to be confused with <em>length</em>.
181: *
182: * @return the capacity of this <code>StringBuilder</code>
183: * @see #length()
184: * @see #ensureCapacity(int)
185: */
public int capacity()
{
return value.length;
}
  
/**
192: * Increase the capacity of this <code>StringBuilder</code>. This will
193: * ensure that an expensive growing operation will not occur until
194: * <code>minimumCapacity</code> is reached. The buffer is grown to the
195: * larger of <code>minimumCapacity</code> and
196: * <code>capacity() * 2 + 2</code>, if it is not already large enough.
197: *
198: * @param minimumCapacity the new capacity
199: * @see #capacity()
200: */
public void ensureCapacity(int minimumCapacity)
{
if (minimumCapacity > value.length)
{
int max = value.length * 2 + 2;
minimumCapacity = (minimumCapacity < max ? max : minimumCapacity);
char[] nb = new char[minimumCapacity];
VMSystem.arraycopy(value, 0, nb, 0, count);
value = nb;
}
}

/**
214: * Set the length of this StringBuilder. If the new length is greater than
215: * the current length, all the new characters are set to ''. If the new
216: * length is less than the current length, the first <code>newLength</code>
217: * characters of the old array will be preserved, and the remaining
218: * characters are truncated.
219: *
220: * @param newLength the new length
221: * @throws IndexOutOfBoundsException if the new length is negative
222: * (while unspecified, this is a StringIndexOutOfBoundsException)
223: * @see #length()
224: */
public void setLength(int newLength)
{
if (newLength < 0)
throw new StringIndexOutOfBoundsException(newLength);
  
int valueLength = value.length;

/* Always call ensureCapacity in order to preserve copy-on-write
semantics. */
ensureCapacity(newLength);
  
if (newLength < valueLength)
{
/* If the StringBuilder's value just grew, then we know that
value is newly allocated and the region between count and
newLength is filled with ''. */
count = newLength;
}
else
{
/* The StringBuilder's value doesn't need to grow. However,
we should clear out any cruft that may exist. */
while (count < newLength)
value[count++] = '';
}
}

/**
* Get the character at the specified index.
254: *
255: * @param index the index of the character to get, starting at 0
256: * @return the character at the specified index
257: * @throws IndexOutOfBoundsException if index is negative or &gt;= length()
258: * (while unspecified, this is a StringIndexOutOfBoundsException)
259: */
public char charAt(int index)
{
if (index < 0 || index >= count)
throw new StringIndexOutOfBoundsException(index);
return value[index];
}
/**
268: * Get the specified array of characters. <code>srcOffset - srcEnd</code>
269: * characters will be copied into the array you pass in.
270: *
271: * @param srcOffset the index to start copying from (inclusive)
272: * @param srcEnd the index to stop copying from (exclusive)
273: * @param dst the array to copy into
274: * @param dstOffset the index to start copying into
275: * @throws NullPointerException if dst is null
276: * @throws IndexOutOfBoundsException if any source or target indices are
277: * out of range (while unspecified, source problems cause a
278: * StringIndexOutOfBoundsException, and dest problems cause an
279: * ArrayIndexOutOfBoundsException)
280: * @see System#arraycopy(Object, int, Object, int, int)
281: */
public void getChars(int srcOffset, int srcEnd,
char[] dst, int dstOffset)
{
if (srcOffset < 0 || srcEnd > count || srcEnd < srcOffset)
throw new StringIndexOutOfBoundsException();
VMSystem.arraycopy(value, srcOffset, dst, dstOffset, srcEnd - srcOffset);
}
  
/**
* Set the character at the specified index.
292: *
293: * @param index the index of the character to set starting at 0
294: * @param ch the value to set that character to
295: * @throws IndexOutOfBoundsException if index is negative or &gt;= length()
296: * (while unspecified, this is a StringIndexOutOfBoundsException)
297: */
public void setCharAt(int index, char ch)
{
if (index < 0 || index >= count)
throw new StringIndexOutOfBoundsException(index);
// Call ensureCapacity to enforce copy-on-write.
ensureCapacity(count);
value[index] = ch;
}
  
/**
308: * Append the <code>String</code> value of the argument to this
309: * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert
310: * to <code>String</code>.
311: *
312: * @param obj the <code>Object</code> to convert and append
313: * @return this <code>StringBuilder</code>
314: * @see String#valueOf(Object)
315: * @see #append(String)
316: */
public SimpleStringBuilder append(Object obj)
{
return append(obj == null ? "null" : obj.toString());
}
  
/**
323: * Append the <code>String</code> to this <code>StringBuilder</code>. If
324: * str is null, the String "null" is appended.
325: *
326: * @param str the <code>String</code> to append
327: * @return this <code>StringBuilder</code>
328: */
public SimpleStringBuilder append(String str)
{
if (str == null)
str = "null";
int len = str.count;
ensureCapacity(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}
  
/**
341: * Append the <code>StringBuilder</code> value of the argument to this
342: * <code>StringBuilder</code>. This behaves the same as
343: * <code>append((Object) stringBuffer)</code>, except it is more efficient.
344: *
345: * @param stringBuffer the <code>StringBuilder</code> to convert and append
346: * @return this <code>StringBuilder</code>
347: * @see #append(Object)
348: */
public SimpleStringBuilder append(StringBuffer stringBuffer)
{
if (stringBuffer == null)
return append("null");
synchronized (stringBuffer)
{
int len = stringBuffer.count;
ensureCapacity(count + len);
VMSystem.arraycopy(stringBuffer.value, 0, value, count, len);
count += len;
}
return this;
}

/**
364: * Append the <code>char</code> array to this <code>StringBuilder</code>.
365: * This is similar (but more efficient) than
366: * <code>append(new String(data))</code>, except in the case of null.
367: *
368: * @param data the <code>char[]</code> to append
369: * @return this <code>StringBuilder</code>
370: * @throws NullPointerException if <code>str</code> is <code>null</code>
371: * @see #append(char[], int, int)
372: */
public SimpleStringBuilder append(char[] data)
{
return append(data, 0, data.length);
}

/**
379: * Append part of the <code>char</code> array to this
380: * <code>StringBuilder</code>. This is similar (but more efficient) than
381: * <code>append(new String(data, offset, count))</code>, except in the case
382: * of null.
383: *
384: * @param data the <code>char[]</code> to append
385: * @param offset the start location in <code>str</code>
386: * @param count the number of characters to get from <code>str</code>
387: * @return this <code>StringBuilder</code>
388: * @throws NullPointerException if <code>str</code> is <code>null</code>
389: * @throws IndexOutOfBoundsException if offset or count is out of range
390: * (while unspecified, this is a StringIndexOutOfBoundsException)
391: */
public SimpleStringBuilder append(char[] data, int offset, int count)
{
if (offset < 0 || count < 0 || offset > data.length - count)
   throw new StringIndexOutOfBoundsException();
ensureCapacity(this.count + count);
VMSystem.arraycopy(data, offset, value, this.count, count);
this.count += count;
return this;
}

/**
403: * Append the <code>String</code> value of the argument to this
404: * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert
405: * to <code>String</code>.
406: *
407: * @param bool the <code>boolean</code> to convert and append
408: * @return this <code>StringBuilder</code>
409: * @see String#valueOf(boolean)
410: */
public SimpleStringBuilder append(boolean bool)
{
return append(bool ? "true" : "false");
}
  
/**
417: * Append the <code>char</code> to this <code>StringBuilder</code>.
418: *
419: * @param ch the <code>char</code> to append
420: * @return this <code>StringBuilder</code>
421: */
public SimpleStringBuilder append(char ch)
{
ensureCapacity(count + 1);
value[count++] = ch;
return this;
}
  
/**
* Append the characters in the <code>CharSequence</code> to this
431: * buffer.
432: *
433: * @param seq the <code>CharSequence</code> providing the characters
434: * @return this <code>StringBuilder</code>
435: */
public SimpleStringBuilder append(CharSequence seq)
{
return append(seq, 0, seq.length());
}

/**
442: * Append some characters from the <code>CharSequence</code> to this
443: * buffer. If the argument is null, the four characters "null" are
444: * appended.
445: *
446: * @param seq the <code>CharSequence</code> providing the characters
447: * @param start the starting index
448: * @param end one past the final index
449: * @return this <code>StringBuilder</code>
450: */
public SimpleStringBuilder append(CharSequence seq, int start,
int end)
{
if (seq == null)
return append("null");
if (end - start > 0)
{
ensureCapacity(count + end - start);
for (; start < end; ++start)
value[count++] = seq.charAt(start);
}
return this;
}
  
/**
466: * Append the code point to this <code>StringBuilder</code>.
467: * This is like #append(char), but will append two characters
468: * if a supplementary code point is given.
469: *
470: * @param code the code point to append
471: * @return this <code>StringBuilder</code>
472: * @see Character#toChars(int, char[], int)
473: * @since 1.5
474: */
public synchronized StringBuilder appendCodePoint(int code)
{
int len = Character.charCount(code);
ensureCapacity(count + len);
Character.toChars(code, value, count);
count += len;
return this;
}

/**
* Append the <code>String</code> value of the argument to this
<code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert
487: * to <code>String</code>.
488: *
489: * @param inum the <code>int</code> to convert and append
490: * @return this <code>StringBuilder</code>
491: * @see String#valueOf(int)
492: */
// This is native in libgcj, for efficiency.
public SimpleStringBuilder append(int inum)
{
return append(String.valueOf(inum));
}

/**
* Append the <code>String</code> value of the argument to this
* <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert
* to <code>String</code>.
*
* @param lnum the <code>long</code> to convert and append
505: * @return this <code>StringBuilder</code>
506: * @see String#valueOf(long)
507: */
public SimpleStringBuilder append(long lnum)
{
return append(Long.toString(lnum, 10));
}

/**
14: * Append the <code>String</code> value of the argument to this
515: * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert
516: * to <code>String</code>.
517: *
518: * @param fnum the <code>float</code> to convert and append
519: * @return this <code>StringBuilder</code>
520: * @see String#valueOf(float)
521: */
public SimpleStringBuilder append(float fnum)
{
return append(Float.toString(fnum));
}

/**
528: * Append the <code>String</code> value of the argument to this
529: * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert
530: * to <code>String</code>.
531: *
532: * @param dnum the <code>double</code> to convert and append
533: * @return this <code>StringBuilder</code>
534: * @see String#valueOf(double)
535: */
public SimpleStringBuilder append(double dnum)
{
return append(Double.toString(dnum));
}

/**
42: * Delete characters from this <code>StringBuilder</code>.
543: * <code>delete(10, 12)</code> will delete 10 and 11, but not 12. It is
544: * harmless for end to be larger than length().
545: *
546: * @param start the first character to delete
547: * @param end the index after the last character to delete
548: * @return this <code>StringBuilder</code>
549: * @throws StringIndexOutOfBoundsException if start or end are out of bounds
550: */
public SimpleStringBuilder delete(int start, int end)
{
if (start < 0 || start > count || start > end)
throw new StringIndexOutOfBoundsException(start);
if (end > count)
end = count;
// This will unshare if required.
ensureCapacity(count);
if (count - end != 0)
VMSystem.arraycopy(value, end, value, start, count - end);
count -= end - start;
return this;
}
  
/**
566: * Delete a character from this <code>StringBuilder</code>.
567: *
568: * @param index the index of the character to delete
569: * @return this <code>StringBuilder</code>
570: * @throws StringIndexOutOfBoundsException if index is out of bounds
571: */
public SimpleStringBuilder deleteCharAt(int index)
{
return delete(index, index + 1);
}
  
/**
578: * Replace characters between index <code>start</code> (inclusive) and
579: * <code>end</code> (exclusive) with <code>str</code>. If <code>end</code>
580: * is larger than the size of this StringBuilder, all characters after
581: * <code>start</code> are replaced.
582: *
583: * @param start the beginning index of characters to delete (inclusive)
584: * @param end the ending index of characters to delete (exclusive)
585: * @param str the new <code>String</code> to insert
586: * @return this <code>StringBuilder</code>
587: * @throws StringIndexOutOfBoundsException if start or end are out of bounds
588: * @throws NullPointerException if str is null
589: */
public SimpleStringBuilder replace(int start, int end, String str)
{
if (start < 0 || start > count || start > end)
throw new StringIndexOutOfBoundsException(start);
  
int len = str.count;
// Calculate the difference in 'count' after the replace.
int delta = len - (end > count ? count : end) + start;
ensureCapacity(count + delta);

if (delta != 0 && end < count)
VMSystem.arraycopy(value, end, value, end + delta, count - end);
  
str.getChars(0, len, value, start);
count += delta;
return this;
}
  
/**
609: * Creates a substring of this StringBuilder, starting at a specified index
610: * and ending at the end of this StringBuilder.
611: *
612: * @param beginIndex index to start substring (base 0)
613: * @return new String which is a substring of this StringBuilder
614: * @throws StringIndexOutOfBoundsException if beginIndex is out of bounds
615: * @see #substring(int, int)
616: */
public String substring(int beginIndex)
{
return substring(beginIndex, count);
}
  
/**
623: * Creates a substring of this StringBuilder, starting at a specified index
624: * and ending at one character before a specified index. This is implemented
625: * the same as <code>substring(beginIndex, endIndex)</code>, to satisfy
626: * the CharSequence interface.
627: *
628: * @param beginIndex index to start at (inclusive, base 0)
629: * @param endIndex index to end at (exclusive)
630: * @return new String which is a substring of this StringBuilder
631: * @throws IndexOutOfBoundsException if beginIndex or endIndex is out of
632: * bounds
633: * @see #substring(int, int)
634: */
public CharSequence subSequence(int beginIndex, int endIndex)
{
return substring(beginIndex, endIndex);
}
  
/**
641: * Creates a substring of this StringBuilder, starting at a specified index
642: * and ending at one character before a specified index.
643: *
644: * @param beginIndex index to start at (inclusive, base 0)
645: * @param endIndex index to end at (exclusive)
646: * @return new String which is a substring of this StringBuilder
647: * @throws StringIndexOutOfBoundsException if beginIndex or endIndex is out
* of bounds
*/
public String substring(int beginIndex, int endIndex)
{
int len = endIndex - beginIndex;
if (beginIndex < 0 || endIndex > count || endIndex < beginIndex)
throw new StringIndexOutOfBoundsException();
if (len == 0)
return "";
return new String(value, beginIndex, len);
}

/**
661: * Insert a subarray of the <code>char[]</code> argument into this
662: * <code>StringBuilder</code>.
663: *
664: * @param offset the place to insert in this buffer
665: * @param str the <code>char[]</code> to insert
666: * @param str_offset the index in <code>str</code> to start inserting from
667: * @param len the number of characters to insert
668: * @return this <code>StringBuilder</code>
669: * @throws NullPointerException if <code>str</code> is <code>null</code>
670: * @throws StringIndexOutOfBoundsException if any index is out of bounds
*/
public SimpleStringBuilder insert(int offset,
char[] str, int str_offset, int len)
{
if (offset < 0 || offset > count || len < 0
|| str_offset < 0 || str_offset > str.length - len)
throw new StringIndexOutOfBoundsException();
ensureCapacity(count + len);
VMSystem.arraycopy(value, offset, value, offset + len, count - offset);
VMSystem.arraycopy(str, str_offset, value, offset, len);
count += len;
return this;
}

/**
686: * Insert the <code>String</code> value of the argument into this
687: * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert
688: * to <code>String</code>.
689: *
690: * @param offset the place to insert in this buffer
691: * @param obj the <code>Object</code> to convert and insert
692: * @return this <code>StringBuilder</code>
693: * @exception StringIndexOutOfBoundsException if offset is out of bounds
694: * @see String#valueOf(Object)
*/
public SimpleStringBuilder insert(int offset, Object obj)
{
return insert(offset, obj == null ? "null" : obj.toString());
}

/**
702: * Insert the <code>String</code> argument into this
703: * <code>StringBuilder</code>. If str is null, the String "null" is used
704: * instead.
705: *
706: * @param offset the place to insert in this buffer
* @param str the <code>String</code> to insert
* @return this <code>StringBuilder</code>
* @throws StringIndexOutOfBoundsException if offset is out of bounds
*/
public SimpleStringBuilder insert(int offset, String str)
{
if (offset < 0 || offset > count)
throw new StringIndexOutOfBoundsException(offset);
if (str == null)
str = "null";
int len = str.count;
ensureCapacity(count + len);
VMSystem.arraycopy(value, offset, value, offset + len, count - offset);
str.getChars(0, len, value, offset);
count += len;
return this;
}
/**
726: * Insert the <code>CharSequence</code> argument into this
727: * <code>StringBuilder</code>. If the sequence is null, the String
728: * "null" is used instead.
729: *
730: * @param offset the place to insert in this buffer
731: * @param sequence the <code>CharSequence</code> to insert
732: * @return this <code>StringBuilder</code>
733: * @throws IndexOutOfBoundsException if offset is out of bounds
*/
public synchronized StringBuilder insert(int offset, CharSequence sequence)
{
if (sequence == null)
sequence = "null";
return insert(offset, sequence, 0, sequence.length());
}

/**
743: * Insert a subsequence of the <code>CharSequence</code> argument into this
744: * <code>StringBuilder</code>. If the sequence is null, the String
745: * "null" is used instead.
746: *
747: * @param offset the place to insert in this buffer
748: * @param sequence the <code>CharSequence</code> to insert
749: * @param start the starting index of the subsequence
750: * @param end one past the ending index of the subsequence
751: * @return this <code>StringBuilder</code>
752: * @throws IndexOutOfBoundsException if offset, start,
753: * or end are out of bounds
*/
public synchronized StringBuilder insert(int offset, CharSequence sequence,
int start, int end)
{
if (sequence == null)
sequence = "null";
if (start < 0 || end < 0 || start > end || end > sequence.length())
throw new IndexOutOfBoundsException();
int len = end - start;
ensureCapacity(count + len);
VMSystem.arraycopy(value, offset, value, offset + len, count - offset);
for (int i = start; i < end; ++i)
value[offset++] = sequence.charAt(i);
count += len;
return this;
}
  
/**
772: * Insert the <code>char[]</code> argument into this
773: * <code>StringBuilder</code>.
774: *
775: * @param offset the place to insert in this buffer
776: * @param data the <code>char[]</code> to insert
777: * @return this <code>StringBuilder</code>
778: * @throws NullPointerException if <code>data</code> is <code>null</code>
779: * @throws StringIndexOutOfBoundsException if offset is out of bounds
: * @see #insert(int, char[], int, int)
*/
public SimpleStringBuilder insert(int offset, char[] data)
{
return insert(offset, data, 0, data.length);
}
  
/**
788: * Insert the <code>String</code> value of the argument into this
789: * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert
790: * to <code>String</code>.
791: *
792: * @param offset the place to insert in this buffer
793: * @param bool the <code>boolean</code> to convert and insert
794: * @return this <code>StringBuilder</code>
795: * @throws StringIndexOutOfBoundsException if offset is out of bounds
796: * @see String#valueOf(boolean)
*/
public SimpleStringBuilder insert(int offset, boolean bool)
{
return insert(offset, bool ? "true" : "false");
}

/**
804: * Insert the <code>char</code> argument into this <code>StringBuilder</code>.
805: *
806: * @param offset the place to insert in this buffer
807: * @param ch the <code>char</code> to insert
808: * @return this <code>StringBuilder</code>
809: * @throws StringIndexOutOfBoundsException if offset is out of bounds
*/
public SimpleStringBuilder insert(int offset, char ch)
{
if (offset < 0 || offset > count)
throw new StringIndexOutOfBoundsException(offset);
ensureCapacity(count + 1);
VMSystem.arraycopy(value, offset, value, offset + 1, count - offset);
value[offset] = ch;
count++;
return this;
}

/**
823: * Insert the <code>String</code> value of the argument into this
824: * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert
825: * to <code>String</code>.
826: *
827: * @param offset the place to insert in this buffer
828: * @param inum the <code>int</code> to convert and insert
829: * @return this <code>StringBuilder</code>
830: * @throws StringIndexOutOfBoundsException if offset is out of bounds
831: * @see String#valueOf(int)
: */
public SimpleStringBuilder insert(int offset, int inum)
{
return insert(offset, String.valueOf(inum));
}
  
/**
839: * Insert the <code>String</code> value of the argument into this
840: * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert
841: * to <code>String</code>.
842: *
843: * @param offset the place to insert in this buffer
844: * @param lnum the <code>long</code> to convert and insert
845: * @return this <code>StringBuilder</code>
846: * @throws StringIndexOutOfBoundsException if offset is out of bounds
847: * @see String#valueOf(long)
*/
public SimpleStringBuilder insert(int offset, long lnum)
{
return insert(offset, Long.toString(lnum, 10));
}

/**
855: * Insert the <code>String</code> value of the argument into this
856: * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert
857: * to <code>String</code>.
858: *
859: * @param offset the place to insert in this buffer
860: * @param fnum the <code>float</code> to convert and insert
861: * @return this <code>StringBuilder</code>
* @throws StringIndexOutOfBoundsException if offset is out of bounds
* @see String#valueOf(float)
*/
public SimpleStringBuilder insert(int offset, float fnum)
{
return insert(offset, Float.toString(fnum));
}
  
/**
871: * Insert the <code>String</code> value of the argument into this
872: * <code>StringBuilder</code>. Uses <code>String.valueOf()</code> to convert
873: * to <code>String</code>.
874: *
875: * @param offset the place to insert in this buffer
876: * @param dnum the <code>double</code> to convert and insert
877: * @return this <code>StringBuilder</code>
878: * @throws StringIndexOutOfBoundsException if offset is out of bounds
879: * @see String#valueOf(double)
*/
public SimpleStringBuilder insert(int offset, double dnum)
{
return insert(offset, Double.toString(dnum));
}
  
/**
887: * Finds the first instance of a substring in this StringBuilder.
888: *
889: * @param str String to find
890: * @return location (base 0) of the String, or -1 if not found
891: * @throws NullPointerException if str is null
892: * @see #indexOf(String, int)
*/
public int indexOf(String str)
{
return indexOf(str, 0);
}
  
/**
900: * Finds the first instance of a String in this StringBuilder, starting at
901: * a given index. If starting index is less than 0, the search starts at
902: * the beginning of this String. If the starting index is greater than the
903: * length of this String, or the substring is not found, -1 is returned.
904: *
905: * @param str String to find
* @param fromIndex index to start the search
* @return location (base 0) of the String, or -1 if not found
* @throws NullPointerException if str is null
*/
public int indexOf(String str, int fromIndex)
{
if (fromIndex < 0)
fromIndex = 0;
int limit = count - str.count;
for ( ; fromIndex <= limit; fromIndex++)
if (regionMatches(fromIndex, str))
return fromIndex;
return -1;
}
/**
922: * Finds the last instance of a substring in this StringBuilder.
923: *
924: * @param str String to find
925: * @return location (base 0) of the String, or -1 if not found
926: * @throws NullPointerException if str is null
927: * @see #lastIndexOf(String, int)
*/
public int lastIndexOf(String str)
{
return lastIndexOf(str, count - str.count);
}

/**
935: * Finds the last instance of a String in this StringBuilder, starting at a
936: * given index. If starting index is greater than the maximum valid index,
937: * then the search begins at the end of this String. If the starting index
938: * is less than zero, or the substring is not found, -1 is returned.
939: *
940: * @param str String to find
941: * @param fromIndex index to start the search
942: * @return location (base 0) of the String, or -1 if not found
943: * @throws NullPointerException if str is null
*/
public int lastIndexOf(String str, int fromIndex)
{
fromIndex = Math.min(fromIndex, count - str.count);
for ( ; fromIndex >= 0; fromIndex--)
if (regionMatches(fromIndex, str))
return fromIndex;
return -1;
}
/**
955: * Reverse the characters in this StringBuilder. The same sequence of
956: * characters exists, but in the reverse index ordering.
957: *
958: * @return this <code>StringBuilder</code>
*/
public SimpleStringBuilder reverse()
{
// Call ensureCapacity to enforce copy-on-write.
ensureCapacity(count);
for (int i = count >> 1, j = count - i; --i >= 0; ++j)
{
char c = value[i];
value[i] = value[j];
value[j] = c;
}
return this;
}
/**
974: * Convert this <code>StringBuilder</code> to a <code>String</code>. The
975: * String is composed of the characters currently in this StringBuilder. Note
976: * that the result is a copy, and that future modifications to this buffer
977: * do not affect the String.
978: *
979: * @return the characters in this StringBuilder
*/
public String toString()
{
return new String(this);
}

/**
987: * Predicate which determines if a substring of this matches another String
988: * starting at a specified offset for each String and continuing for a
989: * specified length. This is more efficient than creating a String to call
990: * indexOf on.
991: *
992: * @param toffset index to start comparison at for this String
993: * @param other non-null String to compare to region of this
994: * @return true if regions match, false otherwise
995: * @see #indexOf(String, int)
996: * @see #lastIndexOf(String, int)
997: * @see String#regionMatches(boolean, int, String, int, int)
*/
private boolean regionMatches(int toffset, String other)
{
int len = other.count;
int index = other.offset;
while (--len >= 0)
if (value[toffset++] != other.value[index++])
return false;
return true;
}

/**
1010: * Get the code point at the specified index. This is like #charAt(int),
1011: * but if the character is the start of a surrogate pair, and the
1012: * following character completes the pair, then the corresponding
1013: * supplementary code point is returned.
1014: * @param index the index of the codepoint to get, starting at 0
1015: * @return the codepoint at the specified index
1016: * @throws IndexOutOfBoundsException if index is negative or &gt;= length()
1017: * @since 1.5
: */
public int codePointAt(int index)
{
return Character.codePointAt(value, index, count);
}

/**
1025: * Get the code point before the specified index. This is like
1026: * #codePointAt(int), but checks the characters at <code>index-1</code> and
1027: * <code>index-2</code> to see if they form a supplementary code point.
1028: * @param index the index just past the codepoint to get, starting at 0
: * @return the codepoint at the specified index
* @throws IndexOutOfBoundsException if index is negative or &gt;= length()
* @since 1.5
*/
public int codePointBefore(int index)
: {
: // Character.codePointBefore() doesn't perform this check. We
// could use the CharSequence overload, but this is just as easy.
if (index >= count)
throw new IndexOutOfBoundsException();
return Character.codePointBefore(value, index, 1);
}

/**
1043: * Returns the number of Unicode code points in the specified sub sequence.
1044: * Surrogate pairs count as one code point.
1045: * @param beginIndex the start of the subarray
1046: * @param endIndex the index after the last char in the subarray
1047: * @return the number of code points
1048: * @throws IndexOutOfBoundsException if beginIndex is less than zero or
1049: * greater than endIndex or if endIndex is greater than the length of this
* StringBuilder
*/
public int codePointCount(int beginIndex,int endIndex)
{
if (beginIndex < 0 || beginIndex > endIndex || endIndex > count)
throw new IndexOutOfBoundsException("invalid indices: " + beginIndex
+ ", " + endIndex);
return Character.codePointCount(value, beginIndex, endIndex - beginIndex);
public void trimToSize()
{
if (count < value.length)
{
char[] newValue = new char[count];
VMSystem.arraycopy(value, 0, newValue, 0, count);
value = newValue;
}
}
}

main function

package sb;
public class Test {
   /*
   * A simple program used to test the SimpleStringBuilder class
   */
   public static void main(String[] args) {
   SimpleStringBuilder myBuilder = new SimpleStringBuilder("Hello World");
   System.out.println(myBuilder.toString());
   // Code below this comment will not work until you complete Exercise 1
   System.out.println(myBuilder.length());
   System.out.println(myBuilder.charAt(3));
   myBuilder.replaceCharAt(3, 'p');
   System.out.println(myBuilder.toString());
   // Code below this comment will not work until you complete Exercise 2
   myBuilder.append('!');
   System.out.println(myBuilder.toString());
   myBuilder.deleteCharAt(3);
   System.out.println(myBuilder.toString());
   System.out.println(myBuilder.length());
   myBuilder.insert(3, 'z');
   System.out.println(myBuilder.toString());
   System.out.println(myBuilder.length());
   }
   }

}