Begin by copying the program skeleton Split.java from the class directory into your new directory. Then use your favorite editor to open the file, and take a moment to study it, to see how it implements our basic algorithm.
To write a subprogram that performs step 2 of our algorithm, we need some means for that subprogram to send back two pieces of information: the two parts of the string being split.
In other languages, this might be accomplished by defining the subprogram as a procedure. However, Java has no procedures, supporting only method subprograms. In place of the procedure, Java provides the void method -- a method that returns nothing.
MethodDef ::= Type identifier( ParameterDefs )
Block
Type ::= void | char | int | ...
The most efficient approach for parameter passing in C++ would be to
use the reference parameter mechanism, but Java does not have
reference parameters -- the only parameter mechanism Java has
is pass-by-value. However, if we pass-by-value a reference to
an object, changes to the object referenced are persistent, because our
parameter will be an alias for the object.
In the picture below, if x is passed-by-value to a method whose name
for the argument (i.e. formal parameter) is y,
this results in both x and y referring to the
same object in memory:
Java's strings are reference objects, but they are also immutable, which means that their values cannot be changed. Put differently, just passing string references will not be enough to cause the behavior we desire. However, if we put the strings in an array (also a reference object) that we pass-by-value, it will do what we wish (because arrays ARE mutable). As in other C-based languages, Java uses square brackets for array literals, but we must specify both what kind of data the array will hold, and the size of the array when created:
String [] resultList = new String[2];Now we can pass an object of this type to our method splitter() that will do the work to satisfy our problem specification, as follows:
public static void splitter(String theString, int pos, String[] results)
{
}
To fill in the body of the method, we can use the String method member substring(), whose syntax is:
StringMsg ::= substring(Start, Stop) Start ::= IntExpression Stop ::= IntExpression
Computing the length of the second substring in order to know where to stop requires that we know the length of our original string. This can be determined using the String method member length():
StringMsg ::= length()
Drawing on these observations, we can write expressions to compute each part:
theString.substring(0, theSplitData.position);
theString.substring(theSplitData.position,
theSplitData.theString.length());
Finally, we need to save these results into the array. Updating the array uses the same syntax as C++:
arrayVar[pos] = newValue;
Given that we want the first expression above to be saved in the first (i.e. 0th position) of the result array, and the second expression in the second (i.e. 1st position) incorporate each substring expression into an appropriate assignment statement to complete your method.
Compile and test your code for correctness; when it is correct, use script to create a file in which you cat your file, compile it, and show that it works correctly for the input string hello and the position values 0, 3 and 5. Save the resulting script file to be concatenated to your submission.
Calvin > CS > 214 > Labs > 06 > Java