Skip to content

Instantly share code, notes, and snippets.

@agushuley
Created October 2, 2012 07:24
Show Gist options
  • Save agushuley/3817034 to your computer and use it in GitHub Desktop.
Save agushuley/3817034 to your computer and use it in GitHub Desktop.
Scala for impatients, chapter 5, 3th task
class Time( private var _hour: Int, private var _minute: Int ) {
if ( _minute > 59 ) {
_hour += ( _minute / 60 )
_minute = _minute % 60
}
if ( hour > 23 ) {
_hour = 23
}
def hour = _hour
def minute = _minute
def before( other: Time ) =
if ( other.hour > _hour ) true
else if ( other.hour < _hour ) false
else if ( other.minute > _minute ) true
else false
override def toString(): String = hour + ":" + minute
}
val time1 = new Time( 1, 10 )
val time2 = new Time( 2, 10 )
val time3 = new Time( 1, 11 )
println( time1 + " before " + time2 + "? " + time1.before( time2 ) )
println( time2 + " before " + time1 + "? " + time2.before( time1 ) )
println( time1 + " before " + time3 + "? " + time1.before( time3 ) )
println( time3 + " before " + time1 + "? " + time3.before( time1 ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment