简体   繁体   English

如何将学生添加到名册中(来自Java中的不同班级)?

[英]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. 我正在尝试在驱动程序中调用该方法,以添加要在“学生”类中定义的名册中添加的“学生”类中定义的名为“ students”的对象,以便驱动程序可以打印出课程中的人。 The Course-class has a method called addStudent which returns a boolean expression. Course类有一个名为addStudent的方法,该方法返回布尔表达式。 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). 不允许使用ArrayList-class(即ArrayList-不能使用某些东西)。

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. 我希望它可以扩展,以便当students [1]存在时,也可以轻松地将其添加到课程表中,依此类推。

(PS First post. Here's to not giving up. :)) (PS第一篇文章。这是不放弃的。:))

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." 相反,您的当前代码显示“如果容量大于或等于5,然后添加一个新条目”,实际上您想说“如果容量不大于或等于5,则添加一个新条目”。条目。”

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. 鉴于我从您的问题中获得的信息有限,我将假定roster是“ Student[] ”类型的“ Course成员,其中包含所有正在学习该课程的学生。 I have no idea why you have another local two-dimensional array of String within addStudent method though. 我不知道为什么在addStudent方法中为什么还有另一个本地的String二维数组。

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 . 我建议将roster的类型更改为List<Student> (否则,由于数组的长度是固定的,因此您需要单独保存计数),并将课程的容量保留在名为capacity的类成员中。 I would also suggest to return true for the method addStudent if the call is successful instead of the other way around. 如果调用成功,我也建议为方法addStudent返回true ,而不是相反。

A possible implementation for the addStudent would be: addStudent可能实现是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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