Compsci 100e, Spring 2011, Classwork 2

Names and NetID of students in group (min 2, max 3)
Name: __________________________   NetID: _____________

Name: __________________________   NetID: _____________

Name: __________________________   NetID: _____________

Calculating the Mode

NOTE: THIS CLASSWORK NO LONGER WORKS all in one file. The APT method has to be in a separate file to test on the APT page and cannot have: import java.io.*

This classwork focuses on implementing computing the mode of a list of numbers. The mode is the most commonly occuring value(s) in a data set.

  1. The ClassScores class includes a method called getScanner. This method opens up a file chooser and returns a Scanner to the selected file. Write the necessary code in your main method to construct a ClassScores object and call the getScanner method to bind the selected file to a Scanner named in.

  2. When working with files, you often need to read in the data and check that it matches your expectations. Complete the method readAndPrintFile, to print each token from the file - one per line.

    For example, if given the following file excerpt of library holdings from the data directory:

    (* Library holdings (# of books in each library), *) (* collected by Christian Ayotte. *) (* Labels not available. *) 12201 600778 14926 37863 ...
    your program should print:
    (* Library holdings (# of books in each library), *) (* collected by Christian Ayotte. *) (* Labels not available. *) 12201 600778 14926 37863 ...
  3. The mode of a list of numbers is the most commonly occuring number(s). For example, if you have the following numbers, [88, 70, 65, 70, 88], the modes are 70 and 88 because they occur twice, while 65 ocurs once. The following problems ask you to complete methods in the ClassScores class that you snarfed.

    1. The ClassScores class has two different findMode methods. Why? How does Java distinguish the two methods? Write your answer in your README.

    2. Complete the findMode(int[]) method. Test using:
      1. the array example in the main method
      2. the APT page.

    3. Complete the findMode(Scanner) method. You should be able to ignore everything other than numbers by using Scanner's hasNextInt() method. Test your code using:
      • Files in the data directory.
      • Files that you create

Submitting

You will submit your code as part of the current APT set due. Include answers to these questions in the README file that gets submitted.
Last modified: Mon Feb 7 20:58:13 EST 2011