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

I\'m trying to get the get the length, charAt, and replaceCharAt methods to func

ID: 3692369 • Letter: I

Question

I'm trying to get the get the length, charAt, and replaceCharAt methods to function properly in Lab14a by editing SimpleStringBuilder.

Lab14a:

public class Lab14a {
   public static void main(String[] args) {
        SimpleStringBuilder myBuilder = new SimpleStringBuilder("Hello World");
        System.out.println(myBuilder.toString());


        System.out.println(myBuilder.length());
        System.out.println(myBuilder.charAt(3));
        myBuilder.replaceCharAt(3, 'p');
        System.out.println(myBuilder.toString());

}

SimpleStringBuilder:

import java.util.ArrayList;

public class SimpleStringBuilder {

    /**
     * A private member variable used to hold the internal state of the
     * SimpleStringBuilder. This ArrayList holds the characters of the String
     * that will be built. So the String "Hello" would be represented by an
     * ArrayList containing the characters ['H', 'e', 'l', 'l', 'o'].
     */
    ArrayList list;

    private void createEmptyBuilder() {
        this.list = new ArrayList<>();
    }

    /**
     * Constructs an empty SimpleStringBuilder.
     *
     */
    public SimpleStringBuilder() {
        this.createEmptyBuilder();
    }

    /**
     * Constructs a SimpleStringBuilder that contains the same characters as the
     * String input
     *
     * @param input
     *            the String to copy into the StringBuilder
     */
    public SimpleStringBuilder(String input) {
        this.createEmptyBuilder();

        for (int i = 0; i < input.length(); i++) {
            this.list.add(input.charAt(i));
        }
    }

    /**
     * Returns a String object built from the SimpleStringBuilder characters
     */
    @Override
    public String toString() {

        String myString = "";
        for (int i = 0; i < this.list.size(); i++) {
            myString = myString + this.list.get(i);
        }
        return myString;
    }

    /**
     * Returns the character at position i
     *
     * @param i
     *            the index to be checked
     * @return the character at position i
     */
    public char charAt(int i) {

        // TODO - your code here

        // The line below is only here to remove compiler errors.
        // Replace it with your own appropriate return statement and
        // delete this comment
        return 0;
    }

    /**
     * Returns the length of the SimpleStringBuilder object
     *
     * @return the length of the StringBuilder
     */
    public int length() {

        // TODO - your code here

        // The line below is only here to remove compiler errors.
        // Replace it with your own appropriate return statement and
        // delete this comment
        return 0;
    }

    /**
     * Replaces the character at position i with the character c
     *
     * @param i
     *            index to be replaced
     * @param c
     *            character to use in replacement
     */
    public void replaceCharAt(int i, char c, String s) {
        // TODO - your code here
      
      
  

    }

    /**
     * Appends the character c to the end of the StringBuilder
     *
     * @param c
     *            character to append
     */
    public void append(char c) {
        // TODO - your code here

    }

    /**
     * Inserts the character c at position i
     *
     * @param i
     *            index to insert at
     * @param c
     *            character to be inserted
     */
    public void insert(int i, char c) {
        // TODO - your code here

    }

    /**
     * Deletes the character at position i
     *
     * @param i
     *            index to delete
     */
    public void deleteCharAt(int i) {
        // TODO - your code here

    }
}

Explanation / Answer

Lab14a.java

package org.students;

public class Lab14a {
   public static void main(String[] args) {
   SimpleStringBuilder myBuilder = new SimpleStringBuilder("Hello World");
   System.out.println("Displaying Content of StringBuilder :"+myBuilder.toString());

   System.out.println("Length of String Builder :"+myBuilder.length());
   System.out.println("Charecter at Position 3 :"+myBuilder.charAt(3));
   myBuilder.replaceCharAt(3, 'p',myBuilder.toString());
   System.out.println("After Replacing Index position 3 with 'p' :"+myBuilder.toString());
     
   myBuilder.append('!');
   System.out.println("After Appending ! to StringBuilder :"+myBuilder.toString());
   myBuilder.deleteCharAt(3);
   System.out.println("After Deleting the Charecter at Index position 3:"+myBuilder.toString());
   System.out.println("The Length of String Builder :"+myBuilder.length());
   myBuilder.insert(3, 'z');
   System.out.println("After Inserting 'z' at index position 3 :"+myBuilder.toString());
   System.out.println("The Length of the StringBuilder :"+myBuilder.length());
   }
}

_________________________________________________________________________________________

SimpleStringBuilder.java

package org.students;

import java.util.ArrayList;
public class SimpleStringBuilder {
/**
* A private member variable used to hold the internal state of the
* SimpleStringBuilder. This ArrayList holds the characters of the String
* that will be built. So the String "Hello" would be represented by an
* ArrayList containing the characters ['H', 'e', 'l', 'l', 'o'].
*/
ArrayList list;
private void createEmptyBuilder() {
this.list = new ArrayList<>();
}
/**
* Constructs an empty SimpleStringBuilder.
*
*/
public SimpleStringBuilder() {
this.createEmptyBuilder();
}
/**
* Constructs a SimpleStringBuilder that contains the same characters as the
* String input
*
* @param input
* the String to copy into the StringBuilder
*/
public SimpleStringBuilder(String input) {
this.createEmptyBuilder();
for (int i = 0; i < input.length(); i++) {
this.list.add(input.charAt(i));
}
}
/**
* Returns a String object built from the SimpleStringBuilder characters
*/
@Override
public String toString() {
String myString = "";
for (int i = 0; i < this.list.size(); i++) {
myString = myString + this.list.get(i);
}
return myString;
}
/**
* Returns the character at position i
*
* @param i
* the index to be checked
* @return the character at position i
*/
public char charAt(int i) {

return (char) list.get(i-1);
}
/**
* Returns the length of the SimpleStringBuilder object
*
* @return the length of the StringBuilder
*/
public int length() {
  
return list.size();
}
/**
* Replaces the character at position i with the character c
*
* @param i
* index to be replaced
* @param c
* character to use in replacement
*/
public void replaceCharAt(int i, char c, String s) {
list.set(i-1,c);
  
  
  
}
/**
* Appends the character c to the end of the StringBuilder
*
* @param c
* character to append
*/
public void append(char c) {
list.add(c);
}
/**
* Inserts the character c at position i
*
* @param i
* index to insert at
* @param c
* character to be inserted
*/
public void insert(int i, char c) {
list.add(i-1,c);
}
/**
* Deletes the character at position i
*
* @param i
* index to delete
*/
public void deleteCharAt(int i) {
list.remove(i-1);
}
}

__________________________________________________________________________________________

output:

Displaying Content of StringBuilder :Hello World
Length of String Builder :11
Charecter at Position 3 :l
After Replacing Index position 3 with 'p' :Heplo World
After Appending ! to StringBuilder :Heplo World!
After Deleting the Charecter at Index position 3:Helo World!
The Length of String Builder :11
After Inserting 'z' at index position 3 :Hezlo World!
The Length of the StringBuilder :12

__________________________________________________________________________________________