created 01/28/06


Chapter 9C Programming Exercises

General Instructions: Write each of these programs as specified. None of these programs expect the user to enter data. The values one of these programs uses is "hard wired" into the program with declaration statements or assignment statements. Usually this is a poor way to write a program.


Exercise 1 --- Object Created at Run Time

Write a program that creates a String object that contains the string "Hello World" (or some other favorite string). Assign the reference to the object to a reference variable, perhaps str. Now write the characters of the object to the monitor:

System.out.println( str );

Run the program to confirm that it works as you expect. Now, put a second reference variable in the program, but don't create an object for it to refer to. Put in a statment that attempts to use the object it refers to and observe the effect:

System.out.println( secondVariable );

Play around with this program for a while to cement the notion in you head that an object and its reference variable are different things, and that an object is created at run time.

Click here to go back to the main menu.


Exercise 2 --- String Length

Compile and run the StringDemo2 program from the chapter.

class StringDemo2
{
  public static void main ( String[] args )
  {
    String str;
    int    len;

    str = new String( "Elementary, my dear Watson!" );

    len = str.length();

    System.out.println("The length is: " + len );

  }
}

Now change the String in various ways and observe the effect on the computed string length. Add extra spaces on either side and in the middle of the string; add more punctuation; insert a tab character (a real tab character, not just spaces). Note that the length of the string "MMMMMM" is the same as "iiiiii".

Click here to go back to the main menu.


Exercise 3 --- A String Method that creates a new String

It is often useful to trim off extra spaces from both sides of a String. The trim() method of String objects does that. It creates a new String object from an existing one, where the new String object has all the characters of the original except that spaces and tabs have been trimmed off the sides. The original String is not changed. For example, after the following statements have executed

String first = new String("    In a Hole    in the ground there lived a Hobbit.   ")
String second;

second = first.trim();

there will be a second object, referred to by second, containing the characters

"In a Hole    in the ground there lived a Hobbit."

Notice that internal extra spaces remain.

Write a program (or modify the previous one) that creates a String by calling the trim() of an original String. Write both Strings to the monitor. Experiment with spaces and tabs to determine exactly what is trimmed from the original.

Click here to go back to the main menu.


Exercise 4 --- Play with substring()

Compile and run the program from the chapter to confirm it works as described. Then change the 8 to some other values to create some other substrings.

class StringDemo3
{
  public static void main ( String[] args )
  {
    String str = new String( "Golf is a good walk spoiled." ); // create the original object

    String sub = str.substring(8); //create a new object from the original

    System.out.println( sub );

  }
}

String objects have another version of the substring() method that looks like this:

substring(int  beginIndex, int endIndex)

This method creates a new String object, based on the original object, but containing only the characters that start at beginIndex and end at endIndex. A completely new object is created; the characters it contains are copies of those in the original object. For example

String str = new String( "Golf is a good walk spoiled." ); // create the original object

String sub = str.substring(8, 18); //create a new object from the original

create a new object (referenced by sub) containing the characters "a good walk" .

Modify your program so that it uses this two-parameter substring(). Experiment with the two parameters to confirm how they work. Try the following:

  1. Make both parameters the same value.
  2. Make the first parameter 0, and the last parameter the index of the last character (26 for the example).
  3. Instead of using a literal 26 in the above, use str.length()-1
  4. Make the first parameter negative.
  5. Make the second parameter longer than the string.
  6. Reverse the order of the parameters.

Good programmers write methods that deal sensibly with all possible input values, even those that make no sense. And for good security, programmers must anticipate users that maliciously try to find parameters that cause problems or cause odd behavior that can somehow be exploited.

Click here to go back to the main menu.


End of Exercises