Question: Modify (enhance) the Course class to include the following: Add an int variable called MAX and set it to 3 for the array students Add
Modify (enhance) the Course class to include the following:
Add an int variable called MAX and set it to 3 for the array students
Add a static int variable numberOfCourses that is incremented each time a new course is created
a getNumberOfCourses method
a displayNumberOfCourses method
a displayNumberOfStudents method that prints the number of students in a specific class with an appropriate label, such as Total students enrolled: 32
a displayRoster method that shows the name of the course followed by a numerical listing of all students in the course followed by a line showing the total students enrolled in the course (use the displayNumberOfStudents method)
a setCourseName method that allows the course name to be changed to the value passed in as a parameter
The array size is fixed in the book. Improve addStudent to automatically increase the size if needed by creating a larger array and copying the current array to it. (Listing 10.8 shows it)
public void addStudent(String student) {
if (numberOfStudents< MAX)
students[numberOfStudents] = student;
else { // make more room
MAX = 2*MAX;
String[] temp = new String[MAX];
System.arraycopy(students,0, temp,0, getNumberOfStudents());
students = temp;
students[numberOfStudents] = student;
}//else
numberOfStudents++;
}//add
an overloaded addStudent method with no parameters that asks for the students name to be added to the course and then calls the addStudent method from above
Implement a dropStudent method
Implement a clear method which will remove all the students in a class
I am writing in JAVA and so far I have
public class Course {
private int MAX =3;
private String courseName;
private String[] students = new String[MAX];
private int numberOfStudents=0;
static int numberOfCourses;
public Course(String courseName) {
this.courseName = courseName;
}
public void show(){
System.out.println("Course:"+ courseName);
for (int i = 0; i < numberOfStudents; i++) {
System.out.println(students[i]);
}//i
System.out.println("There are "+ numberOfStudents+ " students in the class");
}//show
public void addStudent(String student) {
if (numberOfStudents< MAX){
students[numberOfStudents] = student;
// System.out.println(student);
}//if
else { // make more room
MAX = 2*MAX;
String[] temp = new String[MAX];
System.arraycopy(students,0, temp,0, getNumberOfStudents());
students = temp;
students[numberOfStudents] = student;
}//else
numberOfStudents++;
}//add
public void addStudent() {
Scanner kb = new Scanner(System.in);
students[numberOfStudents] = student;
numberOfStudents++;
}//addStudent2
public String[] getStudents() {
return students;
}//getStudents
public int getNumberOfStudents() {
return numberOfStudents;
}//getNumberOfStudents
public String getCourseName() {
return courseName;
}//getCourseName
public void dropStudent(String student) {
}//dropStudent
public void clearStudents(String student){
}//clear
public void getNumberOfCourses(){
//add to numberOfCourses when a new course is added
}//number of courses
public void displayNumberOfStudents(){
//display students with heading
System.out.println("Total students enrolled: "+ numberOfStudents);
}//display courses
public void displayRoster(){
System.out.println("");
}//display roster
}//course
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
