Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Grade averaging program with array and multiple methods.

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    10
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default 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


    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.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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).

    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.

    int sumOfGrades = 0;
    for (int i = 0; i < grades.length; i++)
    {
         sumOfGrades += grades[i];
    }

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    jeremykatz (November 9th, 2009)

  4. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    10
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default 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.



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

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:

    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

    public [b]static[/b] double getGrades()

    This will eliminate the need to create an object to call the methods.

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    jeremykatz (November 9th, 2009)

  7. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default 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 ...


    public static void newMethod(String parameters)  // this is the receiving side

    and

    public static void usingThisMethod("arguments") // the passing side


    i just notice something on what you say helloworld.. i just want to consult this thing..
    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....
    Last edited by chronoz13; November 9th, 2009 at 09:41 AM.

  8. The Following User Says Thank You to chronoz13 For This Useful Post:

    jeremykatz (November 9th, 2009)

  9. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    10
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Grade averaging program with array and multiple methods.

    Quote Originally Posted by helloworld922 View Post
    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:

    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


    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?

    	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;
    	}

  10. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:

    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
    }

  11. The Following User Says Thank You to helloworld922 For This Useful Post:

    jeremykatz (November 9th, 2009)

  12. #8
    Junior Member
    Join Date
    Oct 2009
    Posts
    10
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default 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.




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

  13. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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:

    return someVar[b];[/b]

Similar Threads

  1. Average program with array and loop and JOptionPane.
    By jeremykatz in forum AWT / Java Swing
    Replies: 6
    Last Post: October 25th, 2009, 02:33 PM
  2. User Defined Methods
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 11
    Last Post: October 20th, 2009, 06:57 PM
  3. Novice with java methods, please help!
    By raidcomputer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2009, 04:23 AM
  4. numerical conversion methods..
    By chronoz13 in forum Java SE APIs
    Replies: 12
    Last Post: September 27th, 2009, 04:29 AM
  5. help me to do this Array Program
    By fahdsaad in forum Member Introductions
    Replies: 1
    Last Post: June 30th, 2009, 02:19 AM