【问题标题】:How do I add students to a roster (from different classes in Java)?如何将学生添加到名册(来自 Java 中的不同班级)?
【发布时间】:2015-07-13 00:20:28
【问题描述】:

简洁在这里并不明智,因此我做了一些更改并列出了所有内容。 我仍然束手无策,试图弄清楚这一点并做出了一些建议的更改。

我正在尝试调用驱动程序中的方法来添加在 Student 类中定义的称为学生的对象,以将其添加到 Course 类中的花名册中,以便驱动程序可以打印出谁在课程。 Course 类有一个名为 addStudent 的方法,它返回一个布尔表达式。根据是否有足够的空间来添加学生,将添加学生。不允许使用 ArrayList 类(即 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

}

}

毕竟,我得到了一个哈希码,或者一个内存地址,被打印出来了。我希望这是可扩展的,以便当学生 [1] 存在时,也可以轻松地将其添加到课程名册中等等。

(P.S 第一篇文章。这是为了不放弃。:))

【问题讨论】:

  • 第一个问题:既然课程名册是一个学生列表,为什么不声明为Student的列表或数组?我不确定你为什么要让它成为String 的二维数组。这只会使事情复杂化。
  • 这段代码是什么的一部分?你应该至少包括你的课程大纲,因为当前版本很难遵循。为什么在看起来不像 Course 类的东西中间有 addStudent 的定义?
  • String[][] roster 是该方法的本地,并在您退出该函数时收集垃圾。它应该在这个方法之外。你必须弄清楚如何操作这个数组。

标签: java arrays class object methods


【解决方案1】:

您的代码中有逻辑错误:

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添加一个新条目。”

【讨论】:

  • 这有几个问题: (1) if (!roster.length&gt;=5) 不编译。 (2) 在这种情况下无论如何都没有人使用!;他们会将其更改为if (roster.length&lt;5). (3) 这根本不能解决主要问题;分配给本地 roster 的代码并不能满足提问者的需求。
  • @ajb 否定一个声明是完全合法的。只是忘记了多余的括号。我已经更新了我的答案以包括它们。事实仍然是该学生之前没有被添加,因为他的逻辑相反。也许您可以帮助提供解决方案,而不是投反对票。
  • 不,没有添加学生,因为应该添加学生的语句完全错误,您在答案中保留了该语句。而且我几乎总是对错误的答案投反对票,因为它们无法编译。虽然您说该语句现在是合法的是对的,但既然您已经对其进行了编辑,那么当他们可以使用不同的比较时,仍然没有经验丰富的程序员会编写这样的代码。
  • @ajb 听着 AJB,我不打算与你讨论谁是最有经验的程序员,也不会反驳你关于有经验的程序员做什么和不做什么的毫无根据的猜想。我毫不掩饰我编辑了我最初的答案以包括最初省略的额外括号的事实,因此在您的评论中提请注意这一点在所有帐户中都是多余的。您最好将自己的意见保密,尤其是当您考虑到即使您也很可能在某些时候运行过我帮助开发的代码。先生,您好。
【解决方案2】:

鉴于我从您的问题中获得的有限信息,我假设rosterCourse 的成员,其类型为Student[],其中包含所有正在学习该课程的学生。我不知道为什么在addStudent 方法中有另一个本地二维字符串数组。

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

addStudent 的可能实现是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 2017-10-19
    相关资源
    最近更新 更多