CS 160, Spring 2015
Programming Assignment P9
Plotting Data: Arrays and Files

Programming due Monday, Apr. 13 at 6:00pm; late deadline Apr. 13 at 11:59pm.

This programming assignment has four objectives:
  1. Read data from a file into arrays
  2. Declare and use class variables
  3. Implement an interface given to you
  4. Plot data using an external package


The purpose of the assignment is to write a Java class that 1) declares a set of class variables, 2) reads the contents of a file into those variables, and 3) implements an interface that allows the Plotter object to make pretty graphs and charts. When complete, a pie chart, bar chart, and line graph will be displayed simultaneously and in their own windows.


Part One

Create a project and class called P9, with no main method. Make the class implement the interface provided in Interface.java, which you must download to the src directory from here. Here is the interface that is defined in this file:
// Java interface definition
public interface Interface {

    // Enumeration of chart types
    public enum eType {

    // Method to read chart file
    public boolean readFile(String filename);
    // Method to get chart title
    public String getTitle (eType type);
    // Method to get chart labels
    public String[] getLabels(eType type);
    // Method to get chart data
    public double[] getData(eType type, int series);
After you import the interface into the project, Eclipse will show a compile error on the class. If you fly over the error with the mouse, Eclipse will give you the option to create a stub for each method, so that you don't have to type in all the method signatures. Letting Eclipse make the stubs ensures that all the methods will be correctly defined.

Part Two

Before you implement the methods, declare the following class variables. Provide your own name for the variables, and you can declare them to be public or private, static or not.
  1. Array of data labels for the pie chart (String[])
  2. Array of data elements for the pie chart (double[])
  3. First series of data elements for the bar chart (double[])
  4. Second series of data elements for the bar chart (double[])
  5. First series of data elements for the line graph (double[])
  6. Second series of data elements for the line graph (double[])
  7. Third series of data elements for the line graph (double[])
The arrays must be declared but not allocated.

Part Three

Import the Plotter.java file from here. NOTE: Do not make any changes to Plotter.java or your warranty will be null and void! This file uses Java features that we do not discuss in CS 160. Use the Eclipse menu item for "Run Configurations" and setup the command line arguments as chart.txt. Create an input file in the main P9 project directory (not src or bin) and copy the contents from chart.txt. The file format is described below. Your program should now compile, but will not run. Download the two Java archives called jcommon-1.0.17.jar and jfreechart-1.0.14.jar to your system. These must be imported to the Eclipse by right clicking the project, then following the "Build Path->Add External Archives" menu and browsing for the archive files, as shown during the recitation.

File Format

The file format is as follows:

Field Type Field Description
integer Number of data items in pie chart
integer Number of data items in bar chart
integer Number of data items in line graph
String[] Sequence of labels for pie chart
double[] Sequence of data for pie chart
double[] First data sequence for bar chart
double[] Second data sequence for bar chart
double[] First data sequence for line graph
double[] Second data sequence for line graph
double[] Third data sequence for line graph

Part Four

Implement the readFile method in P9.java to open the input file passed as a parameter, read in the input variables (except for the title) in the order shown in the File Format section of this document, and close the file. Your program should read the number of data elements from the input file, then dynamically allocate all label and data arrays for all the charts. These sizes must also be used for the loops that read labels or data elements from the file. You should check that the values are read correctly by using the Eclipse debugger or print statements. Exceptions caused by the file read should be handled as shown in class and in the previous recitation. Return true if the file is read correctly, and false otherwise.

Part Five

Implement the remaining methods which return titles, labels, and data for the charts. Each method takes an enumeration of the chart type. For getTitle method, return "My Pie Chart" for the PIECHART type, "My Bar Chart" for the BARCHART type, and "My Line Graph" for the LINEGRAPH type. For the getLabels method, return the pie chart labels for PIECHART, otherwise return null. For the getData method, return the appropriate data series associated with the chart type specified. The Plotter object will never call this method with an invalid series, i.e. it only asks for series 0 for the pie chart, series 0 and 1 for the bar chart, and series 0, 1, and 2 for the line graph.


NOTE: We will test your code with a different inputfile, so your methods should work on arbitrary input values, however the file format and number of data sequences will not change.

Please follow the usual rules for submitting Java programs.

Grading Criteria


Submit your modified source file named P9.java to the the Checkin tab on the course web site.

© 2015 CS160 Colorado State University. All Rights Reserved.