1 gentle introduction to programming tirgul 2: scala “hands on” in the lab

17
1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

Post on 22-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

1

Gentle Introduction to Programming

Tirgul 2: Scala “hands on” in the lab

Page 2: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

2

Today• Understanding compiler messages• Debugger• Self work - recursion• Home work

Page 3: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

3

Understanding Compiler MessagesOpen the interpreter, try the following lines of code, and try to understand what the compiler means, and how to solve it

val x = 5

x = 6

Println(“123”)

val f = (x : Int => x + 1)

if x > f(x) x = 9

val f = (x : int) => x + 1

Page 4: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

4

And In Eclipse

• Select and copy a program of your choice to Eclipse• How are compilation errors marked in Eclipse?• Perform compilation errors purposely and see how the compiler’s errors look like:

• Change an upper-case letter to lower case• Remove a closing brackets

• Where are the compiled files?

Page 5: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

5

Today• Understanding compiler messages• Debugger• Self work - recursion• Home work

Page 6: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

6

The Debugger

• Use the program you used earlier• Set a break point and start following your

program step by step• Step in to a function and out from it• Make sure you take a look at the program’s stack

and variables’ state

• Here are the technical instructions

Page 7: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

7

Debugger – Add Breakpoint

• Right click on the desired line

• “Toggle Breakpoint”

Page 8: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

8

Debugger – Start Debugging

breakpoint

debug

Page 9: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

9

Debugger – Debug Perspective

Page 10: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

10

Debugger – Debugging

Current state

Current location

Back to Scala perspective

Page 11: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

11

Today• Understanding compiler messages• Debugger• Self work - recursion• Home work

Page 12: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

Decimal Binary

• We want to print the binary representation of a decimal number

• Examples:• 0 -> 0• 8 -> 1000• 12 -> 1100• 31 -> 11111

Page 13: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

Decimal Binary Recursively

• The conversion of a number d from decimal to binary:• If d is 0 or 1 – write d to the left and stop, else:• If d is even, write ‘0’ to the left• If d is odd, write ‘1’ to the left• Repeat recursively with floor(d/2)

Page 14: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

Example: d = 14

d Output at the end of stage Action at the end of stage

14 0 Insert 0 to left of output; divide d by 2

7 10 Insert 1 to left of output; divide d by 2 and round down

3 110 Insert 1 to left of output; divide d by 2 and round down

1 1110 Insert 1 to left of output; return.

Page 15: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

Solution Dec2Bin.scala

Page 16: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

16

Today• Understanding compiler messages• Debugger• Self work - recursion• Home work

Page 17: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab

Exercise

• Write a function that simulates print on positive integers

• The function should print one digit at time• Example:

• printRec(1234) 1234