Solved

Java Program - Write a program that calculates grades

Posted on 2009-03-29
23
1,195 Views
Last Modified: 2012-05-06
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, 2009
 

import 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

Open in new window

0
Comment
Question by:calbanese13
  • 15
  • 8
23 Comments
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
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.
0
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
For ( gradeCounter = 1; grade != 0; i++ )
---> for (int  gradeCounter = 1; grade != 0; i++ )
0
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
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, 2009

 

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;

     

    // 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

Open in new window

0
 

Author Comment

by:calbanese13
Comment Utility
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.  
0
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
Take the above code that I have copy pasted above. It is compiling without any error.
0
 

Author Comment

by:calbanese13
Comment Utility
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
0
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
As you are not sure, how many values user will input, so use 'while loop' instead of 'for' loop.
0
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
Check this:

// File:  AverageGrades.java

// Name:  Clifford Albanese

// Project 2

// Date:  March 28, 2009

 

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;

     

    // 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

Open in new window

0
 

Author Comment

by:calbanese13
Comment Utility
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
0
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
Good Work.

You need to compute the average at the end. i.e. after reading all the values of the grades. So move the average computation out of the 'for' loop.

0
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
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

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:calbanese13
Comment Utility
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  
0
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
If you had started the gradeCounter with '-1' then you don't need to decrement.  Its upto the programmer to implement.
0
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
Program is working fine. But you have not yet completed this part of the question:  Use two separate functions (methods)


Check this for how to use methods in java: http://www.otherwise.com/Lessons/MethodsCreationAndUse.html
0
 

Author Comment

by:calbanese13
Comment Utility
I'm assuming I have to use something like;
public void enterGrades ( ) {
}
public void aveGrades ( ) {
}

Does public static void main (String[] args) { get removed?  I somewhat get this but I'm unclear!
r/
Cliff
0
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
Yes.. you have to use the above 2 methods.

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
 

Author Comment

by:calbanese13
Comment Utility
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

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.util.Scanner;
 
public class AverageGrades {
 
   public static void main(String[] args){
       
     Scanner input = new Scanner ( System.in );
 
     double grade = 0;
     double sum = 0;
     int gradeCounter = 0;

   public void enterGrades () {
     // Enter grades
     System.out.println( "Enter grades (Grade of 0 causes average calculation: ");
     grade = input.nextDouble();
   }
     
   public void aveGrades ( ) {

     //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.nextDouble();
     }

     //calculate the average
 
     double average = 0;
     
     average = (double) sum / gradeCounter;
     System.out.println( "The grade average is " + average );
   } // end Main
} // end Class
Errors.jpg
0
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
You should write those 2 methods outside the main method and call the 2 methods from main program.
0
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
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
 
LVL 12

Accepted Solution

by:
basav_com earned 500 total points
Comment Utility
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

Open in new window

0
 

Author Closing Comment

by:calbanese13
Comment Utility
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
0
 
LVL 12

Expert Comment

by:basav_com
Comment Utility
Do come to EE whenever you need help :).
0
 

Author Comment

by:calbanese13
Comment Utility
Thanks again and as McArther said "I shall return"
V/r
Cliff
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now