luna621
asked on
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(c ourseToChe ck);
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 =^^=
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(c
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 =^^=
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!!
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!!
what defines whether two courses are compatible?
ASKER
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
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
ASKER
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(co urseToChec k) < 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.
/*
* 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(co
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I see your logic. I think that's what I'm looking for. Thank you!
{
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