Lab exercise #1: Experimenting with strings

Course links

External links

The objective

Our goal for today is to write, compile, and execute an experimental, "scaffolding" program in Java that invokes some commonly used methods relating to strings and displays the results.

Part 1: The framework

We'll write the program as a definition for a class called StringExperiments. In order to be able to execute this class as a program, we'll have to declare it to be a public class, save it in a file named StringExperiments.java, and provide it with a main method. For this exercise, just have the main method output the string "Ready." (by sending the println message to System.out and giving it that string as an argument).

Use an editor (Emacs, ProfessorJ, or gedit) to create and save the file. Compile it with javac, and run the resulting executable with java (the simulator for the Java virtual machine).

Part 2: The length method for strings

We'll write each of our experiments as a separate method of the StringExperiments class, and run it by invoking that method from inside main. Because we won't need to construct any StringExperiment objects, we'll declare all of these experiment methods static, so that they belong to the StringExperiment class itself. The experiment methods can be either public or private -- it makes no difference, since we'll only invoke them from inside this class definition.

Write an experiment method called lengthExperiment that determines and outputs the length of the string "How long am I?", appropriately labelled. This method should take no arguments and return no value; we'll invoke it only for its side effect.

Rewrite main so that it invokes lengthExperiment. Save the revised class definition, compile it, and run the program. What is the length of that string?

Revise lengthExperiment so that it also determines and reports the length of the string "\"\"\\\\\"\"\\\\", the length of a string that is newly created as the value of the expression new String(), and the length of a string that is computed as the value of the expression "foo" + "bar". Save, compile, and run. Examine the output and explain any surprising results that you get.

Part 3: toString and parsing methods

Write an experiment method called conversionExperiment that determines and reports the string representation of the double value -387.521, by invoking the toString method of the Double class. Similarly determine and report the string representation of the boolean value false and base-2 string representation of the long value 1234567890123456789L. (Note: the L at the end is part of the Java numeral and must be used whenever the value of the number expressed won't fit into a 32-bit twos-complement representation.)

Further, have this method parse the string "892.743" to obtain a double value (by invoking the parseDouble method of the Double class), and the string "-462.559" to obtain another double value, and finally add these values together and report their sum, appropriately labelled.

Rewrite main so that it invokes conversionExperiment. Save the revised class definition, compile it, and run the program. Examine the output and explain the results.

Part 4: String comparisons

Write an experiment method called comparisonExperiment that determines whether the string "cat" is lexicographically greater than, less than, or equal to the string "CAT" and outputs an English sentence that reports the outcome of the comparison.

Further, have this method compare the null string expressed as a literal, "", with the null string created as the value of new String() and report the outcome.

Rewrite main so that it invokes comparisonExperiment. Save the revised class definition, compile it, and run the program. Examine the output and explain the results.

Part 5: Other string methods

A complete list of the public methods of the String class is available on the Web. Read over this list and design and implement additional experiment methods to clarify points that are unclear or ambiguous in this documentation.]