Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Thanked 0 Times in 0 Posts

    Question Help with simple bar chart program

    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.

    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());
            fInsets = getInsets();
            setSize(WIDTH + fInsets.left + fInsets.right, HEIGHT + fInsets.top + 
            setTitle("BAR GRAPH WITH AVERAGE LINE");
            if(((fImageBuffer = createImage(WIDTH, HEIGHT)) != null) && 
                    ((g = fImageBuffer.getGraphics()) != null))
            else System.exit(1);
        class WindowCloser extends WindowAdapter
            public void windowClosing(WindowEvent e)
        private void Run()
            DrawLine(0,100,WIDTH,100, AVERAGE_COLOR);
            DrawLine(0,50,WIDTH,50, AVERAGE_COLOR);
        private void DrawFixedObjects()
            g.clearRect(0, 0, WIDTH, HEIGHT);
            g.fillRect(0, 0, WIDTH, HEIGHT);
        private void DrawLine(int x, int y, int X, int Y, Color color)
            g.drawLine(x, y, X, Y);
        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)
    Last edited by ddeben1; 11-01-2012 at 05:15 AM.

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,668 Times in 2,637 Posts
    Are these class and constructor signatures provided in this way? If so, I don't see any purpose to adding these private methods to BarWithAverage either. Since they can't be called outside of the class, nor is the BarWithAverage provided with any information regarding the averages of other items or a file in which it can read, it has no practical use within the BarWithAverage itself. That really makes more sense to use on the BarChartWithAverage given what you have here for the two classes and their construction. Now if you have other setters such as taking the average or giving it the data array, then sure that would be alright in the BarWithAverage.
    What I'm trying to figure out looking at this is if the BarWithAverage is to paint itself, or simply return data. Since drawBars isn't provided, we cannot tell if it intends to paint itself or simply describe itself to the BarChartWithAverge.

    So the only way I can see the BarWithAverage actually making use of these private methods is if you provide it with a graphics to paint on, the entire dataset available (or a file or whatever to get them from), and then you can paint with the class. To me, these still seem like they should be on the BarChartWithAverage class (non-static of course; the drawBars you have is static). This way it can iterate an array of BarWithAverage objects and paint accordingly.

    I think you'll need to get more clarification on what these classes are responsible for doing.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts