目前,我正在开发一个包含 3 个类的小型练习初学者程序,我正在尝试修改它。我想修改 Student 类,以便它计算创建的对象总数,并且我想修改类 Course 以便它包含 Student 类的数组。这是我到目前为止所拥有的;任何援助将不胜感激。我敢肯定它很明显,而且我只是很密集:
课堂课程:
public class Course {
private String courseName;
private Student[]
students = new Student[100];
private int numberOfStudents;
public Course(String courseName) {
this.courseName = courseName;
}
public void addStudent(String[] Student) {
Student[numberOfStudents] = students[];
numberOfStudents++;
}
public Student[] getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public String getCourseName() {
return courseName;
}
public void dropStudent(String student) {
}
}
班级学生:
public class Student {
private int id;
private String name;
private java.util.Date dateCreated;
public Student(int ssn, String newName) {
id = ssn;
name = newName;
dateCreated = new java.util.Date();
}
public int getID() {
return id;
}
public String getName() {
return name;
}
public java.util.Date getDateCreated() {
return dateCreated;
}
}
以及驱动类,TestCourse类:
public class TestCourse {
public static void main(String[] args) {
Course course1 = new Course("Data Structures");
Course course2 = new Course("Database Systems");
course1.addStudent("Peter Jones",);
course1.addStudent("Brian Smith");
course1.addStudent("Anne Kennedy");
course2.addStudent("Peter Jones");
course2.addStudent("Steve Smith");
System.out.println("Number of students in " +course1.getCourseName() + ": "
+ course1.getNumberOfStudents());
String[] students = course1.getStudents();
for (int i = 0; i < course1.getNumberOfStudents(); i++)
System.out.print(students[i] + ", ");
System.out.println();
System.out.print("Number of students in " +course2.getCourseName() +": "
+ course2.getNumberOfStudents());
System.out.println();
String[] students1 = course2.getStudents();
for (int i = 0; i < course2.getNumberOfStudents(); i++)
System.out.print(students1[i] + ", ");
}
}