Q:

(The Course class) Revise the Course class as follows:

0

(The Course class) Revise the Course class as follows:

The array size is fixed in Listing 10.6. Improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array to it.
Implement the dropStudent method.
Add a new method named clear() that removes all students from the course.

Write a test program that creates a course, adds three students, removes one, and displays the students in the course.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (The Course class) Revise the Course class as follows:                         *
*                                                                                *
* ■ The array size is fixed in Listing 10.6. Improve it to automatically         *
*   increase the array size by creating a new larger array and copying the       *
*   contents of the current array to it.                                         *
* ■ Implement the dropStudent method.                                            *
* ■ Add a new method named clear() that removes all students from the            *
*   course.                                                                      *
*                                                                                *
* Write a test program that creates a course, adds three students, removes one,  *
* and displays the students in the course.                                       *
*********************************************************************************/
public class Exercise_10_09 {
	/** Main method */
	public static void main(String[] args) {
		// Create a course
		Course math101 = new Course("math101");

		// Add three students
		math101.addStudent("Mark");
		math101.addStudent("Tom");
		math101.addStudent("Joan");

		// remove one student
		math101.dropStudent("Tom");

		// Displays the students in the course
		System.out.println("\nThe students in the course " + 
			math101.getCourseName() + ":");
		String[] students = math101.getStudents();
		for (int i = 0; i < math101.getNumberOfStudents(); i++) {
			System.out.print(students[i] + " ");
		}
		System.out.println();
	}
}

Course.java 

public class Course {
	private String courseName;
	private String[] students = new String[1];
	private int numberOfStudents;

	public Course(String courseName) {
		this.courseName = courseName;
	}

	public void addStudent(String student) {
		// Automatically increases the array size 
		if (numberOfStudents == students.length) {
			String[] a = new String[students.length + 1];
			for (int i = 0; i < numberOfStudents; i++) {
				a[i] = students[i];
			}
			students = a;
		}
		students[numberOfStudents] = student;
		numberOfStudents++;
	}

	public String[] getStudents() {
		return students;
	}

	public int getNumberOfStudents() {
		return numberOfStudents;
	}

	public String getCourseName() {
		return courseName;
	}

	/** Remove a student from a course */
	public void dropStudent(String student) {
		int index = findStudent(student);
		if (index >= 0) {
			dropStudent(index);
		}
		else {
			System.out.println(student + " is not in the course: " + courseName);
		}
	}

	/** deletes a Student */
	private void dropStudent(int index) {
		String[] s = new String[students.length - 1];
		for (int i = 0, j = 0; i < s.length; i++, j++) {
			if (i == index) {
				j++;
			}
			s[i] = students[j];
		}
		this.students = s;
		numberOfStudents--;
	}

	/** removes all students from the course */
	public void clear() {
		students = new String[1];
		numberOfStudents = 0;
	}

	/** Returns the index if student is found. Otherwise returns -1 */
	private int findStudent(String student) {
		for (int i = 0; i < numberOfStudents; i++) {
			if (students[i].equals(student)) {
				return i;
			}
		}
		return -1;
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now