I need to make this program GradeCalculator.java, and I can't figure out what is wrong with it. I'm really struggling, could someone please help! Thanks!!
In the GradeCalculator.java program the user clicks the Enter button.
The first input is a JOptionPane that asks the user, “Number of score to be entered?”
scoreAmount = JOptionPane.showInputDialog("Number of scores to be entered?"); numberOfScores = Integer.parseInt(scoreAmount);
When the OK button is clicked another JOptionPane opens asking the user to enter a score (integer) and the JOptionPane will be repeated depending in the value entered for the “Number of score to be entered” (for loop).
The values entered are stored in an accumulator: accumulatorVariable += scoreVariable
The += adds the current value of the scoreVariable to the value already stored in the accumulatorVariable. When the last number is entered (end of for loop) a method calls calculateAverageScore();
The calculateAverageScore method determines the letter score using the ‘if block’:
averageScore > 89, letter score is A
averageScore > 79, letter score is B
averageScore > 69, letter score is C
averageScore > 64, letter score is D
when the averageScore values is below 64 the default letter score is an F
//import statements
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
public class GradeCalculator extends JFrame
{
//declaration section
Color white = new Color(255, 255, 255);
Color black = new Color(0, 0, 0);
JLabel gradeLetterJLabel;
JTextField gradeLetterJTextField;
JTextField clickEnterJLabel;
JButton enterJButton;
JButton clearJButton;
JButton closeJButton;
int numberOfScores;
int scoreAmount;
int averageScore;
String gradeLetterLetter;
public GradeCalculator()
{
createUserInterface();
}
public void createUserInterface()
{
Container contentPane = getContentPane();
contentPane.setBackground(white);
contentPane.setLayout(null);
gradeLetterJLabel = new JLabel();
gradeLetterJLabel.setBounds(50, 50, 120, 20);
gradeLetterJLabel.setFont(new Font("Default", Font.PLAIN, 12));
gradeLetterJLabel.setText("Grade");
gradeLetterJLabel.setForeground(black);
gradeLetterJLabel.setHorizontalAlignment(JLabel.RIGHT);
contentPane.add(gradeLetterJLabel);
gradeLetterJTextField = new JTextField();
gradeLetterJTextField.setBounds(200, 50, 50, 20);
gradeLetterJTextField.setFont(new Font("Default", Font.PLAIN, 12));
gradeLetterJTextField.setHorizontalAlignment(JTextField.CENTER);
gradeLetterJTextField.setForeground(black);
gradeLetterJTextField.setBackground(white);
gradeLetterJTextField.setEditable(false);
contentPane.add(gradeLetterJTextField);
clickEnterJLabel.setBounds(50, 50, 120, 20);
clickEnterJLabel.setFont(new Font("Default", Font.PLAIN, 12));
clickEnterJLabel.setText("Click Enter To Start!");
clickEnterJLabel.setForeground(black);
clickEnterJLabel.setHorizontalAlignment(JLabel.CENTER);
contentPane.add(clickEnterJLabel);
enterJButton = new JButton();
enterJButton.setBounds(25, 300, 100, 20);
enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
enterJButton.setText("Enter");
enterJButton.setForeground(black);
enterJButton.setBackground(white);
contentPane.add(enterJButton);
enterJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
enterJButtonActionPerformed(event);
}
}
);
clearJButton = new JButton();
clearJButton.setBounds(150, 300, 100, 20);
clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
clearJButton.setText("Clear");
clearJButton.setForeground(black);
clearJButton.setBackground(white);
contentPane.add(clearJButton);
clearJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
clearJButtonActionPerformed(event);
}
}
);
closeJButton = new JButton();
closeJButton.setBounds(275, 300, 100, 20);
closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
closeJButton.setText("Close");
closeJButton.setForeground(black);
closeJButton.setBackground(white);
contentPane.add(closeJButton);
closeJButton.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
closeJButtonActionPerformed(event);
}
}
);
setTitle("Grade Calculator");
setSize(400, 400);
setVisible(true);
}
public static void main(String[] args)
{
GradeCalculator application = new GradeCalculator();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void enterJButtonActionPerformed(ActionEvent event)
{
//number of scores
scoreAmount = JOptionPane.showInputDialog("Number of scores to be entered?");
numberOfScores = Integer.parseInt(scoreAmount);
button = new JButton("OK");
JOptionPane.add(button);
button.addActionListener(this);
button = new JButton("Cancel");
JOptionPane.add(button);
button.addActionListener(this);
}
//FOR Loop for enter score box.
public void getAccumulatorVariable()
{
for (int numberOfScores; numberOfScores > 0; JOptionPane.showInputDialog("Enter Score")) {
accumulatorVariable += scoreVariable;
button = new JButton("OK");
JOptionPane.add(button);
button.addActionListener(this);
button = new JButton("Cancel");
JOptionPane.add(button);
button.addActionListener(this);
}
//calculate average score
public void calculateAverageScore()
{
averageScore = accumulatorVariable / numberofScores;
}
//find Grade Letter
public void calculateGrade()
{
if (averageScore > 89)
{
gradeLetterJTextField.setText("A");
}
if (averageScore > 79)
{
gradeLetterJTextField.setText("B");
}
if (averageScore > 69)
{
gradeLetterJTextField.setText("C");
}
if (averageScore > 64)
{
gradeLetterJTextField.setText("D");
}
if (averageScore <= 64)
{
gradeLetterJTextField.setText("F");
}
}
public void clearJButtonActionPerformed(ActionEvent event)
{
gradeLetterJTextField.setText("");
gradeLetterJTextField.requestFocusInWindow();
ticketPriceJTextField.setText("");
admissionJTextField.setText("");
}
public void closeJButtonActionPerformed(ActionEvent event)
{
System.exit(0);
}
}