Skip to content

Commit d12e5ea

Browse files
committed
Add complete chapter 4
Signed-off-by: DanielDG <danielramososielrodrigues@gmail.com>
1 parent 2e5bdf1 commit d12e5ea

6 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package chapter4.exercises.becompiler.clock;
2+
3+
public class Clock {
4+
String time;
5+
void setTime(String t) {
6+
time = t;
7+
}
8+
String getTime() {
9+
return time;
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package chapter4.exercises.becompiler.clock;
2+
3+
public class ClockTestDrive {
4+
public static void main(String[] args) {
5+
Clock c = new Clock();
6+
7+
c.setTime("1245");
8+
String tod = c.getTime();
9+
System.out.println("time: " + tod);
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package chapter4.exercises.becompiler.xcopy;
2+
3+
public class XCopy {
4+
public static void main(String[] args) {
5+
int orig = 42;
6+
XCopy x = new XCopy();
7+
int y = x.go(orig);
8+
System.out.println(orig + " " + y);
9+
}
10+
int go(int arg) {
11+
arg = arg * 2;
12+
return arg;
13+
}
14+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package chapter4.exercises.mixedmessages;
2+
3+
public class Mix4 {
4+
int counter = 0;
5+
public static void main(String[] args) {
6+
int count = 0;
7+
Mix4[] m4a = new Mix4[20];
8+
int x = 0;
9+
while(x<19) {
10+
m4a[x] = new Mix4();
11+
m4a[x].counter = m4a[x].counter + 1;
12+
count = count + 1;
13+
count = count + m4a[x].maybeNew(x);
14+
x = x + 1;
15+
}
16+
System.out.println(count + " " + m4a[1].counter);
17+
}
18+
public int maybeNew(int index) {
19+
if(index < 1) {
20+
Mix4 m4 = new Mix4();
21+
m4.counter = m4.counter + 1;
22+
return 1;
23+
}
24+
return 0;
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package chapter4.exercises.poopuzzle;
2+
3+
public class Puzzle4 {
4+
public static void main(String[] args) {
5+
Puzzle4b[] obs = new Puzzle4b[6];
6+
int y = 1;
7+
int x = 0;
8+
int result = 0;
9+
while(x<6) {
10+
obs[x] = new Puzzle4b();
11+
obs[x].ivar = y;
12+
y = y * 10;
13+
x = x + 1;
14+
}
15+
x = 6;
16+
while(x>0) {
17+
x = x - 1;
18+
result = result + obs[x].doStuff(x);
19+
}
20+
System.out.println("result " + result);
21+
}
22+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package chapter4.exercises.poopuzzle;
2+
3+
public class Puzzle4b {
4+
int ivar;
5+
public int doStuff(int factor){
6+
if(ivar>100) {
7+
return ivar*factor;
8+
} else {
9+
return ivar * (5-factor);
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)