PadmaJS

Course

Oct 2nd, 2022 (edited)
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 2.42 KB | None | 0 0
  1. //Course.java
  2. public class Course {
  3.   private String courseName;
  4.   private String[] students = new String[4];
  5.   private int numberOfStudents;
  6.  
  7.   /**
  8.    *
  9.    * @param courseName is a string passed on when an object of Course class is inititated
  10.    */
  11.   public Course(String courseName) {
  12.     this.courseName = courseName;
  13.   }
  14.  
  15.   /**
  16.    * This method is used to add students in our string array students.
  17.    * It automatically doubles it's size if the array is running out of space.
  18.   * @param student is only parameter to addStudent method
  19.   */
  20.  public void addStudent(String student) {
  21.    if (students.length == numberOfStudents) {
  22.      String[] temp = new String[numberOfStudents * 2];
  23.      for (int i = 0; i < students.length; i++) {
  24.        temp[i] = students[i];
  25.      }
  26.      students = temp;
  27.    }
  28.    students[numberOfStudents] = student;
  29.    numberOfStudents++;
  30.  }
  31.  
  32.  /**
  33.   * This method is used to get all students enrolled in current course.
  34.   * @return String[] This returns array of students.
  35.   */
  36.  public String[] getStudents() {
  37.    return students;
  38.  }
  39.  
  40.  /**
  41.   * This method is used to get number of students enrolled in our course.
  42.   * @return int This retuns int value of students in our course.
  43.   */
  44.  public int getNumberOfStudents() {
  45.    return numberOfStudents;
  46.  }
  47.  
  48.  /**
  49.   * This method is used to get name of the course.
  50.   * @return String This returns course name String value.
  51.   */
  52.  public String getCourseName() {
  53.    return courseName;
  54.  }
  55.  
  56.  /**
  57.   * This method is used to remove student from our course.
  58.   * @param student is the only parameter required to know which student to be removed.
  59.   */
  60.  public void dropStudent(String student) {
  61.    String[] temp = new String[numberOfStudents];
  62.    int count = 0;
  63.    for (int i = 0; i < students.length; i++) {
  64.      if (students[i] == null || students[i].equals(student)) {
  65.        continue;
  66.      }
  67.      temp[count] = students[i];
  68.      count++;
  69.    }
  70.    students = temp;
  71.    numberOfStudents--;
  72.  }
  73. }
  74. //TestCourse.java
  75. public class TestCourse {
  76.  public static void main(String[] args) {
  77.    Course c1 = new Course("MBBS");
  78.    c1.addStudent("Rick Astley");
  79.    c1.addStudent("Dick Dale");
  80.    c1.addStudent("Candice");
  81.    c1.dropStudent("Rick Astley");
  82.    String[] students = c1.getStudents();
  83.    for (String student : students) {
  84.      System.out.println(student);
  85.    }
  86.  }
  87. }
  88.  
Add Comment
Please, Sign In to add comment