Enrollments closing soon for Post Graduate Certificate Program in Applied Data Science & AI By IIT Roorkee | 3 Seats Left
Apply NowLogin using Social Account
     Continue with GoogleLogin using your credentials
Let's take a look at a program which will return different greeting messages according to the time of the day.
We will write a Scala program with the main class name of Greet
having a function named greet
which would return different greeting messages as described below, according to the time input to the function.
Input time - 4 to till before 12 : Message Output - "Good Morning"
Input time - 12 to till before 16 : Message Output - "Good Afternoon"
Input time - 16 to till before 21 : Message Output - "Good Evening"
Input time - 21 to till before 4 : Message Output - "Good Night"
Example Usage:
If we execute Greet.greet(5)
, then it should return "Good Morning"
If we execute Greet.greet(12)
, then it should return "Good Afternoon"
and so on...
Write the code in Jupyter and press Shift + Enter. Your code should look like the following:
object Greet{ def greet(hour:Int):String = { if (hour >= 4 && hour < 12){ return "Good Morning" } <<< YOUR CODE>>>> } }
Test the function with various inputs to make sure that it is returning the outputs as per requirements.
Click on "Submit Answer".
Taking you to the next exercise in seconds...
Want to create exercises like this yourself? Click here.
Note - Having trouble with the assessment engine? Follow the steps listed here
Loading comments...