CS 160, Spring 2013
Programming Assignment P5
Plotting Data: Arrays and Files

Programming due Monday, Apr. 15 at noon; late deadline Apr. 15 at 10 p.m.


This programming assignment has four objectives:
  1. Read data from a file into arrays
  2. Declare and use instance data in a class
  3. Instantiate and call methods on an object
  4. Plot data using an external package

Description

The purpose of the assignment is to write a Java class that 1) declares a set of instance variables, 2) reads the contents of a file into those variables, and 3) calls the Plotter object to make pretty graphs and charts.

Instructions

Part One

Create a project and class called P5, with a main method. Above the main method, declare the following instance variables:
  1. Title of the pie chart (String) = "My Pie Chart"
  2. Number of data elements for the pie chart (int)
  3. Array of data labels for the pie chart (String[])
  4. Array of data elements for the pie chart (double[])
You can name your variables whatever you would like, but you should assign the title String using the exact value shown. No other variables need to be initialized, and the arrays must be declared but not allocated. Below the main method, create two methods with the following signatures:

private void readFile(String inputFile) {}

private void displayCharts() {}

In the main method, instantiate the class P5 into a variable called p5. Call the readFile method with the first command line argument (args[0]), then call displayCharts with no argument, Use the Eclipse menu item for "Run Configurations" and setup the command line arguments as chart.txt. Create an input file in the main P5 project directory (not src or bin) and copy the contents from chart.txt. Your program should now compile and run, but not do anything.

Part Two

Implement the readFile method in P5.java to open the input file chart.txt, read in the input variables (except for the title) in the order shown above, and close the file. Your program should read the number of data elements from the input file, then allocate the label and data arrays for the pie chart dynamically. This number 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.

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. Also 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.

Part Four

For each chart type you must instantiate a Plotter and send it the chart data before calling the drawGraph method. The pie chart requires one call to pieChartData and another to pieChartLabels to send the labels. The drawGraph method is always called last, with a variable of eType that tells it which kind of chart to draw. Implement the displayCharts method by instantiating a Plotter for the pie chart, We have provided an example of instantiation call below. The parameter to the Plotter constructor is a Java String with the title of the chart. You must replace the parameter shown with the chart title defined in the instance variables.
// Instantiation example
Plotter pieChart = new Plotter("Chart Title");
Now add the code to setup and display the pie chart, by calling the following methods that are implemented in the Plotter class:

public void pieChartData(int count, double data[]);

public void pieChartLabels(int count, String labels[]);

// Enumeration
public enum eType {
    PIECHART, BARCHART, LINEGRAPH
}
public void drawGraph(eType type);

After sending labels and data, call the drawGraph method with Plotter.eType.PIECHART. At this point your program should display the pie chart with the data from chart.txt.

Part Five

Repeat the process for the bar chart. First, add the following instance variables to the class:
  1. Title of the bar chart (String) = "My Bar Chart"
  2. Number of data elements for the bar chart (int)
  3. First series of data elements for the bar chart (double[])
  4. Second series of data elements for the bar chart (double[])
Next, extend the readFile to read the data elements for the bar chart. You must allocate both series of data elements based on the number of data elements read from the file. Then use the following method to send the data to the bar chart:

public void barChartData(int series, int count, double data[]);

The bar chart requires two calls to barChartData, with the series parameter set to 0 and 1 successively. Make sure that the data in each series corresponds to the data read from the file. Finally call the drawGraph method with Plotter.eType.BARCHART. At this point your program should display the bar chart with the data from chart.txt.

Part Six

Repeat the process for the line graph chart. First, add the following instance variables to the class:
  1. Title of the line graph (String) = "My Line Graph"
  2. Number of data elements for the line graph (int)
  3. First series of data elements for the line graph (double[])
  4. Second series of data elements for the line graph (double[])
  5. Third series of data elements for the line graph (double[])
Next, extend the readFile to read the data elements for the line graph. As with the previous chart types, dynamically allocate all three arrays of data elements based on the number read from the file. Then use the following method to send the data for the line graph:

public void lineGraphData(int series, int count, double data[]);

The line graph requires three calls to lineGraphData, with the series parameter set to 0, 1, and 2. Finally call the drawGraph method with Plotter.eType.LINEGRAPH. At this point your program should display the line graph with the data from chart.txt.

Testing

NOTE: We will test your code with a different chart.txt file, so your methods should work on arbitrary input values.

Please follow the usual rules for submitting Java programs.

Grading Criteria

NOTE: We will test your program with different data than is shown above!

Submission

Submit your modified source file named P5.java to the RamCT drop box.

© 2013 CS160 Colorado State University. All Rights Reserved.