Menu

[solved]-Create New Project Named Stringbuilderexploration Inside Folder Make New Class Named Simpl Q39095172

Create a new Project named StringBuilderExploration and insidethat folder make a new class named SimpleStringBuilder. Paste inthe following:

/** * A class that implements a very simplified StringBuilder-like class. This * class will not execute on its own – you will need to use code provided in * the TestSimpleStringBuilder.java class to run this code. */import java.util.ArrayList;import java.util.List;public class SimpleStringBuilder { /** * A private member variable used to hold the internal state of the * SimpleStringBuilder. This List holds the characters of the String that * will be built. So the String “Hello” would be represented by an List * containing the characters [‘H’, ‘e’, ‘l’, ‘l’, ‘o’]. */ List<Character> 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. * (Note that this is not terribly efficient, and not how the Java library * actually would do it). */ @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 // TODO – the following line is only here to allow this program to // compile. Replace it and remove this comment when you complete // this method. return 0; } /** * Returns the length of the SimpleStringBuilder object * * @return the length of the StringBuilder */ public int length() { // TODO – your code here // TODO – the following line is only here to allow this program to // compile. Replace it and remove this comment when you complete // this method. 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) { // 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 }}

SimpleStringBuilder.java contains the skeleton of code for a simple StringBuilder type class. For this closed lab, we have gi

Now take a look at the constructor from String: /** Constructs a SimpleStringBuilder that contains the same characters as t h

/** * A program that tests the SimpleStringBuilder class created in this lab. * */public class TestSimpleStringBuilder { /** * A simple program used to test the SimpleStringBuilder class * * @param args */ public static void main(String[] args) { SimpleStringBuilder myBuilder = new SimpleStringBuilder(“Hello World”); System.out.println(“String is: ” + myBuilder.toString()); // Code below this comment will not work until you complete the required methods System.out.println(“Length is: ” + myBuilder.length()); System.out.println(“Char 3 is: ” + myBuilder.charAt(3)); myBuilder.replaceCharAt(3, ‘p’); System.out.println(“String is: ” + myBuilder.toString()); // Code below this comment will not work until you complete the extra methods // myBuilder.append(‘!’); // System.out.println(“String is: “+myBuilder.toString()); // myBuilder.deleteCharAt(3); // System.out.println(“String is: “+myBuilder.toString()); // System.out.println(“Length is: “+myBuilder.length()); // myBuilder.insert(3, ‘z’); // System.out.println(“String is: “+myBuilder.toString()); // System.out.println(“Length is: “+myBuilder.length()); }}

If you run this code now, it will not work because theimplementations for length, charAt and replaceCharAt are allincomplete. Finish the implementations for each of these methods sothat this code will work.

SimpleStringBuilder.java contains the skeleton of code for a simple StringBuilder type class. For this closed lab, we have given you a few methods already implemented, including the empty constructor, the constructor from String, and the toString method. Using the code in these methods as examples you will implement the length, charAt, and replaceCharAt methods. Before we start we need to think about how the internal state will be represented. Take a look at the empty constructor for this class: Constructs an empty SimpleStringBuilder. */ public SimpleStringBuilder ( { this.create Empty Builder ( ); This method has been written for you and it does one thing – call the helper method createEmptyBuilder. This helper method sets up a new empty ArrayList of characters that the SimpleStringBuilder class uses to maintain its internal state. It is basically creating a StringBuilder type object that will create an empty String. The code for this method is below: / ** Sets the internal state of the object to be an empty List private void createEmptyBuilder () this.list new ArrayList< >(); Now take a look at the constructor from String: /** Constructs a SimpleStringBuilder that contains the same characters as t he 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)); It is also setting up an empty internal state, but it needs to do more as well. It is looping through the String character by character and putting them into the list that the SimpleStringBuilder class uses to maintain its internal state. Finally, take a look at the code for the method toString: String object built from the SimpleStringBuilder characters Returns a (Note that this is not terribly efficient, and not how the Java library actually would do it) @Override public String toString() String myString = “”; for (int i 0; i < this.list.size) i++ { myString myString this.list.get ( i) return myString; This method takes the internal state of the SimpleStringBuilder class (a list of characters), builds a String object from it, and returns that String. Before you start working on your code, you need to set up a program to test it with. Create a new class in the same project named TestSimpleStringBuilder.java and paste in the code below: Show transcribed image text SimpleStringBuilder.java contains the skeleton of code for a simple StringBuilder type class. For this closed lab, we have given you a few methods already implemented, including the empty constructor, the constructor from String, and the toString method. Using the code in these methods as examples you will implement the length, charAt, and replaceCharAt methods. Before we start we need to think about how the internal state will be represented. Take a look at the empty constructor for this class: Constructs an empty SimpleStringBuilder. */ public SimpleStringBuilder ( { this.create Empty Builder ( ); This method has been written for you and it does one thing – call the helper method createEmptyBuilder. This helper method sets up a new empty ArrayList of characters that the SimpleStringBuilder class uses to maintain its internal state. It is basically creating a StringBuilder type object that will create an empty String. The code for this method is below: / ** Sets the internal state of the object to be an empty List private void createEmptyBuilder () this.list new ArrayList();
Now take a look at the constructor from String: /** Constructs a SimpleStringBuilder that contains the same characters as t he String input @param input the String to copy into the StringBuilder public SimpleStringBuilder (String input) { this.createEmptyBuilder (); for (int i= 0; i

Expert Answer


Answer to Create a new Project named StringBuilderExploration and inside that folder make a new class named SimpleStringBuilder. P… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *