Results 1 to 3 of 3
  1. #1
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default 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().

    Please need help..


    Method1:
    Java 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:
    Java 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));
    	}



    Java 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);
    	}

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    2

    Default 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.)

    Java 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.)

    Java 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));
    	}
    Last edited by awinston; 07-13-2012 at 07:06 AM. Reason: Grammar
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default Re: I cant find the difference between two methods, please help

    very well said, thanks

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Java Noob, trying to call methods from another methods
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 09:07 PM
  3. Replies: 1
    Last Post: 10-23-2011, 12:37 PM
  4. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  5. where can I find description functions of methods
    By manhtungtnk28@gmail.com in forum AWT / Swing
    Replies: 16
    Last Post: 11-17-2009, 05:40 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
  •