Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import org.jfree.data.xy.AbstractXYDataset; import org.jfree.data.xy.XYDataset;

ID: 3790339 • Letter: I

Question

import org.jfree.data.xy.AbstractXYDataset;
import org.jfree.data.xy.XYDataset;

import java.util.ArrayList;
import java.util.List;


public class PolyDataset extends AbstractXYDataset implements XYDataset {


private List<Double> polyList;
private List<Double> itemList;

public PolyDataset(){
super();
polyList = new ArrayList<Double>();
}

public void addPolynome(double poly) {
polyList.add(poly);
}

public void setItemList(List<Double> list){
itemList = list;
}
/**
* Returns the x-value for the specified series and item. Series are
* numbered 0, 1, ...
*
* @param series the index (zero-based) of the series.
* @param item the index (zero-based) of the required item.
*
* @return the x-value for the specified series and item.
*/
public Number getX(int series, int item) {
// return new Double( item);
return new Double( itemList.get(item));
}

/**
* Returns the y-value for the specified series and item. Series are
* numbered 0, 1, ...
*
* @param series the index (zero-based) of the series.
* @param item the index (zero-based) of the required item.
*
* @return the y-value for the specified series and item.
*/
public Number getY(int series, int item) {
return new Double(Math.pow(itemList.get(item),polyList.get(series)));
}

/**
* Returns the number of series in the dataset.
*
* @return the number of series in the dataset.
*/
public int getSeriesCount() {
return polyList.size();
}

/**
* Returns the key for a series.
*
* @param series the index (zero-based) of the series.
*
* @return The key for the series.
*/
public Comparable getSeriesKey(int series) {
return "k = " + polyList.get(series);
}

/**
* Returns the number of items in the specified series.
*
* @param series the index (zero-based) of the series.
* @return the number of items in the specified series.
*
*/
public int getItemCount(int series) {
return itemList.size();
}

}


import java.awt.Color;
import javax.swing.JPanel;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;
import org.jfree.chart.renderer.category.StatisticalLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYErrorRenderer;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.YIntervalSeries;
import org.jfree.data.xy.YIntervalSeriesCollection;

import java.util.*;

/**
* The class <b>ITI1121Chart</b> uses the library
* <b>JFreeChart</b>. It can be used to create a chart
* made of a series of points, each with a confidence
* interval. It can also superimpose on the graph
* drawings of functions of the form y=x^k.
*
* @author gvj (gvj@eecs.uottawa.ca)
*
*/
public class ITI1121Chart extends ApplicationFrame {


private YIntervalSeriesCollection dataset;
private YIntervalSeries series;

private PolyDataset polyDataset;

private List<Double> itemList;

/**
* Constructor.
*
* @param title the title for the graph
*/
public ITI1121Chart(String title) {
super(title);
dataset = new YIntervalSeriesCollection();
series = new YIntervalSeries("Birthday Results");
itemList =new ArrayList<Double>();
polyDataset = new PolyDataset();
}

/**
* This method draws the graph. The method is to be called
* only once, after all the data has been added
*/
public void render(){

JPanel chartPanel = createPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(800, 600));
setContentPane(chartPanel);
pack();
RefineryUtilities.centerFrameOnScreen(this);
setVisible(true);
}

/**
* Adds a data point to the graph. A dot at the position
* (x,y) will be added, and a vertical line of length
* ``confidence'' centered on the point will also be drawn
*
* @param x the abscissa
* @param y the ordinate
* @param confidence the confidence interval
*/

public void addDataPoint(double x, double y, double confidence){
series.add(x,y, y - confidence/2, y + confidence/2);
itemList.add(x);
}

/**
* Adds a curve of the form y=x^k to the graph.
*
* @param k the polynomial degree
*/
public void addPolynome(double k){
polyDataset.addPolynome(k);
}

private JPanel createPanel() {
Collections.sort(itemList);
polyDataset.setItemList(itemList);
JFreeChart chart = createChart(polyDataset);


XYPlot plot = (XYPlot) chart.getPlot();

dataset.addSeries(series);
plot.setDataset(1, dataset);
XYErrorRenderer renderer = new XYErrorRenderer();
renderer.setBaseLinesVisible(true);
renderer.setBaseShapesVisible(false);
plot.setRenderer(1, renderer);


ChartPanel panel = new ChartPanel(chart);
return panel;
}


private JFreeChart createChart(XYDataset dataset) {

// create the chart...
JFreeChart chart = ChartFactory.createXYLineChart(
"Birthday Paradox Experiments", // chart title
"X", // x axis label
"Y", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);

XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainZeroBaselineVisible(true);
plot.setRangeZeroBaselineVisible(true);
plot.getDomainAxis().setLowerMargin(0.0);
plot.getDomainAxis().setUpperMargin(0.0);
plot.setDomainPannable(true);
plot.setRangePannable(true);

XYLineAndShapeRenderer renderer
= (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setLegendLine(new Rectangle2D.Double(-4.0, -3.0, 8.0, 6.0));
return chart;
}

}

Running and graphing a series of experiments We now want to run the same nimal of experiments on an increasingly larger set, and display the resilt on a graph ber We are going lo nuodify the class BirthdayParadox lo have arger sel. B y dela bould run the experiments 1,000 linles each linule u el of size 100 el of size 200. aad until the siz of the set is 10000. So the following command wil run 1,000 experiment on each of 100 sets 000 experiments on a set of size 100, ments on a set of size 200. and 1,000 experiments on a set of size 000 exper ctc. 0000 Birthday default values should be overwritten when 3 paramete on through the command line. The first parameter is the size by which the set w increment each time (as we as the initial size), the second parameter are pa is the maximum size tor the set, and the third parameter is the mmber of experiments to nin each time So the following comruatnd will rull 10.000 experitueuls on a sel of ze 500, 10.000 experiments on a sel of size 1000. and 10.000 experiment s on a set of size 5,000) Birthday oing to graph. he mean and the standard deviation obtained for cach set size. Wc are going to e the Library JFreeChart for this. mplify our worl k, a class named ITI1121Chart has been prepared and n every case, we are packaged into the file ITT 21 jar. An instauke of the class IIIll21Chart van be created, passing the title of the graph as the parauueter of the coustructor. The method addDataPoint can then be used to add a point to the graph. This mcthod has 3 paranctcrs, nanuely x, y, and the standard deviation around y Once all the data points have been added, the method render can be used to display the graph. Once the grap is being displa ed on can be answered: w at simple function seem o behave more or le ike the re sult of our experiments? Lucki e class ITI1121Chart bas other methods addPo One ore que This method takes one parameter k as input, and will superimpose to the graph the polynomial y xk. ghtly faster, and another one y trial and erro we want to add 3 enrve o the graph: one cr ve that seem to grow ke" our expe ghtly another curve ar grow grows To make things a little cleaner, we will use three subdirectories: lib contains the jar files, src contains the java files, and classes the compiled class files Q2uzip is a zip file that contains the directory structure and the jar files in the lib subdirectory. The file Howio.txt in the root direct ontains compiling and running intructions yadonc for the c TTI12 Chart The actual class ITIll21Chart java bas been ed into the directory lib of the zip file if you want to have a look at it. This is llot required to complete this question You have to implement this new feature and experiment with the results until you find suitable polynonials. The file you submit should automatically add the 3 polynomial to the drawing.

Explanation / Answer

mport java.awt.Color;
import javax.swing.JPanel;
import java.awt.geom.Rectangle2D;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;
import org.jfree.chart.renderer.category.StatisticalLineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYErrorRenderer;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.YIntervalSeries;
import org.jfree.data.xy.YIntervalSeriesCollection;

import java.util.*;

/**
* The class <b>ITI1121Chart</b> uses the library
* <b>JFreeChart</b>. It can be used to create a chart
* made of a series of points, each with a confidence
* interval. It can also superimpose on the graph
* drawings of functions of the form y=x^k.
*
* @author gvj (gvj@eecs.uottawa.ca)
*
*/
public class ITI1121Chart extends ApplicationFrame {


private YIntervalSeriesCollection dataset;
private YIntervalSeries series;

private PolyDataset polyDataset;

private List<Double> itemList;

/**
* Constructor.
*
* @param title the title for the graph
*/
public ITI1121Chart(String title) {
super(title);
dataset = new YIntervalSeriesCollection();
series = new YIntervalSeries("Birthday Results");
itemList =new ArrayList<Double>();
polyDataset = new PolyDataset();
}

/**
* This method draws the graph. The method is to be called
* only once, after all the data has been added
*/
public void render(){

JPanel chartPanel = createPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(800, 600));
setContentPane(chartPanel);
pack();
RefineryUtilities.centerFrameOnScreen(this);
setVisible(true);
}

/**
* Adds a data point to the graph. A dot at the position
* (x,y) will be added, and a vertical line of length
* ``confidence'' centered on the point will also be drawn
*
* @param x the abscissa
* @param y the ordinate
* @param confidence the confidence interval
*/

public void addDataPoint(double x, double y, double confidence){
series.add(x,y, y - confidence/2, y + confidence/2);
itemList.add(x);
}

/**
* Adds a curve of the form y=x^k to the graph.
*
* @param k the polynomial degree
*/
public void addPolynome(double k){
polyDataset.addPolynome(k);
}

private JPanel createPanel() {
Collections.sort(itemList);
polyDataset.setItemList(itemList);
JFreeChart chart = createChart(polyDataset);


XYPlot plot = (XYPlot) chart.getPlot();

dataset.addSeries(series);
plot.setDataset(1, dataset);
XYErrorRenderer renderer = new XYErrorRenderer();
renderer.setBaseLinesVisible(true);
renderer.setBaseShapesVisible(false);
plot.setRenderer(1, renderer);


ChartPanel panel = new ChartPanel(chart);
return panel;
}


private JFreeChart createChart(XYDataset dataset) {

// create the chart...
JFreeChart chart = ChartFactory.createXYLineChart(
"Birthday Paradox Experiments", // chart title
"X", // x axis label
"Y", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
true, // include legend
true, // tooltips
false // urls
);

XYPlot plot = (XYPlot) chart.getPlot();
plot.setDomainZeroBaselineVisible(true);
plot.setRangeZeroBaselineVisible(true);
plot.getDomainAxis().setLowerMargin(0.0);
plot.getDomainAxis().setUpperMargin(0.0);
plot.setDomainPannable(true);
plot.setRangePannable(true);

XYLineAndShapeRenderer renderer
= (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setLegendLine(new Rectangle2D.Double(-4.0, -3.0, 8.0, 6.0));
return chart;
}

}