1 gentle introduction to programming tirgul 2: scala “hands on” in the lab
Post on 22-Dec-2015
217 views
TRANSCRIPT
1
Gentle Introduction to Programming
Tirgul 2: Scala “hands on” in the lab
2
Today• Understanding compiler messages• Debugger• Self work - recursion• Home work
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
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?
5
Today• Understanding compiler messages• Debugger• Self work - recursion• Home work
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
7
Debugger – Add Breakpoint
• Right click on the desired line
• “Toggle Breakpoint”
8
Debugger – Start Debugging
breakpoint
debug
9
Debugger – Debug Perspective
10
Debugger – Debugging
Current state
Current location
Back to Scala perspective
11
Today• Understanding compiler messages• Debugger• Self work - recursion• Home work
Decimal Binary
• We want to print the binary representation of a decimal number
• Examples:• 0 -> 0• 8 -> 1000• 12 -> 1100• 31 -> 11111
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)
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.
Solution Dec2Bin.scala
16
Today• Understanding compiler messages• Debugger• Self work - recursion• Home work
Exercise
• Write a function that simulates print on positive integers
• The function should print one digit at time• Example:
• printRec(1234) 1234