-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
98 lines (78 loc) · 4 KB
/
Solution.java
File metadata and controls
98 lines (78 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//Pilate therefore said to them, "Take him yourselves, and judge him according to your law." Therefore the Jews said to him, "It is not lawful for us to put anyone to death," (John 18:31)
package com.javarush.task.task19.task1927;
/*
Контекстная реклама
*/
import java.io.*;
public class Solution {
public static TestString testString = new TestString();
public static void main(String[] args) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream stream = new PrintStream(baos);
PrintStream systemOut = System.out;
System.setOut(stream);
testString.printSomething();
System.setOut(systemOut);
BufferedReader reader = new BufferedReader(new StringReader(baos.toString()));
String string;
int count = 0;
while ((string = reader.readLine()) != null) {
System.out.println(string);
if (++count % 2 == 0) {
System.out.println("JavaRush - курсы Java онлайн");
}
}
}
public static class TestString {
public void printSomething() {
System.out.println("first");
System.out.println("second");
System.out.println("third");
System.out.println("fourth");
System.out.println("fifth");
}
}
}
/*
Контекстная реклама
В методе main подмени объект System.out написанной тобой реадер-оберткой.
Твоя реадер-обертка должна выводить на консоль контекстную рекламу после каждого второго println-а.
Вызови готовый метод printSomething(), воспользуйся testString.
Верни переменной System.out первоначальный поток.
Рекламный текст: «JavaRush - курсы Java онлайн»
Пример вывода:
first
second
JavaRush - курсы Java онлайн
third
fourth
JavaRush - курсы Java онлайн
fifth
Требования:
1. Класс Solution должен содержать класс TestString.
2. Класс Solution должен содержать публичное статическое поле testString типа TestString, которое сразу проинициализировано.
3. Класс TestString должен содержать публичный void метод printSomething().
4. Метод printSomething() класса TestString должен выводить на экран строки: "first","second","third","fourth","fifth".
5. Метод main(String[] args) класса Solution должен создавать поток PrintStream (используй PrintStream c параметром конструктора ByteArrayOutputStream).
6. Метод main(String[] args) класса Solution должен подменять и восстанавливать поток вывода в консоль объекта System.out.
7. Метод main(String[] args) класса Solution должен вызывать метод printSomething(),объекта testString.
8. Метод main(String[] args) класса Solution должен модифицировать строки(вставлять контекстную рекламу) выведенные методом printSomething() согласно заданию, и выводить её в консоль.
package com.javarush.task.task19.task1927;
*
Контекстная реклама
*
public class Solution {
public static TestString testString = new TestString();
public static void main(String[] args) {
}
public static class TestString {
public void printSomething() {
System.out.println("first");
System.out.println("second");
System.out.println("third");
System.out.println("fourth");
System.out.println("fifth");
}
}
}
*/