Copy the lab materials to your account. Use the -r option on the Unix cp command in order to copy not only files, but directories. To copy, enter
cp -r /home/Classes/Cs1/Labs/Lab09 .After running this copy command, you will see a new directory in your account called Lab09. What you copied was actually a directory, plus all of its contents. Everything you need for the lab exercises today is contained in this new directory.
Keep paper and pens or pencils handy to help you and your partner work through this lab.
import javabook.*; public class Demo { public static void main(String [] args) { String line; SimpleInput simpleInput = new SimpleInput(); // Get a sentence line = simpleInput.getString("Enter words: "); // What are we doing here? line = line.trim(); // Get each word in the sentence and reverse it while (!line.equals("")) { int blankPosition = line.indexOf(' '); String word; if (blankPosition > 0) { word = line.substring(0,blankPosition); line = line.substring(blankPosition+1); } else { word = line; line = ""; } System.out.println(word); } } }Using paper and pencil, trace out what will happen if you enter the words:
The catAssume that there are several blanks before "The" and after "cat", but exactly one blank between the two words.
1
Show us your trace and the anticipated output.
Now change directories into Lab09 and start BlueJ by entering bluej&. Open the project RevWord and edit the class Demo. Compile this class, run it, and see if your prediction agrees with the results.
Let's try a slightly different example. Run the program with the input
The catwhere there are several (six or seven) blanks between the words.
Fix the program so that you get the same output from multiple blanks that you got when there was only a single blank between "the" and "cat".
Hint: copy the line = line.trim(); statement
to another useful location.
Test your modification on input containing groups of spaces before, between,
and following the words.
2 Show us how you modified your program, and demonstrate that it works
correctly.
Go to the while loop in your program and comment out the lines that are commented out below, plus any others inside the while loop that you may have added other than those relating to the trimming:
while (!line.equals("")) { line = line.trim(); int blankPosition = line.indexOf(' '); String word; // if (blankPosition > 0) { word = line.substring(0,blankPosition); line = line.substring(blankPosition+1); // } // else { // word = line; // line = ""; // } System.out.println(word); }Compile and run the program. You will get a run-time error. Why?
3 Explain what caused the run-time error.
Now it's time to look at the instantiable class defined in this project: ReverseWord. The ReverseWord class has two constructors. The default constructor, which can be used in this way:
ReverseWord revWord1 = new ReverseWord();creates a ReverseWord object whose value is the empty (null) string. The second constructor takes one argument that is a String:
ReverseWord revWord2 = new ReverseWord("happy");This constructor creates a ReverseWord object whose value is "yppah". This, of course, has the same content as the value of the parameter "happy", but with the characters in reverse order.
Now, uncomment the lines that we asked you to comment out for the previous checkpoint.
Look for this output statement at the end of the while loop body:
System.out.println(word);Replace it with the following object creation and output statements:
ReverseWord reverseWord = new ReverseWord(word); System.out.println(reverseWord);
Recompile and run your program.
By reading the output, you can see that the second constructor is really just
a stub.
Your job is to finish the second constructor so that it works as intended.
Hint: research the append() and insert() methods available in the StringBuffer class. With your partner, discuss the differences between the two. You may use either one or both of these methods for this checkpoint. You will need a loop that works with one character at a time.
For example, if you enter
Hello to allyou should get this resulting output:
olleH ot lla
For your next checkpoint, you will add more flexibility to the second constructor. Change its implementation so that all letters are reversed, but all punctuation (found either within or at the end of a word) appears after the letters, on the right. For example, the input
Hi, aren't labs fun?!will produce this output:
iH, tnera' sbal nuf?!Hint: if you didn't use both append() and insert() before, you'll probably want to use both of them now!
5 Show us how you modified the second ReverseWord constructor.
Close project RevWord.
Compile and run the Demo program, entering the following values:
Enter a non-negative value, or a negative value to quit: 1 Enter a non-negative value, or a negative value to quit: 2 Enter a non-negative value, or a negative value to quit: 3 Enter a non-negative value, or a negative value to quit: 4 Enter a non-negative value, or a negative value to quit: 5Note the result. In particular, we are getting a message that the size of the Data object seems to be 0 even though we entered 5 values.
Correct this problem, so that when you enter 5 values, the output is the average of those five values.
For example, if you enter the following values:
Enter a non-negative value, or a negative value to quit: 5 Enter a non-negative value, or a negative value to quit: 3 Enter a non-negative value, or a negative value to quit: 7 Enter a non-negative value, or a negative value to quit: 2 Enter a non-negative value, or a negative value to quit: 2You should get this output line:
Mean of 5 values is: 3.8Hint: When a Data object is constructed, its actual size is initialized to 0, which is correct. However, when you place data into the object, the actual size of the object should be changed.
6 Show us how you fixed the object size problem so
that the above example, and any other example where you enter
5 values will now work correctly.
Now try these input values:
Enter a non-negative value, or a negative value to quit: 2 Enter a non-negative value, or a negative value to quit: 3 Enter a non-negative value, or a negative value to quit: -1The program still averages all three values entered. However, the -1 is supposed to signal the end of the data. It is not supposed to be part of the data to be averaged!
Your next task is to fix this problem.
7 Show us how you fixed the end-of-data problem.
Add a public method to the Data class called displayContents() that will print the contents of each array element (starting at position 0), all on one line, each separated by a space for readability. Make sure you don't try to print (display) elements from positions outside the array!
To test your new method, add code to the Demo class that will call it before displaying the mean. Run the program using an array of four values.
8 Show us your new code, plus the output obtained from a four-element array.