[英]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.