Java Program for Course Grade Calculation

Hey everyone! I’m stuck on a Java assignment and could use some help. I missed class this week and I’m trying to write a program that calculates grades for multiple courses. Here’s what I need it to do:

  1. Ask how many courses to calculate for
  2. For each course, get the course name and scores (stop when you enter -1)
  3. Display the course name, number of scores, average score, minimum score, and maximum score

I’ve started the code, but I’m at a loss on how to proceed. I’m not sure how to implement the calculations, especially when it comes to using Math.min() and Math.max().

import java.util.Scanner;

public class ScoreCalculator {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter number of courses: ");
        int courseCount = keyboard.nextInt();
        
        // Not sure what to do next
        // Need to use Math.min() and Math.max()
    }
}

Any tips on how to move forward from here? Thanks for your help!

hey hugo, heres a tip: use arrays to store course names and scores. loop thru each course, get scores til -1 is entered. keep track of sum, min, and max as u go. calculate average after. heres a quick example:

for (int i = 0; i < courseCount; i++) {
    // get course name
    while (true) {
        // get score
        // update sum, min, max
        if (score == -1) break;
    }
    // calc average, display results
}

hope this helps!

I’ve been in your shoes before, Hugo. Here’s what worked for me:

Start by creating an array to store the course names. Then, for each course, use an ArrayList to hold the scores. This way, you can easily add scores as the user inputs them.

For the calculations, keep a running sum, and update min and max as you go. It’s more efficient than using Math.min() and Math.max() on the whole list later.

Here’s a rough outline:

String[] courseNames = new String[courseCount];
ArrayList<Double>[] courseScores = new ArrayList[courseCount];

for (int i = 0; i < courseCount; i++) {
    // Get course name
    // Initialize ArrayList for scores
    // Input scores, updating sum, min, max
    // Calculate average
    // Display results
}

This approach should get you started. Let me know if you need more specific help!

Hey Hugo! I totally get how frustrating it can be to miss class and then tackle a tricky assignment. No worries though, we’ve all been there! :blush:

Your approach looks good so far. Here’s a little trick I learned that might help you out:

Instead of using Math.min() and Math.max() at the end, why not update them as you go? It’s way easier and you won’t have to store all the scores. Check this out:

double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
double sum = 0;
int count = 0;

while (true) {
    double score = keyboard.nextDouble();
    if (score == -1) break;
    
    min = Math.min(min, score);
    max = Math.max(max, score);
    sum += score;
    count++;
}

This way, you’re constantly updating min and max with each new score. Pretty neat, right?

Oh, and don’t forget to handle the case where no scores are entered. You don’t want to divide by zero when calculating the average!

How does this sound? Got any questions about it? I’m curious to hear how you end up solving this!