代码不会让学生离开课程[Java]

时间:2016-04-24 18:08:53

标签: java class oop object

this post相同的问题,但我认为我越来越接近解决它了。

简而言之,我的代码有两个课程,我必须从他们每个人中删除一些学生,然后完全清除第二个课程。此代码使用两个类完成。

这是我到目前为止所做的:

npm start

/*
Program description:
Starting with listing 10.5 and 10.6 below, you are to modify the two source
files to:

Course.java:


 add method increaseArray().
Increase the array when students are added by 1, (hint utilize
System.arraycopy method ). Then add the student to the last location. I
would suggest you set the size of the “students” array to a size initially
of zero, so when the any student is added, your code to increase the
array will be triggered.

add method dropStudent().
Drop student would need to find the String match by iterating on the
students array using the .equals String method. When a match is found,
remove that entry by assigning the next student into the match location,
and do for all the remaining students (effectively shift remaining
students by one ). Also you would have also decrement the
numberOfStudents count.

 add method clear().
Iterating on the students array assign all to null and set the
numberOfStudents count to 0.
TestCourse.java:
You need to test your new methods; this is done by modifying listing 10.5 to
meet the requirements of assignment which are:

Create a two courses
Add six students to the first, three students to the second
Remove the first two student from course one
Remove the second student from course two
Display students in both classes
Clear the second course
Display students in both classes


Add a static method to TestCourse that handles printing the students in a
class. This method receives the reference of the course and the course
number. This way, you just call the method to print out the students.
 */

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) {

    int IndexOfStudentToDrop = -1;

    for (int i = 0; i < numberOfStudents; i++) {
        if (students[i].equalsIgnoreCase(student)) {
            IndexOfStudentToDrop = i;
            if (IndexOfStudentToDrop != -1) {
                for (i = IndexOfStudentToDrop; i < numberOfStudents; i++)
                    students[i] = students[i+1];
            } // end if found
            // decrement number of students by 1
            numberOfStudents--;
        }
    }

}

public void clear() {

    for (int i = 0; i < numberOfStudents; i ++){
        students [i] = null;
    }
    numberOfStudents = 0;

}

public void increaseArray() {
    if (numberOfStudents >= students.length) {
        String[] temp = new String[students.length * 2];
        System.arraycopy(students, 0, temp, 0, students.length);
        students = temp;
    }



} // end of increaseArray()

public String toString ()
{
    String output = "";
     output += getCourseName()+ (getNumberOfStudents() + "students");
        for (int i = 0; i < getNumberOfStudents(); i++) {
                output += "\n("+ (i + 1)+")"+ students [i];
            }
    return output;

}
}

输出应该是这样的:

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");
    System.out.println();
    System.out.println();




    // add three students to course2
    course2.addStudent("\n1: Tom Servo");
    course2.addStudent("\n2: Crow T. Robot");
    course2.addStudent("\n3: Zap Rowsdower");
    System.out.println();
    System.out.println();



    // output to the console
    System.out.println ("Number of students in Course 1: " + 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("Number of students in Course 2: " + course2.getNumberOfStudents() + " Students are: ");
    String [] students1 = course2.getStudents();

    for (int i = 0; i < course2.getNumberOfStudents(); i++)
        System.out.print(students1[i]);

    // output to the console how many students will be dropped from each class
    System.out.println("dropping 2 students from course 1");
    System.out.println("dropping 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: " + course1.getNumberOfStudents() + " Students are: ");

            course2.dropStudent("Crow T. Robot");
            System.out.println("\nNumber of students in Course 2: " + course2.getNumberOfStudents() + " Students are: ");

    // clear course2, but keep course1 as it currently stands
            System.out.println("\nclearing course 2 course 2");
            course2.clear();

            System.out.println("\nNumber of students in Course 1: " + course1.getNumberOfStudents() + " Students are: " );
            System.out.println("\nNumber of students in Course 2: " + course2.getNumberOfStudents() + " Students are: ");
    }

}

但这就是发生的事情:

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:

正如您所看到的,我的代码在我需要时不会丢弃学生(尽管清算似乎不是问题)。我知道我的格式搞砸了,但这不是我最关心的问题,我可以自己解决这个问题。除了格式化之外,丢弃的学生是我唯一的问题,如果我能帮忙解决这个问题,我可以从这里开始。

1 个答案:

答案 0 :(得分:1)

这不起作用的原因如下:您将1: Tom Servo之类的字符串与该号码放在一起,并尝试将其删除,如course1.dropStudent("Tom Servo");

如果您在添加1:时删除Student,或者您尝试将其删除为:

course1.dropStudent("1: Tom Servo");

我确定它能够正常运作。