Skip to content

Commit e94fbe3

Browse files
committed
fix Activity ch11
1 parent d07fa78 commit e94fbe3

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

Diff for: Chapter11/Activity01/Child.java

+27-15
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,37 @@ public class Child {
99
public static void main(String[] args) throws java.io.IOException, InterruptedException {
1010
int ch;
1111
System.out.print ("Let's echo: ");
12+
13+
// echo out whatever comes from system in
1214
while ((ch = System.in.read ()) != '\n')
1315
System.out.print ((char) ch);
14-
BufferedWriter bw=new BufferedWriter(
16+
17+
// the Child process will be logged in a file here
18+
BufferedWriter bufferedWriter = new BufferedWriter(
1519
new FileWriter(new File("mycal2022.txt")));
16-
int cont = 0;
17-
while(cont < 49) {
18-
System.out.println(cont++);
19-
cont %= 50;
20-
bw.write(cont + "\n");
21-
22-
sleep(200);
23-
24-
if (System.in.available() > 0) {
25-
ch = System.in.read();
26-
if (ch == '*') {
27-
cont = 0;
28-
}
20+
21+
int cont = 0;
22+
while(cont <= 50) {
23+
System.out.println(cont++);
24+
25+
// add this to loop the counting (useful for testing)
26+
//cont %= 50;
27+
28+
// added this to log data in local file + EOL
29+
bufferedWriter.write(cont + "\n");
30+
bufferedWriter.flush();
31+
32+
sleep(1000);
33+
34+
if (System.in.available() > 0) {
35+
ch = System.in.read();
36+
37+
// reset counter if asterisk
38+
if (ch == '*') {
39+
cont = 0;
2940
}
3041
}
31-
bw.close();
42+
}
43+
bufferedWriter.close();
3244
}
3345
}

Diff for: Chapter11/Activity01/Parent.java

+21-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import java.util.concurrent.TimeUnit;
33

44
public class Parent {
5-
public static void main(String[] args) throws IOException {
5+
public static void main(String[] args) throws IOException {
66
Runtime runtime = Runtime.getRuntime();
77
Process process = null;
88

@@ -43,15 +43,26 @@ public static void main(String[] args) throws IOException {
4343
// send to file
4444
bufferedWriter.write(line);
4545
bufferedWriter.flush();
46-
while (line != null) {
47-
System.out.println(line + " - " + Integer.parseInt(line));
48-
line = br.readLine();
49-
if (Integer.parseInt(line) == 37) {
50-
writer.write('*');
51-
writer.flush(); // needed because of the buffered output
52-
System.out.println("sent message");
53-
}
54-
}
46+
47+
while (line != null) {
48+
// send to screen
49+
System.out.println(line);
50+
51+
// send to file + EOL
52+
bufferedWriter.write(line + "\n");
53+
bufferedWriter.flush();
54+
55+
// read next line
56+
line = bufferedReader.readLine();
57+
58+
// this will force the reset of the counter
59+
// the program will therefore never end
60+
if (Integer.parseInt(line) == 37) {
61+
writer.write('*');
62+
writer.flush(); // needed because of the buffered output
63+
System.out.println("sent message");
64+
}
65+
}
5566
process.destroy();
5667
}
5768
}

0 commit comments

Comments
 (0)