object TravelTimeCalculator {
def main(args: Array[String]): Unit = {
// Initialize distance and speed
val distance: Double = 30.0 // (in km)
val speed: Double = 90.0 // (in km/h)
// Check to avoid division by zero
if (speed <= 0) {
Console.err.println("Speed must be greater than zero.")
sys.exit(1) // exit with error code
}
// Calculate time
val tm: Double = distance / speed
println(f"Time required: $tm%.2f hours")
// Optional: convert to hours and minutes
val hours: Int = math.floor(tm).toInt
val minutes: Int = math.round((tm - hours) * 60).toInt
println(s"Which is approximately $hours hours and $minutes minutes.")
}
}
/*
run:
Time required: 0.33 hours
Which is approximately 0 hours and 20 minutes.
*/