CS 163/164, Fall 2016
Lab 19 - Static versus Non-static
Wed-Fri, Nov. 16-18, 2016
Objectives of this Lab
- Learn how to choose between class (static) and instance (non-static) variables, and
- see how this influences the static and non-static modifier for associated methods, and
- demonstrate that each object has its own copy of instance (non-static) variables, and
- show that there is only one copy of class (static) variables.
Description
This lab requires you to complete two Java classes. The first is named Utilities.java, which
contains a set of utility methods, all of which are static. The second is named FileStatistics.java,
which contains file information as instance (non-static) data, and a collection of FileStatistic objects
as class (static) data. The goal is to provide a class that can store information about multiple
files simultaneously. Here is are the methods in the two files:
Utilities.java
// Read the lines in a file into an array list
public static ArrayList<String> readLines(String infile);
// Read the tokens in a file into an array list
public static ArrayList<String> readWords(String infile);
// Count the frequency of specified character in array list
public static int frequency(ArrayList<String> strings, char match);
FileStatistic.java
// Constructor
public FileStatistics(String filePath){
Instructions
- Make an Eclipse project named R19.
- Download Utilities.java and FileStatistics.java.
- Download the three data files:
- Look through the provided code, which your TA will help explain.
- First, complete the method readWords in Utilities.java.
- Second, complete the constructor in FileStatistics.java.
- Third, write the test code in main.
- Comments in the code will guide you through the process.
- Finally, test the code by running main, no command line arguments are necessary.
Testing
Here is the correct output of the program after all methods and test code are
complete. You must exactly match this output!
BenefitsOfJava.txt
Lines: 19
Words: 144
Length: 915
Path: /s/bach/a/class/cs163/workspace/R19/BenefitsOfJava.txt
Mars.txt
Lines: 5
Words: 72
Length: 401
Path: /s/bach/a/class/cs163/workspace/R19/Mars.txt
RobertFrost.txt
Lines: 23
Words: 144
Length: 731
Path: /s/bach/a/class/cs163/workspace/R19/RobertFrost.txt
Words in all three files:
[and, to, the, a]
Frequency of a for BenefitsOfJava.txt: 85, Mars.txt: 28, RobertFrost.txt: 57
Frequency of b for BenefitsOfJava.txt: 16, Mars.txt: 3, RobertFrost.txt: 12
Frequency of c for BenefitsOfJava.txt: 20, Mars.txt: 9, RobertFrost.txt: 9
Frequency of d for BenefitsOfJava.txt: 28, Mars.txt: 10, RobertFrost.txt: 36
Frequency of e for BenefitsOfJava.txt: 85, Mars.txt: 41, RobertFrost.txt: 68
Frequency of f for BenefitsOfJava.txt: 18, Mars.txt: 12, RobertFrost.txt: 8
Frequency of g for BenefitsOfJava.txt: 20, Mars.txt: 3, RobertFrost.txt: 14
Frequency of h for BenefitsOfJava.txt: 17, Mars.txt: 16, RobertFrost.txt: 36
Frequency of i for BenefitsOfJava.txt: 41, Mars.txt: 20, RobertFrost.txt: 30
Frequency of j for BenefitsOfJava.txt: 11, Mars.txt: 0, RobertFrost.txt: 1
Frequency of k for BenefitsOfJava.txt: 2, Mars.txt: 1, RobertFrost.txt: 7
Frequency of l for BenefitsOfJava.txt: 28, Mars.txt: 18, RobertFrost.txt: 26
Frequency of m for BenefitsOfJava.txt: 23, Mars.txt: 15, RobertFrost.txt: 7
Frequency of n for BenefitsOfJava.txt: 47, Mars.txt: 17, RobertFrost.txt: 37
Frequency of o for BenefitsOfJava.txt: 58, Mars.txt: 21, RobertFrost.txt: 49
Frequency of p for BenefitsOfJava.txt: 16, Mars.txt: 6, RobertFrost.txt: 5
Frequency of q for BenefitsOfJava.txt: 0, Mars.txt: 0, RobertFrost.txt: 1
Frequency of r for BenefitsOfJava.txt: 47, Mars.txt: 24, RobertFrost.txt: 32
Frequency of s for BenefitsOfJava.txt: 56, Mars.txt: 30, RobertFrost.txt: 32
Frequency of t for BenefitsOfJava.txt: 57, Mars.txt: 33, RobertFrost.txt: 51
Frequency of u for BenefitsOfJava.txt: 22, Mars.txt: 6, RobertFrost.txt: 10
Frequency of v for BenefitsOfJava.txt: 16, Mars.txt: 0, RobertFrost.txt: 8
Frequency of w for BenefitsOfJava.txt: 12, Mars.txt: 2, RobertFrost.txt: 18
Frequency of x for BenefitsOfJava.txt: 0, Mars.txt: 1, RobertFrost.txt: 0
Frequency of y for BenefitsOfJava.txt: 13, Mars.txt: 4, RobertFrost.txt: 11
Frequency of z for BenefitsOfJava.txt: 0, Mars.txt: 0, RobertFrost.txt: 0
Grading Criteria
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- Preliminary Tests:
- compileTest: checks that Utilities.java and FileStatistics.java compile. (0 points)
- checkReadLines: Checks that readLines works correctly. (15 points)
- checkReadWords: Checks that readWords works correctly. (20 points)
- checkFrequency: Checks that frequency works correctly. (15 points)
- checkConstructor: Checks that the constructor in FileStatistics works correctly. (25 points)
- checkTestCode: Checks that the test code in main works correctly, based on output. (25 points)
- Final grading is identical to the preliminary tests.
Build an archive named R19.jar with the two source files and submit to the Checkin tab. Automated grading will be running.