comparing objects using compareTo
Im modeling a car odometer, by making an odometer class, where when the user makes a new odometer(n), n is the number of counters shown. for instance an odometer(6) would be 000000 and the be incremented through the driver program.
My problem is when i get down to the compareTo method, i cant figure out how to compare the parameter object o to an odometer object to determine whether their mileage is less than equal to or greater than the one being compared. My teacher created a driver program to test our classes, and I have everything else working except comparing the objects. any advice would be great.
this is the driver
Code Java:
public class OdometerDriver {
public static void main(String [] args) {
Odometer od6 = new Odometer(), od4 = new Odometer(4);
System.out.println("od6 #1: " + od6);
System.out.println("od4 #1: " + od4);
for (int i = 1; i <= 21993; i++) {
od6.inc(); od4.inc();
}
System.out.println("od6 #2: " + od6);
System.out.println("od4 #2: " + od4);
System.out.println("od6 rollovers: " + od6.rollovers());
System.out.println("od4 rollovers: " + od4.rollovers());
System.out.println("od4 total mileage: " + od4.totalMileage());
System.out.println("od4 = od6? " + od4.equals(od6));
System.out.println("od4.compareTo(od6): "+ od4.compareTo(od6)); od4.inc();
System.out.println("od4 #3: " + od4);
System.out.println("od4.compareTo(od6): "+ od4.compareTo(od6));
}
}
and this is my class
Code Java:
import java.text.*;
public class Odometer implements Comparable{
int n;
int miles, totalMiles;
int rolls;
public Odometer(){
this.n = 6;
}
public Odometer(int n){
this.n = n;
if(n < 3 || n > 6){
System.out.println("N is outside the range. Must be 3 - 6.");
n = 6;
}
miles = 0; rolls = 0; totalMiles = 0;
}
public void inc(){
miles++;
if(n == 3){
if(miles > 999){
miles = 0;
rolls++;
}totalMiles = (miles + (rolls * 1000));
}
if(n == 4){
if(miles > 9999){
miles = 0;
rolls++;
}totalMiles = (miles + (rolls * 10000));
}
if(n == 5){
if(miles > 99999){
miles = 0;
rolls++;
}totalMiles = (miles + (rolls * 100000));
}
if(n == 6){
if(miles > 999999){
miles = 0;
rolls++;
}totalMiles = (miles + (rolls * 1000000));
}
}
public int currentMileage(){
return miles;
}
public int totalMileage(){
return totalMiles;
}
public int rollovers(){
return rolls;
}
public String toString(){
String value = "";
DecimalFormat df1 = new DecimalFormat("000");
DecimalFormat df2 = new DecimalFormat("0000");
DecimalFormat df3 = new DecimalFormat("00000");
DecimalFormat df4 = new DecimalFormat("000000");
if(n == 3)
value = "" + df1.format(miles);
else if(n == 4)
value = "" + df2.format(miles) + "";
else if(n == 5)
value = "" + df3.format(miles);
else if(n == 6)
value = "" + df4.format(miles);
return value;
}
public int compareTo(Object o){
o = (Odometer)o;
if( == )
return 0;
else if( > )
return 1;
else
return -1;
// Returns a -1, 0, or 1 as this object is less than, equal to, or
// greater than the specified object (the formal argument). Comparison
// made on total mileage.
}
public boolean equals(Object odometer){
if( == )
return true;
else
return false;
// returns true if the total mileage of 2
// odometers are equal (i.e. including rollovers),
// false otherwise. Note that odometers of different
// lengths can be equal, i.e. have the same
// total mileage
}
public boolean hasRolledOver(){
if(rolls >= 1)
return true;
else
return false;
}
}
Re: comparing objects using compareTo
This is meaningless:
You need to create an Odometer variable that is local to this method, have it refer to o, using a cast to Odometer of course, and use that variable to get the mileage from the parameter object so that you can compare it with the current object.
Re: comparing objects using compareTo
is this what you mean by creating a local odometer and referring to o?
Code Java:
Odometer od = (Odometer)o;
Re: comparing objects using compareTo
Quote:
Originally Posted by
jslice3
is this what you mean by creating a local odometer and referring to o?
Code Java:
Odometer od = (Odometer)o;
Yes, exactly. Then you can call Odometer methods on the od variable or interact directly with od's fields which will allow you to compare its state with that of the current object on which the compareTo(...) method is being called.