Skip to content

Commit 698c11e

Browse files
committed
add answers to exercise 1-4 chapter 5
1 parent a2bac23 commit 698c11e

File tree

4 files changed

+210
-0
lines changed

4 files changed

+210
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.learningconcurrency.exercises.ch5
2+
3+
object Ex2 {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.learningconcurrency.exercises.ch5
2+
3+
object Ex3 {
4+
5+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.learningconcurrency
2+
package exercises
3+
package ch5
4+
5+
/**
6+
* Measure the average running time of allocating a simple object on the JVM.
7+
*/
8+
9+
object Ex1 extends App {
10+
11+
object Timed {
12+
13+
@volatile
14+
var dummy: Any = _
15+
16+
def buildObjects(count:Int) = {
17+
var i = 0
18+
val start = System.nanoTime
19+
while (i < count) {
20+
dummy = new Object
21+
i += 1
22+
}
23+
(System.nanoTime - start)/count.toDouble
24+
}
25+
26+
}
27+
28+
var i = 0
29+
var summ = 0D
30+
31+
var timePrev = 0D
32+
while (i < 30) {
33+
34+
val time = Timed.buildObjects(10000000)
35+
val e = Math.abs(time - timePrev)/time*100
36+
37+
//check steady state
38+
if (e < 10) {
39+
i += 1
40+
summ += time
41+
} else {
42+
i = 0
43+
summ = time
44+
}
45+
46+
timePrev = time
47+
log(s"time = ${time.toString} e = ${Math.round(e)}, i = $i")
48+
49+
}
50+
51+
log("----------------------------------------------------")
52+
log(s"avg = ${summ/(i+1)} nanoseconds")
53+
54+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package org.learningconcurrency
2+
package exercises
3+
package ch5
4+
5+
import javafx.scene.layout.HBox
6+
7+
import scalafx.Includes._
8+
import scalafx.application.JFXApp
9+
import scalafx.application.JFXApp.PrimaryStage
10+
import scalafx.geometry.Insets
11+
import scalafx.scene.Scene
12+
import scalafx.scene.control._
13+
import scalafx.scene.layout.{Pane, VBox}
14+
import scalafx.scene.paint.Color
15+
import scalafx.scene.shape.Rectangle
16+
17+
object Ex4 extends JFXApp {
18+
19+
/**
20+
* Implement a program that simulates a cellular automaton in parallel.
21+
*/
22+
23+
val maxX = 40
24+
val maxY = 40
25+
26+
case class Cell(state: Boolean, index: Int, neighbors: Seq[Int])
27+
28+
def index(i: Int, j: Int) = i + j * maxX
29+
30+
def findNeighbors(x: Int, y: Int) = {
31+
for {
32+
i <- x - 1 to x + 1
33+
j <- y - 1 to y + 1
34+
if i > -1 && i < maxX && j > -1 && j < maxY && ((i != x) || (j != x))
35+
} yield index(i, j)
36+
}
37+
38+
def checkInitialState(x: Int, y: Int) = (x == maxX / 2 - 1 || x == maxX / 2 + 1) && y == maxY / 2
39+
40+
def initialize: IndexedSeq[Cell] = {
41+
(
42+
for {
43+
x <- 0 until maxX
44+
y <- 0 until maxY
45+
} yield (x, y)
46+
).map {
47+
case (x, y) => Cell(
48+
state = checkInitialState(x,y),
49+
index = index(x, y),
50+
neighbors = findNeighbors(x, y)
51+
)
52+
}
53+
}
54+
55+
def countNeighbors(cell: Cell, cells: Seq[Cell]) =
56+
cell.neighbors.count((i) => cells(i).state)
57+
58+
def defState(countNeighbors: Int) = {
59+
(countNeighbors == 2) || (countNeighbors == 3)
60+
}
61+
62+
def next(cells: Seq[Cell]) = {
63+
cells.par.map(
64+
(cell) => cell.copy(state = defState(countNeighbors(cell, cells)))).toIndexedSeq
65+
}
66+
67+
//Test App
68+
69+
var cells = List(initialize)
70+
71+
val cellAreaWidth = 400
72+
val cellAreaHeight = 400
73+
74+
val rectWidth = cellAreaWidth / maxX
75+
val rectHeight = cellAreaHeight / maxY
76+
77+
def buildRectangles = (
78+
for {
79+
y <- 0 until maxY
80+
x <- 0 until maxX
81+
} yield (x, y)).
82+
map {
83+
case (posX, posY) => new Rectangle() {
84+
x = posX * rectWidth
85+
y = posY * rectHeight
86+
width = rectWidth
87+
height = rectHeight
88+
fill = Color.GRAY
89+
}
90+
}.toArray
91+
92+
93+
val rectangles = buildRectangles
94+
95+
val cellsPane = new Pane {
96+
maxWidth = cellAreaWidth
97+
maxHeight = cellAreaHeight
98+
}
99+
rectangles.foreach(cellsPane.children.add(_))
100+
101+
def drawCells = cells.head.foreach((c) => rectangles(c.index).setFill(if (c.state) Color.RED else Color.WHITE))
102+
103+
val nextButton = new Button() {
104+
text = "=>"
105+
onAction = handle {
106+
cells = next(cells.head) :: cells
107+
drawCells
108+
}
109+
}
110+
111+
val prevButton = new Button() {
112+
text = "<="
113+
onAction = handle {
114+
cells = cells match {
115+
case h :: Nil => cells
116+
case h :: t => t
117+
}
118+
drawCells
119+
}
120+
}
121+
122+
val hbox = new HBox()
123+
hbox.children.add(prevButton)
124+
hbox.children.add(nextButton)
125+
126+
127+
val vbox = new VBox {
128+
padding = Insets(20)
129+
spacing = 10
130+
}
131+
132+
vbox.children.add(hbox)
133+
vbox.children.add(cellsPane)
134+
135+
stage = new PrimaryStage {
136+
title.value = "Ch5 Ex4"
137+
scene = new Scene {
138+
content = vbox
139+
}
140+
}
141+
142+
stage.sizeToScene()
143+
144+
drawCells
145+
146+
}

0 commit comments

Comments
 (0)