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 MyStringBuilder1):

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 MyStringBuilder1):

public MyStringBuilder1(String s);
public MyStringBuilder1 append(MyStringBuilder1 s);
public MyStringBuilder1 append(int i);
public int length();
public char charAt(int index);
public MyStringBuilder1 toLowerCase();
public MyStringBuilder1 substring(int begin, int end);
public String toString();

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 MyStringBuilder1):                                         *
*                                                                                *
* public MyStringBuilder1(String s);                                             *
* public MyStringBuilder1 append(MyStringBuilder1 s);                            *
* public MyStringBuilder1 append(int i);                                         *
* public int length();                                                           *
* public char charAt(int index);                                                 *
* public MyStringBuilder1 toLowerCase();                                         *
* public MyStringBuilder1 substring(int begin, int end);                         *
* public String toString();                                                      *
*********************************************************************************/
public class Exercise_10_27 {
	/** Main method */
	public static void main(String[] args) {
		// Create a MyStringBuilder1 object
		MyStringBuilder1 str1 = new MyStringBuilder1("TEST");

		System.out.println("\nAppend the string " MyStringBuilder1" to string: " + 
			str1.append(new MyStringBuilder1(" MyStringBuilder1")));

		// Display string with the integer 101 append to it
		System.out.println("\nAppend integer 101 to the string: " + str1.append(101));

		// Display the length of str1
		System.out.println("\nLength of string: " + str1.length());

		// Display character at index 2
		System.out.println("\nCharacter at index 2: " + str1.charAt(2));

		// Display str1 as lowercase
		System.out.println("\nString to lower case: " + str1.toLowerCase());

		// Display the substring of str1 from index 1 to 3
		System.out.println("\nSubstring of string from index 1 to 3: " + 
			str1.substring(1, 3));

		// Return the string
		System.out.println("\nDisplay string: " + str1.toString());
	}
}

MyStringBuilder1.java 

public class MyStringBuilder1 {
	private String s;

	public MyStringBuilder1(String s) {
		this.s = s;
	}

	public MyStringBuilder1 append(MyStringBuilder1 s) {
		String newStr  = this.s; 
		newStr += s;
		return new MyStringBuilder1(newStr);
	}

	public MyStringBuilder1 append(int i) {
		String newStr  = this.s; 
		newStr += i + "";
		return new MyStringBuilder1(newStr);
	}

	public int length() {
		return s.length();
	}

	public char charAt(int index) {
		return s.charAt(index);
	}

	public MyStringBuilder1 toLowerCase() {
		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) + "";
			}
		}
		return new MyStringBuilder1(newStr);
	}

	public MyStringBuilder1 substring(int begin, int end) {
		String newStr = "";
		for (int i = begin; i < end; i ++) {
			newStr += s.charAt(i) + "";
		}
		return new MyStringBuilder1(newStr);
	}

	public String toString() {
		return s;
	}
}

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