The following is a challenge for new developers that have completed:
Java Tutorial - 36-37 Displaying Regular Time by theNewBoston
To get the most out of these challenges try them a few hours or the day after watching/following along with the actual video.
To get the most out of these challenges try them a few hours or the day after watching/following along with the actual video.
CHALLENGE!!
(Before starting any of the challenges always declare a class and a main method unless told otherwise)
Difficulty:★★
1.Create a class and name it "Time".Unlike classes we've created in the past you dont need to declare a main method. 2.Create 3 int vars. name them hour, minute, second.
3.Create a method,name it setTime, it should accept 3 parameters(int h, int m , int s)
Inside the setTime method:
- if h is greater than 0 and less than 24 then set h to hour, else set the hour to 0 by default.This should be accomplished in one line of code( Hint:use the "ternary operator" instead of a if statement)
- if m is greater than 0 and less than 60 then set m to minute, else set the minute to 0 by default.(Hint: "ternary operator")
- if s is greater than 0 and less than 60 then set s to second, else set the second to 0 by default.(Hint: "ternary operator")
End "setTime" method
4.Create another method.Name it toMilitary.
Inside the to Military method:
- Create a return statement that will format the "hour" , "minute" and "second" vars to display in military format.
Example of console output:18:30:00
End "toMilitary" method
Inside the "toRegTime" method:
- Create a return statement that will format the "hour" , "minute" and "second" vars to display in standard time format. (hint:Ull need to use the Modulus (%) operator
Example of console output:6:30:00 PM
End "toRegTime" method
End of "Time" class
6.Create another class and name it testTime .Create a main method.
Inside the main method of testTime class:
- Declare a new Time object and name it tickTock.
- Print tickTock.toMilitary
- Print tickTock.toRegTime
- Call the setTime method with the parameters (13,27,6)
- Print tickTock.toMilitary
- Print tickTock.toRegTime
End of "testTime" class
COMPILE & PRAY TO DUKE!!!
View the video tutorial: http://bit.ly/qkxfja


- Follow Us on Twitter!
- "Join Us on Facebook!
- RSS
Contact