# I cant find the difference between two methods, please help

• 07-13-2012, 07:33 AM
RichersooN
I cant find the difference between two methods, please help
Hi please tell me why the two methods returns different result,

method1() return TRUE while method2() false, I'm expected both return TRUE when calling isShiftStartEnd().

Method1:
Code:

```private static void method1(double startTimeDouble, double shiftBeginDouble, double shiftEndDouble){                 double shiftEndDoubleOrig = shiftEndDouble;                             boolean addWholeDay = false;             if (shiftBeginDouble > shiftEndDouble) {                     addWholeDay = true;                     shiftEndDouble = shiftEndDouble + 24.00;             }                         if(addWholeDay && (startTimeDouble < shiftEndDoubleOrig)){                         startTimeDouble = startTimeDouble + 24.00;                 }                             System.out.println("CAN Shift 1: " +isShiftStartEnd(startTimeDouble, shiftBeginDouble, shiftEndDouble));         }```
method2:
Code:

```private static void method2(double startTimeDouble, double shiftBeginDouble, double shiftEndDouble){                 double shiftEndDoubleOrig = shiftEndDouble;                                 //if shiftBeginDouble and shiftEndDouble are 2 different day: e.g. 23 > 5+24                 //add 1 whole day to shiftEndDouble                 if (shiftBeginDouble > shiftEndDouble) {                   shiftEndDouble = shiftEndDouble + 24.00;                 }                                 if((shiftBeginDouble > shiftEndDouble) && (startTimeDouble < shiftEndDoubleOrig)){                         startTimeDouble = startTimeDouble + 24.00;                 }                                 System.out.println("CAN Shift 1b: " +isShiftStartEnd(startTimeDouble, shiftBeginDouble, shiftEndDouble));         }```

Code:

```         private static boolean isShiftStartEnd(double startTimeDouble, double shiftBeginDouble, double shiftEndDouble){                 boolean result = false;                                           if (startTimeDouble >= shiftBeginDouble) {                           if (startTimeDouble <= shiftEndDouble) {                                   result = true;                           }                   }                         System.out.println(startTimeDouble +">=" +shiftBeginDouble +"\n"                             +startTimeDouble +"<=" +shiftEndDouble             );                 return result;                        }                 public static void main(String[]args){                 Scanner input = new Scanner(System.in);                 double startTimeDouble, shiftBeginDouble, shiftEndDouble;                                 do{                 System.out.println("Enter start time: ");                 startTimeDouble = input.nextDouble();                 System.out.println("Enter shift begin time: ");                 shiftBeginDouble = input.nextDouble();                 System.out.println("Enter shift end time: ");                 shiftEndDouble = input.nextDouble();                                 method1(startTimeDouble, shiftBeginDouble, shiftEndDouble);                 method2(startTimeDouble, shiftBeginDouble, shiftEndDouble);                                                 System.out.println("\n");                 }while(startTimeDouble != 143);         }```
• 07-13-2012, 07:58 AM
awinston
Re: I cant find the difference between two methods, please help
In method 1, you add 24 hours to ShiftEndDouble after you set addWholeDay equal to true. This addition of 24 hours does not effect the value of addWholeDay, so addWholeDay remains true. Your if(x && x) statement also evaluates to true because of this. (Check comment for explanation.)

Code:

```private static void method1(double startTimeDouble, double shiftBeginDouble, double shiftEndDouble){                 double shiftEndDoubleOrig = shiftEndDouble;                             boolean addWholeDay = false;             if (shiftBeginDouble > shiftEndDouble) {                     addWholeDay = true;                     shiftEndDouble = shiftEndDouble + 24.00;             }                         //This boolean statement evaluates to true because addWholeDay is true.         if(addWholeDay && (startTimeDouble < shiftEndDoubleOrig)){                         startTimeDouble = startTimeDouble + 24.00;                 }                             System.out.println("CAN Shift 1: " +isShiftStartEnd(startTimeDouble, shiftBeginDouble, shiftEndDouble));         }```
In method 2, you simply add 24 hours to shiftEndDouble without creating a boolean value to capture (shiftBeginDouble > shiftEndDouble) beforehand. You then re-evaluate (shiftBeginDouble > shiftEndDouble) and the result is different because shiftEndDouble has a different value. This causes your if(x && x) statement to evaluate to false. (Check comment for explanation.)

Code:

```private static void method2(double startTimeDouble, double shiftBeginDouble, double shiftEndDouble){                 double shiftEndDoubleOrig = shiftEndDouble;                                 //if shiftBeginDouble and shiftEndDouble are 2 different day: e.g. 23 > 5+24                 //add 1 whole day to shiftEndDouble                 if (shiftBeginDouble > shiftEndDouble) {                   shiftEndDouble = shiftEndDouble + 24.00;                 }                                 //this boolean statement evaluates to false because (shiftBeginDouble > shiftEndDouble) is false.                 if((shiftBeginDouble > shiftEndDouble) && (startTimeDouble < shiftEndDoubleOrig)){                         startTimeDouble = startTimeDouble + 24.00;                 }                                 System.out.println("CAN Shift 1b: " +isShiftStartEnd(startTimeDouble, shiftBeginDouble, shiftEndDouble));         }```
• 07-13-2012, 11:15 AM
RichersooN
Re: I cant find the difference between two methods, please help
very well said, thanks