CSC 120 Lab 11

This lab will introduce the use of the split method of the String class, and illustrate how TextAreas can be used to process large amounts of data in a program.

  1. If you didn't do so before today's class, watch this video that demonstrates the use of the trim method and the split method of the String class (approx. 9 minutes in length).

  2. Get the beginning version of the Lab11 program up and running.

    1. Create a new Netbeans project called Lab11. Make sure the Location is in your H:\CSC120\Java folder.

    2. In your web browser, right click on MUFrame.java and choose Save Target As... (or Save Link As...) and save the file in the src folder of your project. 

    3. Compile and run the program at this time.  Then type your name and some text on multiple lines in the input area at the top of the window and click the Process Input button to see what happens.  Right now, the program just displays the contents of the input area unchanged in the output area.  If you click the Clear Input Area button, the contents of the input area are cleared, but the output area keeps its contents.  The output area has its "editable" property turned off, so you cannot make any changes in the output area by clicking or typing in it.

  3. Break the input into separate lines with the split method.

    1. Replace the "outputArea.append" statement in the processInputButtonActionPerformed method with a statement that declares an array of String objects named lineOfInput.  Then place the following code after the array declaration:
          lineOfInput = theInput.split("\n");
          
          outputArea.setText("There are " + lineOfInput.length + " lines in the input.\n\n");
          outputArea.append("Here is the input, line by line:\n");
      
      After this code, write a for loop that appends each element of the lineOfInput array to the outputArea on separate lines, preceded by the word "line", the line number and a colon.  For example, if you run the program and enter the first 5 letters of the alphabet on separate lines in the input area, you should obtain this output:
      There are 5 lines in the input.

      Here is the input, line by line:
      line 0: A
      line 1: B
      line 2: C
      line 3: D
      line 4: E
      Ask your instructor if you need help accomplishing this task.

    2. Here is a text area on the web page that contains all the letters of the alphabet on separate lines.  Copy all of the data in this text area (hint: click in the text area, then press [Ctrl]-A to highlight all the text in the text area, then press [Ctrl]-C to copy the contents).  Then run your program, paste the data into the input area (you can press [Ctrl]-V to paste), and process the input by clicking on the appropriate button.  There are 26 lines of input, so your program should display that in the output area.
    3. Copy the following data, run your program, paste the data into the input area, and process the input.  You should obtain 98 lines of output, numbered 0 to 97.
  4. Use the split method to find the minimum and maximum of a huge collection of data values.

    1. Find the statement in your program that says:
          outputArea.append("Here is the input, line by line:\n");
      
      and put 2 forward slashes "//" in front of the "outputArea" -- in other words, turn that statement into a comment.  We don't want to print every line of input in what we will do next, because there will be too many lines and it will take too long.

    2. Inside the for loop, put 2 forward slashes in front of the statement that appends each line to the outputArea so that each line is not appended to the output area.  If you run the program and paste input into the input area, the only output you should receive is a count of how many lines there were in the input.

    3. Here is a text area that contains numeric values that have decimal parts.  Copy all of the data in this text area, run your program and paste the data into the input area; then process the input.  There are 1089 lines of input; your program should display that in the output area.
    4. Before the for loop, declare two Double variables named max and min, initialize max to Double.MIN_VALUE and initialize min to Double.MAX_VALUE.

    5. Inside the for loop, insert the following statement:
          Double num = Double.parseDouble(lineOfInput[index]);
      
      You may need to change "index" in the statement above to the name of the control variable of your for loop if you didn't call that index. Then test num to see if it is bigger than max; if so, then set max to be num.  Test num to see if it is smaller than min; if so, then set min to be num.  Both if tests must be inside the for loop body.

      After the end of the for loop body, print the values of max and min on spearatle lines, like this:
          outputArea.append("\nMinimum = " + min);
          outputArea.append("\nMaximum = " + max);
      
    6. Run the program now, paste in the numeric data from the text area on this web page, and click the "Process the Input" button.  The program should report that there are 1089 lines in the input, and that the minimum value is 6.7 while the maximum value is 959.4.

  5. Submit your Lab11 project as a .zip file to the D2L dropbox for grading, as usual.