A Student (you need to declare a class for it) is a class defining a Student with information (such as name, id , average, letter grade and a list of 6 test scores) and associated operations (Constructors, mutators, accessors, auxiliary operation anything else you like).

Respuesta :

Answer:

  1. public class Student {
  2.    private String name;
  3.    private String id;
  4.    private double averageScore;
  5.    private double [] testScores = new double[6];
  6.    private String [] letterGrade = new String[6];
  7.    public Student(String name, String id, double [] testScores)
  8.    {
  9.        this.name = name;
  10.        this.id = id;
  11.        setTestScores(testScores);
  12.        setLetterGrade();
  13.        setAverageScore();
  14.    }
  15.    public String getName()
  16.    {
  17.        return this.name;
  18.    }
  19.    public String getId()
  20.    {
  21.        return this.id;
  22.    }
  23.    public void setTestScores(double [] testScores)
  24.    {
  25.        for(int i=0; i < testScores.length; i++)
  26.        {
  27.            if(testScores[i] >=0 && testScores[i] <=100) {
  28.                this.testScores[i] = testScores[i];
  29.            }
  30.            else{
  31.                throw new IllegalArgumentException("The score must be within 0-100");
  32.            }
  33.        }
  34.    }
  35.    public double [] getTestScores()
  36.    {
  37.        return this.testScores;
  38.    }
  39.    public void setLetterGrade()
  40.    {
  41.        for(int i = 0; i < this.letterGrade.length; i++)
  42.        {
  43.            this.letterGrade[i] = convert_to_grade(this.testScores[i]);
  44.        }
  45.    }
  46.    public String [] getLetterGrade()
  47.    {
  48.        return this.letterGrade;
  49.    }
  50.    public void setAverageScore()
  51.    {
  52.        double total = 0;
  53.        for(int i = 0; i < this.testScores.length; i++)
  54.        {
  55.            total += this.testScores[i];
  56.        }
  57.        this.averageScore = total / 6;
  58.    }
  59.    
  60.   public double getAverageScore()
  61.    {
  62.        return this.averageScore;
  63.     }
  64.    private String convert_to_grade(double score)
  65.    {
  66.        if(score >= 80) {
  67.            return "A";
  68.        }
  69.        else if(score >=70) {
  70.            return "B";
  71.        }
  72.        else if(score >= 60) {
  73.            return "C";
  74.        }
  75.        else if(score >=50) {
  76.            return "D";
  77.        }
  78.        else {
  79.            return "F";
  80.        }
  81.    }
  82.    public void displayStudentInfo()
  83.    {
  84.        System.out.println("Name         : " + this.name);
  85.        System.out.println("ID           : " + this.id);
  86.        System.out.println("Average score: " + this.averageScore);
  87.    }
  88. }

Explanation:

Line 1:

  • Create a class and name it as Student

Line 3 - 4:

  • Define class properties to hold the information related to student such as name, id, test scores, letter grade and average score.

Line 9 - 17:

  • Create Constructor.
  • This Constructor will accept three arguments, name, id and testScores.
  • The Constructor is used to initialize all the class properties

Line 19 - 22; Line 24 - 27; Line 42 - 45; Line 55 - 58; Line 72-75:

Create Accessors for

  • name property (Line 19 - 22)
  • id property (Line 24 - 27)
  • testScores property (Line 42 - 45)
  • letterGrade property (Line 55-58)
  • averageScore property (Line 72-75)

Line 29 - 40; Line 47 - 53; Line 60 - 70:

Create Mutators for

  • testScores property (Line 29 - 40)
  • letterGrade property (Line 47 - 53)
  • averageScore property (Line 60 -67)

Line 77 - 94 and Line 96 - 101:

Create auxiliary methods for

  • converting score to a corresponding grade (Line 77 - 94)
  • displaying student summary (Line 96 - 101)