Vous êtes sur la page 1sur 9

Addition (concatenation) of strings using + operator.

Addition of variables and seeing how the type is already converted.

Declaring multiple variables at a time. We can only assign two values and not
more than that.
Declaring a function and calling it. Here, we wrote a function for the product of
two numbers. +c tells to include the value of c.

Defined a class Quadrilateral and an object Area based on the class. Classes
helps in code reusability. Based on the class Quadrilateral, we created an
object of square to find the area.
Simple if..else condition. Compared two variables using if..else to find which one
is greater.

Simple while loop example. Initialized a with 10 and print a+1 until a<50. So, it
prints from 10 to 49.
Passing command line arguments and comparing two numbers. Above screenshot
shows that we passed command line argument in one case and no command line
arguments in another case. We used if..else and args.length in this example.

Written a scala program to demonstrate the working on returning a value in


functions. We returned the value of Sum to the function in this example.

In the above example, a function literal is defined as an alternate syntax for


defining a function. It's useful when you want to pass a function as an argument
to a method but you don't want to define a separate function.
The above is an example for clojure function in Scala. Clojure is a special kind of
function where the return value of the function depends on the variable defined
outside the scope of the function. We tried the concept on converting the value
of dollars to INR.

Created two arrays and printed those using different ways. One of the ways
used in the example to print the arrays is using for loop. The mkString method
is defined on collections and is a method which joins the elements of the
collection with the provided string.

The above are also the ways of initializing array elements.


The above example shows different types of creating a list (nothing but a
collection of items) in Scala. We can also get the class of the list using getClass
command. Lists are quite similar to arrays which means, all the elements of a list
have the same type but there are two important differences. First, lists are
immutable, which means elements of a list cannot be changed by assignment.
Second, lists represent a linked list whereas arrays are flat.

The above are some other ways to create lists. List object takes the range and
returns the list. Above example takes range from 1 to 50 (1,50) and another
example prints the list with a difference between the elements (1,50,2) =>
difference of 2. We can also create tuples with heterogeneous collection of
elements. As shown above, we can retrieve the elements in a tuple.

Assignment Question

object Assignment

def main(args: Array[String])

val list_of_numbers = (1 to 100).toList //creating a list of numbers from 1 to


100

println(list_of_numbers) //printing the list of numbers

println("\nThe list of the numbers that are divisible by 7 is:")

for(i <- list_of_numbers)

if(i%7 == 0) //if condition which checks if the values are divisible by 7

println(i) //printing the list of numbers that are divisible by 7 using for loop
}

var list_7 = List.range(7,100,7) //creating a list of numbers divisible by 7

var len = list_7.length //finding and assigning the length of list_7 to len

println("\nThe number of numbers that are divisible by 7 is: " +len) //printing
the length of list of numbers that are divisible by 7

*The above Scala program prints a list of numbers from 1 to 100 and checks the
numbers that are divisible by 7 from the list and prints them. We also find how
many numbers in the list are divisible by 7.

Vous aimerez peut-être aussi