CS 160, Spring 2014
Programming Assignment P6
Plotting Data: Arrays and Files

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


This programming assignment has four objectives:
  1. Read data from a file into arrays
  2. Declare and use class variables
  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 class variables, 2) reads the contents of a file into those variables, and 3) calls the Plotter object to make pretty graphs and charts. When complete, a pie chart, bar chart, and line graph will all be displayed simultaneously and in their own windows.

Instructions

Part One

Create a project and class called P6, with a main method. Below the main method, create two methods with the following signatures:

private static void readFile(String inputFile) {}

private static void displayCharts() {}

In the main method, call the readFile method with the first command line argument (args[0]), and 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 P6 project directory (not src or bin) and copy the contents from chart.txt. The file format is described below. Your program should now compile and run, but not do anything.

File Format

The file format is as follows:

Field Name Field Type Field Description
pieSize integer Number of data items in pie chart
barSize integer Number of data items in bar chart
lineSize integer Number of data items in line graph
pieLabel[0] String First label for pie chart
pieData[0] double First data value for pie chart
pieLabel[1] String Second label for pie chart
pieData[1] double Second data value for pie chart
...
pieLabel[pieSize-1] String Last label for pie chart
pieData[pieSize-1] double Last data value for pie chart
barData0 double[barSize] First data sequence for bar chart
barData1 double[barSize] Second data sequence for bar chart
lineData0 double[lineSize] First data sequence for line graph
lineData1 double[lineSize] Second data sequence for line graph
lineData2 double[lineSize] Third data sequence for line graph


Used the supplied data file as an example, and ask questions in the lab if necessary! Above the main method, declare the following class (static) variables:
  1. Title of the pie chart (String) = "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[])
  5. Title of the bar chart (String) = "Bar Chart"
  6. Number of data elements for the bar chart (int)
  7. First series of data elements for the bar chart (double[])
  8. Second series of data elements for the bar chart (double[])
  9. Title of the line graph (String) = "Line Graph"
  10. Number of data elements for the line graph (int)
  11. First series of data elements for the line graph (double[])
  12. Second series of data elements for the line graph (double[])
  13. Third series of data elements for the line graph (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 should be initialized, and the arrays must be declared but not allocated.

Part Two

Implement the readFile method in P6.java to open the input file chart.txt, 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.

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, as shown below:
pieChart.drawGraph(Plotter.eType.PIECHART);
At this point your program should display the pie chart with the data from chart.txt.

Part Five

Next, extend drawGraph to send data to the bar chart and display it. The method used to send data is as follows:

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, using 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, however the file format and number of data sequences will not change.

Please follow the usual rules for submitting Java programs.

Grading Criteria

Submission

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

© 2014 CS160 Colorado State University. All Rights Reserved.