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 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. Write a method that uses split 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.  Here are some examples of the format that will be used for this data:
      French Horn; Brass; 3
      Cornet; Brass; 5
      Timpani; Percussion; 2
      
      This example means that there are 3 French Horn players in the Brass section of the orchestra, 5 Cornet players in the Brass section, and 2 Timpanis (Kettle Drums) in the Percussion section.  Our goal in this program is to use this text input data to create an array of Instrument objects that we can process.

      To split a String object named myString into an array of Strings, using a semicolon as the delimiter, we can use the following code:
          String[] part = myString.split(";");
      
      Then we would have an array of Strings called part, and if myString contained one of the sample lines above, 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 with the specific name instArray.  Then, immediately before the for loop in the actionPerformed method for the processInput button, instantiate this 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, replace the outputArea.append statement that is in the loop with the following code:
          instArray[index] = makeANewInstrument( lineOfInput[index] );
          outputArea.append( "[" + index + "]: " + instArray[index].toString() + "\n" );
      
      When you do this, NetBeans will give you an error, saying that makeANewInstrument is not defined.  We need to write a method with this name in the program, and we will do that in the next step of these instructions.

    5. Scroll up in the program until you see the MUFrame constructor method.  Notice that right below that, there is a method that is commented out.  Uncomment those lines to reveal a method named makeANewInstrument.  This method accepts one String parameter, and will return an Instrument object as its result.  Your task in this step of the lab will be to write code to take a line of input, split it into pieces using the delimiter character, and then create a new Instrument object to be sent back as the return value for the method.

      The first and last lines in the body of this method need to remain unchanged.  Between those lines, do the following:
      • split the parameter of this method into an array of Strings called part, using a semicolon as the delimiter.  See the code example in instruction B. 1) above.
      • for (Integer p = 0; p < part.length; p++) {
            part[p] = part[p].trim();
        } // end for
      • instantiate answer, 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] )

    6. 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.

    7. 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.