CSci 161 Lab #5
 

Complete the coding of the following Java pipeline program which implements a source (from which raw materials are sent for processing), a sink (to which processed items are exported), and a middleman (where some processing is normally done). The source will create new objects, initialize them with data, and store them in a buffer for the middleman. The middleman will take these items from the source's buffer, process them, and store them in a different buffer for the sink. The sink will take the object from its buffer and export it. Source code to use and/or modify can be found in the files Person.java, Buffer.java and Lab5.java.

For this lab, the buffers will only have a capacity of one object, so it is necessary to coordinate the source, middleman and sink to avoid buffer overflows. You can't add something to a buffer that is not empty. You can't remove anything from an empty buffer. We will find an easy, but perhaps inefficient way to deal with the coordination problem here, but later on we may revisit this exercise and try to implement a multi-threaded Java application.

Here's what you have to do for this lab:

  • Write code for the source routine in the Lab5 java class. It should read data values (name, height and weight) from the three parallel arrays and store those values in a new object which it will then place in buffer1 (assuming, of course, that there isn't already something in buffer1). The first object should contain the first name, height and weight; the second object should contain the second name, height and weight; etc. Be sure that source is able to stop when there is no more data in the arrays. The logic for the source routine can be something like the following:
                   if (there is more data to be processed AND buffer1 is not full) then
                       display a message on the console saying that you are creating a Person
                       create a new Person object and initialize with the data for the next person
                       insert the new Person object in buffer1
                       add 1 to the counter that keeps track of the amount of data that has been read
                   else
                       display a message on the console saying that you are doing nothing
                       do nothing
                   endif
                   return
    
  • Write code for the middleman routine in the Lab5 class. All the middleman needs to do for this lab is to reduce the person's weight by 10 pounds and move the person from buffer1 to buffer2. The logic for the middleman routine can be something like the following:
                   if (buffer1 is not empty and buffer2 is not full) then
                       display a message on the console saying that you are processing a Person
                       remove a Person object from buffer1
                       reduce the weight of this Person by 10 pounds
                       store the Person object in buffer2
                   else
                       display a message on the console saying that you are doing nothing
                       do nothing
                  endif
                  return
    
  • Write the code for the sink routine in the Lab5 class. It should display the name, height and weight of the item being exported, and count up the number of items exported (by incrementing the value of the "exported" variable). Note that exporting means removing the object from buffer 2 and counting it (and, in a sense, discarding it). Java's garbage collection routines will find this discarded storage later and make it available for reuse. The logic for the sink routine can be something like the following:
                   if (buffer2 is not empty) then
                       display a message on the console saying that you are exporting a Person
                       remove a Person object from buffer2
                       add 1 to the number of exported items
                   else
                       display a message on the console saying that you are doing nothing
                       do nothing
                   endif
                   return
    

The main routine will call each of your 3 routines repeatedly until all the data has been processed, so don't worry about the do nothing actions. There will be plenty of opportunities for each of your routines to do all of their work. You might want to try rearranging the three subroutine call statements in main() and convince yourself that the program will still run correctly.

In a later lab, we will revisit this problem and allow source, middleman and sink to execute concurrently at their own speeds after they have been started by the main routine. At a later point in time, when source, middleman and sink are all finished, the main routine will shut things down and exit normally.

 
When you are finished, ask your lab instructor to check your work and record your scores.