Logic Error in Adding and Dropping Students [Java]

2020-01-20 06:04发布

问题:

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

回答1:

First things first, you really should be using a list, as you are changing the size of the array when dropping and adding students. Anyways...

So what you are seeing at the bottom of the output you got are the pointers to the Course objects. In other words, you are seeing the directions to the information stored in the objects, not the info itself. This happens when you try to print an object that does not have a toString() method (as in when you write System.out.println(... + course1). This method lets you basically set what is displayed when printing an object, and sort of overrides the default of the object pointer.

In this case, the info you want to display are the students' names, how many students there are, and possibly the course name. Your toString() method (in the Course class) would then look something like this:

public String toString(){

    String toReturn = "";

    toReturn += "Number of students in " + courseName + ": "
                    + numberOfStudents + " Students are:\n";

    for(int i = 0; i < number of students - 1; i++){

        toReturn += (i + 1) + ": " + students[i] + "\n";

    }

    return toReturn;

}

If you add this method to the class, then you don't even need all the code printing the info that worked before you printed the pointers. You can just print the Course object as is.