[CSC 204] Comparing objects

Andrew J. Pounds pounds_aj at mercer.edu
Sat Apr 26 17:59:32 EDT 2014


On 04/26/14 13:31, Molly M Safigan wrote:
> Dr. Pounds,
>
> I'm trying to sort the ArrayList<State> by temp and in my State class 
> I wrote:
>
> public int compare(State a, State b){
> if(a.getTemp() < b.getTemp()){
> return -1;
> }
> else if (a.getTemp() == b.getTemp()){
> return 0;
> }
> else{
> return 1;
> }
>
> and it seems to compile in the class but I don't know how to call it 
> in the main program.
>
> Maybe I'm totally off…
>
> Molly
>

It looks like you are trying to use Java "Comparators", but without the 
comparator "machinery".   Comparators are VERY powerful, but are, in my 
opinion, a bit much for this particular problem.  I will talk about them 
last.  There is another way that you could do this problem that is just 
like some of the things we discussed in class. Let's talk about that 
first...


*--- Method ONE - The simple selection sort:*

Since you are sorting a small number (less than 100 items), if your 
State class has a method called getTemp (or something similar) then you 
could implement the following...

Imagine I have an ArrayList<State> stateData defined...

// I am looping over all of the elements in my arraylist and comparing 
them in these two loops...
for (int i=0; i< stateData.size(); i++ ) {
          for ( int j = i+1; j< stateData.size(); j++ ) {

               // Here I actually do the comparison.  If the temp of 
element "j" is greater than element "i", swap them
               if ( stateData.get(i).getTemp() > 
stateData.get(j).getTemp() ) {

                   State hold = stateData.get(i);     // Here is where I 
store the temporary "state information"
                   stateData.set(i,stateData.get(j));
                   stateData.set(j,hold);

              }
         }
   }


Put this section of code right before you print everything out and you 
should be done....



*--- Method TWO - Using Comparators*

At the top of your program include the following....

import java.util.Collections;
import java.util.Comparator;

In your main, preferably after your ArrayList declaration, create the 
comparator for the temperatures.

     Comparator<State> tempComparator = new Comparator<State>(){
     public int compare(State o1, State o2){
            if ( o1.getTemp() < o2.getTemp() ) {
                    return -1;
            }
            else if ( o1.getTemp() > o2.getTemp() ) {
                   return 1;
           }
           else return 0;
           }
    };

Note -- this is comparing two fields in the class (like you were trying 
to do in your example)

Now, right before you print out out results, do the following...


Collections.sort(stateData, tempComparator);

and your arraylist will be sorted as with the selection sort from 
above.  We didn't cover this in class, but based on what you sent me I 
thought you might be trying it out.



-- 
Andrew J. Pounds, Ph.D.  (pounds_aj at mercer.edu)
Professor of Chemistry and Computer Science
Mercer University,  Macon, GA 31207   (478) 301-5627
http://faculty.mercer.edu/pounds_aj

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://theochem.mercer.edu/pipermail/csc204/attachments/20140426/26ba16bd/attachment.html>


More information about the csc204 mailing list