For this program, I have to add and drop students from two courses, which uses two classes (object-oriented programming). So far, this is my code:
public class Course {
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;
public Course(String courseName) {
this.courseName = courseName;
}
public void addStudent(String student) {
students[numberOfStudents] = student;
numberOfStudents++;
}
public String[] getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getCourseName() {
return courseName;
}
public void dropStudent(String student) {
// set up a variable for the for loop
int indexOfStudentToDrop = -1;
// set up a for loop, with two if statements that drop students accordingly
for (int i = 0; i < numberOfStudents; i++) {
// if this if statement is true, drop student
if(students[i].equalsIgnoreCase(student)) {
indexOfStudentToDrop = i;
}
// if this if statement is true, DON'T drop student and increase the array
if (indexOfStudentToDrop != -1) {
for (i = indexOfStudentToDrop; i < numberOfStudents; i++)
students[i] = students[i+1];
} // end if found
// decrement number of students by 1
numberOfStudents--;
} // end if equal
}
public void clear() {
// iterating on the students array, assign all to null
for (int i = 0; i < numberOfStudents; i++) {
students[i] = null;
}
// assign number of students to zero
numberOfStudents = 0;
}
public void increaseArray(int amount) {
students = new String[students.length + amount];
}
}
and
public class TestCourse {
public static void main(String[] args) {
// create two courses
Course course1 = new Course("Data Structures");
Course course2 = new Course("Database Systems");
// introduce the program
System.out.println("Creating Two Courses");
System.out.println("Adding 6 students to course 1");
System.out.println("Adding 3 students to course 2");
// add six students to course1
course1.addStudent("\n1: Tom Servo");
course1.addStudent("\n2: Joel Robinson");
course1.addStudent("\n3: Mike Nelson");
course1.addStudent("\n4: Pearl Forrester");
course1.addStudent("\n5: TV's Frank");
course1.addStudent("\n6: Zap Rowsdower");
// add three students to course2
course2.addStudent("\n1: Tom Servo");
course2.addStudent("\n2: Crow T. Robot");
course2.addStudent("\n3: Zap Rowsdower");
// print out the number of students in each course
System.out.println("Number of students in course1: " + course1.getNumberOfStudents() + " Students are: ");
String[] students = course1.getStudents();
for (int i = 0; i < course1.getNumberOfStudents(); i++)
System.out.print(students[i] + " ");
System.out.println();
System.out.print("\nNumber of students in course2: " + course2.getNumberOfStudents() + " Students are: ");
String[] students2 = course2.getStudents();
for (int i = 0; i < course2.getNumberOfStudents(); i++)
System.out.print(students2[i] + " ");
// tell user how many students you plan to drop from each course
System.out.println ("\n\ndropping 2 students from course 1");
System.out.println ("\ndropping 1 student from course 2");
// drop some students.
course1.dropStudent("Tom Servo");
course1.dropStudent("Joel Robinson");
System.out.println ("\nNumber of students in Course 1: " + " Students are: " + "\n" + course1);
course2.dropStudent("Crow T. Robot");
System.out.println("\nNumber of students in Course 2: " + " Students are: " + "\n" + course2);
// clear course2, but keep course1 as it currently stands
System.out.println("\n\nclearing course 2 course 2");
course2.clear();
System.out.println("\nNumber of students in Course 1: " + " Students are: " + "\n" + course1);
System.out.println("\nNumber of students in Course 2: " + " Students are: " + "\n" + course2);
}
}
This is how it's supposed to look:
Creating Two Courses
Adding 6 students to course 1
Adding 3 students to course 2
Number of students in Course 1: 6 Students are:
1: Tom Servo
2: Joel Robinson
3: Mike Nelson
4: Pearl Forrester
5: TV's Frank
6: Zap Rowsdower
Number of students in Course 2: 3 Students are:
1: Tom Servo
2: Crow T. Robot
3: Zap Rowsdower
dropping 2 students from course 1
dropping 1 student from course 2
Number of students in Course 1: 4 Students are:
1: Mike Nelson
2: Pearl Forrester
3: TV's Frank
4: Zap Rowsdower
Number of students in Course 2: 2 Students are:
1: Tom Servo
2: Zap Rowsdower
clearing course 2 course 2
Number of students in Course 1: 4 Students are:
1: Mike Nelson
2: Pearl Forrester
3: TV's Frank
4: Zap Rowsdower
Number of students in Course 2: 0 Students are:
But this is what happens when I run the code:
Creating Two Courses
Adding 6 students to course 1
Adding 3 students to course 2
Number of students in course1: 6 Students are:
1: Tom Servo
2: Joel Robinson
3: Mike Nelson
4: Pearl Forrester
5: TV's Frank
6: Zap Rowsdower
Number of students in course2: 3 Students are:
1: Tom Servo
2: Crow T. Robot
3: Zap Rowsdower
dropping 2 students from course 1
dropping 1 student from course 2
Number of students in Course 1: Students are:
Course@6d06d69c
Number of students in Course 2: Students are:
Course@7852e922
clearing course 2 course 2
Number of students in Course 1: Students are:
Course@6d06d69c
Number of students in Course 2: Students are:
Course@7852e922
As you can see, after the initial listings, two things have happened:
1) The program has stopped counting 2) The names have stopped being listed