【发布时间】: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