HW4: Bar Chart and Pie Chart

Description

In this homework you are going to write a class Chart.java. The program shows data in both bar chart format and pie chart format.

Data.java

public class Data {
    private String name;
    private double quantity;
    
    public Data(String name, double quantity) {
	this.name = name;
	this.quantity = quantity;
    }
    
    public String getName() {
	return name;
    }
    
    public double getQuantity() {
	return quantity;
    }
}
The class represent a data in a chart, it contains 2 members You don't have to modify this file.

Chart.java

The class represent an array of data (Data[] data) by a bar chart and pie chart. There are 2 constructors Your program show give similar results as my sample output. Things that I don't care Here are some sample outputs. Your output show look similar (but not necessarily same as) my output.
Test1
data.length == color.length

import java.awt.*;
import javax.swing.*;

// same number of color and data
public class Test1 {
    public static void main(String[] args) {
	Data[] data = new Data[2];
	data[0] = new Data("Own house", 491882);
	data[1] = new Data("Rent", 783530);
	Color[] color = {Color.blue, Color.red};
	Chart chart = new Chart(data, color);
	chart.setTitle("Los Angeles Housing");
	 chart.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        chart.setVisible(true);
    }
}
Test2
data.length < color.length

import java.awt.*;
import javax.swing.*;

// more color than data
public class Test2 {
    public static void main(String[] args) {
	Data[] data = new Data[4];
	data[0] = new Data("Charles", 22222.5);
	data[1] = new Data("Dianne", 32000);
	data[2] = new Data("Eric", 10003.25);
	data[3] = new Data("Florence", 5999.99);
	Color[] color = {Color.red, Color.green, Color.yellow, Color.pink, Color.cyan};
	Chart chart = new Chart(data, color);
        chart.setTitle("Family Income"); 
	chart.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        chart.setVisible(true);
    }
}
Test3
No color
Use random color.
Your result may be different from the output

//  The United States Federal Budget for Fiscal Year 2004
// source : http://www.warresisters.org/piechart.htm

import java.awt.*;
import javax.swing.*;

// no color 
public class Test3 {
    public static void main(String[] args) {
        // the  unit of the money is in Billion
	Data[] data = new Data[5];
	data[0] = new Data("Current Military", 459);
	data[1] = new Data("Past Military", 345);
	data[2] = new Data("Human Resources", 593);
	data[3] = new Data("General Government", 235);
	data[4] = new Data("Physical Resources", 99);
	Chart chart = new Chart(data);
        chart.setTitle("The United States Federal Budget for Fiscal Year 2004"); 
	chart.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        chart.setVisible(true);
    }
}
Test4
data.length > color.length
Use random color for the remaining data
Your result may be different from the output

//  The United States Federal Budget for Fiscal Year 2004
// source : http://www.warresisters.org/piechart.htm

import java.awt.*;
import javax.swing.*;

// more data than color
public class Test4 {
    public static void main(String[] args) {
        // the  unit of the money is in Billion
	Data[] data = new Data[5];
	data[0] = new Data("Current Military", 459);
	data[1] = new Data("Past Military", 345);
	data[2] = new Data("Human Resources", 593);
	data[3] = new Data("General Government", 235);
	data[4] = new Data("Physical Resources", 99);
	Color[] color = {Color.orange, Color.magenta};
	Chart chart = new Chart(data, color);
        chart.setTitle("The United States Federal Budget for Fiscal Year 2004"); 
	chart.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        chart.setVisible(true);
    }
}

What to submit

Call your source code file Data.java, Chart.java. Put these files in your submit folder. Do not place this file inside another folder within your submit folder.

Remark

Solution

Chart.java