Java Program - Write a program that calculates grades

Write a program that allows the user to a series of numerical grades and then calculates an average. Entering an integer of value 0 ends the input and produces the result. Use two separate functions (methods) to 1) input data and calculate the total of all grades entered and 2)calculate the average.

I keep getting errors, int grades, etc is not a statement. This program won't compile. I'm a beginner programmer and could use some help in cleaning this up and making this compile. I have to use a for statement. Please take a look at this and let me know what you think. Thanks for the help in advance....

// File: AverageGrades.java// Name: Clifford Albanese// Project 2// Date: March 28, 2009import java.util.Scanner;public class AverageGrades {public static void main(String[] args){ scanner input = new Scanner ( System.in ); int double grade; int double sum = 0; int double gradeCounter = 0; int double grade = 0; // read the number of grades System.out.println( "Enter the number of grades: "); grade = input.nextInt();//calculate the total of all grades entered For ( gradeCounter = 1; grade != 0; i++ ) // read grade from the user System.out.println( "Enter grade(s):"); grade = input.nextInt(); // add the grade to the sum, and add 1 to count sum = sum + grade; //calculate the average int double average = 0; average = (double) sum / gradeCounter; System.out.println( "The grade average is " + average ); } // end Main} // end Class

I have declared 2 methods:
i) public double enterGrades ()
ii) public double aveGrades (double sum )

From the main program I created the object of the AverageGrades and called these 2 methods from the main code.
Work on the following code then you will understand more. :)

import java.util.Scanner;public class AverageGrades { int count; public double enterGrades () { Scanner input = new Scanner ( System.in ); double grade = 0; double sum = 0; int gradeCounter = 0; // Enter grades System.out.println( "Enter grades (Grade of 0 causes average calculation: "); grade = input.nextDouble(); //calculate the total of all grades entered for (gradeCounter = 0; grade != 0; gradeCounter++ ){ sum = sum + grade; System.out.println( "Enter grades (Grade of 0 causes average calculation: "); grade = input.nextDouble(); } count = gradeCounter; return sum; } public double aveGrades (double sum ) { //calculate the average double average = 0; average = (double) sum / count; System.out.println( "The grade average is " + average ); return average; } public static void main(String[] args){ AverageGrades aGrades = new AverageGrades(); double sum = aGrades.enterGrades(); System.out.println("Sum : "+sum); double average = aGrades.aveGrades(sum); System.out.println("Count : "+aGrades.count); System.out.println("Average : "+average); } // end Main} // end Class

scanner input = new Scanner ( System.in ); ---> Scanner not scanner.

int double grade;
int double sum = 0;
int double gradeCounter = 0;
int double grade = 0;
----> you have to declare either int type or double. not both.

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

I have corrected the problem to overcome the COMPILATION problem. But it is not the full logic what you are looking for.
Work on the following program and ask more questions to complete your homework.

// File: AverageGrades.java// Name: Clifford Albanese// Project 2// Date: March 28, 2009import java.util.Scanner;public class AverageGrades {public static void main(String[] args){ Scanner input = new Scanner ( System.in ); double grade; double sum = 0; int gradeCounter = 0; // read the number of grades System.out.println( "Enter the number of grades: "); grade = input.nextInt();//calculate the total of all grades entered for (gradeCounter = 1; grade != 0; gradeCounter++ ){ // read grade from the user System.out.println( "Enter grade(s):"); grade = input.nextInt(); // add the grade to the sum, and add 1 to count sum = sum + grade; //calculate the average double average = 0; average = (double) sum / gradeCounter; System.out.println( "The grade average is " + average ); System.out.println( "Enter the number of grades: "); grade = input.nextInt(); } } // end Main} // end Class

I've made the corrections you suggested but the For statment is erroring out on compilation. I'm not sure what I'm missing. The only requirements for this is to have two methods, input and calculate the total grades. And calculate the average. I believe I have this but I must be missing something that is obvious.

Take the above code that I have copy pasted above. It is compiling without any error.

0

calbanese13Author Commented:

You are right and it did compile but I did make a mistake in my understanding. The sentinel value will make it calculate the average if a "0" is entered. I did not incorporate this into my problem. I have the statement:
for (gradeCounter = 1; grade != 0; gradeCounter++ ){

But it seems that I still enter a predefined value of grades. I want to omit this and enter as many grades as possible until a 0 is entered. Once this occurs, it should go to calculating the average.
Do I enter and "if-else" statement after the "for" statement?
if (grade ! = 0)
system out print ( Enter grade ");
else
System.out.println( "The grade average is " + average );

Please let me know if this is right. I know I'm close to having this totally finished. You have definitely helped me quite a bit.
Thanks again!
r/
Cliff

// File: AverageGrades.java// Name: Clifford Albanese// Project 2// Date: March 28, 2009import java.util.Scanner;public class AverageGrades {public static void main(String[] args){ Scanner input = new Scanner ( System.in ); double grade; double sum = 0; int gradeCounter = 0; // read the number of grades //System.out.println( "Enter the number of grades: "); System.out.println( "Enter grade(s):"); grade = input.nextInt(); //calculate the total of all grades entered while(grade!=0){ sum = sum + grade; gradeCounter++; // read grade from the user System.out.println( "Enter grade. Type 0 to exit:"); grade = input.nextInt(); } //calculate the average double average = 0; average = (double) sum / gradeCounter; System.out.println( "The grade Sum is " + sum ); System.out.println( "The grade average is " + average );} // end Main} // end Class

I have to use a "for" loop for this excercise. I fixed a couple of things but have induced something else. I can now enter grades in but it prints out an average line after entering the second grade. The average is not correct either. When I enter 0, it does give me the correct average. I have to get the program to calculate the average of all grades entered after entering 0. Here is what I have so far after modifying your suggestions which really helped me a bunch.

Thanks again for looking at this with me.
r/
Cliff

import java.util.Scanner;

public class AverageGrades {

public static void main(String[] args){

Scanner input = new Scanner ( System.in );

double grade;
double sum = 0;
int gradeCounter = 0;

// Enter grades
System.out.println( "Enter grades (Grade of 0 causes average calculation: ");
grade = input.nextInt();

//calculate the total of all grades entered

for (gradeCounter = 1; grade != 0; gradeCounter++ ){
sum = sum + grade;
System.out.println( "Enter grades (Grade of 0 causes average calculation: ");
grade = input.nextInt();

//calculate the average

double average = 0;

average = (double) sum / gradeCounter;
System.out.println( "The grade average is " + average );
}
} // end Main
} // end Class

For last grade(0 exit) also you are incrementing the counter. Hence you are getting the wrong average value. Hence decrement 1 for this.

Output:
Enter grades (Grade of 0 causes average calculation:
10
Enter grades (Grade of 0 causes average calculation:
20
Enter grades (Grade of 0 causes average calculation:
0
Counter :2
The grade sum is 30.0
The grade average is 15.0

import java.util.Scanner;public class AverageGrades {public static void main(String[] args){ Scanner input = new Scanner ( System.in ); double grade; double sum = 0; int gradeCounter = 0; // Enter grades System.out.println( "Enter grades (Grade of 0 causes average calculation: "); grade = input.nextInt();//calculate the total of all grades entered for (gradeCounter = 1; grade != 0; gradeCounter++ ){ sum = sum + grade; System.out.println( "Enter grades (Grade of 0 causes average calculation: "); grade = input.nextInt(); } gradeCounter--; //calculate the average double average = 0; average = (double) sum / gradeCounter; System.out.println( "Counter :" + gradeCounter ); System.out.println( "The grade sum is " + sum ); System.out.println( "The grade average is " + average ); } // end Main} // end Class

Great advice on your part all the way through this problem. I really learned a great deal. Please interpret something for me in the initial problem. It says the following; "Write a program that allows the user to a series of numerical grades and then calculates an average. Entering an integer of value 0 ends the input and produces the result. Use two separate functions (methods) to 1) input data and calculate the total of all grades entered and 2)calculate the average." Now my program works but it also calculate the last 0, the sentinel value, in the average. While you read this, should the 0 be calculated or should the program average the grades, minus the 0 value? What is your interpretation.

Otherwise, everything works fine. Thanks so much!
r/
Cliff

main method is required for the JVM to understand your code and start the execution. So don't remove main method.

I hope you understand everything and the actual posted problem is solved. All the Best :)

0

calbanese13Author Commented:

I guess I'm a bit lost although I understand there should be two methods. When I entered these methods, I get multiple errors in there names like illegal start of an expression and ; expected after enterGrades and aveGrades. Below is what I implemented but it did not work. What am I not doing correctly? Attached are the errors I encountered.
r/
Cliff

I suggest you to read the material of how to 'write methods' & 'call the methods' in java....basically passing the arguments, get the return values from the functions.

0

calbanese13Author Commented:

This was a great learning process, although I have to review using multiple methods. Great job on walking me through much of this. My initial code was changed quite a bit and became more detailed then expected. Especially what happened with all the main values but I do understand the calls for sum and average. Thanks for the great help in stepping me through this. I'm sure I'll be back for more assistance. Great job!
V/r
Cliff

i) public double enterGrades ()

ii) public double aveGrades (double sum )

From the main program I created the object of the AverageGrades and called these 2 methods from the main code.

Work on the following code then you will understand more. :)

Open in new window