3 Replies - 2506 Views - Last Post: 11 April 2010 - 09:24 PM Rate Topic: -----

#1 JavaBunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-February 10

Highest Grade Array

Posted 11 April 2010 - 07:49 PM

I am getting some compile errors on my Highest Grade Array program. Can someone shed some light as to why I am getting the errors and what I can do to fix them? Of course, I have to turn this in tonight so any help would be greatly appreciated. Thanks in advance.
//HighestGradeArray.java

public class HighestGradeArray
{
	public static void main (String[] args)
	{ // Output Grade Array
	int [] gradesArray = {95, 79, 89, 78, 82};
	outputGrades(gradesArray);
	System.out.println( "Grades:\n");
	System.out.printf("%s%10s", "Student", "Grades");
	for (int student  = 0; student < gradesArray.length; student++)
	System.out.printf("Student %2d", student+1);
	//call method getMaximum
	System.out.printf ("\n%s %d\n%s %d\n\n", "Highest Grade Is", getMaximum(gradesArray));
	}//end main
	
	public static int getMaximum(int grades [])
	{//assume first element of grades array is largest
		int highGrade = grades [0];
		//loop through rows of grades array
		{
		for (int[] studentGrades : grades)
		//if grade is greater than highGrade; assign it to highGrade
		if (grade > highGrade)
		highGrade = grade;
		}//end for
		
	return highGrade; //return the highest grade
	}//end Method getMaximum
}//end class HighestGradeArray


Is This A Good Question/Topic? 0
  • +

Replies To: Highest Grade Array

#2 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Highest Grade Array

Posted 11 April 2010 - 07:50 PM

Can you post where the errors are at?
Was This Post Helpful? 0
  • +
  • -

#3 JavaBunny  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 21-February 10

Re: Highest Grade Array

Posted 11 April 2010 - 08:01 PM

java:8 cannot find symbol
symbol : method outputGrades(int[])
location: class HighestGradeArray
outputGrades(gradesArray);
java:22: incompatible types
found : int
required: int[]
for(int[] studentGrades: grades)
java:24: cannot find symbol
symbol : variable grade
location: class HighestGradeArray
if (grade > highGrade)
java:25: cannot find symbol
symbol : variable grade
location: class HighestGradeArray
highGrade = grade;

View Postzim1985, on 11 April 2010 - 07:50 PM, said:

Can you post where the errors are at?

Was This Post Helpful? 0
  • +
  • -

#4 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Highest Grade Array

Posted 11 April 2010 - 09:24 PM

So assuming you made an outputGrades(int[]) method somewhere else, you need to use a class object to use the method unless you made the method static, which is bad OO design in most cases. If you didn't make this method, then you need to make that method.

In you modified for-loop, you have an int array as what will be cycling through the array. You have to have an int, and then the array this will be cycling through. Think about this:
for(int x = 0; x < array.length; x++)
{
    System.out.println(array[x]);
}
// same as
for(int x : array)
{
    System.out.println(x);
}


Notice that you need a number to iterate through the array with in both cases.

That last error is simple. There is no grade variable...it was never declared.

For you loop that tests for the highest grade, I would actually recommend using the normal for-loop. Like this example:
int[] array = {20, 21, 54, 12, 36};
int highest = array[0];
for(int i = 1; i < array.length; i++)
{
    int temp = array[i];
    if(temp > highest)
    {
        highest = temp;
    }
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1