created 01/28/06; small changes 03/01/2014


Chapter 9C Programming Exercises

Instructions: Write each of these programs as specified. None of these programs ask the user to enter data. Data are "hard coded" with declaration statements or assignment statements.


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 the reference variable str. 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 add a second reference variable to the program, but don't create an object for it to refer to. Add a statement that uses the second variable as if it held a valid object reference. Run the program, and see what happens:

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". The length is the number of characters, not the actual length of line it takes to print them.

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 (repeated below) to confirm it works as described. Then change the 8 to some other value and check if the result is what you expect.

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-1. It creates a completely new object; the characters it contains are copies of those in the original object. For example, look at the following:

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

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

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

Modify your program so that it calls this two-parameter substring(). Experiment with the two parameters to see 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 plus one (28 for the example).
  3. Instead of using a literal 28 in the above, use str.length() which will have the value 28.
  4. Change the second parameter to 29 in the above.
  5. Make the first parameter negative.
  6. Reverse the order of the parameters.

Both parameters must be in the range 0 .. str.length() and the first must be less than or equal to the second.

Good methods that deal sensibly with all possible input values, even those that make no sense. For good security, programmers must expect that some users will 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