Skip to content

Instantly share code, notes, and snippets.

@Haaroon
Created December 30, 2020 15:11
Show Gist options
  • Save Haaroon/4567769898b5bb32c68d7b04b472ecba to your computer and use it in GitHub Desktop.
Save Haaroon/4567769898b5bb32c68d7b04b472ecba to your computer and use it in GitHub Desktop.
// ex1
class Counter {
private var value = 0
def increment() {
if (value != Int.MaxValue)
value += 1
}
}
// ex2
class BankAccount {
private var _balance = 0
def deposit(amt : Int){
_balance += amt
}
def withdraw(amt : Int) {
if (amt <= _balance){
_balance -= amt
}
}
def balance = _balance
}
class Time {
private var _hours = 0
private var _minutes = 0
private var _sinceMidnight = 0
def hours = _hours
def minutes = _minutes
def sinceMidnight = _sinceMidnight
def this(setHours: Int, setMins: Int){
this()
if (setHours >= 0 & setHours <= 23)
_hours = setHours
if (setMins >= 0 & setMins <= 59)
_minutes = setMins
_sinceMidnight = _hours * _minutes
}
def before(other: Time): Boolean = {
if (other.hours > _hours)
return true
else if (other.hours < _hours)
return false
else if (other.minutes <= _minutes)
return false
else
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment