172k views
5 votes
7. Test Average and Grade Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Write the following methods in the program: calcAverage: This method should accept five test scores as arguments and return the average of the scores. determineGrade: This method should accept a test score as an argument and return a letter grade for the score, based on the following grading scale: Score Letter Grade 90-100 A 80-89 B 70-79 C 60-69 D Below 60 F

1 Answer

3 votes

Answer:

The code solution is written in Java.

  1. import java.util.Scanner;
  2. public class TestScore {
  3. public static void main(String[] args) {
  4. Scanner input = new Scanner(System.in);
  5. System.out.print("Please enter first score: ");
  6. double firstScore = input.nextDouble();
  7. System.out.println("Grade: " + determineGrade(firstScore));
  8. System.out.print("Please enter second score: ");
  9. double secondScore = input.nextDouble();
  10. System.out.println("Grade: " + determineGrade(secondScore));
  11. System.out.print("Please enter third score: ");
  12. double thirdScore = input.nextDouble();
  13. System.out.println("Grade: " + determineGrade(thirdScore));
  14. System.out.print("Please enter fourth score: ");
  15. double fourthScore = input.nextDouble();
  16. System.out.println("Grade: " + determineGrade(fourthScore));
  17. System.out.print("Please enter fifth score: ");
  18. double fifthScore = input.nextDouble();
  19. System.out.println("Grade: " + determineGrade(fifthScore));
  20. System.out.println("Average score: " + calcAverage(firstScore, secondScore, thirdScore, fourthScore, fifthScore));
  21. }
  22. public static double calcAverage(double score1, double score2, double score3, double score4, double score5){
  23. double average = (score1 + score2 + score3 + score4 + score5) / 5;
  24. return average;
  25. }
  26. public static String determineGrade(double score){
  27. if(score >= 90){
  28. return "A";
  29. }
  30. else if(score >= 80 ){
  31. return "B";
  32. }
  33. else if(score >=70){
  34. return "C";
  35. }
  36. else if(score >=60){
  37. return "D";
  38. }
  39. else{
  40. return "F";
  41. }
  42. }
  43. }

Step-by-step explanation:

Firstly, create the method, calcAverage(), that takes five test scores. Within the method, calculate the average and return it as output. (Line 33 - 36)

Next, create another method, determineGrade(), which takes only one score and return the grade based on the range of the score. (Line 38 -54)

Once the two required methods are created, we are ready to prompt use for input five test scores using Java Scanner class. To use get user input, create a Scanner object (Line 7). Next, use getDouble() method to get an input score and assign it to variables firstScore, secondScore, thirdScore, fourthScore & fifthScore, respectively. Once a score input by user, call determineGrade() method by passing the input score as argument and immediately print out the return grade. (Line 9 - 27)

At last, call calcAverage() method by passing the first test score variables as argument and print out the returned average value. (Line 29).

User Chris Herbst
by
8.7k points

No related questions found