正在写一个关于Java的作业,大部分已经弄好了,但是undergrad student 却加不上课,不知道是什么写错了,哪位同学帮忙看看,
感激不尽。下面是我写的Java code
person class
public abstract class Person {
protected String name;
protected int UFID;
protected String dob;
public Person(String name, int UFID, String dob) {
this.name = name;
this.UFID = UFID;
this.dob = dob;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUFID() {
return UFID;
}
public void setUFID(int UFID) {
this.UFID = UFID;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String toString() {
String str = " Name: " + this.name +"\n"
+ " UFID: " + this.UFID + "\n"
+ " D.O.B: " + this.dob + "\n" ;
return str;
}
public boolean equals(Object ob) {
if(ob instanceof Person) {
Person p = (Person) ob;
if (this.name.equals(p.getName()) && this.UFID == p.getUFID()){
return true;
}
else return false;
}
else return false;
}
}
Student class
public abstract class Student extends Person {
protected Course[] courses;
protected final int maxCourses = 4;
protected int numCoursesEnrolled;
protected double gpa;
public Student(String name, int UFID, String dob, double gpa) {
super(name, UFID, dob);
this.gpa = gpa;
this.courses = new Course[maxCourses];
numCoursesEnrolled = 0;
}
public Student(String name, int UFID, String dob, double gpa, Course[] courses) {
super(name, UFID, dob);
this.courses = new Course[maxCourses];
this.gpa = gpa;
for(int i=0; i<courses.length; i++) {
if(courses[i] != null){
numCoursesEnrolled ++;
}
}
}
public int getNumCoursesEnrolled() {
return numCoursesEnrolled;
}
public void setNumCoursesEnrolled(int numCoursesEnrolled) {
this.numCoursesEnrolled = numCoursesEnrolled;
}
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public abstract boolean addCourse(Course course);
public boolean dropCourse(Course course){
if(course.removeStudent(this)) {
for(int i=0; i<courses.length; i++) {
if(courses[i] == course){
courses[i] = null;
numCoursesEnrolled--;
return true;
}
else return false;
}
}
else return false;
return false;
}
public String toString() {
String str = "";
str += super.toString();
str += " GPA: " + gpa + "\n";
str += "Courses enrolled in: " + "\n";
for(int i = 0; i<courses.length; i++){
if(courses[i] != null){
str += "Course " + (i+1) + ":\n";
str += courses[i].toString() + "\n";
}
else if(courses[i] == null){
str += "Course " + (i+1) + ":\n";
str += "null \n";
}
}
return str;
}
}
UndergradStudent class
public class UndergradStudent extends Student {
public UndergradStudent(String name, int UFID, String dob, double gpa) {
super(name, UFID, dob, gpa);
}
public UndergradStudent(String name, int UFID, String dob, double gpa, Course[] courses)
{
super(name, UFID, dob, gpa, courses);
}
public boolean addCourse(Course course) {
for(int i=0; i<courses.length; i++) {
if(courses[i] != null){
if(course.getNumber() < 5000 && super.getNumCoursesEnrolled() < 4 && course.addStudent(this)){
courses[i] = course;
numCoursesEnrolled++;
return true;
}
else return false;
}return false;
}return false;
}
public String toString() {
String str = " Undergrad Student: \n";
str += super.toString() + "\n";
return str;
}
}
GradStudent class
public class GradStudent extends Student {
private Course courseTA;
public GradStudent(String name, int UFID, String dob, double gpa) {
super(name, UFID, dob, gpa);
}
public GradStudent(String name, int UFID, String dob, double gpa, Course[] courses){
super(name, UFID, dob, gpa, courses);
}
public GradStudent(String name, int UFID, String dob, double gpa, Course[] courses, Course courseTA) {
super(name, UFID, dob, gpa, courses);
this.courseTA = courseTA;
}
public Course getCourseTA() {
return courseTA;
}
public void setCourseTA(Course courseTA) {
this.courseTA = courseTA;
}
public boolean addCourse(Course course) {
if(course.getNumber() >= 5000 && super.getNumCoursesEnrolled() < 4 && course.addStudent(this)){
for(int i=0; i<courses.length; i++) {
if(courses[i] == null){
courses[i] = course;
numCoursesEnrolled++;
return true;
}
else return false;
}
} return false;
}
public String toString() {
String str = " Grad Student: \n";
str += super.toString() + "\n";
str += courseTA.toString();
return str;
}
}
Instructor class
public class Instructor extends Person {
private Course course;
public Instructor(String name, int UFID, String dob){
super(name, UFID, dob);
}
public Instructor(String name, int UFID, String dob, Course course){
super(name, UFID, dob);
this.course = course;
}
public void setCourse(Course course){
this.course = course;
}
public Course getCourse(){
return course;
}
public String getName() {
return name;
}
public String toString(){
String str = "Instructor: Instructor: \n" + super.toString();
str += "Course Being Taught: " + "\n";
str += course.toString();
return str;
}
}
Course class
public class Course {
private String type;
private String title;
private int number;
private int numCredits;
private Instructor instructor;
private GradStudent[] TAs;
private Student[] students;
private int capacity;
private int currentEnrollment;
public Course(String type, int number, String title, int numCredits) {
this.type = type;
this.number = number;
this.title = title;
this.numCredits = numCredits;
}
public Course(String type, int number, String title, int numCredits, Instructor instructor, GradStudent[] TAs, int capacity) {
students = new Student[capacity];
this.type = type;
this.number = number;
this.title = title;
this.numCredits = numCredits;
this.instructor = instructor;
this.TAs = TAs;
this.capacity = capacity;
}
public boolean addStudent(Student student) {
for(int i=0; i<students.length; i++) {
if(students[i] != null){
if(this.getCurrentEnrollment() < this.getCapacity()) {
students[i] = student;
currentEnrollment++;
return true;
}
else return false;
}
}
return false;
}
public boolean removeStudent(Student student) {
for(int i=0; i<students.length; i++) {
if(this.equals(students[i])){
students[i] = null;
currentEnrollment--;
return true;
}
else return false;
}return false;
}
public void setType(String type) {
this.type = type;
}
public String getType() {
return type;
}
public void setNumber(int number) {
this.number = number;
}
public int getNumber() {
return number;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setNumCredits(int numCredits) {
this.numCredits = numCredits;
}
public int getNumCredits() {
return numCredits;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
Student[] students2 = new Student[capacity];
for(int i = 0; i<students.length; i++){
if(students[i] != null){
students2[i] = students[i];
}
}
students = students2;
}
public int getCapacity() {
return capacity;
}
public void setCurrentEnrollment(int currentEnrollment) {
this.currentEnrollment = currentEnrollment;
}
public int getCurrentEnrollment() {
return currentEnrollment;
}
public void setStudents(Student[] students) {
this.students = students;
}
public Student[] getStudents() {
return students;
}
public void setInstructor(Instructor instructor) {
this.instructor = instructor;
this.instructor.setCourse(this);
}
public Instructor getInstructor() {
return instructor;
}
public void setTAs(GradStudent TAs[]) {
this.TAs = TAs;
}
public GradStudent[] getTAs() {
return TAs;
}
public String toString() {
String str = "";
str += "Course Info: " + "\n";
str += type + number + "\n";
str += "Title: " + title + "\n";
str += "Instructor: " + instructor.getName() + "\n";
str += "TAs:" + "\n";
for(int i = 0; i<TAs.length; i++) {
str += TAs[i].getName() + "\n";
}
str += "Number of Students: " + currentEnrollment + "\n";
str += "Capacity: " + capacity;
return str;
}
}
University class
public class University {
private String name;
private String currentTerm;
private int year;
private Course[] courses;
private Student[] students;
private Instructor[] instructors;
public University (String name, String currentTerm, int year, Student[] students, Instructor[] instructors, Course[] courses) {
this.name = name;
this.currentTerm = currentTerm;
this.year = year;
this.students = students;
this.instructors = instructors;
this.courses = courses;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setCurrentTerm(String currentTerm) {
this.currentTerm = currentTerm;
}
public String getCurrentTerm() {
return currentTerm;
}
public void setYear(int year) {
this.year = year;
}
public int getYear() {
return year;
}
public void setCourses(Course[] courses) {
this.courses = courses;
}
public Course[] getCourses() {
return courses;
}
public void setStudents(Student[] students) {
this.students = students;
}
public Student[] getStudents() {
return students;
}
public void setInstructors(Instructor[] instructors) {
this.instructors = instructors;
}
public Instructor[] getInstructors() {
return instructors;
}
public String toString() {
String str = "";
str += "University Name: " + name + "\n";
str += "Current Term: " + currentTerm + "\n";
str += "Current Year: " + year + "\n";
str += "Number of Students: " + students.length + "\n";
str += "Number of Instructors: " + instructors.length + "\n";
str += "Number of Courses: " + courses.length;
return str;
}
}
下面是TestDriver1
import java.util.Calendar;
public class TestDriver1 {
public static void main(String[] args) {
// Create some Undergrad Students
UndergradStudent student1 = new UndergradStudent("John Smith", 871311, new String("1990/01/21"), 3.5);
UndergradStudent student2 = new UndergradStudent("Peter Parker", 871312, new String("1989/02/03"), 4.0);
UndergradStudent student3 = new UndergradStudent("Rachel Smith", 871320, new String("1990/10/01"), 3.8);
UndergradStudent student4 = new UndergradStudent("Karen Hicks", 871322, new String("1991/12/31"), 3.6);
// Create some grad Students
GradStudent gs1 = new GradStudent("Chao Chen", 118491, new String("1984/05/05"), 3.8);
GradStudent gs2 = new GradStudent("Mike Smith", 128191, new String("1986/01/02"), 3.7);
GradStudent gs3 = new GradStudent("Nathan Rodriguez", 714133, new String("1984/04/04"), 3.7);
GradStudent gs4 = new GradStudent("Katy Chu", 144211, new String("1987/08/03"), 3.9);
GradStudent gs5 = new GradStudent("Jay Sanders", 144215, new String("1987/11/03"), 3.5);
// Create some instructors
Instructor i1 = new Instructor("Shayan Javed", 119042, new String("1985/01/01"));
Instructor i2 = new Instructor("Jack Davis", 119042, new String("1975/10/11"));
// Create an undergrad course
GradStudent[] tas = {gs1, gs2};
UndergradStudent[] students1 = {student1, student3};
System.out.println("---Creating undergrad course1 with capacity 2---");
Course course1 = new Course("COP", 3503, "Programming Fundamentals", 3, i1, tas, 2);
System.out.println("--" + course1.getType() + course1.getNumber() + ": " + course1.getTitle());
// Set the course TA for the grad students
gs1.setCourseTA(course1);
gs2.setCourseTA(course1);
// Try enrolling some students
System.out.println("Trying to add student1 to course1: " + student1.addCourse(course1));
System.out.println("Trying to add gs1 to course1: " + gs1.addCourse(course1));
System.out.println("Trying to add student3 to course1: " + student3.addCourse(course1));
System.out.println("Trying to add student2 to course1: " + student2.addCourse(course1));
// Increase capacity of course1
System.out.println("-Increasing capacity of course1 to 3--");
course1.setCapacity(3);
System.out.println("Trying to add student2 to course1: " + student2.addCourse(course1));
System.out.println("student4 trying to drop course1: " + student4.dropCourse(course1));
System.out.println("student1 trying to drop course1: " + student1.dropCourse(course1));
System.out.println("Trying to add student4 to course1: " + student4.addCourse(course1));
// Create a grad course
GradStudent[] tas2 = {gs3};
System.out.println("\n---Creating grad course2 with capacity 2---");
Course course2 = new Course("CAP", 5024, "Advanced Networking", 3, i2, tas2, 2);
System.out.println("--" + course2.getType() + course2.getNumber() + ": " + course2.getTitle());
// Set the course TA for the grad students
gs3.setCourseTA(course2);
// Try enrolling some students
System.out.println("Trying to add student1 to course2: " + student1.addCourse(course2));
System.out.println("Trying to add gs1 to course2: " + gs1.addCourse(course2));
System.out.println("Trying to add gs2 to course2: " + gs2.addCourse(course2));
System.out.println("Trying to add gs4 to course2: " + gs4.addCourse(course2));
//System.out.println("\n--Printing out course2 info:--");
//System.out.println(course2);
// Increase capacity of course2
System.out.println("-Increasing capacity of course2 to 4--");
course2.setCapacity(4);
System.out.println("Trying to add gs4 to course2: " + gs4.addCourse(course2));
System.out.println("gs5 trying to drop course2: " + gs5.dropCourse(course2));
System.out.println("gs1 trying to drop course2: " + gs1.dropCourse(course2));
System.out.println("Trying to add gs5 to course2: " + gs5.addCourse(course2));
//System.out.println("\n--Printing out course2 info:--");
//System.out.println(course2);
// Create another undergrad course
Instructor i3 = new Instructor("Larry David", 119080, new String("1965/02/15"));
Student[] students2 = {student1, student2, student3, student4, gs3};
GradStudent[] tas3 = {gs4, gs5};
System.out.println("\n---Creating undergrad course3 with capacity 10---");
Course course3 = new Course("SOC", 1001, "Sociology 101", 3, i3, tas3, 10);
System.out.println("--" + course3.getType() + course3.getNumber() + ": " + course3.getTitle());
gs4.setCourseTA(course3);
gs5.setCourseTA(course3);
for (Student s : students2) {
System.out.println("Trying to add " + s.getName() + " to " + course3.getType() + course3.getNumber() + ": " + s.addCourse(course3));
}
// Print out all the students info. (Test polymorphism)
Student[] students = {student1, student2, student3, student4, gs1, gs2, gs3, gs4, gs5};
System.out.println("\n--Printing out Students info--");
for (Student s : students)
System.out.println("\n" + s);
// Create the University object
Course[] courses = {course1, course2, course3};
Instructor[] instructors = {i1, i2, i3};
University ufl = new University("University of Florida", "Spring", 2012, students, instructors, courses);
System.out.println("\n--Printing out University info--");
System.out.println(ufl);
}
}
在运行完testdriver1时,我的输出数据和正确答案完全不同,下面是正确的输出数据
---Creating undergrad course1 with capacity 2--- --COP3503: Programming Fundamentals Trying to add student1 to course1: true Trying to add gs1 to course1: false Trying to add student3 to course1: true Trying to add student2 to course1: false -Increasing capacity of course1 to 3-- Trying to add student2 to course1: true student4 trying to drop course1: false student1 trying to drop course1: true Trying to add student4 to course1: true ---Creating grad course2 with capacity 2--- --CAP5024: Advanced Networking Trying to add student1 to course2: false Trying to add gs1 to course2: true Trying to add gs2 to course2: true Trying to add gs4 to course2: false -Increasing capacity of course2 to 4-- Trying to add gs4 to course2: true gs5 trying to drop course2: false gs1 trying to drop course2: true Trying to add gs5 to course2: true ---Creating undergrad course3 with capacity 10--- --SOC1001: Sociology 101 Trying to add John Smith to SOC1001: true Trying to add Peter Parker to SOC1001: true Trying to add Rachel Smith to SOC1001: true Trying to add Karen Hicks to SOC1001: true Trying to add Nathan Rodriguez to SOC1001: false --Printing out Students info-- Undergrad Student: Name: John Smith UFID: 871311 D.O.B: 1990/01/21 GPA: 3.5 Courses enrolled in: Course 1: Course Info: SOC1001 Title: Sociology 101 Instructor: Larry David TAs: Katy Chu Jay Sanders Number Of Students: 4 Capacity: 10 Course 2: null Course 3: null Course 4: null Undergrad Student: Name: Peter Parker UFID: 871312 D.O.B: 1989/02/03 GPA: 4.0 Courses enrolled in: Course 1: Course Info: COP3503 Title: Programming Fundamentals Instructor: Shayan Javed TAs: Chao Chen Mike Smith Number Of Students: 3 Capacity: 3 Course 2: Course Info: SOC1001 Title: Sociology 101 Instructor: Larry David TAs: Katy Chu Jay Sanders Number Of Students: 4 Capacity: 10 Course 3: null Course 4: null Undergrad Student: Name: Rachel Smith UFID: 871320 D.O.B: 1990/10/01 GPA: 3.8 Courses enrolled in: Course 1: Course Info: COP3503 Title: Programming Fundamentals Instructor: Shayan Javed TAs: Chao Chen Mike Smith Number Of Students: 3 Capacity: 3 Course 2: Course Info: SOC1001 Title: Sociology 101 Instructor: Larry David TAs: Katy Chu Jay Sanders Number Of Students: 4 Capacity: 10 Course 3: null Course 4: null Undergrad Student: Name: Karen Hicks UFID: 871322 D.O.B: 1991/12/31 GPA: 3.6 Courses enrolled in: Course 1: Course Info: COP3503 Title: Programming Fundamentals Instructor: Shayan Javed TAs: Chao Chen Mike Smith Number Of Students: 3 Capacity: 3 Course 2: Course Info: SOC1001 Title: Sociology 101 Instructor: Larry David TAs: Katy Chu Jay Sanders Number Of Students: 4 Capacity: 10 Course 3: null Course 4: null Grad Student: Name: Chao Chen UFID: 118491 D.O.B: 1984/05/05 GPA: 3.8 Courses enrolled in: Course 1: null Course 2: null Course 3: null Course 4: null Course TA for: Course Info: COP3503 Title: Programming Fundamentals Instructor: Shayan Javed TAs: Chao Chen Mike Smith Number Of Students: 3 Capacity: 3 Grad Student: Name: Mike Smith UFID: 128191 D.O.B: 1986/01/02 GPA: 3.7 Courses enrolled in: Course 1: Course Info: CAP5024 Title: Advanced Networking Instructor: Jack Davis TAs: Nathan Rodriguez Number Of Students: 3 Capacity: 4 Course 2: null Course 3: null Course 4: null Course TA for: Course Info: COP3503 Title: Programming Fundamentals Instructor: Shayan Javed TAs: Chao Chen Mike Smith Number Of Students: 3 Capacity: 3 Grad Student: Name: Nathan Rodriguez UFID: 714133 D.O.B: 1984/04/04 GPA: 3.7 Courses enrolled in: Course 1: null Course 2: null Course 3: null Course 4: null Course TA for: Course Info: CAP5024 Title: Advanced Networking Instructor: Jack Davis TAs: Nathan Rodriguez Number Of Students: 3 Capacity: 4 Grad Student: Name: Katy Chu UFID: 144211 D.O.B: 1987/08/03 GPA: 3.9 Courses enrolled in: Course 1: Course Info: CAP5024 Title: Advanced Networking Instructor: Jack Davis TAs: Nathan Rodriguez Number Of Students: 3 Capacity: 4 Course 2: null Course 3: null Course 4: null Course TA for: Course Info: SOC1001 Title: Sociology 101 Instructor: Larry David TAs: Katy Chu Jay Sanders Number Of Students: 4 Capacity: 10 Grad Student: Name: Jay Sanders UFID: 144215 D.O.B: 1987/11/03 GPA: 3.5 Courses enrolled in: Course 1: Course Info: CAP5024 Title: Advanced Networking Instructor: Jack Davis TAs: Nathan Rodriguez Number Of Students: 3 Capacity: 4 Course 2: null Course 3: null Course 4: null Course TA for: Course Info: SOC1001 Title: Sociology 101 Instructor: Larry David TAs: Katy Chu Jay Sanders Number Of Students: 4 Capacity: 10 --Printing out University info-- University Name: University of Florida Current Term: Spring Current Year: 2012 Number of Students: 9 Number of Instructors: 3 Number of Courses: 3
我知道帖子很长,但是急需,要是哪位同学帮我修改的话将感激不尽 可以给我发邮件或者QQ联系: rayy2007@hotmail.com ; QQ:1834972557