简体   繁体   中英

How do I add students to a roster (from different classes in Java)?

Brevity isn't wise here, so I made some changes and laid everything out. I'm still at my wits' end trying to figure this out and made some changes that was recommended.

I'm trying to invoke the method in the driver program to add objects called students, defined in the Student-class, to be added to a roster found in the Course-class so the driver program can print out who is in the course. The Course-class has a method called addStudent which returns a boolean expression. Depending if there is enough room to add the student, the student will be added. ArrayList-class is not allowed (ie ArrayList - something or other cannot be used).

Here's what I have:

Below is my Student class:

public class Student {
private String name;
private String iD;
private boolean tuitionPaid;
/**Constructor*/
public Student(String studentName, String studentID)
{
    name = studentName;
    iD = studentID; 
}
/**Getters and Setters are below*/
public String getStudentName()
{
    return name;
}
public void setStudentName(String studentName)
{
    name = studentName;
}
public String getStudentID()
{
    return iD;
}
public void setStudentID(String studentID)
{
    name = studentID;
}
/**The toString method below*/
public String toString()
{
    String message = name + iD;
    return message;
}
}

Below is the Course class:

public class Course {
private String name;
private int maxSize;
private String[] roster;

public Course(String courseName, int courseMaxSize)
{
    name = courseName;
    maxSize = courseMaxSize;
}


/**Getters and Setters are below*/
public String getCourseName()
{
    return name;
}
public void setCourseName(String courseName)
{
    name = courseName;
}
public int getCourseMaxSize()
{
    return maxSize;
}
public void setCourseMaxSize(int courseMaxSize)
{
    maxSize = courseMaxSize;
}
public String[] getCourseRoster()
{
    return roster;
}
public void setCourseRoster(Student s)
{
    String[] courseRoster = {s.toString()};//intended to pass the student name and ID
        roster = courseRoster;             //to the instance data variable

}

/**The toString method is below*/
public String toString()
{
    String message = name + " course has a class size of " + maxSize;
    return message;
}


/**Three requested methods are below*/
public boolean addStudent(Student s)
{

    boolean atCapacity = false;
    if(roster.length>=5)
    {
        String courseRoster[] = {s.toString()};//intended to pass the formal parameter
        roster = courseRoster;                 //to store as an instance data
        atCapacity = false;

    }
    else
    {
        atCapacity = true;      
    }
    return atCapacity;
}
public boolean dropStudent(Student s)
{
    boolean dropStudent = true;
    for( int index=0; index<roster.length - 1; index++ )//Goes through the roster
    {                                                   
        if( roster[index] == s.getStudentID() )//If a student matches, they are
        {
            s.setStudentID(null);               //dropped a student from a course
            s.setStudentName(null);             //their existence should be null
            dropStudent = true;
        }
        else
        {
            dropStudent = false;
        }
    }   
    return dropStudent;
}
public void printRoster()
{

        if(roster.length == 0)//In case there is no one in the roster
        {
            System.out.println("There is no one in this roster.");//this message prints
        }
        else
        {
        System.out.println(roster);//Everyone in class will be printed
        }
}
}

Below is the Driver Program:

public class Project2DriverProgram {

public static void main(String[] args) {

    Student[] students = new Student[5];//Creates an array of objects from the Student-class
    students[0] = new Student("Bill", "123");//Instantiates the array at 0 with actual parameters

    Course newCourse = new Course("Philosophy", 5);//Creates an object and instantiates


    newCourse.getCourseRoster();
    newCourse.setCourseRoster(students[0]); //Sets the student to be in the roster

    newCourse.addStudent(students[0]);//Adds student to the course 


    newCourse.printRoster();//prints values of the roster

}

}

After all that, I get a hashcode, or a memory address, is printed. I would like this to be expandable so that when students[1] becomes existent, then that too can easily be added to the course roster and so on.

(PS First post. Here's to not giving up. :))

You have a logic error in your code:

if(roster.length>=5){
atCapacity = true; 
String[][] roster = {{s.getStudentName()}, {s.getStudentID()}};
}
else{
    atCapacity = false;
}//end of else statement

Should be changed to:

if(!(roster.length>=5)){
 String[][] roster = {{s.getStudentName()}, {s.getStudentID()}};
 atCapacity = false;
}
else{
  atCapacity = true;
}

You had it the other way round, your current code reads "if the capacity is more than or equal to 5 then add a new entry" when infact you want to say "if the capacity is NOT more than or equal to 5 add a new entry."

Given the limited information I have from your question, I would assume that roster is a member of Course with the type Student[] which contains all students that are following the course. I have no idea why you have another local two-dimensional array of String within addStudent method though.

I would suggest to change the type of roster to List<Student> (otherwise you need to keep the count separately because array's length is fixed) and keep the capacity of the course in a class member called capacity . I would also suggest to return true for the method addStudent if the call is successful instead of the other way around.

A possible implementation for the addStudent would be:

public boolean addStudent(Student s) {
    if (this.roster.size() < this.capacity) { 
        this.roster.add(s);
        return true;
    }
    return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM