简体   繁体   中英

Adding an object in Java

I'm not getting any errors but I couldn't add Course

    //in my Course class i use equals method to check whether they are the sam
    public boolean equals (Course other){
    Course c = (Course) other;
    if(c != null){
        if (this.name.equals(c.name) && this.instructor.equals(c.instructor) && this.numberOfSection == (c.numberOfSection) && this.year == (c.year))
            return true;
        else
            return false;   
        }
    else 
        return false;
}

//in my CourseCatalog class i use the equals method in Course and if they are not same 
// i add the course to the catalog
public void addCourse (Course other) {
    if(other != null){
        if( !other.equals(course1) && !other.equals(course2) && !other.equals(course3) && !other.equals(course4))
        {
            if (noOfCourse == 0){
                course1 = new Course(other);
                noOfCourse ++;
            }
            if (noOfCourse == 1){
                course2 = new Course(other);
                noOfCourse ++;
            }
            if (noOfCourse == 2){
                course3 = new Course(other);
                noOfCourse ++;
            }
            if(noOfCourse == 3){
                course4 = new Course(other);
                noOfCourse ++;
            }   
        }
    }
}

            //the following code is what i do in the tester class 
            CourseCatalog myCourseCatalog  = new CourseCatalog();
    Course course1 = new Course();
    course1.setName("Math101");
    course1.setInstructor("Jack Smith");
    course1.setYear(2007);
    course1.setNumberOfSection(3);
    myCourseCatalog.addCourse(course1);

            // i add a different course 
    Course course2 = new Course("Cs101", "David Brown", 2003 ,3);
    myCourseCatalog.addCourse(course2);
    Course copyCourse = new Course(course2);
    myCourseCatalog.addCourse(copyCourse);

However the program prints out in this way;

Name: Math101
Instructor: Jack Smith
Year: 2007
Number Of Sections: 3Name: Math101
Instructor: Jack Smith
Year: 2007
Number Of Sections: 3Name: Math101
Instructor: Jack Smith
Year: 2007
Number Of Sections: 3Name: Math101
Instructor: Jack Smith
Year: 2007
Number Of Sections: 3

So that means i cannot addcourse why? I'm a new student in java so I'd appreciate any help.

Wow this is a beauty...

You are using a chain of if-else which makes sure that all the if are true and gets executed. In first if you check for noOfCourse == 0 and then increment it; in the next one you check for noOfCourse==1 which will be true due to the increment.

So when you call you method addCourse first time all courses are already set.

Please Replace

if (noOfCourse == 0){
    course1 = new Course(other);
    noOfCourse ++;
}
if (noOfCourse == 1){
    course2 = new Course(other);
    noOfCourse ++;
}
if (noOfCourse == 2){
    course3 = new Course(other);
    noOfCourse ++;
}
if(noOfCourse == 3){
    course4 = new Course(other);
    noOfCourse ++;
}   

with

if (noOfCourse == 0){
    course1 = new Course(other);
    noOfCourse ++;
} else if (noOfCourse == 1){
    course2 = new Course(other);
    noOfCourse ++;
} else if (noOfCourse == 2){
    course3 = new Course(other);
    noOfCourse ++;
} else if(noOfCourse == 3){
    course4 = new Course(other);
    noOfCourse ++;
}   

The problem is that you're actually having a cascading if problem. See below...

if (noOfCourse == 0){
    course1 = new Course(other);
    noOfCourse ++;
}
if (noOfCourse == 1){
    course2 = new Course(other);
    noOfCourse ++;
}
if (noOfCourse == 2){
    course3 = new Course(other);
    noOfCourse ++;
}
if(noOfCourse == 3){
    course4 = new Course(other);
    noOfCourse ++;
}   

No matter what noOfCourse is, it will increment it, thus satisfying the if immediately below it. This causes your course to fill multiple course slots. The solution is to use else if .

if (noOfCourse == 0){
    course1 = new Course(other);
    noOfCourse ++;
} else if (noOfCourse == 1) {
    course2 = new Course(other);
    noOfCourse ++;
} else if (noOfCourse == 2) {
    course3 = new Course(other);
    noOfCourse ++;
} else if (noOfCourse == 3) {
    course4 = new Course(other);
    noOfCourse ++;
}   

This way, it will only add the course once . :-)

By the way, you should really use an array or List here.

final Count[] courses = new Course[4];
final int coursesAssigned = 0;
...
if (coursesAssigned < 4) {
  courses[coursesAssigned++] = new Course(other);
}

Additionally, why are you copying other ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM