dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Help accumulating test scores. data enters once but doesn't accumulate.

  1. #1
    Join Date
    Nov 2014
    Posts
    1

    Help accumulating test scores. data enters once but doesn't accumulate.

    I have the code working to an extent. I can enter the first test score and all the calculations run, but if I try to enter another test score and hit enter score button, I get no result. Any advice?
    package StudentScoresFrame;

    */
    public class StudentScoresFrame extends javax.swing.JFrame {

    /**
    * Creates new form StudentScoresFrame
    */
    public StudentScoresFrame() {
    initComponents();
    }
    // declare variables
    double average = 0.0;
    double best = 0.0;
    double scores = 0.0;
    double sum = 0.0;
    double count = 0.0;


    private static double scoreCount(double scores)
    {
    int count = 0;
    double num = 0.0;
    for(int i = 0; i <= num; i++)
    {
    count += 1;
    }
    return count;
    } // end
    private static double calculateAverage(double scores, double count)
    {

    double average = 0.0;
    double sum = 0.0;

    for(int i = 1; i <= count; i++)
    {
    sum += scores;
    average = sum / count;
    }
    return average;

    } // end
    private static double bestScore(double scores)
    {
    int best = 0;
    for(int i = 0; i < scores; i++){
    if(best >= scores)
    {
    best = (int) scores;
    }
    }
    return best;
    } // end
    /**
    * This method is called from within the constructor to initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is always
    * regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    testScoreLabel = new javax.swing.JLabel();
    numScoresLabel = new javax.swing.JLabel();
    averageScoreLabel = new javax.swing.JLabel();
    bestScoreLabel = new javax.swing.JLabel();
    testScoreTextField = new javax.swing.JTextField();
    numScoresTextField = new javax.swing.JTextField();
    averageScoreTextField = new javax.swing.JTextField();
    bestScoreTextField = new javax.swing.JTextField();
    enterButton = new javax.swing.JButton();
    clearButton = new javax.swing.JButton();
    exitButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    testScoreLabel.setText("Test Score:");

    numScoresLabel.setText("Number of Scores:");

    averageScoreLabel.setText("Average Score:");

    bestScoreLabel.setText("Best Score:");

    numScoresTextField.setEditable(false);
    numScoresTextField.setEnabled(false);
    numScoresTextField.setFocusable(false);

    averageScoreTextField.setEditable(false);
    averageScoreTextField.setEnabled(false);
    averageScoreTextField.setFocusable(false);
    averageScoreTextField.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    averageScoreTextFieldActionPerformed(evt);
    }
    });

    bestScoreTextField.setEditable(false);
    bestScoreTextField.setEnabled(false);
    bestScoreTextField.setFocusable(false);

    enterButton.setText("Enter Score");
    enterButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    enterButtonActionPerformed(evt);
    }
    });

    clearButton.setText("Clear");
    clearButton.setToolTipText("");
    clearButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    clearButtonActionPerformed(evt);
    }
    });

    exitButton.setText("Exit");
    exitButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    exitButtonActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
    .addComponent(enterButton)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(numScoresLabel)
    .addComponent(averageScoreLabel)
    .addComponent(bestScoreLabel)
    .addComponent(testScoreLabel)))
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(testScoreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
    .addComponent(averageScoreTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
    .addComponent(bestScoreTextField))
    .addGroup(layout.createSequentialGroup()
    .addComponent(clearButton)
    .addGap(18, 18, 18)
    .addComponent(exitButton))
    .addComponent(numScoresTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addContainerGap(128, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(29, 29, 29)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(testScoreLabel)
    .addComponent(testScoreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(numScoresLabel)
    .addComponent(numScoresTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(averageScoreLabel)
    .addComponent(averageScoreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(bestScoreLabel)
    .addComponent(bestScoreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
    .addGap(18, 18, 18)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(enterButton)
    .addComponent(clearButton)
    .addComponent(exitButton))
    .addContainerGap(86, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    System.exit(0);
    }

    private void averageScoreTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }

    private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:

    if (isValidData())
    {
    scores = Double.parseDouble(testScoreTextField.getText());


    count = scoreCount(scores);

    average = calculateAverage(scores, count);

    numScoresTextField.setText("" + count);
    averageScoreTextField.setText("" + average);

    best = bestScore(scores);
    bestScoreTextField.setText("" + best);

    } // end


    }
    private boolean isValidData()
    {
    SwingValidator sv = new SwingValidator();
    return
    sv.isPresent(testScoreTextField,
    "Test Score") &&
    sv.isDouble(testScoreTextField,
    "Test Score");
    } // end
    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:T
    testScoreTextField.setText("");
    numScoresTextField.setText("");
    averageScoreTextField.setText("");
    bestScoreTextField.setText("");

    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
    * For details see http://download.oracle.com/javase/tu...feel/plaf.html
    */
    try {
    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
    javax.swing.UIManager.setLookAndFeel(info.getClassName());
    break;
    }
    }
    } catch (ClassNotFoundException ex) {
    java.util.logging.Logger.getLogger(StudentScoresFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
    java.util.logging.Logger.getLogger(StudentScoresFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
    java.util.logging.Logger.getLogger(StudentScoresFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
    java.util.logging.Logger.getLogger(StudentScoresFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new StudentScoresFrame().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel averageScoreLabel;
    private javax.swing.JTextField averageScoreTextField;
    private javax.swing.JLabel bestScoreLabel;
    private javax.swing.JTextField bestScoreTextField;
    private javax.swing.JButton clearButton;
    private javax.swing.JButton enterButton;
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel numScoresLabel;
    private javax.swing.JTextField numScoresTextField;
    private javax.swing.JLabel testScoreLabel;
    private javax.swing.JTextField testScoreTextField;
    // End of variables declaration
    }

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,856

    Re: Help accumulating test scores. data enters once but doesn't accumulate.

    Please edit your post and wrap the code in code tags to make it easier to read.
    Norm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)