Compare method always returning false.
I am working on comparing two timeslot objects. Which always returns false.
A timeslot object has a begintime, endtime and a day. I created 4 timeslot objects. I set the value of two of the objectrs to different values to check the false condition. I set the values of the other two to the same values, to check the true condition. The problem is though, that even the two timeslot objects that I set to the same values to, returns false as well.
Here is my TimeSlotTest.java class where I create the four timeslot objects then set the values. I use IF condition statements to call the compare() methods for the timeslot objects. I then just print a message telling whether it is true or not.
Code:
public class TimeslotTest
{
public TimeslotTest()
{
Timeslot ts1 = new Timeslot();
Timeslot ts2 = new Timeslot();
Timeslot ts3 = new Timeslot();
Timeslot ts4 = new Timeslot();
//set begin end time for ts1
ts1.setBeginTime(3,00);
ts1.setEndTime(4,20);
ts1.setDay("M");
//set begin end time for ts2
ts2.setBeginTime(4,30);
ts2.setEndTime(6,00);
ts2.setDay("M");
//Print variables before comparing
System.out.println("TS1.getBeginTime: " + ts1.getBeginTime().getTime());
System.out.println("TS1.getEndTime: " + ts1.getEndTime().getTime());
System.out.println("TS2.getBeginTime: " + ts2.getBeginTime().getTime());
System.out.println("TS2.getEndTime: " + ts2.getEndTime().getTime());
System.out.println();
//compare ts1 and ts2 to see if they are equal
if(ts1.compare(ts2))
{
System.out.println("TS1.Compare(TS2) returned True");
}
else if(!ts1.compare(ts2))
{
System.out.println("TS1.Compare(TS2) returned False");
}
System.out.println();
//set begin end time for ts3
ts3.setBeginTime(3,00);
ts3.setEndTime(4,20);
ts3.setDay("M");
//set begin end time for ts4
ts4.setBeginTime(3,00);
ts4.setEndTime(4,20);
ts4.setDay("M");
//Print variables before comparing
System.out.println("TS3.getBeginTime: " + ts3.getBeginTime().getTime());
System.out.println("TS3.getEndTime: " + ts3.getEndTime().getTime());
System.out.println("TS4.getBeginTime: " + ts4.getBeginTime().getTime());
System.out.println("TS4.getEndTime: " + ts4.getEndTime().getTime());
System.out.println();
//compare ts1 and ts4 to see if they are equal
if(ts3.compare(ts4))
{
System.out.println("TS3.Compare(TS4) returned True");
}
else if(!ts3.compare(ts4))
{
System.out.println("TS3.Compare(TS4) returned False");
}
}
public static void main(String[] args)
{
new TimeslotTest();
}
}
Here is my Timeslot.java class that has the compare method at the end.
Code:
import java.util.*;
public class Timeslot {
private String day;
private Calendar beginTime = Calendar.getInstance();
private Calendar endTime = Calendar.getInstance();
private double duration = 1;
public Timeslot()
{
}
public Calendar getBeginTime()
{
return beginTime;
}
public void setBeginTime(Calendar bTime)
{
this.beginTime = bTime;
}
public void setBeginTime(int hour, int minute)
{
this.beginTime.set(1,1,1,hour, minute,0);
}
public Calendar getEndTime()
{
return endTime;
}
public void setEndTime(Calendar eTime)
{
this.endTime = eTime;
}
public void setEndTime(int hour, int minute)
{
this.endTime.set(1,1,1,hour, minute,0);
}
public String getDay()
{
return day;
}
public void setDay(String dow)
{
this.day = dow;
}
public boolean compare(Timeslot tsCompare)
{
if(this.getBeginTime().equals(tsCompare.getBeginTime()) & this.getEndTime().equals(tsCompare.getBeginTime()) & this.getDay().equals(tsCompare.getDay()))
{
return true;
}
else
{
return false;
}
}
}
I am wondering if I havent set all the values that a calendar object can take, so part of the beginTime or endTime variables are different. Then, if they are just slightly different, my compare() method will return false when I try to check to see if the two calendar objects are equal using .equals()
What do you think? Any ideas?
Re: Compare method always returning false.
Yeah, I meant to use && and not & in my timeslot compare() method. Either way it still returns false though. I have went added a few more if statements that printed out whether the individual attributes of each timeslot are equal or not. Here is the new Test class with the added if statements. What is weird though is that the first two timeslot object evaluate to false, like I hard coded them to do. But the two other timeslot objects evaluate to true each time, yet my compare method still returns false. I will include my output from my dos console to show you what I mean.
Code:
public class TimeslotTest
{
public TimeslotTest()
{
Timeslot ts1 = new Timeslot();
Timeslot ts2 = new Timeslot();
Timeslot ts3 = new Timeslot();
Timeslot ts4 = new Timeslot();
//set begin end time for ts1
ts1.setBeginTime(3,00);
ts1.setEndTime(4,20);
ts1.setDay("M");
//set begin end time for ts2
ts2.setBeginTime(4,30);
ts2.setEndTime(6,00);
ts2.setDay("M");
//Print variables before comparing
System.out.println("TS1.getBeginTime: " + ts1.getBeginTime().getTime());
System.out.println("TS1.getEndTime: " + ts1.getEndTime().getTime());
System.out.println("TS2.getBeginTime: " + ts2.getBeginTime().getTime());
System.out.println("TS2.getEndTime: " + ts2.getEndTime().getTime());
System.out.println();
//compare ts1 and ts2 to see if they are equal
if(ts1.compare(ts2))
{
System.out.println("TS1.Compare(TS2) returned True");
}
else if(!ts1.compare(ts2))
{
System.out.println("TS1.Compare(TS2) returned False");
}
//compares day attricute of timeslot objects
if(ts1.getDay().equals(ts2.getDay()))
{
System.out.println("ts1.getDay().equals(ts2.getDay()) returned True");
}
else if(!ts1.getDay().equals(ts2.getDay()))
{
System.out.println("ts1.getDay().equals(ts2.getDay()) returned False");
}
//compare the BeginTimes of the two timeslot objects
if(ts1.getBeginTime().equals(ts2.getBeginTime()))
{
System.out.println("ts1.getBeginTime().equals(ts2.getBeginTime()) returned True");
}
else if(!ts1.getBeginTime().equals(ts2.getBeginTime()))
{
System.out.println("ts1.getBeginTime().equals(ts2.getBeginTime()) returned False");
}
//compare the EndTimes of the two timeslot objects
if(ts1.getEndTime().equals(ts2.getEndTime()))
{
System.out.println("ts1.getEndTime().equals(ts2.getEndTime()) returned True");
}
else if(!ts1.getEndTime().equals(ts2.getEndTime()))
{
System.out.println("ts1.getEndTime().equals(ts2.getEndTime()) returned False");
}
System.out.println();
//set begin end time for ts3
ts3.setBeginTime(3,00);
ts3.setEndTime(4,20);
ts3.setDay("M");
//set begin end time for ts4
ts4.setBeginTime(3,00);
ts4.setEndTime(4,20);
ts4.setDay("M");
//Print variables before comparing
System.out.println("TS3.getBeginTime: " + ts3.getBeginTime().getTime());
System.out.println("TS3.getEndTime: " + ts3.getEndTime().getTime());
System.out.println("TS4.getBeginTime: " + ts4.getBeginTime().getTime());
System.out.println("TS4.getEndTime: " + ts4.getEndTime().getTime());
System.out.println();
//compare ts1 and ts4 to see if they are equal
if(ts3.compare(ts4))
{
System.out.println("TS3.Compare(TS4) returned True");
}
else if(!ts3.compare(ts4))
{
System.out.println("TS3.Compare(TS4) returned False");
}
//compare day attributes timeslot objects
if(ts3.getDay().equals(ts4.getDay()))
{
System.out.println("ts3.getDay().equals(ts4.getDay()) returned True");
}
else if(!ts3.getDay().equals(ts4.getDay()))
{
System.out.println("ts3.getDay().equals(ts4.getDay()) returned False");
}
//compare the BeginTimes of the two timeslot objects
if(ts3.getBeginTime().equals(ts4.getBeginTime()))
{
System.out.println("ts3.getBeginTime().equals(ts$.getBeginTime()) returned True");
}
else if(!ts3.getBeginTime().equals(ts4.getBeginTime()))
{
System.out.println("ts3.getBeginTime().equals(ts4.getBeginTime()) returned False");
}
//compare the EndTimes of the two timeslot objects
if(ts3.getEndTime().equals(ts4.getEndTime()))
{
System.out.println("ts3.getEndTime().equals(ts4.getEndTime()) returned True");
}
else if(!ts3.getEndTime().equals(ts4.getEndTime()))
{
System.out.println("ts3.getEndTime().equals(ts4.getEndTime()) returned False");
}
System.out.println();
}
public static void main(String[] args)
{
new TimeslotTest();
}
}
Here is the output from my program. TS1 and TS2 evaluate to false, like expected. TS3 and TS4 evaluate to false as well, even though the other if statements says that the other attributes are equal.
Quote:
TS1.getBeginTime: Tue Feb 01 03:00:00 CST 1
TS1.getEndTime: Tue Feb 01 04:20:00 CST 1
TS2.getBeginTime: Tue Feb 01 04:30:00 CST 1
TS2.getEndTime: Tue Feb 01 06:00:00 CST 1
TS1.Compare(TS2) returned False
ts1.getDay().equals(ts2.getDay()) returned True
ts1.getBeginTime().equals(ts2.getBeginTime()) returned False
ts1.getEndTime().equals(ts2.getEndTime()) returned False
TS3.getBeginTime: Tue Feb 01 03:00:00 CST 1
TS3.getEndTime: Tue Feb 01 04:20:00 CST 1
TS4.getBeginTime: Tue Feb 01 03:00:00 CST 1
TS4.getEndTime: Tue Feb 01 04:20:00 CST 1
TS3.Compare(TS4) returned False
ts3.getDay().equals(ts4.getDay()) returned True
ts3.getBeginTime().equals(ts$.getBeginTime()) returned True
ts3.getEndTime().equals(ts4.getEndTime()) returned True
Press any key to continue...
It seems to me that I have a problem with the compare method in my timeslot object in the Timeslot.java class. I would prefer not to have a long nested if statement but I may have to if I cant figure this other way out.
Re: Compare method always returning false.
I wrote another compare method in my timeslot class with nested ifs instead of a one liner like my original. Here is my new compare method and it seems to work.
Code:
public boolean compare2(Timeslot tsCompare)
{
if(this.getDay().equals(tsCompare.getDay()))
{
if(this.getBeginTime().equals(tsCompare.getBeginTime()))
{
if(this.getEndTime().equals(tsCompare.getEndTime()))
{
return true;
}
else //else of End Time check
{
return false;
}
}
else//else of Begin Time check
{
return false;
}
}
else//else of Day Check
{
return false;
}
}
Here is the full code of my timeslot class
Code:
import java.util.*;
public class Timeslot {
private String day;
private Calendar beginTime = Calendar.getInstance();
private Calendar endTime = Calendar.getInstance();
private double duration = 1;
public Timeslot()
{
}
public Calendar getBeginTime()
{
return beginTime;
}
public void setBeginTime(Calendar bTime)
{
this.beginTime = bTime;
}
public void setBeginTime(int hour, int minute)
{
this.beginTime.set(1,1,1,hour, minute,0);
}
public Calendar getEndTime()
{
return endTime;
}
public void setEndTime(Calendar eTime)
{
this.endTime = eTime;
}
public void setEndTime(int hour, int minute)
{
this.endTime.set(1,1,1,hour, minute,0);
}
public String getDay()
{
return day;
}
public void setDay(String dow)
{
this.day = dow;
}
public boolean compare(Timeslot tsCompare)
{
if(this.getDay().equals(tsCompare.getDay()))
{
if(this.getBeginTime().equals(tsCompare.getBeginTime()))
{
if(this.getEndTime().equals(tsCompare.getEndTime()))
{
return true;
}
else //else of End Time check
{
return false;
}
}
else//else of Begin Time check
{
return false;
}
}
else//else of Day Check
{
return false;
}
}
}
Here is my TimeslotTest.java class
Code:
public class TimeslotTest
{
public TimeslotTest()
{
Timeslot ts1 = new Timeslot();
Timeslot ts2 = new Timeslot();
Timeslot ts3 = new Timeslot();
Timeslot ts4 = new Timeslot();
//set begin end time for ts1
ts1.setBeginTime(3,00);
ts1.setEndTime(4,20);
ts1.setDay("M");
//set begin end time for ts2
ts2.setBeginTime(4,30);
ts2.setEndTime(6,00);
ts2.setDay("M");
//Print variables before comparing
System.out.println("TS1.getBeginTime: " + ts1.getBeginTime().getTime());
System.out.println("TS1.getEndTime: " + ts1.getEndTime().getTime());
System.out.println("TS2.getBeginTime: " + ts2.getBeginTime().getTime());
System.out.println("TS2.getEndTime: " + ts2.getEndTime().getTime());
System.out.println();
//compare2 ts1 and ts2 to see if they are equal
if(ts1.compare(ts2))
{
System.out.println("TS1.compare(TS2) returned True");
}
else if(!ts1.compare(ts2))
{
System.out.println("TS1.compare(TS2) returned False");
}
System.out.println();
//set begin end time for ts3
ts3.setBeginTime(3,00);
ts3.setEndTime(4,20);
ts3.setDay("M");
//set begin end time for ts4
ts4.setBeginTime(3,00);
ts4.setEndTime(4,20);
ts4.setDay("M");
//Print variables before comparing
System.out.println("TS3.getBeginTime: " + ts3.getBeginTime().getTime());
System.out.println("TS3.getEndTime: " + ts3.getEndTime().getTime());
System.out.println("TS4.getBeginTime: " + ts4.getBeginTime().getTime());
System.out.println("TS4.getEndTime: " + ts4.getEndTime().getTime());
System.out.println();
//compare2 ts3 and ts4 to see if they are equal
if(ts3.compare(ts4))
{
System.out.println("TS3.compare(TS4) returned True");
}
else if(!ts3.compare(ts4))
{
System.out.println("TS3.compare(TS4) returned False");
}
System.out.println();
}
public static void main(String[] args)
{
new TimeslotTest();
}
}
Re: Compare method always returning false.
Im glad to hear its working. Referring to your old compare method, the problem was that in the 2nd condition, you had:
Code:
this.getEndTime().equals(tsCompare.getBeginTime())
I'm sure you know what it should be :P. Also, just to keep things neat, always try putting all separate conditions inside brackets. It helps to figure out what conditions you are actually checking.
Re: Compare method always returning false.
Quote:
Originally Posted by fartman_900
Im glad to hear its working. Referring to your old compare method, the problem was that in the 2nd condition, you had:
Code:
this.getEndTime().equals(tsCompare.getBeginTime())
I'm sure you know what it should be :P. Also, just to keep things neat, always try putting all separate conditions inside brackets. It helps to figure out what conditions you are actually checking.
OMG!!!! I cant believe I did that... LOL LMAO :eek2: I feel real bad about that one.