Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8498557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:26:09+00:00 2026-06-11T00:26:09+00:00

I’m not getting any errors but I couldn’t add Course //in my Course class

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:26:11+00:00Added an answer on June 11, 2026 at 12:26 am

    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 ++;
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.