1

I was looking for some input on my code. What i'm trying to do is get user input in the form of scores on a test or something. The user can input how many grades they need to enter and it stores them on the array. Then the program will grade on a curve with the highest being an A. if a score is within 10 points of the highest score, it is also an A, if its in between 10 to 20 points less than the higher score its a B, if its 20 to 30 points less than the highest score then its a C, and so on till F.

My issue is that I don't know how to compare a certain element to the highest to see what grade it is. So my question is how do you compare a certain element in an array to the highest element in the array?

import java.util.Scanner;

public class StudentGrades {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int students;


        System.out.println("How many students do you have?");
        students = keyboard.nextInt();
        while (students < 0) {
            System.out.println("Invalid student amount");
            students = keyboard.nextInt();
        }
        int[] kids = new int[students];

        double[] scores = new double[students];

        for (int index = 0; index < students; index++) {

            System.out.println("Enter score for student #" + (index + 1) + ": ");
            scores[index] = keyboard.nextDouble();

        }
        double high = scores[0];
        for (int index = 1; index < scores.length; index++) {
            if (scores[index] > high) ;
            high = scores[index];

        }

        if (//place in array >= (highest-10)
                System.out.println("Student (whatever there position is) has an A")
    }

}

So if anyone can give me some insight into this issue I'll be grateful, also if you see anything wrong with the code please tell me. Any help would be great. Just to clarify my issue is with trying to find the letter grade. look at the if statement at the bottom.

John Smth
  • 31
  • 2
  • 11
  • You don't know how to figure out the highest index? You don't know how to get a value at a specific index? You don't know how to compare two values? What exactly are you stuck on? – shmosel Nov 07 '16 at 19:33
  • Possible duplicate of [How would i find the maximum value in an array?](http://stackoverflow.com/questions/16325168/how-would-i-find-the-maximum-value-in-an-array) – Nicole Phillips Nov 07 '16 at 19:35
  • @shmosel I'm stuck on the comparing one grade that is entered into the array lest say the value in scores element 0 to the highest score to figure out the letter grade. I don't know the syntax to do that. – John Smth Nov 07 '16 at 19:37
  • 1
    You're not helping by repeating yourself. I'm not asking what you need, I'm asking what you're stuck on. We're not a coding service, but we'll be happy to help you work through a specific problem. So explain the problem, not (just) the requirement. – shmosel Nov 07 '16 at 19:40
  • @shmosel the problem is that I cant/ don't know how to pick a certain part of the array to compare to the highest. That is my problem. – John Smth Nov 07 '16 at 19:42
  • You obviously know how to index an array. You're doing it all over your code. – shmosel Nov 07 '16 at 19:46
  • @shmosel My only issue is that I dont know the syntax for picking a certain value out of the array. – John Smth Nov 07 '16 at 19:48
  • Yes you do. What do you think `scores[index]` is? – shmosel Nov 07 '16 at 19:51
  • @shmosel so are you saying that if I wanted to compare the element in 0 I would do something like scores[0] >=(highest-10)? – John Smth Nov 07 '16 at 19:53
  • I assume you mean `high`. But yes it looks like you're on the right track. – shmosel Nov 07 '16 at 19:55
  • @JohnSmth I decided to show you how it's supposed to be done in Java in the answer below. Enjoy your Java learning! – xenteros Nov 07 '16 at 19:57

1 Answers1

1

If I were you, I would do that in the following way:

public class Student implements Comparable{

    int index;
    private double grade;

    public Student(int i, double d) {
        grade = d;
        index = i;
    }

    public int getIndex() {
        return index;
    }

    public double getGrade() {
        return grade;
    }

    public void setGrade(double grade) {
        this.grade = grade;
    }

    @Override
    public int compareTo(Object o) {
        Student s = (Student)o;

        if (s.getGrade() < grade){
            return -1;
        } else if (s.getGrade() == grade) {
            return 0;
        }
        return 1;
    }
}

And the driver for the application is:

   public static void main(String[] args) throws Exception {

        Scanner keyboard = new Scanner(System.in);
        int amount;

        System.out.println("How many students do you have?");
        amount = keyboard.nextInt();
        while (amount < 0) {
            System.out.println("Invalid student amount");
            amount = keyboard.nextInt();
        }

        Student[] students = new Student[amount];
        for (int index = 0; index < amount; index++) {
            System.out.println("Enter score for student #" + (index + 1) + ": ");
            students[index] = new Student(index, keyboard.nextDouble());
        }

        Arrays.sort(students);

        for (int i = 0; i < amount / 10 + 1; i++) {
            System.out.println("Student " + (students[i].getIndex() + 1) + " has an A");
        }
    }

Edit:
additional clarification for the OP asked in comments:

double highest = students[0].getGrade();
for (Student s : students) {
    if (s.getGrade() > highest) {
        highest = s.getGrade();
    }
}
xenteros
  • 15,586
  • 12
  • 56
  • 91
  • I have a quick question for you since you seem to know your stuff, is there a way to have a for statement that that steps through each element and compares it to the highest score? – John Smth Nov 07 '16 at 20:15
  • @JohnSmth I edited my answer. Going to sleep now. In case you have more questions , you need to wait till tommorow for the answer. – xenteros Nov 07 '16 at 20:18
  • 1
    The `Student` class should not be `Comparable`. A better way is to write `Arrays.sort(students, Comparator.comparing(Student::getGrade).reversed());`. Alternatively, the `compareTo` method should make use of `Integer.compare(s.grade, grade)` to avoid the large `if` statement. – Roland Illig Nov 07 '16 at 20:29