starting foreach CS230: Data Structures

CS230

 

Goals:

Practice with:

  • Manipulating arrays of integers (small values).
  • Using looping constructs for processing array elements
  • Practicing with testing and debugging programs using arrays
  • Basic IO (File reading)
 

Exercise: A Histogram of data read from a file

In this exercise you will experiment with creating arrays and using them to process integer values read from a file. These integers are considered in groups of 10, and the frequency of each group is computed. Study the printing example below.

Aim for a final output that resembles the following example:

Printing the Histogram from file "integerData1.txt"
Initial Data (14 integers):
12 19 3 58 51 12 24 27 1 57 57 22 28 28
****** Results: *******
range 0- 9 (2) |++
range 10-19 (3) |+++
range 20-29 (5) |+++++
range 30-39 (0) |
range 40-49 (0) |
range 50-59 (4) |++++
  • Note that the number of plus signs (+) in each line indicates the number of integers from the input data that are found to belong in that range. E.g., The given file contained 5 integers in the range 20-29, so 5 + signs are printed in that line.

Specifications

Write a class called HistogramIO.java with the following design:

instance variables

  1. maxInt: You can assume that each integer in the input is in the range 0 - maxInt.
  2. size: the number of integers in the input.
  3. An array, named data, to hold all the integers in the input.
  4. numOfBuckets: an integer to indicate the number of ranges/buckets we will use in the application. You can assume that each bucket has range 10. In the example shown above, we used 6 buckets: the first ranging from 0-9 (included), one ranging from 10-19, etc, with the last one ranging from 50-59. The value of this instance variable should be computed based on other input information.
  5. frequencies: an array of integers which will hold the frequencies of each range/bucket. In the above printout for example, the bucket/range [20-29] has frequency of 5, since 5 integers in the input found to belong there.

constructor and instance methods

Write the following methods:

  1. a constructor: It takes as input the name of the file where input data will be read from, reads the input, creates and initializes the instance variables. (See "Format of the input file" below for details on the format of the input file.)
  2. the toString() method: Use the output provided earlier in this page to guide you about implementing this method. Of course, you are encouraged to start with a more basic version of it, and improve it little by little.
  3. a main() method to contain your testing. Create 4 text files according to the specifications outlined above. (Do NOT use Microsoft Word for this, as it may insert invisible characters which can break your program. Atom or other plain text applications work fine.) Save them in the folder that contains your java file. Use the .txt files to debug and test your program.
  4. the computeFrequencies() method, that does exactly that: Counts the number of integers in the input that belong to each of the buckets/ranges.

While you develop and debug your program, you can have some helper printing methods available. For example, having a method to print the frequencies array was useful to me.

Notes

  • No use of Java's Arrays package is permitted in this exercise.
  • Format of the input file: The first line of the input file shows the size of the input, i.e, the number of integers to be processed. The second line contains the maxInt to be processed. After that, size lines follow, each containing one integer: that's your input. Once more, in this basic version, you can assume that the input consists of valid integers, in the range 0 to maxInt. Make sure you state these, and any other assumptions, in your comments.
  • Here is the file that created the printout above: integerData1.txt
  • The main() method should not be too long. It should be directing your algorithm using private helper methods that are called in the main.
  • Use proper javadoc to document your work.
  • In your comments make a note to inform us whether the program produces the expected results. If it doesn't, let us know where you think things seem to have gone wrong.

What to submit

Your Gradescope submission should contain the following:

  1. your HistogramIO.java file
  2. your HistogramIO.txt file that contains your testing results

Good luck!!