-1

So I'm writing a program for school and it involves having two objects of Time (t1 and t2). which have hours, mins, secs. One method is to compare the two to see if they are equal. In my driver file its supposed to be like "t1.equals(t2);" and compare the two. within the method how do I get it so that the program "knows" to compare the variables from t1?

Heres what I have now but this is before I realized that its not supposed to be "equals(t1,t2);" but IS supposed to be "t1.equals(t2);"

public boolean equals(Time one, Time two)
{
   boolean areEqual=true;
   int timeOneSecs=one.getSecs();
   int timeOneMins=one.getMins();
   int timeOneHrs=one.getHrs();

   int timeTwoSecs=two.getSecs();
   int timeTwoMins=two.getMins();
   int timeTwoHrs=two.getHrs();


   if (timeOneSecs!=timeTwoSecs)
     { 
      areEqual=false;
     }

   if (timeOneMins!=timeTwoMins)
     { 
      areEqual=false;
     }

   if (timeOneHrs!=timeTwoHrs)
     { 
      areEqual=false;
     }


return areEqual;
}

I'm just not sure how to get the program to know which two times to compare if the first one is calling the second? (if that makes sense).

  • `t1.equals(t2)` and `t2.equals(t1)` will return the same result . – Mathews Mathai Feb 16 '16 at 17:43
  • You should read about `this` – user3707125 Feb 16 '16 at 17:43
  • 1
    It sounds like you want to override equals: http://stackoverflow.com/questions/8180430/how-to-override-equals-method-in-java – Dyrborg Feb 16 '16 at 17:45
  • boolean methods will either return true or false. Isn't `return areEqual;` giving you an error? – Mathews Mathai Feb 16 '16 at 17:47
  • All java objects inherit from the java.lang.Object class which has the `public boolean equals(Object o)` method defined. All subclasses should override this method if they want to compare more than memory addresses. [The Oracle Tutorials](https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html) talk about this more in detail. – callyalater Feb 16 '16 at 18:05

3 Answers3

0

You are calling the method on an instance, so you need to compare that instance's members with those of the passed argument:

public boolean equals (Time other) {
    return getSecs() == other.getSecs() &&
           getMins() == other.getMins() &&
           getHrs() == other.getHrs();
}

Note that although the above would work, you should probably follow java's standards and override Object#equals(Object), handling the notion that a Time instance cannot be equal to an object that isn't a Time instance:

@Override
public boolean equals (Object o) {
    if (!(o instanceof Time)) {
        return false;
    }

    Time other = (Time)o;
    return getSecs() == other.getSecs() &&
           getMins() == other.getMins() &&
           getHrs() == other.getHrs();
}
Mureinik
  • 252,575
  • 45
  • 248
  • 283
0

Any reasonable IDE can auto-generate an overridden equals(Object o) and hashCode() for you.

For example, all I did was define the three int fields, then let the generator to do rest. This allows you to call t1.equals(t2) and vice-versa.

public class Time {
    private int hrs, mins, secs;

    public Time(int hrs, int mins, int secs) {
        this.hrs = hrs;
        this.mins = mins;
        this.secs = secs;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Time time = (Time) o;

        if (hrs != time.hrs) return false;
        if (mins != time.mins) return false;
        return secs == time.secs;

    }

    @Override
    public int hashCode() {
        int result = hrs;
        result = 31 * result + mins;
        result = 31 * result + secs;
        return result;
    }
}
OneCricketeer
  • 126,858
  • 14
  • 92
  • 185
0

This is basically what you want:

public class Main{

     public static void main(String []args){
        Time t1 = new Time(1,2,3);
        Time t2 = new Time(1,2,3);
        Time t3 = new Time(1,2,4);
        Object t4 = new Object();
        System.out.println(t1.equals(t2)); //Writes true
        System.out.println(t1.equals(t1)); //Writes true
        System.out.println(t1.equals(t3)); //Writes false
        System.out.println(t1.equals(t4)); //Writes false
        System.out.println(t1.equals(null)); //Writes false
     }
}

And this is how your Time class would look like:

public class Time
{
    private int sec;
    private int min;
    private int hour;

    public Time(int sec, int min, int hour){
        this.sec = sec;
        this.min = min;
        this.hour = hour;
    }

    @Override
    public boolean equals(Object obj){
        //If you compare to null, obviously they are not the same.
        if (obj == null) return false;
        //If they point to the same address in the heap they must be the same.
        if (this == obj) return true;
        //If you compare it to something that is not a time object, they must be different
        if (!(obj instanceof Time)) return false; 
        Time t2 = (Time)obj;
        return this.sec == t2.getSec() && 
               this.min == t2.getMin() && 
               this.hour == t2.getHour();
    }

    public int getSec(){
        return this.sec;
    }
    public int getMin(){
        return this.min;
    }
    public int getHour(){
        return this.hour;
    }
}

The equals method is inherited from Object for all objects in Java. However if you want to implement your own comparison, then you should override the method inherited from Object as shown above.

Dyrborg
  • 867
  • 7
  • 16