Using jGrasp and the Software Development Kit, write a program in response to th
ID: 3568992 • Letter: U
Question
Using jGrasp and the Software Development Kit, write a program 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.
Remember to follow proper form and design in your response.
Submit your compiled Java code for this assignment, compress the .java file into a single .zip file.
Explanation / Answer
Java Program to find the weighted average of four test scores:
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Evaluate_7 extends JFrame
{
private JLabel blankoneL, blanktwoL, blankthreeL,
scoreL, weightL, oneL, twoL, threeL,fourL,averageL;
private JTextField oneTF, twoTF, threeTF, fourTF, woneTF, wtwoTF,
wthreeTF, wfourTF, averageTF;
private JButton submitB, resetB, EXITB;
private SubmitButtonHandler sbHandler;
private ResetButtonHandler
rbHandler;
private EXITButtonHandler ebHandler;
indicate the size of the window
private static final int WIDTH = 500;
private static final int HEIGHT = 400;
public Evaluate_7()
{
Create labels
blankoneL = new JLabel("");
blanktwoL = new JLabel("");
blankthreeL = new JLabel("");
scoreL = new JLabel("Score", SwingConstants.CENTER); swingConstants." " tells the program how can i align the text in the window
weightL = new JLabel("Weight", SwingConstants.CENTER);
JLabel("Test Score One: ", SwingConstants.RIGHT);
twoL = new JLabel("Test Score Two: ", SwingConstants.RIGHT);
threeL = new JLabel("Test Score Three: ", SwingConstants.RIGHT);
fourL = new JLabel("Test Score Four: ", SwingConstants.RIGHT);
averageL = new JLabel("Weighted Average: ", SwingConstants.RIGHT);
Create textfields
JTextField(5);
twoTF = new JTextField(5);
threeTF = new JTextField(5);
fourTF = new JTextField(5);
woneTF = new JTextField(5);
wtwoTF = new JTextField(5);
wthreeTF = new JTextField(5);
wfourTF = new JTextField(5);
averageTF = new JTextField(5);
Create Submit Button
submitB = new JButton("Submit");
sbHandler = new SubmitButtonHandler();
submitB.addActionListener(sbHandler);
Create Reset Button
resetB = new JButton("Reset");
rbHandler = new ResetButtonHandler();
resetB.addActionListener(rbHandler);
Create EXIT Button
EXITB = new JButton("EXIT");
ebHandler = new EXITButtonHandler();
EXITB.addActionListener(ebHandler);
Set the title of the window
setTitle("Evaluate 6 IT145");
Get the container
Container pane = getContentPane();
Set the layout
pane.setLayout(new GridLayout(7,3));
Place the components in the pane
pane$PHX$93$PHX$blankoneL);
pane$PHX$93$PHX$scoreL);
pane$PHX$93$PHX$weightL);
pane$PHX$93$PHX$oneL);
pane$PHX$93$PHX$oneTF);
pane$PHX$93$PHX$woneTF);
pane$PHX$93$PHX$twoL);
pane$PHX$93$PHX$twoTF);
pane$PHX$93$PHX$wtwoTF);
pane$PHX$93$PHX$threeL);
pane$PHX$93$PHX$threeTF);
pane$PHX$93$PHX$wthreeTF);
pane$PHX$93$PHX$fourL);
pane$PHX$93$PHX$fourTF);
pane$PHX$93$PHX$wfourTF);
pane$PHX$93$PHX$averageL);
pane$PHX$93$PHX$averageTF);
pane$PHX$93$PHX$blanktwoL);
pane$PHX$93$PHX$submitB);
pane$PHX$93$PHX$resetB);
pane$PHX$93$PHX$EXITB);
set the size of the window and display it
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLoperating systemE);
}
private class SubmitButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
private class ResetButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double one, wone, two, wtwo,
three, wthree, four, wfour, ave;
DecimalFormat twoDigits = new DecimalFormat("0.00");
>
wone = Double.parseDouble(woneTF.getText());
two = Double.parseDouble(twoTF.getText());
wtwo = Double.parseDouble(wtwoTF.getText());
three = Double.parseDouble(threeTF.getText());
wthree = Double.parseDouble(wthreeTF.getText());
four = Double.parseDouble(fourTF.getText());
wfour = Double.parseDouble(wfourTF.getText());
ave = (one * wone + two * wtwo + three * wthree
+ four * wfour) (wone+wtwo+wthree+wfour);
averageTF.setText(""+ twoDigits.format(ave));
}
}
private class EXITButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.EXIT(0);
}
}
public static void main(String[] args)
{
Evaluate_7 wAveObject = new Evaluate_7();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.