Scala

You are currently auditing this course.
25 / 53

Scala - Assessment - Display Greeting Message

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...

INSTRUCTIONS
  • 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".


Loading comments...