Advertisement
Guest User

StudentTester

a guest
Apr 21st, 2015
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. import com.sun.tools.javac.util.ArrayUtils;
  2.  
  3. import java.rmi.StubNotFoundException;
  4.  
  5. /**
  6. * Created by greg on 4/13/15.
  7. */
  8. public class TestStudent {
  9. //Initialized list of students named myClass
  10. private Student[] myClass = {new Student("Mark Kennedy", 70, 80, 90, 100, 90), new Student("Max Gerard", 80, 85, 90, 85, 80), new Student("Jean Smith", 50, 79, 89, 99, 100), new Student("Betty Farm", 85, 80, 85, 88, 89), new Student("Dilbert Gamma", 70, 70, 90, 70, 80)};
  11.  
  12. /**
  13. * Returns the list of students' names along with their quiz scores
  14. *
  15. * @return String value of students' names along with their quiz scores
  16. */
  17. public String printBook() {
  18. String output = ""; //Initializes variable output
  19. for (int i = 0; i < myClass.length; i++) //Goes through the list of students myClass
  20. output = output + myClass[i] + " "; //Adds the students string value to output, along with a space.
  21. output = output.trim(); //Trims the additional space from the final string
  22. return output; //Returns output
  23. }
  24.  
  25. /**
  26. * Replaces the name of a student with a new name
  27. *
  28. * @param nameToReplace Name of student to get their name replaced
  29. * @param newName New name to replace the old with
  30. */
  31. public void replaceName(String nameToReplace, String newName) {
  32. for (int i = 0; i < myClass.length; i++) //Goes through the list of students myClass
  33. if (myClass[i].name == nameToReplace) //Checks if the student has the name requested to replace
  34. myClass[i].name = newName; //Sets the students name to the new name
  35. }
  36.  
  37. /**
  38. * Replaces a students quiz score
  39. *
  40. * @param studentName Name of student to have their quiz score changed
  41. * @param quizNumber Quiz number to be changed, 1-5
  42. * @param quizValue Score recieved on quiz
  43. */
  44. public void replaceQuiz(String studentName, int quizNumber, int quizValue) {
  45. for (int i = 0; i < myClass.length; i++) //Goes through the list of students myClass
  46. if (myClass[i].name == studentName) //Checks if the student has the name requested to modify
  47. myClass[i].setQuiz(quizNumber, quizValue); //Sets the students quiz score requested to be changed to the given value
  48. }
  49.  
  50. /**
  51. * Replaces a student with an entirely new student
  52. *
  53. * @param studentName Name of student to replace
  54. * @param newName Name of the new student
  55. * @param q1 Quiz 1 Score
  56. * @param q2 Quiz 2 Score
  57. * @param q3 Quiz 3 Score
  58. * @param q4 Quiz 4 Score
  59. * @param q5 Quiz 5 Score
  60. */
  61. public void replaceStudent(String studentName, String newName, int q1, int q2, int q3, int q4, int q5) {
  62. Student newStudent = new Student(newName, q1, q2, q3, q4, q5); //Creates a new student "newStudent" with the given values
  63. for (int i = 0; i < myClass.length; i++) //Goes through the list of students myClass
  64. if (myClass[i].name == studentName) //Checks if the student has the name requested to replace
  65. myClass[i] = newStudent; //Set the spot on the array to the new student with the given values
  66. }
  67.  
  68. /**
  69. * Inserts a student in front of the given student on the list
  70. *
  71. * @param studentName Name of the student the new student will be inserted in front of
  72. * @param newName Name of the new student to be inserted
  73. * @param q1 Quiz 1 Score
  74. * @param q2 Quiz 2 Score
  75. * @param q3 Quiz 3 Score
  76. * @param q4 Quiz 4 Score
  77. * @param q5 Quiz 5 Score
  78. */
  79. public void insertStudent(String studentName, String newName, int q1, int q2, int q3, int q4, int q5) {
  80. Student newStudent = new Student(newName, q1, q2, q3, q4, q5); //Creates the new student using the given values
  81. Student[] tempClass = new Student[myClass.length + 1];
  82. boolean t = false;
  83. for (int i = 0; i < myClass.length; i++)
  84. if (!myClass[i].name.equals(studentName) && !t)
  85. tempClass[i] = myClass[i];
  86. else if (myClass[i].name.equals(studentName)) {
  87. t = true;
  88. tempClass[i] = newStudent;
  89. } else if (t)
  90. tempClass[i] = myClass[i - 1];
  91. tempClass[tempClass.length - 1] = myClass[myClass.length - 1];
  92. myClass = tempClass;
  93. }
  94.  
  95. /**
  96. * Deletes a student
  97. *
  98. * @param studentName Student name to be deleted
  99. */
  100. public void deleteStudent(String studentName) {
  101. Student[] tempClass = new Student[myClass.length - 1];
  102. boolean t = false;
  103. for (int i = 0; i < myClass.length; i++)
  104. if (!myClass[i].name.equals(studentName) && !t)
  105. tempClass[i] = myClass[i];
  106. else if (myClass[i].name.equals(studentName)) {
  107. t = true;
  108. } else if (t)
  109. tempClass[i - 1] = myClass[i];
  110. myClass = tempClass;
  111.  
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement