Q:

(Implement the StringBuilder class) The StringBuilder class is provided in the Java library. Provide your own implementation for the following methods (name the new class MyStringBuilder2):

0

(Implement the StringBuilder class) The StringBuilder class is provided in the Java library. Provide your own implementation for the following methods (name the new class MyStringBuilder2):

public MyStringBuilder2();
public MyStringBuilder2(char[] chars);
public MyStringBuilder2(String s);
public MyStringBuilder2 insert(int offset, MyStringBuilder2 s);
public MyStringBuilder2 reverse();
public MyStringBuilder2 substring(int begin);
public MyStringBuilder2 toUpperCase();

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (Implement the StringBuilder class) The StringBuilder class is provided        *
* in the Java library. Provide your own implementation for the following methods *
* (name the new class MyStringBuilder2):                                         *
*                                                                                *
* public MyStringBuilder2();                                                     *
*                                                                                *
* public MyStringBuilder2(char[] chars);                                         *
* public MyStringBuilder2(String s);                                             *
* public MyStringBuilder2 insert(int offset, MyStringBuilder2 s);                *
* public MyStringBuilder2 reverse();                                             *
* public MyStringBuilder2 substring(int begin);                                  *
* public MyStringBuilder2 toUpperCase();                                         *
*********************************************************************************/
public class Exercise_10_28 {
	/** Main method */
	public static void main(String[] args) {
		// Create an array of characters
		char[] chars = {'I', 'n', 's', 'e', 'r', 't'}; 

		// Create two MyStringBuilder2 objects
		MyStringBuilder2 str1 = new MyStringBuilder2("testString");
		MyStringBuilder2 str2 = new MyStringBuilder2(chars);

		// Insert str2 into str1 at index 3
		System.out.print("\nInsert string 2 into string 1 at index 3: ");
		MyStringBuilder2 str3 = str1.insert(3, str2);
		System.out.println(str3);

		// Reverse str1
		System.out.println("\nReverse string 1: " + str1.reverse());

		// Create a substring of str3 from index 10
		System.out.println("\nSubstring of string 3 beginning index 10: " + 
			str3.substring(10));

		// Display str2 in all upper case
		System.out.println("\nString 2 to upper case: " + str2.toUpperCase());
	}
}
public class MyStringBuilder2 {
	// Data field
	private String s;

	/** Create a default MyStringBuilder object */
	public MyStringBuilder2() {
		s = "";
	}

	/** Create a MyStringBuilder object of a specified character array */
	public MyStringBuilder2(char[] chars) {
		s = String.valueOf(chars);
	}

	/** Create a MyStringBuilder object of a specified string */
	public MyStringBuilder2(String s) {
		this.s = s;
	}

	/** Return a MyStringBuilder object with s inserted at the offset */
	public MyStringBuilder2 insert(int offset, MyStringBuilder2 s) {
		String newStr = "";
		int i;
		for (i = 0; i < offset; i++) {
			if (i < offset)
				newStr += this.s.charAt(i) + "";
		}
		newStr += s;
		return new MyStringBuilder2(newStr + substring(offset));
	}

	// Return a reversed MyStringBuilder2 object
	public MyStringBuilder2 reverse() {
		String newStr = "";
		for (int i = s.length() - 1; i >= 0; i--) {
			newStr += s.charAt(i) + "";
		}
		return new MyStringBuilder2(newStr);
	}

	/** Return a MyStringBuilder object substring beginning at begin */
	public MyStringBuilder2 substring(int begin) {
		String newStr = "";
		for (int i = begin; i < s.length(); i++) {
			newStr += s.charAt(i) + "";
		}
		return new MyStringBuilder2(newStr);
	}

	/** Return a MyStringBuilder object in all upper case */
	public MyStringBuilder2 toUpperCase() {
		String newStr = "";
		for (int i = 0; i < s.length(); i++) {
			if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z')
				newStr += (char)(s.charAt(i) - 32) + "";
			else
				newStr += s.charAt(i) + "";
		}
		return new MyStringBuilder2(newStr);
	}

	/** Return string */
	public String toString() {
		return s;
	}

	/** Return length */
	public int length() {
		return s.length();
	}

	/** Return the character at specified index */
	public char charAt(int index) {
		return s.charAt(index);
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now