CSC 120 Lab 12

This lab is another example of the use of the split method of the String class, and illustrates how we can process lines of information that contain more than one value per line.

  1. Get the beginning version of the Lab12 program up and running.

    1. Create a new Netbeans project called Lab12. 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.  The program should show each line of the input you entered with a line number, starting with line 0.

  2. Use the split method to break each line of input into separate values.

    1. Now that we have an array that contains each line of the input in a separate array element, we can process each line of input in the body of a for loop.  Let's say we have some data that describes the makeup of the Cleveland Orchestra for the current season.  Each line of input will contain three values for a different type of instrument in the orchestra:  the name of the instrument, the orchestra section to which the instrument belongs, and the number of musicians that play that instrument this season.  Each of these values is separated from each other by a semicolon. 

      To split each line of input into three pieces, we can use the following code in the for loop that displays each line in the output area:
          String[] part = lineOfInput[index].split(";");
      
      Now we have an array of Strings called part, where element 0 of the array contains the instrument name, element 1 contains the instrument's section and element 2 contains the number of musicians for this instrument.

    2. In your web browser, right click on Instrument.java and choose Save Target As... (or Save Link As...) and save the file in the src folder of your project.  This file defines the Instrument class with private data members for each of the three pieces of data described above.  Look at the code in this file and make sure you understand how it is written.

    3. At the top of the MUFrame class, declare an array of Instrument objects.  Then, immediately before the for loop in the actionPerformed method for the processInput button, instantiate the array of Instruments so that there are as many elements in this array as there are lines in the input.

    4. Inside the for loop in the actionPerformed method for the processInput button, do the following:
      • split the input line into an array of Strings called part, using a semicolon as the delimiter (see the code example above)
      • for (Integer p = 0; p < part.length; p++) {
            part[p] = part[p].trim();
        } // end for
      • instantiate element index of the array of Instrument objects, using the part array as the values to pass to the constructor method
        • the first parameter for the constructor call should be  part[0]
        • the second parameter for the constructor call should be  part[1]
        • the third parameter for the constructor call should be  Integer.parseInt( part[2] )
      • change the append call inside the loop to be this:
            outputArea.append( "line " + index + ": " + instrumentArray[index].toString() + "\n" );
        
        (you may need to change the name instrumentArray in this sample to be the name you used in your program)

    5. Run your program, and test it with this data from the Cleveland Orchestra:
      The program should print information for 26 different instruments from this test data.

    6. Add code to your program that prints the following information about the orchestra:

      1. after all of the instruments in the orchestra have been printed, print a single line that shows the total number of musicians in the entire orchestra
      2. next, print a line for every instrument in the orchestra that is in the "Brass" section
      3. then, print only one line that shows the total number of musicians in the Strings section
      4. finally, print only one line that shows the maximum number of musicians that play any instrument in the Woodwinds section

      For the test data given for this lab, your program should print that there are 115 musicians in the entire orchestra, 67 of whom play in the Strings section.  There are 7 different instruments in the Brass section (French Horn, Trumpet, Cornet, Trombone, Bass Trombone, Euphonium and Tuba), and the maximum number of musicians for any instrument in the Woodwinds section is 6.

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