Design a GUI program to find the weighted average of four test scores. The four
ID: 653300 • Letter: D
Question
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
How do add weights to the code? I know how to do the averages... my code is listed below. Who would I insert the weights for each test score?
import javax.swing.JOptionPane;
public class Problem {
public static void main(String[] args) {
// Pop up Windows ask the user for test scores
String test1 = JOptionPane.showInputDialog("Enter 1st Test Score"); //10% of grade - How do add this to the code ?
String test2 = JOptionPane.showInputDialog("Enter 2nd Test Score"); //15% of grade - How do add this to the code ?
String test3 = JOptionPane.showInputDialog("Enter 3rd Test Score"); //25% of grade - How do add this to the code ?
String test4 = JOptionPane.showInputDialog("Enter 4th Test Score"); //50% of grade - How do add this to the code ?
// Convert strings to numbers to get the average
int t1 = Integer.parseInt(test1);
int t2 = Integer.parseInt(test2);
int t3 = Integer.parseInt(test3);
int t4 = Integer.parseInt(test4);
// Add all four test scores and then divide by 4 to get the average.
int sum = t1 + t2 + t3 + t4 / 4;
// Display the average of all 4 test scores
JOptionPane.showMessageDialog(null, "Your Weighted Test Score Average is " +sum, "Score Average", JOptionPane.PLAIN_MESSAGE);
}
}
Explanation / Answer
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class testScores extends JFrame implements ActionListener
{
private JTextField test1;
private JTextField weight1;
private JTextField test2;
private JTextField weight2;
private JTextField test3;
private JTextField weight3;
private JTextField test4;
private JTextField weight4;
private JLabel result;
public int average = 0;
public testScores(){
setSize(300,200);
setTitle("Score Calculator");
setLocation(150,150);
Container contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(Color.WHITE);
test1 =new JTextField ("Test 1");
test1.setBounds(25,10,100, 20);
contentPane.add(test1);
weight1 =new JTextField ("Weight 1");
weight1.setBounds(150,10,100, 20);
contentPane.add(weight1);
test2 =new JTextField ("Test 2");
test2.setBounds(25,35,100, 20);
contentPane.add(test2);
weight2 =new JTextField ("Weight 2");
weight2.setBounds(150,35,100, 20);
contentPane.add(weight2);
test3 =new JTextField ("Test 3");
test3.setBounds(25,60,100, 20);
contentPane.add(test3);
weight3 =new JTextField ("Weight 3");
weight3.setBounds(150,60,100, 20);
contentPane.add(weight3);
test4 =new JTextField ("Test 4");
test4.setBounds(25,85,100, 20);
contentPane.add(test4);
weight4 =new JTextField ("Weight 4");
weight4.setBounds(150,85,100, 20);
contentPane.add(weight4);
JButton calculate = new JButton("Calculate");
calculate.setBounds(85, 110, 100, 20);
contentPane.add(calculate);
calculate.addActionListener(this);
result = new JLabel("");
result.setBounds(85,135,120, 25);
contentPane.add(result);
}
public static void main(String[] args)
{
testScores test = new testScores();
test.setVisible(true);
test.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent arg0)
{
calculateAverage();
}
public void calculateAverage()
{
int t1 = Integer.parseInt(test1.getText());
Double w1 = Double.parseDouble(weight1.getText());
int t2 = Integer.parseInt(test2.getText());
Double w2 = Double.parseDouble(weight2.getText());
int t3 = Integer.parseInt(test3.getText());
Double w3 = Double.parseDouble(weight3.getText());
int t4 = Integer.parseInt(test4.getText());
Double w4 = Double.parseDouble(weight4.getText());
average = (int) ((t1*w1) + (t2*w2) + (t3*w3) + (t4*w4));
result.setText(Integer.toString(average));
}
}