Help with programming assignment! New!
This programming assignment has four objectives:
Read data from a file into arrays
Declare and use instance data in a class
Instantiate and call methods on an object
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:
Title of the pie chart (String) = "My Pie Chart"
Number of data elements for the pie chart (int)
Array of data labels for the pie chart (String[])
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.
chart.txt
6
Nokia
Apple
Samsung
Apple
Other
17.1
10.0
16.8
17.8
15.3
40.0
9
44.2
54.0
75.0
63.0
83.0
43.0
46.0
27.0
13.0
41.0
33.0
22.0
34.0
44.1
62.0
32.0
42.0
34.0
7
14.0
25.0
33.0
23.0
21.9
8.0
19.0
21.0
13.0
37.5
42.0
39.0
5.0
17.0
25.0
32.0
31.3
41.0
19.0
1.0
3.0
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.
plotter.txt
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Point;
import java.awt.RadialGradientPaint;
import java.awt.geom.Point2D;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRend erer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
// Pie charts, bar charts, and line graphs
public class Plotter extends ApplicationFrame {
// Serialization identifier
private static final long serialVersionUID = 1L;
// Set a theme using shadow generator
{
ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow", true));
}
// Enumeration
public enum eType {
PIECHART, BARCHART, LINEGRAPH
}
// Chart title
String chartTitle;
// Instance data
int pieCount;
int barCount;
int lineCount;
// Static allocation!
double[] pieData;
String[] pieLabels;
double[][] barData;
double[][] lineData;
public Plotter(String title) {
super(title);
chartTitle = title;
}
public void drawGraph(eType type)
{
JFreeChart chart;
switch (type)
{
case PIECHART:
chart = createPieChart();
break;
case BARCHART:
chart = createBarChart();
break;
case LINEGRAPH:
default:
chart = createLineGraph();
}
// add the chart to a panel...
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(600, 400));
chart.setPadding(new RectangleInsets(4, 8, 2, 2));
setContentPane(chartPanel);
pack();
RefineryUtilities.positionFrameRandomly(this);
setVisible(true);
}
// Initialize pie chart data
public void pieChartData(int count, double data[]) {
// Copy data from user
pieCount = count;
pieData = new double[count];
for (int i = 0; i < count; i++) {
pieData[i] = data[i];
}
}
// Initialize pie chart labels
public void pieChartLabels(int count, String labels[]) {
// Copy labels from user
pieCount = count;
pieLabels = new String[count];
for (int i = 0; i < count; i++) {
pieLabels[i] = labels[i];
}
}
// Create pie chart
private JFreeChart createPieChart()
{
// Fill in data set
DefaultPieDataset dataset = new DefaultPieDataset();
for (int i = 0; i < pieCount; i++)
dataset.setValue(pieLabels[i], pieData[i]);
JFreeChart chart = ChartFactory.createPieChart(chartTitle,
dataset, // data
false, // no legend
true, // tooltips
false // no URL generation
);
// Set a custom background
chart.setBackgroundPaint(new GradientPaint(new Point(0, 0),
new Color(20, 20, 20), new Point(400, 200), Color.DARK_GRAY));
// Customize the title position and font
TextTitle title = chart.getTitle();
title.setHorizontalAlignment(HorizontalAlignment.L EFT);
title.setPaint(new Color(240, 240, 240));
title.setFont(new Font("Arial", Font.BOLD, 26));
PiePlot plot = (PiePlot) chart.getPlot();
plot.setBackgroundPaint(null);
plot.setOutlineVisible(false);
// Use gradients for section colors
plot.setSectionPaint(pieLabels[0], createGradientPaint(new Color(200, 200, 255), Color.BLUE));
plot.setSectionPaint(pieLabels[1], createGradientPaint(new Color(255, 200, 200), Color.RED));
plot.setSectionPaint(pieLabels[2], createGradientPaint(new Color(200, 255, 200), Color.GREEN));
plot.setSectionPaint(pieLabels[3], createGradientPaint(new Color(200, 255, 200), Color.YELLOW));
plot.setBaseSectionOutlinePaint(Color.WHITE);
plot.setSectionOutlinesVisible(true);
plot.setBaseSectionOutlineStroke(new BasicStroke(2.0f));
// Customize the section label appearance
plot.setLabelFont(new Font("Courier New", Font.BOLD, 20));
plot.setLabelLinkPaint(Color.WHITE);
plot.setLabelLinkStroke(new BasicStroke(2.0f));
plot.setLabelOutlineStroke(null);
plot.setLabelPaint(Color.WHITE);
plot.setLabelBackgroundPaint(null);
return chart;
}
// Set bar chart data
public void barChartData(int series, int count, double data[]) {
// Copy data from user
barCount = count;
if (series == 0) barData = new double[2][count];
for (int i = 0; i < count; i++) {
barData[series][i] = data[i];
}
}
// Create bar chart
private JFreeChart createBarChart()
{
// Fill in data set
CategoryDataset dataset;
dataset = DatasetUtilities.createCategoryDataset("Series ", "Factor ", barData);
JFreeChart chart = ChartFactory.createBarChart(
chartTitle, // chart title
"Category", // domain axis label
"Score ", // range axis label
dataset, // data
PlotOrientation.HORIZONTAL, // orientation
true, // include legend
true,
false
);
// Set the background color for the chart...
chart.setBackgroundPaint(Color.lightGray);
// Get a reference to the plot
CategoryPlot plot = chart.getCategoryPlot();
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_L EFT);
// Change the tick unit selection to integer units only
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setRange(0.0, 100.0);
rangeAxis.setStandardTickUnits(NumberAxis.createIn tegerTickUnits());
return chart;
}
// Set line graph data
public void lineGraphData(int series, int count, double data[]) {
// Copy data from user
lineCount = count;
if (series == 0) lineData = new double[3][count];
for (int i = 0; i < count; i++) {
lineData[series][i] = data[i];
}
}
// Create line graph
private JFreeChart createLineGraph()
{
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < 3; i++) // series
{
for (int j = 0; j < lineCount; j++) // data
{
String series = "Series " + i;
String type = "Type " + j;
dataset.addValue(lineData[i][j], series, type);
}
}
// create the chart...
final JFreeChart chart = ChartFactory.createLineChart(
chartTitle, // chart title
"Type", // domain axis label
"Value", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
// Chart customization
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.white);
// Customize the range axis
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIn tegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
// Customize the renderer
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setSeriesStroke(
0, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {10.0f, 6.0f}, 0.0f
)
);
renderer.setSeriesStroke(
1, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {6.0f, 6.0f}, 0.0f
)
);
renderer.setSeriesStroke(
2, new BasicStroke(
2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
1.0f, new float[] {2.0f, 6.0f}, 0.0f
)
);
return chart;
}
// Utility method for creating gradient paints.
private static RadialGradientPaint createGradientPaint(Color c1, Color c2)
{
Point2D center = new Point2D.Float(0, 0);
float radius = 200;
float[] dist = {0.0f, 1.0f};
return new RadialGradientPaint(center, radius, dist,
new Color[] {c1, c2});
}
}
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:
Title of the bar chart (String) = "My Bar Chart"
Number of data elements for the bar chart (int)
First series of data elements for the bar chart (double[])
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:
Title of the line graph (String) = "My Line Graph"
Number of data elements for the line graph (int)
First series of data elements for the line graph (double[])
Second series of data elements for the line graph (double[])
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.