CMSC 132A Lecture Notes - Lecture 30: Apple I, Negative Number

48 views6 pages
CMSC132A Lecture 30: Comparable
We will now discuss how to compare two objects in Java.
What is the Comparable interface used for?
How do we compare and sort things in Java? We could use the less than or greater
than symbols, but those only really work for numbers. How about if we wanted to
compare objects like Strings? How would we do it?
Let’s say we have a set of apples? In how many ways could we sort them?
Do we want to sort them by weight? If so, are we sorting them from lightest to heaviest
or heaviest to lightest? When we are sorting them, we need to repeatedly compare two
apple’s weights until all the apples are in the correct order. Is Apple 1 heavier than
Apple 2? Is it heavier than Apple 3? We need to keep doing that until the list is sorted.
The comparable interface helps us accomplish this goal. Comparable can’t sort the
objects on its own, but the interface defines a method int compareTo(T).
How compareTo() works
Let’s begin by utilizing the compareTo()
method to see which apples are heavier.
The compareTo()
method works by returning an int value that is either positive,
negative, or zero. It compares the object by making the call to the object that is the
argument. A negative number means that the object making the call is “less” than the
argument. If we were comparing the apples by size, the above call would return a
negative number, say -400, because the red apple is smaller than the green apple. If
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in
the two apples were of equal weight, the call would return 0. If the red apple was
heavier, compareTo()
would return a positive number, say 68.
compareTo() Flexibility
If we called the compareTo() method above repeatedly, we could sort our apples by
size, which is great, but that’s not the end of the story. What if we want to sort apples by
color? Or weight? We could do that too. The key is that our client, let’s call him Fatty
Farmer, needs to precisely define how the apples need to be sorted before we can start
development.
He can do this by answering these two questions:
1. How does he want the apples to be sorted? What is the characteristic he would
like us to compare?
2. What does ‘less than’, ‘equal to’, and ‘greater than’ mean in that context?
It’s also possible to use multiple characteristics, as we’ll see a little bit later.
Sorting Apples by Weight
For our first example, we’re going to sort our apples by weight. It only requires one line
of code.
Collections.sort(apples);
The above line of code can do all the sorting for us, as long as we’ve defined how to
sort the apples in advance (That’s where we’ll need more than one line).
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in

Document Summary

We will now discuss how to compare two objects in java. We could use the less than or greater than symbols, but those only really work for numbers. When we are sorting them, we need to repeatedly compare two apple"s weights until all the apples are in the correct order. We need to keep doing that until the list is sorted. The comparable interface helps us accomplish this goal. Comparable can"t sort the objects on its own, but the interface defines a method int compareto(t). Let"s begin by utilizing the compareto() method to see which apples are heavier. The compareto() method works by returning an int value that is either positive, negative, or zero. It compares the object by making the call to the object that is the argument. A negative number means that the object making the call is less than the argument.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents