[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

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 =^^=
0
luna621
Asked:
luna621
  • 5
  • 3
2 Solutions
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
luna621Author Commented:
I see your logic.  I think that's what I'm looking for.  Thank you!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now