你好,我有一个关于如何解决这个程序的问题。我已经完成了这个大纲,但我需要帮助完成它。谢谢大家的意见!
问题。
班级Roster
的对象代表课程的特定部分。名单上有一系列students
,一个int numStudents
,表示该部分中有多少学生,int stopPoint
表示该部分中可能有的学生人数,还有一个course
,说明这个名单的哪个部分是。
请注意,学生在数组中没有任何特定顺序,但它们将位于数组的第一个numStudents
元素中。
代码:
public class Roster {
Student [ ] students;
int numStudents;
int stopPoint;
Course course;
/**
* The constructor for this class.
* Initialize this roster so that it is empty, i.e., holds no students,
* but so that it can hold up to stopPoint students
* and so that it has the given stop point and course
*/
public Roster(int stopPoint, Course course){
this.stopPoint = stopPoint;
this.course = course;
this.students = new Student [stopPoint];
}
/**
* toString is a method every class has. It returns a string
* that represents the object for printing
*/
public String toString( ){
String res = "";
for(int j = 0; j < numStudents; j++){
res = res + "\n" + students[j].toString();
}
return course + " " + numStudents + "/" + stopPoint+res;
}
/**
* isFull returns true if and only if the number of students in it is
* at the stopPoint
*/
public boolean isFull( ){
return false; // replace this line with your code
}
/**
* add given student to this roster
* if student already on roster or numStudents already == stopPoint,
* do not change roster and return false
* worst case O(1) - add the new Student at the end of the array
* @return true if successful, else false
*/
public boolean addStudent(Student student){
return false; // replace this line with your code
}
/**
* returns true if and only if the student is on this roster.
*/
public boolean findStudent(Student student){
return false; // replace this line with your code
}
/**
* Remove given student from this roster.
* If student is not on this roster do not change roster and return false
* @return true if successful, else false
*/
public boolean dropStudent(Student student){
return false; // replace this line with your code
}
}
答案 0 :(得分:0)
我建议你使用List而不是数组来保存学生,然后你拥有的其他方法只会委托给这个列表。
public Roster(int stopPoint, Course course){
this.stopPoint = stopPoint;
this.course = course;
this.students = new ArrayList<Students>(stopPoint);
}
public String toString( ){
String res = "";
for(Student s: students){
res = res + "\n" + s.toString();
}
return course + " " + numStudents + "/" + stopPoint+res;
}
public boolean isFull( ){
return students.size() == stopPoint;
}
public boolean addStudent(Student student){
if(!student.contains(student) && !isFull()){
students.add(student);
return true;
}
return false;
}
public boolean findStudent(Student student){
return students.contains(student);
}
public boolean dropStudent(Student student){
return students.remove(student);
}