简洁在这里并不明智,所以我做了一些更改并列出了所有内容。 我仍然束手无策,试图解决这个问题,并做了一些建议的更改。
我正在尝试调用驱动程序中的方法来添加在 Student 类中定义的名为 Students 的对象,并将其添加到 Course 类中找到的花名册中,以便驱动程序可以打印出谁在类(class)。 Course 类有一个名为 addStudent 的方法,它返回一个 boolean 表达式。根据是否有足够的空间来添加学生,将添加学生。不允许使用 ArrayList 类(即 ArrayList - 不能使用某些东西)。
这是我所拥有的:
下面是我的学生类(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;
}
}
以下是类(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
}
}
}
下面是驱动程序:
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]存在时,也可以轻松地将其添加到类(class)名册等中。
(P.S 第一篇文章。这是为了不要放弃。:))
最佳答案
您的代码中有逻辑错误:
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”添加一个新条目。”
关于java - 如何将学生添加到名册(来自 Java 的不同类(class))?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31373881/