Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. public class Student
  2. {
  3. private String name;
  4. private String major;
  5. private double gpa;
  6. private int hoursCompleted;
  7.  
  8. /**Default constructor
  9. *
  10. */
  11. public Student()
  12. {
  13. super();
  14. this.name = "";
  15. this.major = "";
  16. this.gpa = 0.0;
  17. this.hoursCompleted = 0;
  18. }
  19.  
  20.  
  21. /**Constructor
  22. * @param name
  23. * The student's name
  24. * @param major
  25. * The student's major
  26. */
  27. public Student(String name, String major)
  28. {
  29. super();
  30. this.name = name;
  31. this.major = major;
  32. this.gpa = 0.0;
  33. this.hoursCompleted = 0;
  34. }
  35.  
  36. /**Constructor
  37. * @param name
  38. * The student's name
  39. * @param major
  40. * The student's major
  41. * @param gpa
  42. * The student's cumulative gpa
  43. * @param hoursCompleted
  44. * Number of credit hours the student has completed
  45. */
  46. public Student(String name, String major, double gpa, int hoursCompleted)
  47. {
  48. super();
  49. this.name = name;
  50. this.major = major;
  51. this.gpa = gpa;
  52. this.hoursCompleted = hoursCompleted;
  53. }
  54.  
  55. /**
  56. * @return The student's major.
  57. */
  58. public String getMajor()
  59. {
  60. return this.major;
  61. }
  62. /**
  63. * @param major The major to set.
  64. */
  65. public void setMajor(String major)
  66. {
  67. this.major = major;
  68. }
  69. /**
  70. * @return The student's name.
  71. */
  72. public String getName()
  73. {
  74. return this.name;
  75. }
  76. /**
  77. * @param name The name to set.
  78. */
  79. public void setName(String name)
  80. {
  81. this.name = name;
  82. }
  83. /**
  84. * @return The student's gpa.
  85. */
  86. public double getGpa()
  87. {
  88. return this.gpa;
  89. }
  90. /**
  91. * @return The number of credit hours the student has completed.
  92. */
  93. public int getHoursCompleted()
  94. {
  95. return this.hoursCompleted;
  96. }
  97.  
  98. /**
  99. * @return The student's class (Freshman, Sophomore, Junior, Senior)
  100. */
  101. public String getClassLevel()
  102. {
  103. String classLevel;
  104. if (this.hoursCompleted < 30)
  105. {
  106. classLevel = "Freshman";
  107. }
  108. else if (this.hoursCompleted < 60)
  109. {
  110. classLevel = "Sophomore";
  111. }
  112. else if (this.hoursCompleted < 90)
  113. {
  114. classLevel = "Junior";
  115. }
  116. else
  117. {
  118. classLevel = "Senior";
  119. }
  120. return classLevel;
  121. }
  122.  
  123. /**Update student's information to reflect completion of a semester's work
  124. * @param semHours
  125. * Number of credit hours the student has just completed
  126. * @param semGPA
  127. * GPA for the semester just completed
  128. */
  129. public void updateStudent(int semHours, double semGPA)
  130. {
  131. int oldHours = this.hoursCompleted;
  132. this.hoursCompleted = oldHours + semHours;
  133. this.gpa = (oldHours * this.gpa + semHours * semGPA) / this.hoursCompleted;
  134. }
  135.  
  136. /**Produces a string with information about a student in a nice format with
  137. * appropriate labeling
  138. */
  139. public String toString()
  140. {
  141. DecimalFormat fmt = new DecimalFormat("0.000");
  142. String myString = this.name + " is a " + getClassLevel() + " "
  143. + this.major + "\n"
  144. + "who has completed " + this.hoursCompleted + " hours " +
  145. "with a " + fmt.format(this.gpa) + " gpa.\n";
  146. return myString;
  147. }
  148.  
  149. /** Reads student data in file format with name on first line, hours and gpa
  150. * on second line, and major on third line.
  151. * @param input
  152. * Scanner connected to input source for student information
  153. */
  154. public void read(Scanner input)
  155. {
  156. this.name = input.nextLine();
  157. this.hoursCompleted = input.nextInt();
  158. this.gpa = input.nextDouble();
  159. input.nextLine();
  160. this.major = input.nextLine();
  161. }
  162.  
  163.  
  164. /**
  165. * Prints student information in file format
  166. * @param output
  167. * PrintWriter connected to open output stream
  168. */
  169. public void write(PrintWriter output)
  170. {
  171. output.println(this.name);
  172. output.println(this.hoursCompleted + " " + this.gpa);
  173. output.println(this.major);
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement