Can anyone write this or look at the code I wrote to tell me what I did wrong? I
ID: 3540331 • Letter: C
Question
Can anyone write this or look at the code I wrote to tell me what I did wrong? I am will to pay for a running program.
Java Program
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.
Remember to follow proper form and design in your response.
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class WeightedScores extends JFrame {
JTextField[] sc=new JTextField[4];
JTextField[] wt=new JTextField[4];
JLabel res;
JButton calc;
public WeightedScores() {
JPanel entry = new JPanel(new GridLayout(4,4));
calc = new JButton("Calculate");
for(int i=0;i<4;i++)
{
entry.add(new JLabel("Enter Test Score "+(i+1)+": "));
sc[i]=new JTextField(0);
entry.add(sc[i]);
entry.add(new JLabel("Enter Weight "+(i+1)+": "));
wt[i]=new JTextField(0);
entry.add(wt[i]);
}
calc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
double avg=0,s,w;
for(int i=0;i<4;i++)
{
s = Double.parseDouble(sc[i].getText());
w = Double.parseDouble(wt[i].getText());
avg=avg+ s*w;
}
res.setText("Weighted Average is: "+avg);
}
});
JPanel result=new JPanel();
res=new JLabel("Weighted Average is: ");
result.add(res);
add(entry, BorderLayout.NORTH );
add(calc, BorderLayout.CENTER );
add(result, BorderLayout.SOUTH );
pack();
}
public static void main(String[] args) {
WeightedScores frame = new WeightedScores();
frame.setTitle("Weighted Scores");
frame.setLocationRelativeTo(null );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
frame.setVisible(true);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.