Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: comparing objects using compareTo

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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
    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;
    	}
    }
    Last edited by jslice3; November 19th, 2012 at 06:20 PM. Reason: code tag was entered wrong


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: comparing objects using compareTo

    This is meaningless:
    o = (Odometer)o;

    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.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: comparing objects using compareTo

    is this what you mean by creating a local odometer and referring to o?

    Odometer od = (Odometer)o;

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: comparing objects using compareTo

    Quote Originally Posted by jslice3 View Post
    is this what you mean by creating a local odometer and referring to o?

    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.

Similar Threads

  1. Comparing two objects
    By colerelm in forum Java Theory & Questions
    Replies: 4
    Last Post: December 6th, 2011, 05:52 PM
  2. Implementing the compareTo method?
    By colerelm in forum Java Theory & Questions
    Replies: 2
    Last Post: December 3rd, 2011, 07:47 PM
  3. Help Me with my Program CompareTo!!!!
    By WantHelp in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 7th, 2011, 12:54 PM
  4. Replies: 3
    Last Post: June 1st, 2011, 12:47 AM
  5. error when comparing 2 string objects
    By amr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 9th, 2011, 07:36 PM