the second Objects lab.

Student.java 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * The Student class represents a student in a
  3. * student administration system.
  4. * It holds the student details relevant in our context.
  5. *
  6. * @author Michael Kölling and David Barnes
  7. * @version 2006.03.30
  8. */
  9. public class Student {
  10. // the student’s full name
  11. private String name;
  12. // the student ID
  13. private String id;
  14. // the amount of credits for study taken so far private int credits;
  15. private int credits;
  16. /**
  17. * Create a new student with a given name and ID number.
  18. */
  19. public Student(String fullName, String studentID) {
  20. name = fullName;
  21. id = studentID;
  22. credits = 0;
  23. }
  24. /**
  25. * Return the full name of this student.
  26. */
  27. public String getName() {
  28. return name; }
  29. /**
  30. * Set a new name for this student.
  31. */
  32. public void changeName(String newName) {
  33. name = newName;
  34. }
  35. /**
  36. * Return the student ID of this student.
  37. */
  38. public String getStudentID() {
  39. return id; }
  40. /**
  41. * Add some credit points to the student’s
  42. * accumulated credits.
  43. */
  44. public void addCredits(int newCreditPoints) {
  45. credits += newCreditPoints;
  46. }
  47. /**
  48. * Return the number of credit points this student
  49. * has accumulated.
  50. */
  51. public int getCredits() {
  52. return credits; }
  53. /**
  54. * Return the login name of this student.
  55. * The login name is a combination
  56. * of the first four characters of the
  57. * student’s name and the first three
  58. * characters of the student’s ID number.
  59. */
  60. public String getLoginName() {
  61. if(name.length() < 4 && id.length() < 3){
  62. return name + id;
  63. } else if (name.length() < 4 && id.length() >= 3){
  64. return name + id.substring(0,3);
  65. } else if (name.length() >= 3 && id.length() <3){
  66. return name.substring(0,4) + id;
  67. } else {
  68. return name.substring(0,4) + id.substring(0,3);
  69. }
  70. }
  71. /**
  72. * Print the student’s name and ID number
  73. * to the output terminal.
  74. */
  75. public void print() {
  76. System.out.println(name + " (" + id + ")");
  77. }
  78. /**
  79. * Return the number of characters in this string.
  80. */
  81. public void length() {
  82. if(name.length()<4 || id.length()<3){
  83. System.out.println("length of name or ID are too small");
  84. }
  85. }
  86. }