- Home
- Assessment

2 / 53

Not able to play video? Try with youtube

**Video Transcript:**

In the previous step, we started Scala successfully. Let's continue from there. You can use this Scala console a very simple calculator as well.

So, if you type 2+4 it would compute the sum. You can see that it has given the answer as 6 and also defined a variable called res0 which contains the result. The data type of res0 is Int which means integer.

To see the value of a variable you can simply type the name of the variable and it would print the value contained in the variable.

You can see that it has displayed the value of res0.

We can also define our own variables by using var. So, if I say var x = 10, it would create x having value as 10.

Let's check the value of x by simplifying typing it.

Also, we can use x in other calculation. For example: var y = x * 4 So, this first multiplies x by 4 and then it assigns the result to y. You can see that the value of y is 40.

The way we have used addition and multiplication operators so far, we also have name operators called methods. These methods take inputs via arguments and optionally returns the result.

For example, print displays the value on the screen. Let's see.

if you type: print("Hello, World")

It prints the "Hello, World" on the screen.

Here print method takes a string or text as input by the way of the first argument. Also, notice that we define a string by enclosing something in double quotes.

Also, note that we define a single character by enclosing it in single quotes. You can see that we defined a variable "a" having character "h" as the value.

This print is an inbuilt method. There are some other libraries of methods which are provided by scala. For example, math library.

Let import all functions from maths library it by typing: import math._ Now we can use functions such as sqrt() for computing square root of a value.

You can see the square root of 25 is 5.

Now, let's try to compute simple interest.

Let's define our principal amount is 10 [var principal = 10] And rate_of_interest as 10 percent annually var rate = 10 And duration as 4 year Var duration = 4 So, our interest would be principal * rate * duration divided by 100. var interest = principal * rate * duration / 100.

So, you can see the interest is 4.

Now, we change the principal amount to 100. And re-execute the formula we will see the interest is 40. Also, note that you can up arrow key to go to previous expressions.

If principal amount is 500 and rate is 12 % yearly, how much would be the interest for a duration of 5 years.

We can also define our own methods so that we don't need to write the same code again and again.

Let's define a method called simpleInterest in the following way: def simpleinterest bracket followed by arguments

Our first argument is principal which is integer and second argument is the rate which double or decimal and third argument duration in years which is an integer.

And the return type is of type double.

We start the body of the method after the open curly bracket. Inside this function, we are going to return the simpleinterest which principal * rate * duration divided by 100.

Then we close the curly brackets to mark the end of the method definition.

Let's try to call the method that we have just defined by providing principal as 700, rate of interest is 7, duration as 4 years

You can see that the interest is 196.

So, we have created our own interest computing machine which we can use as many times as we wish.

Taking you to the next exercise in seconds...

Want to create exercises like this yourself? Click here.

XP

## Loading comments...