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 2 of 2

Thread: Time class - previousSecond()

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Time class - previousSecond()

    im unable to work out my previousSecond() in Time class.

    here is my code:
     
    	}void setHour(int hour){
    		if (hour >= 0 && hour <= 23){
    			this.hour = hour; 
    		}else{ 
    			throw new IllegalArgumentException("Invalid hour!");
    		}
     
    	}void setMinute(int minute){
    			if (minute >= 0 && minute <= 59){
    				this.minute = minute; 
    			}else {
    				throw new IllegalArgumentException("Invalid minute!");
    			}
     
    	}void setSecond(int second){
    		if (second >= 0 && second <= 59){
    			this.second = second;
    		}else{ 
    			throw new IllegalArgumentException("Invalid second!");
    		}
     
     
    	}public Time previousSecond(){
    			second--; 
    			if (second == 60){
    				second = 0;
    				minute--; 	
     
    			}if (minute == 60){
    				minute = 0;
    				hour--; 	
     
    			}if (hour == 24){
    				hour = 0; 
    			}
    			return this; 
    	}
     
    }


    this is the test code:

          // Test previousSecond()
          t1.setTime(0, 0, 2);
          System.out.println(t1.previousSecond());
          System.out.println(t1.previousSecond().previousSecond());


    result i get is 00:00:-1 instead of 23:59:59. whats wrong with my code?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Time class - previousSecond()

    Try debugging the code to see what it is doing. I use println() statements that print out the values of the variables as the code executes so you can see what the computer sees when it executes the code.

    You need to post a complete class that compiles and executes if you want help testing the code.


    What does previousSecond() do if the value of second is 0?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help!! Schedual class with time and date
    By passoa in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 30th, 2013, 07:36 AM
  2. Replies: 12
    Last Post: May 9th, 2013, 02:10 PM
  3. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  4. enum class creation to hold time on android
    By jobsfind39 in forum Object Oriented Programming
    Replies: 3
    Last Post: June 21st, 2012, 10:04 AM
  5. First time poster looking for help accessing a variable from a separate class.
    By royalcrown28 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 19th, 2012, 11:41 PM