I am trying to teach myself Java and this project that i\'m trying to work throu
ID: 3622438 • Letter: I
Question
I am trying to teach myself Java and this project that i'm trying to work through is giving me some trouble...
-The user enters test scores one at a time and then clicks the Enter Score button.
-For each entered score, the application adds one to the number of scores, calculates the average score, and determines what the best score is so far. Then it displays the number of scores, average score, and best score in three disabled text fields.
-The user can click a 'Clear' button to reset everything to zero.
I also need to learn how to add data validation to catch exceptions when the user enters invalid data.
Any help with this code would be greatly appreciated!!
Here is my code so far....whenever I run the program it crashes when I enter the first score....
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class StudentScoresApp
{
public static void main(String[] args)
{
JFrame frame = new StudentScoresFrame();
frame.setVisible(true);
}
}
class StudentScoresFrame extends JFrame
{
public StudentScoresFrame()
{
setTitle("Test Scores");
setSize(250,210);
centerWindow(this);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new StudentScoresPanel();
add(panel);
}
private void centerWindow(Window w)
{
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
setLocation((d.width-w.getWidth())/2, (d.height-w.getHeight())/2);
}
}
class StudentScoresPanel extends JPanel implements ActionListener
{
private JButton scoreButton, exitButton, clearButton;
private JLabel testLabel, numberLabel, averageLabel, bestLabel, worstLabel;
private JTextField testField, numberField, averageField, bestField, worstField;
public StudentScoresPanel()
{
setLayout(new BorderLayout());
JPanel displayPanel = new JPanel();
displayPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
testLabel = new JLabel("Test score:");
displayPanel.add(testLabel);
testField = new JTextField(10);
displayPanel.add(testField);
numberLabel = new JLabel("Number of scores:");
displayPanel.add(numberLabel);
numberField = new JTextField(10);
numberField.setEditable(false);
numberField.setFocusable(false);
displayPanel.add(numberField);
averageLabel = new JLabel("Average score:");
displayPanel.add(averageLabel);
averageField = new JTextField(10);
averageField.setEditable(false);
averageField.setFocusable(false);
displayPanel.add(averageField);
bestLabel = new JLabel("Best score:");
displayPanel.add(bestLabel);
bestField = new JTextField(10);
bestField.setEditable(false);
bestField.setFocusable(false);
displayPanel.add(bestField);
worstLabel = new JLabel("Worst score:");
displayPanel.add(worstLabel);
worstField = new JTextField(10);
worstField.setEditable(false);
worstField.setFocusable(false);
displayPanel.add(worstField);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
scoreButton = new JButton("Enter Score");
scoreButton.addActionListener(this);
buttonPanel.add(scoreButton);
exitButton = new JButton("Exit");
exitButton.addActionListener(this);
buttonPanel.add(exitButton);
clearButton = new JButton("Clear");
clearButton.addActionListener(this);
buttonPanel.add(clearButton);
add(buttonPanel, BorderLayout.SOUTH);
add(displayPanel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == exitButton)
System.exit(0);
else if (source == scoreButton)
{
double testScore = Double.parseDouble(testField.getText());
double numberValue = calculateScores(testScore);
double averageValue = calculateScores(testScore);
double bestValue = calculateScores(testScore);
double worstValue = calculateScores(testScore);
NumberFormat output = NumberFormat.getInstance();
output.setMaximumFractionDigits(2);
output.setMinimumFractionDigits(2);
averageField.setText(Double.toString(averageValue));
numberField.setText(Double.toString(numberValue));
bestField.setText(Double.toString(bestValue));
worstField.setText(Double.toString(worstValue));
}
}
private double calculateScores(double testScore)
{
double[] scores = null;
double sum = 0.0;
double max = scores[0];
double min = scores[0];
double bestValue = 0.0;
double worstValue = 0.0;
double averageValue = 0.0;
for (int i = 0; i < scores.length; i++)
{
sum += scores[i];
double numberValue = i;
if( scores[i] > max)
{
max = scores[i];
bestValue = i;
}
if (scores[i] < min)
{
min = scores[i];
worstValue = i;
}
}
averageValue = sum/scores.length;
return averageValue;
}
}
Explanation / Answer
I don't know too much about JOptionPane, but if I were to guess, I would say there is something wrong with using jlabels. If you want me to write it and compare, PM and ill get that for you. ***Keep in mind, it wouldn't be as fancy as yours, but it would get the job done.***
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.