我正在使用Test类中的main方法,并尝试调用对象Course中的函数。这是我的主要方法的代码:
if (decideStatus > .75) {
status = Classification.GRAD;
gradCount++;
if (gpa > 3.2) {
Student newStudent = new Student(sID, status, gpa);
**addStudentToRoster(newStudent);**
} else {
gradRejectCount++;
}
我正在尝试使用addStudentToRoster函数将newStudent添加到私有变量
public class Course {
private int number = 0;
private String title = "";
private int capacity = 0;
//won't compile with float type for cutoff
private float cutoff = 0;
private ArrayList<Student> roster = new ArrayList<>();
private ArrayList<Student> waitlist = new ArrayList<>();
public Course(int number, String name, int maxSize, float cutoff) {
//set value of private fields
this.number = number;
this.title = name;
this.capacity = maxSize;
this.cutoff = cutoff;
}
public void addStudentToRoster(Student student) {
roster.add(student);
}
addStudentToRoster(newStudent)行有一个错误,指出该类的方法未定义。有谁知道我会如何解决这个问题?
答案 0 :(得分:2)
自&#34; addStudentToRoster&#34;在Course类中定义,您需要创建该类的实例并将问题行代码修改为:
Course comp101 = new Course(1, "comp101", 80, 2.0);
comp101.addStudentToRoster(newStudent);