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.
Here is my Timeslot.java class that has the compare method at the end.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(); } }
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()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; } } }
What do you think? Any ideas?




Reply With Quote