-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
121 lines (89 loc) · 4.09 KB
/
Solution.java
File metadata and controls
121 lines (89 loc) · 4.09 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// If I hadn't done among them the works which no one else did, they wouldn't have had sin.
//But now have they seen and also hated both me and my Father. (John 15:24)
package com.javarush.task.task16.task1608;
/*
Продвижение на политических дебатах
*/
public class Solution {
public static int totalCountSpeeches = 200;
public static int soundsInOneSpeech = 1000000;
public static void main(String[] args) throws InterruptedException {
Politician ivanov = new Politician("Иванов");
ivanov.join();
Politician petrov = new Politician("Петров");
Politician sidorov = new Politician("Сидоров");
while (ivanov.getCountSpeeches() + petrov.getCountSpeeches() + sidorov.getCountSpeeches() < totalCountSpeeches) {
}
System.out.println(ivanov);
System.out.println(petrov);
System.out.println(sidorov);
}
public static class Politician extends Thread {
private volatile int countSounds;
public Politician(String name) {
super(name);
start();
}
public void run() {
while (countSounds < totalCountSpeeches * soundsInOneSpeech) {
countSounds++;
}
}
public int getCountSpeeches() {
return countSounds / soundsInOneSpeech;
}
@Override
public String toString() {
return String.format("%s сказал речь %d раз", getName(), getCountSpeeches());
}
}
}
/*
Продвижение на политических дебатах
1. Разберись, что делает программа.
2. Нужно сделать так, чтобы Иванов сказал больше всего речей на политических дебатах.
3. Подумай, какой метод можно вызвать у объекта ivanov, чтобы Иванов разговаривал, пока не завершится всё свободное время.
Требования:
1. Вызови метод join у нужного объекта.
2. Метод toString класса политик Politician должен выводить сколько речей сказал политик, например: "Иванов сказал речь 35 раз".
3. Программа должна создавать 3 объекта типа Politician.
4. Методы, которые отвечают за вывод в консоль, не изменять.
5. Вывод программы должен свидетельствовать о том, что Иванов сказал больше всего речей на политических дебатах.
package com.javarush.task.task16.task1608;
*
Продвижение на политических дебатах
*
public class Solution {
public static int totalCountSpeeches = 200;
public static int soundsInOneSpeech = 1000000;
public static void main(String[] args) throws InterruptedException {
Politician ivanov = new Politician("Иванов");
Politician petrov = new Politician("Петров");
Politician sidorov = new Politician("Сидоров");
while (ivanov.getCountSpeeches() + petrov.getCountSpeeches() + sidorov.getCountSpeeches() < totalCountSpeeches) {
}
System.out.println(ivanov);
System.out.println(petrov);
System.out.println(sidorov);
}
public static class Politician extends Thread {
private volatile int countSounds;
public Politician(String name) {
super(name);
start();
}
public void run() {
while (countSounds < totalCountSpeeches * soundsInOneSpeech) {
countSounds++;
}
}
public int getCountSpeeches() {
return countSounds / soundsInOneSpeech;
}
@Override
public String toString() {
return String.format("%s сказал речь %d раз", getName(), getCountSpeeches());
}
}
}
*/