Results 1 to 2 of 2
  1. #1
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default How to call the equals method

    Here's what I have:

    Java Code:
    // Represents a time span of hours and minutes elapsed.
    // Class invariant: minutes < 60
    
    public class TimeSpan {
        private int hours;
        private int minutes;
    
        // Constructs a time span with the given interval.
        // pre: hours >= 0 && minutes >= 0
        public TimeSpan(int hours, int minutes) {
            this.hours = 0;
            this.minutes = 0;
            add(hours, minutes);
        }
        
        // Adds the given interval to this time span.
        // pre: hours >= 0 && minutes >= 0
        public void add(int hours, int minutes) {
            this.hours += hours;
            this.minutes += minutes;
            
            // convert each 60 minutes into one hour
            this.hours += this.minutes / 60;
            this.minutes = this.minutes % 60;
        }
    
        // Returns whether o is a TimeSpan representing the same
        // number of hours and minutes as this TimeSpan object.
        public boolean equals(Object o) {
            if (o instanceof TimeSpan) {
                TimeSpan other = (TimeSpan) o;
                return hours == other.hours && 
                       minutes == other.minutes;
            } else {   // not a TimeSpan object
                return false;
            }
        }
    
        // Returns a String for this time span such as "6h15m".
        public String toString() {
            return hours + "h " + minutes + " m";
        }
        public static void main(String[] args) {
        	TimeSpan u = new TimeSpan(5, 50) ;
        	TimeSpan t = new TimeSpan(3, 40) ;
        	
        	System.out.println("TimeSpan of u: " + u) ;
        	System.out.println("TimeSpan of u: " + t) ;
        	
        	t.add(u.hours, u.minutes) ;
        	
        	System.out.println("New t after addition: " + t) ;
        	
        	t.equals(u) ;
        	
        	System.out.println("Does t equal u: " + t) ;
        	
        }
    }
    My question is:

    How do I print out the equals method correctly using System.out.println() ?. I'm having trouble having it return false if the two times aren't equal or the hours and minutes if the two times are equal. Thanks.
    Last edited by random0munky; 10-12-2009 at 06:26 PM.

  2. #2
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

Similar Threads

  1. equals method
    By mani_miit in forum Advanced Java
    Replies: 7
    Last Post: 09-09-2009, 11:26 PM
  2. equals method
    By timkd127 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 07:52 PM
  3. Creating a new equals() method help
    By Dave0703 in forum New To Java
    Replies: 2
    Last Post: 09-21-2008, 06:32 PM
  4. Object class's equals() method behavior????
    By skyineyes in forum New To Java
    Replies: 4
    Last Post: 07-20-2008, 12:58 AM
  5. Why Equals method should be over ridden in Hashcode?
    By skyineyes in forum New To Java
    Replies: 1
    Last Post: 05-26-2008, 05:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •