Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: TIME PROGRAM UNSOLVED

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking TIME PROGRAM UNSOLVED

    // main class

    Modify class Time2 to include a tick method that increments the time stored in a Time2 object by one second. Provide method incrementMinute to increment the minute and method incrementHour to increment the hour. The Time2 object should always remain

    a) incrementing into the next minute,

    b) incrementing into the next hour and

    c) incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).

    how to manage case 4 stuff and what's the problem of this CODE .. tnx
     
     import java.util.Scanner;
     
    public class Time2Test
    {
    	public static void main( String args[] )
    	{
     
    		Scanner input = new Scanner( System.in );
     
    		 Time2 time = new Time2();
    // input
    		 System.out.println( "Enter the time" );
    		 System.out.print( "Hours: " );
    		 time.setHour( input.nextInt() );
    		 System.out.print( "Minutes: " );
    		 time.setMinute( input.nextInt() );
    		 System.out.print( "Seconds: " );
    		 time.setSecond( input.nextInt() );
    // output 
    		 System.out.printf( "Hour: %d Minute: %d Second: %d\n",
    		 time.getHour(), time.getMinute(), time.getSecond() );
    		 System.out.printf( "Universal time: %s Standard time: %s\n",
    		 time.toUniversalString(), time.toString() );
    //menu cases
    		 int choice = getMenuChoice();
     
    		 while ( choice != 5 )
    		 {
    			 switch ( choice )
    			 {
    					 case 1: // add 1 second
    					 Time2 t = new Time2();  //create an object of type first
        				  t.tick(); 
     
        				  t.newDisplay();   //use object to display.
    					 break;
     
    					 case 2: // add 1 minute
    					 Time2 m = new Time2();  //create an object of type first
        				  m.incrementMinute();   
        				  	  m.newDisplay(); 
    					 break;
     
    					 case 3: // and 1 hour
    					  Time2 h = new Time2();  //create an object of type first
        				  h.incrementHour();
     
        				  h.newDisplay();
     
     
    					 break;
     
    					 case 4: // add arbitrary seconds
    					 System.out.print( "Enter seconds to tick: " );
     
     
    					 break;
    			 } // end switch
     
     
    			 choice = getMenuChoice();
    			 } // end while
    	} // end main
     
    		 private static int getMenuChoice()
    		 {
    			 Scanner input = new Scanner( System.in );
    //printing the menu
    			 System.out.println( "1. Add 1 second" );
    			 System.out.println( "2. Add 1 Minute" );
    			 System.out.println( "3. Add 1 Hour" );
    			 System.out.println( "4. Add seconds" );
    			 System.out.println( "5. Exit" );
    			 System.out.print( "Choice: " );
     
    			 return input.nextInt();
    	 } // end method getMenuChoice
     } // end class Time2Test
    // class
    public class Time2
     {
    	 private int hour; // 0 - 23
    	 private int minute; // 0 - 59
    	 private int second; // 0 - 5
    	 public Time2()
    	 {
    	 	this( 0, 0, 0 ); // invoke Time2 constructor with three arguments
    	 } // end Time2 no-argument constructor
     
    	  public Time2( int h, int m, int s )
    	 {
    	 	setTime( h, m, s ); // invoke setTime to validate time
    	 } // end Time2 three-argument constructor
     
     
    	 // Set Methods
    	 // set a new time value using universal time; perform
    	 // validity checks on data; set invalid values to zero
    	 public void setTime( int h, int m, int s )
    	 {
    		 setHour( h ); // set the hour
    		 setMinute( m ); // set the minute
    		 setSecond( s ); // set the second
    	 } // end method setTime
     
    	 // validate and set hour
    	 public void setHour( int h )
    	 {
    	 	hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
    	 } // end method setHour
     
    	 // validate and set minute
    	 public void setMinute( int m )
    	 {
    	 	minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
    	 } // end method setMinute
     
    	 // validate and set second
    	 public void setSecond( int s )
    	 {
    	 	second = ( ( s >= 0 && s < 60 ) ? s : 0 );
    	 } // end method setSecond
     
    	 // Get Methods
    	 // get hour value
    	 public int getHour()
    	 {
    	 	return hour;
    	 } // end method getHour
     
    	 // get minute value
    	 public int getMinute()
    	 {
    	 	return minute;
    	 } // end method getMinute
     
    	 // get second value
    	 public int getSecond()
    	 {
    	 	return second;
    	 } // end method getSecond
     
     
    	 // convert to String in universal-time format (HH:MM:SS)
    	 public String toUniversalString()
    	 {
    	 	return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond() );
    	 } // end method toUniversalString
     
    	 // convert to String in standard-time format (H:MM:SS AM or PM)
    	 public String toString()
    	 {
    	 return String.format( "%d:%02d:%02d %s",
    		 ( (getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12 ),
    		  getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );
    	  } // end method toStandardString
     
    // Tick the time by one second
       public int tick() 
       {
     
    setSecond(second+1)
          if ( second == 0 )
             incrementMinute();       return( second + 1 ); 
      }
     
       // Increment the minute
       public void incrementMinute()  
       {
          setMinute( minute + 1 );
     
          if ( minute == 0 )
             incrementHour();
       }
     
       // Increment the hour
       public void incrementHour()
       {
          setHour( hour + 1 );
       }
     
      public void newDisplay()
    { System.out.printf( "Hour: %d Minute: %d Second: %d\n",
    		 getHour(), getMinute(), getSecond() );
    		 System.out.printf( "Universal time: %s Standard time: %s\n",
    		 toUniversalString(),toString() );
    }
     
     } /
    Last edited by memyselfandi; July 9th, 2014 at 08:50 AM. Reason: lack of info


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: TIME PROGRAM UNSOLVED

    That really isn't how this works. What have you tried? Where is your MCVE?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TIME PROGRAM UNSOLVED

    must have the output :

    Enter the time
    Hours: 23
    Minutes: 59
    Seconds: 59
    Hour: 23 Minute: 59 Second: 59
    Universal time: 23:59:59 Standard time: 11:59:59 PM
    1. Add 1 second
    2. Add 1 Minute
    3. Add 1 Hour
    4. Add seconds
    5. Exit
    Choice: 1
    Hour: 0 Minute: 0 Second: 1
    Universal time: 00:00:00 Standard time: 12:00:00 AM


    and so on...

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: TIME PROGRAM UNSOLVED

    That really isn't how this works. What have you tried? Where is your MCVE?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TIME PROGRAM UNSOLVED

    the code is working BUT the new value entered by the user did not change the recent value..

    for case 4 problem
    case 4: // add arbitrary seconds
    					 System.out.print( "Enter seconds to tick: " );
     
    // ???????

    i don't know how to display and add more than the value =1

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,855
    My Mood
    Mellow
    Thanks
    178
    Thanked 631 Times in 618 Posts

    Default Re: TIME PROGRAM UNSOLVED

    I don't see where you're trying to get the user's input. Isn't that what you need to do in Case 4? So get the user's input, set the time based on that input, then continue counting. What part of that don't you understand?

Similar Threads

  1. Unsolved Core Java Program
    By biman001 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 17th, 2014, 02:39 AM
  2. Need Help With First Time Program
    By UnrealOl1ve in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 1st, 2014, 07:03 AM
  3. [SOLVED] How much time should I have on a program?
    By SOG in forum Java Theory & Questions
    Replies: 17
    Last Post: July 19th, 2011, 05:28 PM
  4. please help me with this program, I dont have enough time to do it
    By n.azizabadi in forum Member Introductions
    Replies: 1
    Last Post: June 10th, 2011, 03:35 AM
  5. how to make a program take time...
    By DLH112 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 06:09 PM

Tags for this Thread