Friday, April 17, 2015

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;


}

}



No comments:

Post a Comment