Results 1 to 4 of 4

Thread: time compare

  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default time compare

    Hi All! I have exercise that asks me to code program giving ready pseudocode for that, but I have problem with it... Here is my code:
    Java Code:
    /**
     * This program compares two time values and prints the time that comes first.
     * @author FOX
     *
     */
    import java.util.Scanner;
    
    public class TimeCompare {
    
    	/**
    	 * @param args
    	 */
    	public int hour1, hour2, minute1, minute2;
    	
    	public TimeCompare(int firstHour, int secondHour, int firstMinute, int secondMinute)
    	{
    		hour1=firstHour;
    		hour2=secondHour;
    		minute1=firstMinute;
    		minute2=secondMinute;
    		
    	}
    	
    	public double getEarliest()
    	{
    		int firstH=0, firstM, secondH, secondM;
    		
    		if (hour1<hour2)
    			firstH=hour1;
    		else if(hour1==hour2)
    			if (minute1<minute2)
    				{
    				firstM=minute1;;
    				}
    			else if(minute1==minute2)
    				System.out.println("Times are the same");
    			else
    				firstM=minute2;
    		else
    			firstH=hour2;
    		
    		return 0;
    	}
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner in=new Scanner(System.in);
    		System.out.print("Please enter the first hour, the second hour,first minute and second minute, for example 23 34 12 23");
    		int firstHour=in.nextInt();
    		int firstMinute=in.nextInt();
    		int secondHour=in.nextInt();
    		int secondMinute=in.nextInt();
    		
    		TimeCompare answer = new TimeCompare(firstHour, firstMinute, secondHour, secondMinute);
    		System.out.print("The first time to come is "+answer.getEarliest());
    	
    	}
    
    }
    I don't know how to put two values(hour+minute) in getEarliest() method, that way I could solve it but there will be type collisions...If you run this it will work but incorrect, here is I got so far:
    **************************************
    Please enter the first hour, the second hour,first minute and second minute, for example 23 34 12 2323
    12
    23
    13
    The first time to come is 12.0
    ***************************************
    can someone advise me what to do to fix this problem. Thanks.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: time compare

    That output doesn't come from the code you posted.

    What, exactly, is getEarliest() supposed to do? I see it returns a double value , but what is that value supposed to be?

    It would be a good idea to use braces with all if and else constructs: especially if things get complex as they do in getEarliest().
    Last edited by pbrockway2; 11-10-2012 at 01:06 AM.

  3. #3
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Default Re: time compare

    because I entered 23 12 23 13, it got second value(12), if enter another values you get different one...getEarliest() method needs to compare two entered values and print earliest one, you right about double value, I think I should change it into integer, it seems that I messed everything up.....

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default Re: time compare

    Why don't you convert both times to values measured in minutes only and compare those? (e.g. 1:20 == 80 minutes, 10:40 == 640 minutes etc.)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 5
    Last Post: 02-29-2012, 10:07 AM
  2. Replies: 3
    Last Post: 12-07-2011, 07:00 AM
  3. making a Time class but Universal time always says 0
    By Bravid2001 in forum New To Java
    Replies: 2
    Last Post: 10-24-2011, 07:55 AM
  4. Replies: 3
    Last Post: 08-23-2011, 12:46 PM
  5. how to compare time
    By newbiejava in forum New To Java
    Replies: 3
    Last Post: 07-25-2010, 04:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •