Bottom up Development of Program 6

 

Rather than do an incremental development of program 6 from a top down perspective, we can develop the program bottom up.  By this I mean we first develop the methods generateUsers, generatRandom, and outputHistogram.  Each of the 3 can be developed and tested independent of the others.  Then, once we have all 3 done, we can then write  the main method to tie them all together in a completed program.

 

outputHistogram

 

You develop this program with a nested for loop.  You should look at the MultiplicationTable example given to see how one works with nested for loops. You have to play around a little with this to get a nice output, especially having the labels lining up correctly.

 

        Since this is declared as a public static method, you can unit test it. (You should have already used unit testing on Program 4 with the calculateBill method.) You right click on your class, and you choose the method of outputHistogram as the one to be run. It will open up with an array to be put inside the box.  You just type in an array of 10 numbers. It would look like this after you type them in:
 
 

 

 Then when you click the OK button, if your program is correct, you will see the following output at the console screen:

 

 

 

Test this several times with different input to verify that it works.
 

 

generateUser

 

You can develop the generateUser method independent of any of the other methods as well.  You write this method by first creating an array of ints of size 10.

 

               Then initialize each of the elements of the array to be zero.  Then ask the user for the n elements one-by-one and assign them to the proper index of the array.  Make sure that they are between 1 and 100 before assigning them to the array.  If they are not within the desired range, reject the data and ask again for input.  Be sure to get n total valid values.  That is to say, if an input is not valid for the 4th one, one asks again for the 4th one until one gets a valid value.  Finally return the array.  
 
               Asking for data and verifying it is correct is similar to what you did in Program 3 with the calendar. The difference here is that one is doing this in via a for loop and if the data is invalid, one must ask for it again.  
 
        Remember that you are to "echo" the input numbers to the console window as they are input in via JOptionPane.
 
               You unit test it by choosing the method generateUser as the one to be run. You will insert a value on how many inputs you want.  When you finish running the program, the following window will appear:
 
 
 
The int[] in the shaded box is the returned array. If you want to see the values, you should double click on it. If you do, another window will open with the values of the array:
 
 
Then you can scroll through the array and verify that the values assigned to result[i] is the correct one. For example, our input was 55 and result[5] is one. Similarly we have two 66’s and result[6] is two which is correct. You should try this with several test to verify that it works correctly.
 
 
generateRandom
 
               This is run and tested as generateUser. You should copy and paste generateUser inside the method. Then just modify the inside of the loop to get the data generated from the Random object.  You do not have to check for bad values once you get it working correctly with the Gaussian distribution.  Most of your testing here will be with the parameters used to get a good distribution.  Since your generateUser was working in assigning the score to the correct ductile, this should also work.
 
               You will test it the same way.
 
 
 
 
 
main
 
 
               To write this, you just follow the 5 steps given in the documentation.  It is straight forward and is something you’ve already done.  Since you already have the 3 methods already working, this should not take very long to write.  There is not much to test here.