Array: Assign Grades

Question: Write a program that reads student scores, gets the best score, and then assigns grades based on the following scheme:
The grade is A if the score is ≥ best −10;
The grade is B if the score is ≥ best −20;
The grade is C if the score is ≥ best −30;
The grade is D if the score is ≥ best −40;
The grade is F otherwise.
The program prompts the user to enter the total number of students, then prompts the user to enter all of the scores, and concludes by displaying the grades.

package Array;

import java.util.Scanner;

/**
 *Chapter 7 Exercise 1:
 *
 *      (Assign grades)
 *      Write a program that reads student scores, gets the best score,
 *      and then assigns grades based on the following scheme:
 *    Grade is A if score is >= best - 10
 *           Grade is B if score is >= best - 20
 *           Grade is C if score is >= best - 30
 *           Grade is D if score is >= best - 40
 *           Grade is F otherwise.
 * Created by aarushi on 7/5/21.
 */

/*
    Step 1: Ask user to input number of students (n)
    Step 2: Create an array for grades and an array for scores 
    Step 3: Write a method that gets the best score
    Step 4: Write a method that assigns grades
    Step 5: Display grades
 */
public class Ch7Ex1 {

    public static void main (String [] args){
        //create scanner object
        Scanner sc= new Scanner(System.in);
        //declare variables to store the number of students and the best score
        int numOfStudents, bestScore;
		
      	//input the number of students
        System.out.print("Enter number of students: ");
        numOfStudents= sc.nextInt();
      
		//create one array to store the scores and another to store the grades
        int [] scores= new int[numOfStudents];
        char [] grades = new char[numOfStudents];

      	//Input the scores
        System.out.println("Enter scores: ");
        for (int i=0; i<numOfStudents; i++){
            scores[i]= sc.nextInt();
        }

        bestScore= getBestScore(scores, numOfStudents);
        System.out.println("Best Score: " + bestScore);

        assignGrades(scores, grades, numOfStudents, bestScore);
        displayArray(scores, grades, numOfStudents);

    }

  	//function to get best score
    public static int getBestScore (int [] scores, int size){
        int bestScore = scores[0];

        for (int i=0; i<size; i++){
            if (scores[i]>bestScore){
                bestScore= scores[i];
            }
        }

        return bestScore;
    }

  	//function to assign grades to students based on the best score
    public static void assignGrades (int [] scores, char [] grades, int size, int bestScore){
        for (int i=0; i<size; i++){
            if (scores[i]>=(bestScore-10))
                grades[i]='A';
            else if (scores[i]>=(bestScore-20))
                grades[i]='B';
            else if (scores[i]>=(bestScore-30))
                grades[i]='C';
            else if (scores[i]>=(bestScore-40))
                grades[i]='D';
            else
                grades[i]='F';
        }
    }

  	//function to display array
    public static void displayArray (int [] scores, char [] grades, int size){
        for (int i=0; i<size; i++){
            System.out.println("Student " + (i+1) + " Score: " + scores[i] + " Grade: " + grades[i]);
        }
    }
}

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close