Answer:
- public class Student {
- private String name;
- private String id;
- private double averageScore;
- private double [] testScores = new double[6];
- private String [] letterGrade = new String[6];
- public Student(String name, String id, double [] testScores)
- {
- this.name = name;
- this.id = id;
- setTestScores(testScores);
- setLetterGrade();
- setAverageScore();
- }
- public String getName()
- {
- return this.name;
- }
- public String getId()
- {
- return this.id;
- }
- public void setTestScores(double [] testScores)
- {
- for(int i=0; i < testScores.length; i++)
- {
- if(testScores[i] >=0 && testScores[i] <=100) {
- this.testScores[i] = testScores[i];
- }
- else{
- throw new IllegalArgumentException("The score must be within 0-100");
- }
- }
- }
- public double [] getTestScores()
- {
- return this.testScores;
- }
- public void setLetterGrade()
- {
- for(int i = 0; i < this.letterGrade.length; i++)
- {
- this.letterGrade[i] = convert_to_grade(this.testScores[i]);
- }
- }
- public String [] getLetterGrade()
- {
- return this.letterGrade;
- }
- public void setAverageScore()
- {
- double total = 0;
- for(int i = 0; i < this.testScores.length; i++)
- {
- total += this.testScores[i];
- }
- this.averageScore = total / 6;
- }
-
- public double getAverageScore()
- {
- return this.averageScore;
- }
- private String convert_to_grade(double score)
- {
- if(score >= 80) {
- return "A";
- }
- else if(score >=70) {
- return "B";
- }
- else if(score >= 60) {
- return "C";
- }
- else if(score >=50) {
- return "D";
- }
- else {
- return "F";
- }
- }
- public void displayStudentInfo()
- {
- System.out.println("Name : " + this.name);
- System.out.println("ID : " + this.id);
- System.out.println("Average score: " + this.averageScore);
- }
- }
Step-by-step 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:
- 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)