Write methods that determine both the lowest or highest class grade


#1

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


#2

I am trying to figure this out as well.


#3

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?


#4

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()
{
int highGrade = <=100;
for( )//probably need a for loop in there to scan the numbers in the array
return highGrade;
}


#5

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


#6

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.


#7

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?


#8

I got it to work using this:

public int getLowAndHigh(ArrayList<Integer> grades) {
    if (grades.size() < 1) {
      System.out.println("The ArrayList is empty.");
      return 0;
    } else {
      int min = grades.get(0);
			int max = grades.get(0);
      for(Integer g: grades) {
        if(g < min) min = g;
        if(g > max) max = g;
      }
      System.out.println("Lowest grade: " + min);
      System.out.println("Higheset grade: " + max);
    }
    return 0;
  }

then I call it in the main method:

public static void main(String[] args) {
    ArrayList<Integer> myClassroom = new ArrayList<Integer>();
    myClassroom.add(98);
    myClassroom.add(92);
    myClassroom.add(88);
    myClassroom.add(75);
    myClassroom.add(61);
    myClassroom.add(89);
    myClassroom.add(95);
    
    GradeAnalyzer myAnalyzer = new GradeAnalyzer();
    
    myAnalyzer.getLowAndHigh(myClassroom);
  }
  
}

Resulting output:

Lowest grade: 61
Higheset grade: 98

I got this from one of the answers here.


#9

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?

int min = grades.get(0);
int max = grades.get(0);
for(Integer g: grades) {
if(g < min) min = g;
if(g > max) max = g;


#10

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

int min = grades.get(0);
int max = 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.


#11

@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);
max = Collections.max(grades);

So if you were to implement the functionality of finding min/max, that would of course be avoiding the exercise, but for any other purpose, that might be what you'd rather use.


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.