 # Write methods that determine both the lowest or highest class grade

Anybody figure out how to do this? I’ve finished the project. I would really like to know how to do this though.

I am trying to figure this out as well.

What do you have so far? What have you figured out? How are trying to tackle this problem? What problems do you have with your code strategy or design?

1 Like

I only have ideas but nothing to go off of. My idea is for some kind of count to start at 100(because that is as high as it can go) and work backwards to find the next highest if not 100 already. But i can not think of how to implement this or if that is even possible.

public int getHighest()
{
for( )//probably need a for loop in there to scan the numbers in the array
}

1 Like

If you refer to how you’d do this manually, would you really be using the number 100 if that’s not any of the grades?

What your function should do is no different. So just consider how you do it, and then you can start considering what the matching instructions in Python should look like

100 is not one of the grades but it is a possible grade. If you were to have a program where you could enter grades and change them, it would be possible to eventually have a 100% grade. No?
The problem is that I don’t know how i’d consider to do it. so I am not able to consider anything.

If I write up a list of numbers (about twenty numbers) on a paper and give you that paper to study, can you tell me which is highest, and which is lowest?

What do you have to do to perform that task?

You would read the first number.
And then what?

I got it to work using this:

``````public int getLowAndHigh(ArrayList<Integer> grades) {
System.out.println("The ArrayList is empty.");
return 0;
} else {
if(g < min) min = g;
if(g > max) max = g;
}
}
return 0;
}
``````

then I call it in the main method:

``````public static void main(String[] args) {
ArrayList<Integer> myClassroom = new ArrayList<Integer>();

myAnalyzer.getLowAndHigh(myClassroom);
}

}
``````

Resulting output:

I got this from one of the answers here.

3 Likes

Can anyone explain what these lines mean? I understand the for each loop. I just don’t get how each number is being compared in these lines of code?

if(g < min) min = g;
if(g > max) max = g;

first thing to do is to initialize the values in the first index of the array grades

``````int min = grades.get(0);
``````

It compares each value in the arrayList called `grades` against the variable `g` using the for-each loop

``````for(Integer g: grades) {
if(g < min) min = g;
if(g > max) max = g;
}
``````

It runs the iteration twice–for the min value and then the max value. Each if statement can be run independent of the other.

for each loop.

@willieldanem well done, here’s what I think you can improve, if I was grading your code, these are the comments you’d get back from me:

Your function `getLowAndHigh` would be more useful if you split it in two and returned the results instead of printing them. Right now it’s doing three jobs: Finding min, finding max, printing. It should be doing just one of those things.

The above suggestion is really the only one, but if not for that, then the funciton should be renamed to `void printLowAndHigh` (no return type, and it prints, it doesn’t give/get anything) so that its behaviour matches its signature.

In “real-world” code:

``````min = Collections.min(grades);