Comparing each item in a List

Hello!!

Trying to remember Singly Linked Lists (did I spell it right? XD).  Basically, here is my compareTo method:

/*
 * compareTo --- compares values of the input to determine order.
 * @return returns a negative number if start > finish
 *                 a positive number if start < finish
 */
    public int compareTo( Object obj )
    {
       Course data = ( Course ) obj;
       if ( finish == data.finish )
       {
          return start - data.start;
       } // end if
       return finish - data.finish;
    } // END compareTo

And I want it to do something like this (of course the following is full of errors):

/*
 * isCompatible --- returns true if this course is compatible to EVERY course already
 *                  on the list, otherwise, false
 */
    public boolean isCompatible( Course courseToCheck )
    {
        if( !(isEmpty()) )
        {
            while( head.getNext() !=null )
            {
                head.getItem().compareTo(courseToCheck);
                head = head.getNext();
            } // end while
            return true;
        } // end if
        return false;
    } // END isCompatible
-------------------------------------------------------

The problem is, courseToCheck is a Course, and head is a Node.  I want this method to look through my entire list, comparing each item in the list and return the appropriate boolean value.

Full code located here:

1. http://www2.hawaii.edu/~fklam/Java/Course.java
2. http://www2.hawaii.edu/~fklam/Java/CourseList.java
3. http://www2.hawaii.edu/~fklam/Java/Node.java

Thank you!!

-luna621 =^^=
luna621Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
take a look at this:

    public boolean isCompatible( Course courseToCheck )
    {
        if( !(isEmpty()) )
        {
            while( head.getNext() !=null )
            {
                Object obj = head.getItem();
                if( obj instanceof Course)
                {
                       //then obj is of type course
                       head.getItem().compareTo(courseToCheck);
                 }
                head = head.getNext();
            } // end while
            return true;
        } // end if
        return false;
    } // END isCompatible

does this help u?
but i can't understand what is ur requirement in this method to return
 do u want to return false if one of the head items is not of type Course?
0
objectsCommented:
  public boolean isCompatible( Course courseToCheck )
   {
       boolean compatible = true;
       Node next = head;
       while (compatible && next!=null)
       {
            Course course = (Course)  head.getItem();

            // set compatible to false if course and courseToCheck are not compatible

            next = next.getNext();
       }
       return compatible;
    } // END isCompatible
0
luna621Author Commented:
petmagdy,

Even if one of the items is not compatible (as stated in the compareTo method), the isCompatible method should return false.

I will try both suggestions.  Thank you!!
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

objectsCommented:
what defines whether two courses are compatible?
0
luna621Author Commented:
How's this?

    public boolean isCompatible( Course courseToCheck )
    {
        boolean compatible = true;
        Node next = head;
        while ( compatible && next != null )
        {
             Course course = ( Course )head.getItem();
             if( course.compareTo( courseToCheck ) < 0 )
             {
             compatible = false;
             next = next.getNext();
             }
             compatible = true;
        }
        return compatible;
    } // END isCompatible
0
luna621Author Commented:
objects,

/*
 * compareTo --- compares values of the input to determine order.
 * @return returns a negative number if start > finish
 *                 a positive number if start < finish
 */
    public int compareTo( Object obj )
    {
       Course data = ( Course ) obj;
       if ( finish == data.finish )
       {
          return start - data.start;
       } // end if
       return finish - data.finish;
    } // END compareTo

So, if the start time of one course is greater than the finish time of another course, compareTo() will return a negative number.  Anytime currentCourse.compareTo(courseToCheck) < 0, it should return a "false" compatibility.  Otherwise, they are compatible and shoudl return a "true" compatibility.  This should keep checking until all elements in the list have been checked.  If even one is found not compatible, this method should exit the loop and return a "false" compatibility.
0
luna621Author Commented:
Oh, now that I read that again I think the method should be:

    public boolean isCompatible( Course courseToCheck )
    {
        boolean compatible = true;
        Node next = head;
        while ( compatible && next != null )
        {
             Course course = ( Course )head.getItem();
             if( course.compareTo( courseToCheck ) > 0 )
             {
             compatible = true;
             next = next.getNext();
             }
             compatible = false;
             break;
        }
        return compatible;
    } // END isCompatible
0
objectsCommented:
   public boolean isCompatible( Course courseToCheck )
    {
        boolean compatible = true;
        Node next = head;
        while ( compatible && next != null )
        {
             Course course = ( Course )head.getItem();
             if( course.compareTo( courseToCheck ) <= 0 )
             {
                compatible = false;
             }
             next = next.getNext();
        }
        return compatible;
    } // END isCompatible
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
luna621Author Commented:
I see your logic.  I think that's what I'm looking for.  Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.