Hi, I was assigned a very vague program by my professor. Long story short, we were basically taught half of the material and expected to complete this program.
What we were asked to do was to design a bar chart with an average line, and on this bar chart we were supposed to have the data that was greater than the average have bars above the average line, and the data below the average with bars below the line. He also instructed us (in a series of emails, most of which contradicted the emails before them or what he taught in class) to create the following things:
an auxiliary class called BarWithAverage (detailed below)
private int[] readInputFile(String fileName)
private BarWIthAverage[] createBars(int[] data)
private void drawBars() //for height = 0, draw a line
The program I have so far is as follows.
Code:
package basicbarchartapp;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class BarWithAverage //this is the class he told us to make
{
final int height, //height >= 0
value; //for labeling
int width, yForAverageLine;
double average; //for labeling
final Color color; //blue/red for data >/< average
public BarWithAverage(int height, int value, Color color)
{
this.height = height;
this.value = value;
this.color = color;
this.width = 0;
}
//here he asks a question, How to set values for width, yForAverageLine, and average?
}
public class BarChartWithAverage extends JFrame {
private Image fImageBuffer;
private Insets fInsets;
private Graphics g;
private static final int WIDTH = 800, HEIGHT = 600;
private static final Color BACKGROUND_COLOR = Color.white,
BAR_COLOR_ABOVE = Color.blue, BAR_COLOR_BELOW = Color.red,
AVERAGE_COLOR = Color.green;
public BarChartWithAverage()
{
addWindowListener(new BarChartWithAverage.WindowCloser());
setVisible(true);
fInsets = getInsets();
setSize(WIDTH + fInsets.left + fInsets.right, HEIGHT + fInsets.top +
fInsets.bottom);
setTitle("BAR GRAPH WITH AVERAGE LINE");
setResizable(false);
if(((fImageBuffer = createImage(WIDTH, HEIGHT)) != null) &&
((g = fImageBuffer.getGraphics()) != null))
Run();
else System.exit(1);
}
class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{System.exit(0);}
}
private void Run()
{
DrawFixedObjects();
DrawLine(0,100,WIDTH,100, AVERAGE_COLOR);
DrawLine(0,50,WIDTH,50, AVERAGE_COLOR);
DrawLine(0,500,WIDTH,500,AVERAGE_COLOR);
}
private void DrawFixedObjects()
{
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(BACKGROUND_COLOR);
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.black);
repaint();
}
private void DrawLine(int x, int y, int X, int Y, Color color)
{
g.setColor(color);
g.drawLine(x, y, X, Y);
repaint();
}
public void paint(Graphics g)
{
if(fImageBuffer != null)
g.drawImage(fImageBuffer, fInsets.left, fInsets.bottom, null);
}
private int[] readInputData(String fileName)
throws FileNotFoundException
{
Scanner scan = new Scanner(new FileInputStream(fileName));
int numData = scan.nextInt();
int[] data = new int[numData];
for(int i = 0; scan.hasNext(); i++)
{
data[i] = scan.nextInt();
}
return data;
}
private BarWithAverage[] createBars(int[] data) //both this and drawBars are empty because it is another place where I am stuck and have little clue on how to continue
{
}
private static void drawBars()
{
}
public static void main(String[] args) throws FileNotFoundException{
new BarChartWithAverage();
}
}
I'm stuck, and I'd really like a nudge in the right direction. This is only my second semester programming.
What I'm really confused on is how we're supposed to use the class BarsWithAverage to actually calculate an average using the array we made with createBars. They're part of two separate classes, and I'm following his exact instructions. How can I actually do something like that?
I'm not looking for someone to give me the answer, I really just want a nudge. I've spent the last 4 days stuck in the same spot and researching how to do this, and it hasn't been until today that I've finally broken down and asked online. When I confronted the professor about this, he asked me how I expected to learn if he taught us everything. The TA also hasn't been available, so my classmates and I have pretty much been alone on this. (sorry for the rant)