Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java, design a GUI program to find the weighted averge of four test scores. The four test scores and their respective weights are given

In Java, design a GUI program to find the weighted averge of four test scores. The four test scores and their respective weights are given in the following format: testcore1 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 my code so far :

package ch6exercise;

import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Ch6Exercise extends JFrame { private JLabel jlblTestScore1, jlblTestScore2, jlblTestScore3, jlblTestScore4; private JLabel jlblWeight1, jlblWeight2, jlblWeight3, jlblWeight4; private Jlabel jlblResultMessage; private JTextField jtfTestScore1, jtfTestScore2, jtfTestScore3, jtfTestScore4; private JTextField jtfWeight1, jtfWeight2, jtfWeight3, jtfWeight4; private JTextField jtfResult; private JButton jbtnCalculate, jbtnExit; private static final int WIDTH = 500; private static final int HEIGHT = 200; // public Ch6Exercise() { setTitle("Weighted Average"); // get content pane Container ctnrCalc = getContentPane(); // set grid ctnrCalc.setLayout(new GridLayout(5,4)); // create labels jlblTestScore1 = new JLabel("TestScore1: ", SwingConstants.RIGHT); JLabel jiblTestScore2 = new JLabel("TestScore2: ", SwingConstants.RIGHT); JLabel jiblTestScore3 = new JLabel("Testscore3: ", SwingConstants.RIGHT); JLabel jiblTestScore4 = new JLabel ("Testscore4: ", SwingConstants.RIGHT); JLabel jiblWeight1 = new JLabel ("Weight1: ", SwingConstants.RIGHT); JLabel jiblWeight2 = new JLabel ("Weight2: ", SwingConstants.RIGHT); jLabel jiblWeight3 = new jLabel ("Weight3: ", SwingConstants.RIGHT); JLabel jiblWeight4 = new JLabel ("Weight4: ", SwingConstants.RIGHT); JLabel jiblResultMessage = new JLabel ("Weighted Average: ", SwingConstants.RIGHT); // creating the text fields jtfTestScore1 = new JTextField(); jtfTestScore2 = new JTextField(); jtfTestScore3 = new JTextField(); jtfTestScore4 = new JTextField(); jtfWeight1 = new JTextField(); jtfWeight2 = new JTextField(); jtfWeight3 = new JTextField (); jtfWeight4 = new JTextField (); jtfResult = new JTextField(); // creating buttons jbtnCalculate = new JButton("Calculate"); jbtnExit = new JButton("Exit"); // adding all components ctnrCalc.add(jiblTestScore1); ctnrCalc.add(jtfTestScore1); ctnr.Calc.add(jiblWeight1); ctnr.Calc.add(jtfWeight1); ctnr.Calc.add(jiblTestScore2); ctnr.Calc.add(jtfTestscore2); ctnr.Calc.add(jibWeight2); ctnr.Calc.add(jtfWeight2); ctnr.Calc.add(jiblTestScore3); ctnr.Calc.add(jtfTestScore3); ctnr.Calc.add(jibWeight3); ctnr.Calc.add(jtfWeight3); ctnr.Calc.add(jiblTestScore4); ctnr.Calc.add(jtfTestScore4); ctnr.Calc.add(jibWeight4); ctnr.Calc.add(jtfWeight4); ctnrCalc.add(jbtnCalculate); ctnrCalc.add(jbtnExit); ctnrCalc.add(jiblResultMessage); ctnrCalc.add(jtfResult); // register buttons jbtnCalculate.addActionListener ( new CalculateListener()); jbtn.Exit.addActionListener(new ExitListener()); // set dimensions setSize(WIDTH, HEIGHT); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); } //end of default constructor //calculate inner class implemantation private class CalculateListener implements ActionListener { // public void actionPerformed(ActionEvent ae) { // local variables double score1, score2, score3, score4; double weight1, weight2, weight3, weight4; double totalGrade, totalWeight, weightedAverage; /* get the test scores */ score1 = Double.parseDouble(jtfTestscore1.getText()); weight1 = Double.parseDouble(jtfWeight1.getText()); score2 = Double.parseDouble(jtfTestscore2.getText()); weight2 = Double.parseDouble(jtfWeight2.getText()); score3 = Double.parseDouble(jtfTestscore3.getText()); weight3 = Double.parseDouble(jtfWeight3.getText()); score4 = Double.parseDouble(jtfTestscore4.getText()); weight4 = Double.parseDouble(jtfWeight4.getText()); // calculate sums totalGrade = score1 * weight1 + score2 * weight2 + score3 * weight3 + score4 * weight4; // calculate sum of weights totalWeight = weight1 + weight2 + weight3 + weight4; // calculate the average weightedAverage = totalGrade / totalWeight; jtfResult.setText("" + weightedAverage); } } // exit listener private class ExitListener implements ActionListener { public void actionPerformed (Action event e) } System.exit(0); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions