Colorado State University

Recitation R17 - Arrays & Debugging with Eclipse
Fall 2013

CS160: Foundations in Programming


The purpose of this lab is to learn how to: as well as to:

Arrays


Write the following program from scratch in the following phases:

Phase 1

  1. Create a new R17 project in Eclipse and an associated class.
  2. Declare a class variable (static) array of integers called arrayIntegers of size 100.
  3. Declare a class variable (static) array of doubles called arrayDoubles of size 100.
  4. Declare a class variable (static) array of strings called arrayStrings of size 100.
  5. Declare a class variable (static) array of characters called arrayChars of size 100.
  6. Add four integer class variables (static) to hold the size of each array, initialized to zero.

Phase 2

  1. Add the following methods, with exactly the signatures shown:

    private static void readFile(String inputFile) {}

    private static int computeSum(int size, int [] iArray) {}

    private static double computeAverage(int size, double [] dArray) {}

    private static int computeLength(int size, String [] sArray) {}

    private static char mostCommon(int size, char [] cArray) {}


  2. Create small arrays for testing the static methods in main, for example:
        int iTest[] = {12, 87, 92, 45, 26};
        double dTest[] = {2.34, 4.45, 6.83, 8.33, 13.2};
        String sTest[] = {"a", "ab", "abc", "abcd", "abcde"};
        char cTest[] = {'h', 'j', 'h', 'j', 'k', 'j'};
        
  3. Implement and test the static methods by calling them from main and printing the return value.

Phase 3

  1. Call the readFile method from main, passing the first command line argument.
  2. Modify the run configuration to pass in "mixed.txt" as the first parameter.
  3. Save the file mixed.txt into your R17/ folder
  4. In readFile, create a scanner for the file within a try/catch block.
  5. In the try block, write a loop that continues as long as there are tokens in the input file.
  6. For each token:
  7. After the loop, close the file.
  8. Within the catch block, print a file read error and exit.
  9. After the try/catch block, print the size of all arrays, as follows:
        arrayIntegers size: 14
        arrayDoubles size: 25
        arrayStrings size: 39
        arrayChars size: 13
        
  10. If you have read the file correctly, your answer will match exactly.


Phase 4

  1. After the readFile call in main, call each of the static methods with each size and array.
  2. Store the return value in local variables.
  3. Print the results from from the local variables, as follows:
        Sum of integers in iArray: 131103
        Average of doubles in dArray: 4105.597815575479
        Length of strings in sArray: 239
        Most common character in cArray: g
        
  4. If your code works, your answer will match exactly.

Show your R17.java program to the TA for grading and submit to RamCT to get credit for this portion of the lab.

© 2013 CS160 Colorado State University. All Rights Reserved.