Results 1 to 3 of 3

Thread: So lost

  1. #1
    Join Date
    Apr 2015
    Posts
    1
    Rep Power
    0

    Default So lost

    Alright guys, I am a little lost at this point I feel like I have gotten the majority of the program down but when it comes to coming up with the weighted average Im lost. Here is the assignment:

    . write a program in JAVA in response to the following prompt:

    Design a GUI program to find the weighted average of four test scores. The four test scores and their respective weights are given in the following format:

    testscore1 weight1
    ...

    For example, the sample data is as follows:

    75 0.20
    95 0.35
    85 0.15
    65 0.30

    The user is supposed to enter the data and press a Calculate button. The program must display the weighted average.


    Here is what I have written:


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

    public class weightedaverage2 extends JFrame
    {
    private JLabel Score1L,Score2L,Score3L,Score4L;
    private JLabel Weight1L,Weight2L,Weight3L,Weight4L;

    private JTextField Score1TF,Score2TF,Score3TF,Score4TF;
    private JTextField Weight1TF,Weight2TF,Weight3TF,Weight4TF;

    private JLabel ResultMessage;
    private JTextField Result;

    private JButton CalculateB, ExitB;

    private CalculateButtonHandler cbHandler;
    private ExitButtonHandler ebHandler;

    private static final int WIDTH = 400;
    private static final int HEIGHT = 800;

    public weightedaverage2()
    {

    Score1L = new JLabel("Score 1: ", SwingConstants.RIGHT);
    Weight1L = new JLabel("Weight: ", SwingConstants.RIGHT);
    Score2L = new JLabel("Score 2: ", SwingConstants.RIGHT);
    Weight2L = new JLabel("Weight: ", SwingConstants.RIGHT);
    Score3L = new JLabel("Score 2: ", SwingConstants.RIGHT);
    Weight3L = new JLabel("Weight: ", SwingConstants.RIGHT);
    Score4L = new JLabel("Score 4: ", SwingConstants.RIGHT);
    Weight4L = new JLabel("Weight: ", SwingConstants.RIGHT);

    ResultMessage = new JLabel("Average Weight: ", SwingConstants.RIGHT);


    Score1TF = new JTextField(10);
    Weight1TF = new JTextField(10);
    Score2TF = new JTextField(10);
    Weight2TF = new JTextField(10);
    Score3TF = new JTextField(10);
    Weight3TF = new JTextField(10);
    Score4TF = new JTextField(10);
    Weight4TF = new JTextField(10);

    Result = new JTextField(10);

    Calculate.add(ResultMessage);
    Calculate.add(Result);

    CalculateB = new JButton("Calculate");
    cbHandler = new CalculateButtonHandler();
    CalculateB.addActionListener(cbHandler);

    ExitB = new JButton("Exit");
    ebHandler = new ExitButtonHandler();
    ExitB.addActionListener(ebHandler);

    setTitle("Weighted Average Calculator");

    Container pane = getContentPane();

    pane.setLayout(new GridLayout(9,2));

    pane.add(Score1L);
    pane.add(Score1TF);

    pane.add(Weight1L);
    pane.add(Weight1TF);

    pane.add(Score2L);
    pane.add(Score2TF);

    pane.add(Weight2L);
    pane.add(Weight2TF);

    pane.add(Score3L);
    pane.add(Score3TF);

    pane.add(Weight3L);
    pane.add(Weight3TF);

    pane.add(Score4L);
    pane.add(Score4TF);

    pane.add(Weight4L);
    pane.add(Weight4TF);

    pane.add(CalculateB);
    pane.add(ExitB);

    setSize(WIDTH, HEIGHT);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private class CalculateButtonHandler implements ActionListener
    {

    public void actionPerformed(ActionEvent e)
    {

    double Score1, Weight1, Score2, Weight2, Score3, Weight3, Score4, Weight4;

    Score1 = Double.parseDouble(Score1TF.getText ());
    Weight1 = Double.parseDouble(Weight1TF.getText ());
    Score2 = Double.parseDouble(Score2TF.getText ());
    Weight2 = Double.parseDouble(Weight2TF.getText ());
    Score3 = Double.parseDouble(Score3TF.getText ());
    Weight3 = Double.parseDouble(Weight3TF.getText ());
    Score4 = Double.parseDouble(Score4TF.getText ());
    Weight4 = Double.parseDouble(Weight4TF.getText ());

    Result = Weight1+Weight2+Weight3+Weight4;

    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: So lost

    Are you familiar with how to calculate weighted averages? There are several methods depending on how the data is presented. I recommend you do a web search on weighted averages. Once you figure it out, do examples with paper and pencil before applying it to code.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: So lost

    Quote Originally Posted by kaylawilson0928 View Post
    So lost
    Please go through the Forum Rules, particularly the third paragraph.

    Also go through BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Help im lost!
    By Wintergrasped in forum Advanced Java
    Replies: 3
    Last Post: 07-09-2013, 05:28 AM
  2. Very new, and very lost
    By NoSwing in forum AWT / Swing
    Replies: 1
    Last Post: 03-09-2011, 03:14 AM
  3. Ok i am lost
    By jaisan72980 in forum New To Java
    Replies: 3
    Last Post: 01-17-2011, 04:20 AM
  4. I'm lost :(
    By leonardjr in forum New To Java
    Replies: 8
    Last Post: 03-03-2009, 04:18 AM
  5. So Lost
    By kandt in forum New To Java
    Replies: 5
    Last Post: 12-13-2008, 09:55 PM

Posting Permissions

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