3 Replies - 758 Views - Last Post: 12 December 2013 - 09:03 PM Rate Topic: -----

#1 MLIAkira   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-December 13

How can I implement these get methods into my program?

Posted 10 December 2013 - 04:27 PM

This is a JFrame form by the way. I'm writing a program to read up to (but not more) 25 test grades, then report the number of grades entered; and compute the arithmetic mean (average) and standard deviation of the grades. I have a message box pop up for the user to enter the grade and it keeps continuing until the user clicks cancel. The program keeps track of how many grades is entered and puts them into an array.

Here are my get methods:
  public double getAverage(double[] gradeArray, int numElem) {

        double sum = 0;

        for (int i = 0; i < numElem; i++) {
            sum = sum + gradeArray[i];
        }

        return (sum / numElem);
    }

    public double getStdDev(double[] gradeArray, int numElem, double average) {

        double sum = 0;

        for (int i = 0; i < numElem; i++) {
            sum = sum + Math.pow((gradeArray[i] - average), 2);
        }

        return Math.sqrt(sum / numElem);
    }


I don't know how I can make use of these in my program. Here is my whole code

package my.meancalculator;

import javax.swing.JOptionPane;
import javax.swing.JFrame;

/**
 *
 * @author Akira
 */
public class MeanCalcUI extends javax.swing.JFrame {

    private double average;
    private double stdDeviation;
    public double[] gradeArray;
    public int numElem;
    public double sum;
    public int i;
    public double numGrades;

    public MeanCalcUI() {
        initComponents();
    }

    public double getAverage(double[] gradeArray, int numElem) {

        double sum = 0;

        for (int i = 0; i < numElem; i++) {
            sum = sum + gradeArray[i];
        }

        return (sum / numElem);
    }

    public double getStdDev(double[] gradeArray, int numElem, double average) {

        double sum = 0;

        for (int i = 0; i < numElem; i++) {
            sum = sum + Math.pow((gradeArray[i] - average), 2);
        }

        return Math.sqrt(sum / numElem);
    }

    /**
     * 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")

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {                                        
        System.exit(0);
    }                                       

    private void btnEnterGradesActionPerformed(java.awt.event.ActionEvent evt) {                                               


        gradeArray = new double[25];
        JFrame frame = new JFrame();
        boolean enterGrades = true;

        while (enterGrades) {
            try {
                String gradeInput = JOptionPane.showInputDialog(frame,
                        "Enter Grade",
                        "Enter Grade",
                        JOptionPane.PLAIN_MESSAGE);

                if ((gradeInput != null) && (gradeInput.length() > 0)) {

                    gradeArray[i] = Double.parseDouble(gradeInput);

                    average = getAverage;  

                    numElem = numGrades + 1;  //right here I know it doesn't work but i have no idea on how to make it the total of the grades entered.  numElem is what the sum is getting divided by to find the average

                    txtNumGrades.setText((numGrades) + "");

                    txtMean.setText(average);

                    txtStdDeviation.setText(stdDeviation);
                } else {
                    enterGrades = false;
                }
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(frame,
                        "Your input must be numeric!",
                        "Bad Data!",
                        JOptionPane.ERROR_MESSAGE);

            }


        }


    }       


Instead of creating a new thread I wanted to ask two more questions about my program. 1. How can I make numElem work? numElem is the total of the grades entered and I must divide it by the sum to get the average. 2. How can I make another catch block to catch if the user enters more than 25 grades? I've tried searching all oover the interent but I can't find anything. Can you guys help me out?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: How can I implement these get methods into my program?

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12406
  • View blog
  • Posts: 45,547
  • Joined: 27-December 08

Re: How can I implement these get methods into my program?

Posted 10 December 2013 - 05:00 PM

Why are you instantiating a JFrame in your actionPerformed() method JFrame frame = new JFrame();? Your class is already a JFrame. This is unnecessary.

Quote

I don't know how I can make use of these in my program. Here is my whole code

Invoke them in your actionPerformed() method.
Was This Post Helpful? 0
  • +
  • -

#3 Bradsta   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 12-December 13

Re: How can I implement these get methods into my program?

Posted 12 December 2013 - 08:12 PM

Also you can get rid of int numElements, and use .length on your array variable in the method.

Instead of doing sum = sum + gradeArray[i];

You could do this.

public double getAverage(double[] gradeArray) {

      double sum = 0.00;

      for (int i = 0; i < gradeArray.length; i++) {
          sum += gradeArray[i];
      }
      return (sum / gradeArray.length);

  }


Was This Post Helpful? 0
  • +
  • -

#4 x68zeppelin80x   User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: How can I implement these get methods into my program?

Posted 12 December 2013 - 09:03 PM

You can either separate the catch blocks into consecutive blocks or group them together:

try {
	// Something...
} catch (IllegalArgumentException e) {
	// Catch illegal arguments.
} catch (RuntimeException e) {
	// Catch unchecked type errors. 
} catch (Exception e) {
	// Catch all remaining errors. Use sparingly...
} finally {
	// This always executes regardless of hitting a catch.
}	


try {
	// Something...
} catch (IllegalArgumentException | RuntimeException | Exception e) {
	// Catch multiple arguments. Note: This will not compile because
	// each consecutive Exception will catch the previous.
	// Explanation:
	// - IllegalArgumentException will by caught by RuntimeException
	// - RuntimeException will by caught by Exception
	e.printStackTrace();
} finally {
	// This always executes regardless of hitting a catch.
}	


Here is an example of adding numbers forever until an out of bounds exception occurs:

public static void main(String[] args) {
	double[] grades = new double[25];
	double randomConstant = 100d / (grades.length - 1);
	int currentIndex = 0;
		
	try {
		while(true) {
			grades[currentIndex] = (currentIndex * randomConstant) / 100d;
			currentIndex++;
		}
	} catch (IndexOutOfBoundsException e) {
		System.out.printf("Index of `%d` exceeds array size of `%d`! Done....\n", currentIndex, grades.length);
	} finally {
		for (currentIndex = 0; currentIndex < grades.length; currentIndex++) {
			System.out.printf("%.2f, ", grades[currentIndex]);
		}
	}
}


Output:

Index of `25` exceeds array size of `25`! Done....
0.00, 0.04, 0.08, 0.13, 0.17, 0.21, 0.25, 0.29, 0.33, 0.38, 0.42, 0.46, 0.50, 0.54, 0.58, 0.63, 0.67, 0.71, 0.75, 0.79, 0.83, 0.88, 0.92, 0.96, 1.00, 

This post has been edited by x68zeppelin80x: 12 December 2013 - 09:17 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1