CSE 201 - Lab 6 - Methods and Arrays


Note: This lab assumes that you have completed lab 1, and that you know how to login to your account, and how to open, edit, save your files, as well as compile and run your programs. If you have forgotten any of these techniques, refer back to the lab 1 handout. Be sure to follow the directions very carefully. If you have any problems or questions, be sure to ask your instructor as soon as possible. Points will be deducted if the submitted programs do not have the appropriate comments included.


Table of Contents


Objective

The objective for this lab is to write a Java program to practice the design of class methods and the use of arrays of primitive types. This lab emphasizes program design and decomposition using Java class (static) methods. Monolithic solutions that do not exhibit a good design using appropriate class methods will not receive full credit.


Overview

You will be writing a Java program from scratch. The program is intended to display the histogram of some given data. A histogram is a bar chart in which each bar represents a certain range of values and the bar's length is proportional to the number of values in the corresponding range (see histograms at Wikipedia for more information). For this lab, the data will be generated by the program itself to make it easier to test the histogram functionality.


Materials Provided

For this lab you will need the following file:


Set up

Start Eclipse, create a new project, Lab6, and create a new Lab6 class (the corresponding file name will be Lab6.java) in the project. Import into your Lab6 project the following file from K:\CSE201\Lab6 (see Lab 1 for instructions on how to import files in Eclipse):

After importing the file, you need to tell Eclipse where to find the Generator class. To do that, carefully follow these steps:

  1. Select the Lab6 project in the Package Explorer view;
  2. From the menu bar, select Project->Properties, the Properties window will appear;
  3. Select Java Build Path in the left panel of the Properties window, and the Libraries tab in the right panel;
  4. Click on the Add JARs... button, the JAR Selection dialog will appear;
  5. Expand the Lab6 folder and select the Lab6.jar file, and click OK;
  6. Click OK again, and you are done.

If, when using the Generator class in your lab, you get a compiler error saying that this class is unknown, it probably indicates you made a mistake in this last setup step. In that case, you may want to come back and try the setup again.

Complete the Lab6 program according to the description below. If you don't remember how to create a project or a class in Eclipse, see Lab 1 for instructions.


Description

Here is a sample interaction between a user and the program you will write in the Lab6 class (user inputs are in bold):
    How many numbers? 100
    How many intervals? 10

    Histogram
    --------------------------------------------------------
      1 ****(4)
      2 ******(6)
      3 ***********(11)
      4 *****************(17)
      5 **************************(26)
      6 *************************(25)
      7 *******(7)
      8 ***(3)
      9 (0)
     10 *(1)
    --------------------------------------------------------

The program performs the following actions:

  1. Ask the user to enter the number of numbers that will be generated and the number of intervals that will be used in the histogram, and input the two values;
  2. 
    
  3. Generate the required number of pseudo-random numbers by using the double[] Generator.getData(int n), which, given the number of numbers to generate, n, returns an array of double of size n containing the generated numbers;
  4. 
    
  5. Compute the range of data values (by finding the maximum and the minimum and taking their difference); then compute the width of each equally-sized interval (by dividing the whole range by the number of intervals specified by the user); finally, compute the number of data numbers falling in each interval and store these frequencies in an array of integers;
  6. 
    
  7. Output the histogram of the data as displayed in the sample run above: each bar starts with an index (1 through the number of intervals), followed by a number of '*' equal to the number of numbers in that range, followed by the number of '*' in parentheses.
Note: Pay attention to detail. Make your output look like the output in the sample run. However, note that your program must be able to produce the correct histogram for arbitrary data, not just for the specific data generated by Generator.getData().


Lab Submission

Make sure your program compiles and runs correctly before submitting. To submit, use the Submit tool available on the Widows desktop to submit the Lab6.java file from the Lab6 project (the location of the project in the file system will be z:\eclipse\workspace\Lab6). Make sure that you select lab6 from the list of Assignments. If you don't remember how to use the Submit tool, see Lab 1 for instructions.