美好的一天,我与课程和学生有联系,该学生可以参加很多课程,并且一门课程有很多学生。学生班具有一个可比较的接口,该接口比较姓氏,因此在输出时,我可以使用collections.sort()将名字排序为姓氏。因此,当我使用dirver类中的displayClassList()方法将来自Courses类的学生放出时,输出不会显示任何内容。代码中缺少什么吗?
这些是类:
司机:
import java.util.*;
import java.beans.*;
import java.io.*;
import javax.swing.JOptionPane;
import java.beans.XMLDecoder;
import java.io.FileInputStream;
import java.util.*;
public class As3A_Driver
{
public static void main(String[] args)
throws IOException {
// list of people
Student harry = new Student("Harry", "Potter", 2, 3.65);
Student ron = new Student("Ron", "Weasley", 2, 2.21);
Student hermione = new Student("Hermione", "Granger", 2, 4.5);
Student cho = new Student("Cho", "Chiang", 3, 4.05);
Student ginny = new Student("Ginny", "Weasley", 1, 3.9);
Instructor snape = new Instructor("Severus", "Snape", "M.Sc.");
Instructor mcg = new Instructor("Minerva", "McGonigal", "Ph.D.");
ArrayList<Person> people = new ArrayList<>();
people.add(harry); people.add(ron); people.add(hermione);
people.add(ginny); people.add(cho);
// courses and associations
Course course1 = new Course("WIZ-2666", "Potions II");
Course course2 = new Course("WIZ-2989", "Transfiguration");
course1.addInstructor(snape);
harry.addCourse(course1);
ron.addCourse(course1);
hermione.addCourse(course1);
cho.addCourse(course1);
course2.addInstructor(mcg);
harry.addCourse(course2);
ron.addCourse(course2);
hermione.addCourse(course2);
ginny.addCourse(course2);
// display class list
course1.displayClassList();
course2.displayClassList();
}
}
培训班:
import java.util.*;
public class Course
{
private String courseNumber;
private String courseName;
private ArrayList<Instructor> instructor;
private ArrayList<Student> student;
public Course(){
courseNumber = courseName = "Unknown";
}
public Course (String courseName, String courseNumber){
this.courseNumber=courseNumber;
this.courseName=courseName;
this.student=new ArrayList();
this.instructor = new ArrayList();
}
public String getCourseNumber(){
return courseNumber;
}
public String getCourseName(){
return courseName;
}
public void setCourseNumber(){
this.courseNumber=courseNumber;
}
public void setCourseName(){
this.courseName=courseName;
}
public void setStudent(ArrayList<Student> student){
this.student=student;
}
public ArrayList<Student> student(){
return student;
}
public void addStudent(Student students){
student.add(students);
}
public void addInstructor(Instructor i){
instructor.add(i);
}
public void displayClassList(){
System.out.println(getCourseName()+" "+ getCourseNumber());
for(Instructor i: instructor){
System.out.println("Instructor: "+i+", "+ i.getDegree());
}
System.out.println("Students:");
for(Student s :student){
Collections.sort(student);
System.out.println(s.toString());
}
}
}
讲师:
import java.util.*;
public class Instructor extends Person
{
private String degree;
public ArrayList <Course> courses;
public Instructor instructor;
public Instructor(){
firstName= lastName = degree= "unknown";
}
public Instructor (String F ,String L,String degree){
super(F,L);
this.degree=degree;
this.courses=new ArrayList();
}
public void setCourses(ArrayList<Course> courses){
this.courses=courses;
}
public ArrayList<Course> courses(){
return courses;
}
public void addCourses (Course c){
courses.add(c);
}
public String toString(){
return lastName+","+firstName;
}
public String getDegree(){
return degree;
}
public void setDegree(){
this.degree=degree;
}
}
人:
import java.util.*;
public abstract class Person
{
protected String firstName;
protected String lastName;
public Person(){
this.firstName=lastName = "Unknown";
}
public Person(String F ,String L){
firstName=F;
lastName=L;
}
public void setFirstName(){
this.firstName=firstName;
}
public void setLastName(){
this.lastName=lastName;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public abstract String toString();
public String getName(){
return firstName+" "+lastName;
}
}
学生:
import java.util.*;
public class Student extends Person implements Comparable<Student>
{
protected int year;
protected double gpa;
protected ArrayList<Course> course;
public Student(){
year = 0;
gpa = 0.0;
firstName= lastName= "unknown";
}
public Student(String F ,String L,int y, double g){
super(F,L);
this.year=y;
this.gpa=g;
course = new ArrayList();
}
public void setYear(){
this.year=year;
}
public int getYear(){
return year;
}
public void addCourse(Course c){
course.add(c);
}
public void setCourses(ArrayList<Course> courses){
this.course=courses;
}
public ArrayList<Course> course(){
return course;
}
public void setGpa(){
this.gpa=gpa;
}
public double getGpa(){
return gpa;
}
public String getInfo(){
return getName()+" "+year+" "+gpa;
}
public String toString(){
return lastName+","+firstName;
}
public int compareTo(Student lt){
int lastName = this.lastName.compareTo(lt.lastName);
return lastName == 0 ? this.firstName.compareTo(lt.firstName ): lastName;
}
}
最佳答案
您可以尝试以下方法:
// add students to courses
course1.addStudent(harry);
course1.addStudent(ron);
course1.addStudent(hermione);
course1.addStudent(cho);
course2.addStudent(harry);
course2.addStudent(ron);
course2.addStudent(hermione);
course2.addStudent(ginny);
// display class list
course1.displayClassList();
course2.displayClassList();
输出:
注意:我已经将学生添加到课程中。除此之外,我还修改了
displayClassList()
方法,如下所示:public void displayClassList(){
System.out.println(getCourseName()+" "+ getCourseNumber());
for(Instructor i: instructor){
System.out.println("Instructor: "+i+", "+ i.getDegree());
}
System.out.println("Students:");
Collections.sort(student);
for(Student s :student){
System.out.println(s.toString());
}
}