234k views
0 votes
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).

User Keyadun
by
5.1k points

1 Answer

3 votes

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. public double getAverageScore()
  60. {
  61. return this.averageScore;
  62. }
  63. private String convert_to_grade(double score)
  64. {
  65. if(score >= 80) {
  66. return "A";
  67. }
  68. else if(score >=70) {
  69. return "B";
  70. }
  71. else if(score >= 60) {
  72. return "C";
  73. }
  74. else if(score >=50) {
  75. return "D";
  76. }
  77. else {
  78. return "F";
  79. }
  80. }
  81. public void displayStudentInfo()
  82. {
  83. System.out.println("Name : " + this.name);
  84. System.out.println("ID : " + this.id);
  85. System.out.println("Average score: " + this.averageScore);
  86. }
  87. }

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:

  • 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)
User Roych
by
4.9k points