Exercises
@main
def read(): Unit =
val scanner = new Scanner(System.in)
print("read: ")
val s = scanner.nextLine
println("write: " + s)
Exercise 1 Begin
Create new project.
Create a @main function
Add the code and run
Exercise 2 Welcome
Ask for a user name
Print a welcome message with the name: Hello, [name]
Exercise 3 Add
Ask the user for two numbers n1 and n2
Add and print the numbers
Hint: use readInt()
Next divide the numbers What do you see. Is this OK?
Read a Double Hint use toDouble()
Exercise 4 Menu
Create een choice menu with the next layout
Menu Choose
(1) - print hello
(2) - print bye
Your choice: 1
hello
Exercise 5 Calculator
Create a calculator that can add, subtract, multiply and divide
Use the next layout
Calculator
Give the first number: 3
Give the second number: 4
Give the operator: *
3 * 4 = 12
Exercise 6 Drawing
We will use nested for-loops to draw shapes.
for i <- 0 to 3 do
for j <- 0 to 3 do
print("* ")
println()
println()
An example of a rectangle
Draw the next shapes
*
* *
* * *
* * * *
* * * *
* * *
* *
*
* * * *
* * *
* *
*
*
*
*
*
Exercise 7 List
Create list in the range from 0 to 10
Print de list with a for-loop
var list = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Reverse and print the list
Add all the values
Calculate the average value
Exercise 8 String
Ask the user to give a text string
Calculate the length of the text.
Split de text in words Hint use: split
How many words are in the tekst.
Exercise 9 Function
Write a function that add two numbers.
Write function log(text: String) print the date and the text.