I.Implement the StringBuilder class. The StringBuilder class is provided in the
ID: 663788 • Letter: I
Question
I.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 MyStringBuilder3:
public MyStringBuilder3 ();
public MyStringBuilder3 (String s);
public MyStringBuilder3 (char [] chars);
public MyStringBuilder3 append (MyStringBuilder3 s);
public MyStringBuilder3 append (int i);
public int length ();
public char charAt (int index);
public MyStringBuilder3 toLowerCase();
public MyStringBuilder3 toUpperCase();
public MyStringBuilder3 substring (int begin);
public MyStringBuilder3 substring (int begin, int end);
public MyStringBuilder3 insert (int offset, MyStringBuilder3 s);
public MyStringBuilder3 reverse ();
public String toString();
BELOW IS WHAT I HAVE SO FAR , BUT IM NOT SURE HOW TO CONTINUE
public class MyStringBuilder2
{
int size = 0;
int capacity = 0;
char [] buffer ;
public MyStringbuilder2()
{
size = 0;
capacity = 0;
buffer = new char[];
}
public MyStringBuilder2(char[] chars)
{
buffer = new char[chars.length];
capacity = buffer.length;
for(int i = 0; i < chars.length ; i++)
{
buffer[i] = chars[i];
size++ ;
}
}
public MyStringBuilder2(String s)
{
buffer = new char [s.length()];
capacity = buffer.length;
for(int i=0; i< s.length ; i++)
{
buffer[i] = s[i];
size++;
}
}
public void increasCapacity(int newCapacity)
{
char [] temp = new char[newCapacity];
for (int i =0 ; i< buffer.length ; i++)
{
buffer[i] = temp[i];
}
capacity = newCapacity;
buffer = temp;
}
Explanation / Answer
CONCEPT:
StringBuilder functions to be implemented ..
COMMENTS: I have added comments each line of code for your understanding. and I have imlemented methods as you said ....I have added test class to test these functionalities....
CODE:
//class for the MyStrinBuilder....
public class MyStringBuilder3
{
int size = 0;
int capacity = 0;
char [] buffer ;
StringBuilder sb=new StringBuilder("Chegg");
//this method is for Constructor as specified by you..
public MyStringBuilder3()
{
size = 0;
capacity = 0;
//buffer = new char[];
}
//this method is for Constructor of parameter type String...
public MyStringBuilder3(String s)
{
buffer = new char [s.length()];
capacity = buffer.length;
for(int i=0; i< s.length() ; i++)
{
buffer[i] = s.charAt(i);
size++;
}
}
//this method is for Constructor of parameter type character array...
public MyStringBuilder3(char[] chars)
{
buffer = new char[chars.length];
capacity = buffer.length;
for(int i = 0; i < chars.length ; i++)
{
buffer[i] = chars[i];
size++ ;
}
}
//this method appends the given value to the stringBuilders....
public void append(String string){
StringBuilder sbb=sb.append(string);
//this statement appends the s to the sb ,it is defined above..
System.out.println("StringBuilder after appending s is "+sbb+" ");
}
//this method appends the integers to the stringbuilder...
public void append (int i){
StringBuilder tem=sb.append(i);//this statement appends to the end of the StringBuilder
System.out.println("StringBuilder after appaending int value to the StringBuilder is:"+tem+" ");
}
//this method length of the given stringBuilder onject...
public int length (){
int le=sb.length();//this statement returns the length of the StringBuilder
System.out.println("The length of the StringBuilder is:"+le+" ");
return le;
}
//this method finds the character at specified location...
public char charAt (int index){
char c=sb.charAt(index);//this statement returns the character at the index.
System.out.println("the character at the index "+index+" in uppercase letters is:"+c+" ");
return c;
}
//this method changes string to lower case letters...
public void toLowerCase(){
String lwcase=new String(sb);//this statement changes from stringbuilder to string...
String tem=lwcase.toLowerCase();//this statement changes the string to lower case ....
System.out.println("the String in lower letters is:"+tem+" ");
}
//this method changes string to upper case letters...
public void toUpperCase(){
String upcase=new String(sb);//this statement changes from stringbuilder to string...
String tem=upcase.toUpperCase();//this statement changes the string to upper case ....
System.out.println("the String in uppercase letters is:"+tem+" ");
}
//this method returns the substring from the beginning
public void substring (int begin){
String sdtmp=sb.substring(begin);
// this statement find the substring from begin index..
System.out.println("sustring of given StrongBuilder is:"+sdtmp+" ");
}
//this method is to find the substring of from begin and end indexes as given...
public void substring (int begin, int end){
String sdtmp=sb.substring(begin,end);
//this statemet finds the substring from begin to end ...
System.out.println("sustring of given StrongBuilder is:"+sdtmp+" ");
}
//this method inserts the given StringBuilder at offset position.
public void insert (int offset, String string){
StringBuilder sdtmp=sb.insert(offset, string);
//this line inserts the string at offset position...
System.out.println("StringBuilder after inserting givn string is :"+sdtmp+" ");
}
//this method reverse the given stringbuilder ....
public void reverse (){
StringBuilder sbdr=sb.reverse();
//this statement finds the rverse of the stringbuilder....
System.out.println("Stringbuilder after reversing is:"+sbdr+" ");
}
//this method returns the Stringbuilder into string form...
public String toString(){
String sbdr=sb.toString();
//this statement changes the StringBuilder into string...
System.out.println("Stringbuilder after changing to String is:"+sbdr+" ");
return sbdr;
}
}
Client class to test those functinolaities It you need use it,otherwise ignore
class StrinManip {
public static void main(String gt){
String sb="Chegg";
MyStringBuilder3 mstrbuilder=new MyStringBuilder3();
mstrbuilder.append("Help");
mstrbuilder.append(1);
mstrbuilder.length();
mstrbuilder.charAt(1);
mstrbuilder.toLowerCase();
mstrbuilder.toUpperCase();
mstrbuilder.substring(2);
mstrbuilder.substring(2, 4);
mstrbuilder.insert(2, "E");
mstrbuilder.reverse();
mstrbuilder.toString();
}
}
OUTPUT:
StringBuilder after appending s is CheggHelp
StringBuilder after appaending int value to the StringBuilder is:Chegg1
The length of the StringBuilder is:5
the character at the index 1 is:H
the String in lower letters is: chegg
the String in uppercase letters is: CHEGG
sustring of given StrongBuilder is: EGG
sustring of given StrongBuilder is: EGD
StringBuilder after inserting givn string is :chEegg
Stringbuilder after reversing is: ggehc
Stringbuilder after changing to String is: chegg