The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Grade averaging program with array and multiple methods.

I am working on my home work assignment and I am getting compile errors that I don not understand. Can you take a look and expalin what I am doing wrong please?

Create a program to calculate the average of any number of grades input into the array by the user, up to 10 grades. The user gets to decide how many, and may have 2 grades or 6, or even 10, for example. Include at least 3 methods:

• main, which declares the grades array, calls the other methods, and writes ouput of the grades and their average.
• get_grades, which gets user input of integer grades (You may assume they are 0 through 100 and that at least one grade is entered).
• calculate_average, which uses the grades array and passes the average back to main.

Outside of the compile errors I do not understand how to add up all the grades (the data in the array) together to make a sumOfAllGrades? Can somebody explain that please?

/*Create a program to calculate the average of any number of grades input into the array by the user,
up to 10 grades. The user gets to decide how many, and may have 2 grades or 6, or even 10, for
example. Include at least 3 methods:
• main, which declares the grades array, calls the other methods, and writes ouput of the grades
and their average.
• get_grades, which gets user input of integer grades (You may assume they are 0 through 100 and
that at least one grade is entered).
• calculate_average, which uses the grades array and passes the average back to main. */
import java.io.*;
import java.text.*;
import java.util.*;
public class secondHW4attempt {
/*...This will define the size of the grades array, the size of the array is user defined.This is also a method....*/
public double howManyGrades () {
int howManyGrades;
String inputStr;
inputStr = JOptionPane.showInputDialog(null, "How many grades do you wish to average? ");
int howManyGrades = Integer.parseInt(inputStr);
return howManyGrades;
}
/*...End method howManyGrades method.............................................................*/
/*...With this method we get get passed variable howManyGrades so that it can define the size
of our array. We howManyGrades as a counter for our for_loop. Then each grade entered into
the array is added for the sumOfGrades...*/
public double gradesArray(){
double grades, averageOfGrades, sumOfGrades;
int howManyGrades;
String inputStr;
Scanner input = new Scanner(System.ini);
double average = (howManyGrades + grade)/ 3.0;
double[] grades = new double [howManyGrades];
for(int i = 0; i < grades.length; i++){
inputStr = JOptionPane.showInputDialog(null, "Enter a grade.");
int grades[i] = Integer.parseInt(inputStr);
/*???????????????????????????????????????????????????????????????????????????????????????????????
//sumOfGrades = grades
//How do I add each grade in the array togeather so I can get a sumOfGrades
????????????????????????????????????????????????????????????????????????????????????????????*/
return sumOfGrades;
}
/*...End method gradesArray .............................................................................*/
/*...This is the calculateAverage method were we divide howManyGrades by sumOfGrades for the
average and we output the average for the user...*/
public double calculateAverage(){
DecimalFormat df = new DecimalFormat("0.00");
double average;
average = sumOfGrades / howManyGrades;
if (howManyGrades != 0) {
average = (double) total/howManyGrades;
JOptionPane.showMessageDialog(null, "The average is " + df.format(average) + "\n");
} // end if
else
JOptionPane.showMessageDialog(null, "No grades were entered. ");
//return average;
}
/*...End method calculateAverage method.............................................................*/
public static void main(String[] args) {
workingHomeWork4 clerk = new workingHomeWork4();
clerk.howManyGrades( );
clerk.gradesArray( );
clerk.calculateAverage( );
} // end main
} //end class

/*Create a program to calculate the average of any number of grades input into the array by the user,
up to 10 grades. The user gets to decide how many, and may have 2 grades or 6, or even 10, for
example. Include at least 3 methods:
• main, which declares the grades array, calls the other methods, and writes ouput of the grades
and their average.
• get_grades, which gets user input of integer grades (You may assume they are 0 through 100 and
that at least one grade is entered).
• calculate_average, which uses the grades array and passes the average back to main. */
import java.io.*;
import java.text.*;
import java.util.*;
public class thirdHW4attempt {
/*...This will define the size of the grades array, the size of the array is user defined.This is also a method....*/
public double getGrades() {
int gradeCount;
String inputStr;
inputStr = JOptionPane.showInputDialog(null, "How many grades do you wish to average? ");
int gradeCount = Integer.parseInt(inputStr);
return int gradeCount;
}
/*...End method howManyGrades method.............................................................*/
/*...With this method we get get passed variable howManyGrades so that it can define the size
of our array. We howManyGrades as a counter for our for_loop. Then each grade entered into
the array is added for the sumOfGrades...*/
public double buildGradeArray(){
double grades, averageOfGrades, sumOfGrades;
int gradeCount;
String inputStr;
Scanner input = new Scanner(System.ini);
int[] grades = new grades[gradeCount];
for (int i = 0; i < gradeCount; i++)
{ inputStr = JOptionPane.showInputDialog(null, "Enter a grade.");
grades[i] = Integer.parseInt(inputStr); }
int sumOfGrades = 0;
for (int i = 0; i < grades.length; i++)
{ sumOfGrades += grades[i]; }
return sumOfGrades;
}
/*...End method gradesArray .............................................................................*/
/*...This is the calculateAverage method were we divide howManyGrades by sumOfGrades for the
average and we output the average for the user...*/
public double calculateAverage(){
DecimalFormat df = new DecimalFormat("0.00");
double average;
//double average = (gradeCount + grade)/ 3.0;
average = sumOfGrades / gradeCount;
if (gradeCount != 0) {
average = (double) total/gradeCount;
J OptionPane.showMessageDialog(null, "The average is " + df.format(average) + "\n");
} // end if
else
JOptionPane.showMessageDialog(null, "No grades were entered. ");
//return average;
}
/*...End method calculateAverage method.............................................................*/
public static void main(String[] args) throws IOException {
workingHomeWork4 clerk = new workingHomeWork4();
clerk.getGrades( );
clerk.buildGradeArray( );
int gradeCount = getGrades( grades );
clerk.calculateAverage( );
} // end main
} // end class

Re: Grade averaging program with array and multiple methods.

That's the reason why we are using static methods here: because the clerk object for all intensive purposes is only taking up space to allow us to call methods that act statically (aka. they don't differ from between different objects of the same type).

No, you will still need the return values. You pass parameters as "inputs", and the return value is the "output".