1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab.
-
date post
22-Dec-2015 -
Category
Documents
-
view
219 -
download
1
Transcript of 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab.
![Page 1: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/1.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/2.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/3.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/4.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/5.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/6.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/7.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/8.jpg)
8
Debugger – Start Debugging
breakpoint
debug
![Page 9: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/9.jpg)
9
Debugger – Debug Perspective
![Page 10: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/10.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/11.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/12.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/13.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/14.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/15.jpg)
Solution Dec2Bin.scala
![Page 16: 1 Gentle Introduction to Programming Tirgul 2: Scala “hands on” in the lab.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/16.jpg)
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.](https://reader035.fdocuments.us/reader035/viewer/2022062715/56649d795503460f94a5c02f/html5/thumbnails/17.jpg)
Exercise
• Write a function that simulates print on positive integers
• The function should print one digit at time• Example:
• printRec(1234) 1234