这里的简洁并不明智,所以我做了一些改变,把一切都搞清楚了。 我仍然在我的智慧'最终试图解决这个问题并做出一些建议的改变。
我试图在驱动程序中调用该方法来添加在Student-class中定义的名为students的对象,以添加到Course-class中找到的名单中,以便驱动程序可以打印出谁在课程中。 Course-class有一个名为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
}
}
毕竟,我得到一个哈希码或一个内存地址。我希望这是可扩展的,这样当学生[1]成为现存时,那么也很容易被添加到课程名单中等等。
(P.S第一篇文章。这里不放弃。:))
答案 0 :(得分: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;
}
你反过来了,你当前的代码读取&#34;如果容量大于或等于5,那么添加一个新条目&#34;当你想要说&#34;如果容量不大于或等于5,添加一个新条目。&#34;
答案 1 :(得分:0)
鉴于我的问题信息有限,我认为roster
是Course
的成员,其类型为Student[]
,其中包含所有关注该课程的学生。我不知道你为什么在addStudent
方法中有另一个局部二维String数组。
我建议将roster
的类型更改为List<Student>
(否则您需要单独保留计数,因为数组的长度是固定的)并保持课程的容量类成员名为capacity
。如果调用成功而不是相反,我还建议返回true
方法addStudent
。
addStudent
的可能实现方式是:
public boolean addStudent(Student s) {
if (this.roster.size() < this.capacity) {
this.roster.add(s);
return true;
}
return false;
}