Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Course.java
- public class Course {
- private String courseName;
- private String[] students = new String[4];
- private int numberOfStudents;
- /**
- *
- * @param courseName is a string passed on when an object of Course class is inititated
- */
- public Course(String courseName) {
- this.courseName = courseName;
- }
- /**
- * This method is used to add students in our string array students.
- * It automatically doubles it's size if the array is running out of space.
- * @param student is only parameter to addStudent method
- */
- public void addStudent(String student) {
- if (students.length == numberOfStudents) {
- String[] temp = new String[numberOfStudents * 2];
- for (int i = 0; i < students.length; i++) {
- temp[i] = students[i];
- }
- students = temp;
- }
- students[numberOfStudents] = student;
- numberOfStudents++;
- }
- /**
- * This method is used to get all students enrolled in current course.
- * @return String[] This returns array of students.
- */
- public String[] getStudents() {
- return students;
- }
- /**
- * This method is used to get number of students enrolled in our course.
- * @return int This retuns int value of students in our course.
- */
- public int getNumberOfStudents() {
- return numberOfStudents;
- }
- /**
- * This method is used to get name of the course.
- * @return String This returns course name String value.
- */
- public String getCourseName() {
- return courseName;
- }
- /**
- * This method is used to remove student from our course.
- * @param student is the only parameter required to know which student to be removed.
- */
- public void dropStudent(String student) {
- String[] temp = new String[numberOfStudents];
- int count = 0;
- for (int i = 0; i < students.length; i++) {
- if (students[i] == null || students[i].equals(student)) {
- continue;
- }
- temp[count] = students[i];
- count++;
- }
- students = temp;
- numberOfStudents--;
- }
- }
- //TestCourse.java
- public class TestCourse {
- public static void main(String[] args) {
- Course c1 = new Course("MBBS");
- c1.addStudent("Rick Astley");
- c1.addStudent("Dick Dale");
- c1.addStudent("Candice");
- c1.dropStudent("Rick Astley");
- String[] students = c1.getStudents();
- for (String student : students) {
- System.out.println(student);
- }
- }
- }
Add Comment
Please, Sign In to add comment