CSE 1223: Closed Lab 14.ZIP

CSE 1223: Closed Lab 14

OverviewThese exercises will allow you to have some practice with some basic ideas involving classes and objects. In this lab you will use those ideas to implement your own version of a simple StringBuilder type class.Objectives
Practice with programming fundamentals
Variables - Declaration and Assignment
Primitive types
Arithmetic Expressions
Simple keyboard input and text display output
Branching - if-elseif-else syntax
Loops - simple while loops, nested while loops
Methods - functions and procedures
Classes - implementation and use
Works towards the following Course Goals:
Competency with writing computer programs to implement given simple algorithms
Familiarity with using methods and classes to produce well-structured programs
Exposure to data abstraction concepts and other more advanced programming ideas
Overall Lab 14 InstructionsUsing the instructions from Closed Lab 01, create a new folder named ClosedLab14. You will need to import the following files into your new lab folder. Follow the instructions from Closed Lab 01 to import this file into your ClosedLab14 folder.
You will be exploring the methods of the StringBuilder class. Before starting the exercises below, scan through the StringBuilder API. Read through the top part thoroughly and skim through the description of the various methods the class provides, especially toString, length, charAt, append, and replaceCharAt.

The StringBuilder API provides a class for dealing with mutable Strings. The String objects we have used so far are immutableobjects - once you create a String you cannot change the characters in that String. StringBuilder provides a way of allowing you to create String objects where individual characters can be changed. This behavior is useful when we want to have Strings where we modify the individual characters rather than creating an entirely new String every time we want to make a change to it. In this lab you will write your own version of a StringBuilder class to get a better understanding of how classes are implemented.

Exercise 1 DescriptionSimpleStringBuilder.java contains the skeleton of code for a simple StringBuilder type class. For the first part of 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. At the top of the class there is this line:

Take a look at the empty constructor for this class.
1234567/** * Constructs an empty SimpleStringBuilder. * */public SimpleStringBuilder() { this.createEmptyBuilder();}

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:
12345678/** * A private helper method that we use to set up the empty internal state. * Put into its own method to make sure that there is consistency between * all of the constructors for this class. */private void createEmptyBuilder() { this.list = new ArrayList<();}
Now take a look at the constructor from String:

1234567891011121314/** * 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)); }}
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:

123456789101112/** * Returns a String object built from the SimpleStringBuilder characters */@Overridepublic 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.

Take a look at the code in Lab14a.java. Run that code and look at the output. Notice that it correctly displays the String "Hello World" using the SimpleStringBuilder toString method but it does not correctly report the length, nor does it correctly display the character at position 3, nor does it correctly replace the character at position 3. Your job for this first exercise is to provide the code necessary to get the length, charAt, and replaceCharAt methods to function properly. You may want to add more examples to the code in Lab14a.java to make sure you are testing the methods sufficiently.

Exercise 2 DescriptionOnce you have all of the methods for Exercise 1 working, uncomment the bottom portion of the code in Lab14a.java. This code uses the append, deleteCharAt and insert methods. Provide code for these three methods in the SimpleStringBuilder class so that these methods perform appropriately as well. Again, you may want to provide more examples to the code in Lab14a.java to make sure you are testing these methods thoroughly.
Powered by