繁体   English   中英

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

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

简洁在这里并不明智,因此我进行了一些更改并将所有内容都布置好。 我仍然不知所措,试图弄清楚这一点,并建议进行一些更改。

我正在尝试在驱动程序中调用该方法,以添加要在“学生”类中定义的名册中添加的“学生”类中定义的名为“ students”的对象,以便驱动程序可以打印出课程中的人。 Course类有一个名为addStudent的方法,该方法返回布尔表达式。 根据是否有足够的空间添加学生,将添加学生。 不允许使用ArrayList-class(即ArrayList-不能使用某些东西)。

这是我所拥有的:

以下是我的学生班:

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

以下是课程课程:

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

以下是驱动程序:

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

}

}

毕竟,我得到一个哈希码或一个内存地址,然后打印出来。 我希望它可以扩展,以便当students [1]存在时,也可以轻松地将其添加到课程表中,依此类推。

(PS第一篇文章。这是不放弃的。:))

您的代码中存在逻辑错误:

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

应更改为:

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

相反,您的当前代码显示“如果容量大于或等于5,然后添加一个新条目”,实际上您想说“如果容量不大于或等于5,则添加一个新条目”。条目。”

鉴于我从您的问题中获得的信息有限,我将假定roster是“ Student[] ”类型的“ Course成员,其中包含所有正在学习该课程的学生。 我不知道为什么在addStudent方法中为什么还有另一个本地的String二维数组。

我建议将roster的类型更改为List<Student> (否则,由于数组的长度是固定的,因此您需要单独保存计数),并将课程的容量保留在名为capacity的类成员中。 如果调用成功,我也建议为方法addStudent返回true ,而不是相反。

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