Homework J2

Due: Friday, 30 September 2005 by 10 a.m.



Home | Resources | Homeworks | Slides | Labs | Contacts | Submit | Forums

Purpose

In this homework, you will gain more experience using objects while learning about the StringBuffer class.  The code should be in a HWJ2 class in a file named HWJ2.java.

 

Background

For various reasons, Strings are immutable (i.e. they can never be changed once you create them).  Consider the following code:

String s = "foo";
s = "bar";

The second line makes s point to a new String "bar" (and the first String is garbage collected away) -- the first string is never modified.  Note that you are changing the reference here, not the "foo" object itself.  There are no methods in the String class to allow you to change a String object.

There are times when one wants to change a String, and for that Java provides the StringBuffer class.  You will need to refer to the Java documentation for the StringBuffer class.  A StringBuffer operates much like a String, and has many of the String method that we are familiar with: indexOf(), charAt(), length(), substring(), etc.

 

Design

The program will have the following steps.  Note that in each step the StringBuffer is modified, and those modifications are retained in future steps.  Also, the StringBuffer is printed out after each of the steps 5-10.

  1. The user needs to enter two strings: one long string (say, 10 or so characters at a minimum) and a shorter string that is contained within the longer string.  This input should be obtained via the nextLine() method, as using the next() method will not read in a string that contains spaces.
  2. Create a StringBuffer object from the longer string -- this is the StringBuffer that you will manipulate for the rest of the homework.  There are two ways to do this: create a default constructred StringBuffer, and append() the long string to that, or use the StringBuffer with the appropriate specific constructor.
  3. Include, as a comment in your program, the code for creating the StringBuffer in the other way from step 2.
  4. Find the position of the small string within the StringBuffer, and save that position.
  5. Delete the small string from the StringBuffer, and print out the result.
  6. Insert "CS101" into the position of the StringBuffer where the small string was originally found (from step 3), and print out the result
  7. Remove the last word from the string. You can assume that everything from the last space (found via lastIndexOf()) to the end of the String is the last word. Print out the result.
  8. Append " rocks" to the end of the StringBuffer, and print out the result. Note that there is a space before the work 'rocks'.
  9. Delete the character at position n/2, where n is the length of the StringBuffer.  Print out the result.
  10. Reverse the StringBuffer, and print out the result.

As the course progresses, we will provide you with less step-by-step instructions for the homeworks -- for example, we don't specify that you need to create a Scanner object to obtain the input (this should be obvious from the fact that you need to get user input), or that you need to print out a legend (which is included in the good programming practices, below).

This homework is not providing a skeleton code file.  Your program MUST be in a public class named HWJ2 (note the capitalization), and all your code should be in the main() method.

 

Good programming practices

The good programming practices from HW J1 need to be in this homework as well.

 

Sample execution

Note that the text in red is what was input by the user.  Your program needs to print out similar information, but the format does not have to be the exact same.

This program demonstrates the use of the StringBuffer class.

Enter a long string:
I think that this course really rules

Enter a shorter string within the first string:
this course

I think that  really rules
I think that CS101 really rules
I think that CS101 really
I think that CS101 really rocks
I think that CS01 really rocks
skcor yllaer 10SC taht kniht I

 

Submission

When you are finished, submit the HWJ2.java file.