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?
Code :
/*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
Compile Errors
----jGRASP exec: javac -g C:\Documents and Settings\jeremy\Desktop\SCHOOL\CMIS 141\week 9\secondHW4atempt.java
secondHW4atempt.java:39: ']' expected
int grades[i] = Integer.parseInt(inputStr);
^
secondHW4atempt.java:39: illegal start of expression
int grades[i] = Integer.parseInt(inputStr);
^
secondHW4atempt.java:53: illegal start of expression
public double calculateAverage(){
^
secondHW4atempt.java:53: ';' expected
public double calculateAverage(){
^
secondHW4atempt.java:73: illegal start of expression
public static void main(String[] args) {
^
secondHW4atempt.java:73: illegal start of expression
public static void main(String[] args) {
^
secondHW4atempt.java:73: ';' expected
public static void main(String[] args) {
^
secondHW4atempt.java:73: '.class' expected
public static void main(String[] args) {
^
secondHW4atempt.java:73: ';' expected
public static void main(String[] args) {
^
secondHW4atempt.java:85: reached end of file while parsing
}
^
10 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Re: Grade averaging program with array and multiple methods.
You don't need to re-declare each int in the array grades[] (it's understood that they must be int's).
Code :
int[] grades = new grades[howManyGrades];
for (int i = 0; i < howManyGrades]; i++)
{
inputStr = JOptionPane.showInputDialog(null, "Enter a grade.");
grades[i] = Integer.parseInt(inputStr);
}
To sum up all the grades, just add up all the values inside the array.
Code :
int sumOfGrades = 0;
for (int i = 0; i < grades.length; i++)
{
sumOfGrades += grades[i];
}
Re: Grade averaging program with array and multiple methods.
I am still getting an error passing gradeCount out of my getGrades method, I do not get the error at all.
If you have any other suggjestions or comments please, make them. I am new to Java, the more comments made the more qestions get answered the better I understand.
ERROR
----jGRASP exec: javac -g C:\Documents and Settings\jeremy\Desktop\SCHOOL\CMIS 141\week 9\thirdHW4attempt.java
thirdHW4attempt.java:17: '.class' expected
return int gradeCount;
^
1 error
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Code :
/*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.
hmm.. it looks like all your methods are asking users for grades rather than actually passing along previously entered values.
To pass parameters to a method, put the parameter type and the name inside of the () in the method signature:
Code :
public double calculateAverage([b]double[] gradesList[/b])
{
// code
}
Also, from reading the comments at the header, it looks like your teacher wants this to be a static class, aka. put static in the signature of all your methods
Code :
public [b]static[/b] double getGrades()
This will eliminate the need to create an object to call the methods.
Re: Grade averaging program with array and multiple methods.
exuse me.. i dont want to end up in false .. or merely false, or maybe false knowledge
arguments,, and parameters ...
Code :
public static void newMethod(String parameters) // this is the receiving side
and
Code :
public static void usingThisMethod("arguments") // the passing side
i just notice something on what you say helloworld.. i just want to consult this thing..
Quote:
To pass parameters to a method, put the parameter type and the name inside of the () in the method signature:
do i make any sense?
and because recently, my proffesor and i was having a hard talk regarding with arguments and parameters....
Re: Grade averaging program with array and multiple methods.
Quote:
Originally Posted by
helloworld922
hmm.. it looks like all your methods are asking users for grades rather than actually passing along previously entered values.
To pass parameters to a method, put the parameter type and the name inside of the () in the method signature:
Code :
public double calculateAverage([b]double[] gradesList[/b])
{
// code
}
Also, from reading the comments at the header, it looks like your teacher wants this to be a static class, aka. put static in the signature of all your methods
Code :
public [b]static[/b] double getGrades()
This will eliminate the need to create an object to call the methods.
Should look like this then correct. What is in parentheses is getting passed on, do I still need the return?
Also If I amke all my methods Static I can't use clerk in main correct?
Code :
public double getGrades(double[] gradeCount) {
int gradeCount, howManyGrades;
String inputStr;
inputStr = JOptionPane.showInputDialog(null, "How many grades do you wish to average? ");
int gradeCount = Integer.parseInt(inputStr);
return gradeCount;
}
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".
So, you're methods would look like this:
Code :
public static double getGrades(){
while (true)
{
String inputStr = JOptionPane.showInputDialog(null, "Enter a grade: ");
try
{
return Integer.parseInt(inputStr);
}
catch(Exception e)
{}
}
}
public static void fillArray(double[] grades)
{
// technically I'm kind of cheating here by taking advantage of the fact that arrays pass by reference rather than value, but oh well
for(int i = 0; i < grades.length; i++)
{
grades[i] = getGrades();
}
}
public static void main(String[] args)
{
double[] grades;
int numOfGrades = Integer.parseInt(JOptionPane.showInputDialog(null, "How many grades do you wish to average? "));
grades = new double[numOfGrades];
fillArray(grades);
// you'll have to work out how to implement the average method
double average = getAverage(grades);
System.out.println("The average grade is " + average);
}
public static double getAverage(double[] grades)
{
// you implement this
}
Re: Grade averaging program with array and multiple methods.
Thank you very much for your time and patience hellowworld922. Am I even in the ball park?
Error's
----jGRASP exec: javac -g C:\Documents and Settings\jeremy\Desktop\SCHOOL\CMIS 141\week 9\seventhHW4attempt.java
seventhHW4attempt.java:35: ';' expected
return sumOfGrades
^
seventhHW4attempt.java:52: ';' expected
return average
^
2 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Code :
import java.io.*;
import java.text.*;
import java.util.*;
public class sixthHW4attempt {
public static double getGrades(){
while (true)
{
String inputStr = JOptionPane.showInputDialog(null, "Enter a grade: ");
try
{
return Integer.parseInt(inputStr);
}
catch(Exception e)
{}
}
}
public static void fillArray(double[] grades)
{
int sumOfGrades = 0;
for(int i = 0; i < grades.length; i++)
{
grades[i] = getGrades();
// added line
sumOfGrades += grades[i];
}
//added line
return sumOfGrades
}
public static void main(String[] args)
{
double[] grades;
int numOfGrades = Integer.parseInt(JOptionPane.showInputDialog(null, "How many grades do you wish to average? "));
grades = new double[numOfGrades];
fillArray(grades);
double average = getAverage(grades);
System.out.println("The average grade is " + average);
}
public static double getAverage(double[] grades)
{
average = sumOfGrades / grades;
return average
}
}
Re: Grade averaging program with array and multiple methods.
Sort of. Instead of computing the sumOfGrades in the fillArray method, compute it in the average method. Then divide by the of grades you have (hint: grades.length)
The compile error you're getting is because you forgot a ";" after the return statements: