-1

I am using my main method from class Test and am trying to call a function in an object, Course. This is the code from my main method:

if (decideStatus > .75) {
                status = Classification.GRAD;
                gradCount++;
                if (gpa > 3.2) {
                    Student newStudent = new Student(sID, status, gpa);
                    **addStudentToRoster(newStudent);**
                } else {
                    gradRejectCount++;
                }

I am trying to use the addStudentToRoster function to add newStudent to the private variable

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);
    }

The addStudentToRoster(newStudent) line has an error stating that that method is undefined for that class. Does anyone know how I would fix this?

user3586248
  • 163
  • 1
  • 14

1 Answers1

2

Since "addStudentToRoster" is defined inside the Course class, you need to create an instance of that class and modify the problem line of code to be something like:

Course comp101 = new Course(1, "comp101", 80, 2.0);
comp101.addStudentToRoster(newStudent);
Taelsin
  • 1,030
  • 12
  • 24