Calculator program in Scala

Hey there!

Welcome to ClearUrDoubt.com.

In this post, we will look at a Scala program to implement a Calculator operations.

Let’s look at the program:

Output:

  1. We have created a Calculator class with 4 typical calculator methods +(addition), -(subtraction), *(multiplication) and /(division) methods. “+”, “-“, “*” and “/” are legal identifiers in Scala so we can have them as function names for better readability.
  2. We have named both the class and object names as “Calculator” and it is perfectly acceptable in Scala. These are called Companion objects/classes.
  3. require() is used to make sure a variable satisfies the provided condition to proceed further. If the condition fails(uncomment the line 29), the compiler will throw an exception, as below, with the message passed in the require():
  4. All the functions are returning some value in the above program but we haven’t specified “return” statement in any of them because “return” statement is optional in Scala.

Happy Learning :).

Please leave a reply in case of any queries.

2 Replies to “Calculator program in Scala”

Leave a Reply to Sai Aditya Cancel reply

Your email address will not be published.