-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
170 lines (131 loc) · 5.87 KB
/
Solution.java
File metadata and controls
170 lines (131 loc) · 5.87 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.javarush.task.task15.task1505;
//It is good to not eat meat, drink wine, nor do anything by which your brother stumbles,
//is offended, or is made weak. (Romans 14:21)
import java.util.ArrayList;
import java.util.List;
/*
ООП - исправь ошибки в наследовании
*/
public class Solution {
public static interface Alive {
Object containsBones();
}
public static class BodyPart implements Alive {
private String name;
public BodyPart(String name) {
this.name = name;
}
public Object containsBones() {
return "Yes";
}
public String toString() {
return containsBones().equals("Yes") ? name + " содержит кости" : name + " не содержит кости";
}
}
public static class Finger extends BodyPart {
private boolean isArtificial;
public Finger(String name, boolean isArtificial) {
super(name);
this.isArtificial = isArtificial;
}
public Object containsBones() {
return super.containsBones().equals("Yes") && !isArtificial ? "Yes" : "No";
}
}
public static void main(String[] args) {
printlnFingers();
printlnBodyParts();
printlnAlives();
}
private static void printlnAlives() {
System.out.println(new BodyPart("Рука").containsBones());
}
private static void printlnBodyParts() {
List<BodyPart> bodyParts = new ArrayList<BodyPart>(5);
bodyParts.add(new BodyPart("Рука"));
bodyParts.add(new BodyPart("Нога"));
bodyParts.add(new BodyPart("Голова"));
bodyParts.add(new BodyPart("Тело"));
System.out.println(bodyParts.toString());
}
private static void printlnFingers() {
List<Finger> fingers = new ArrayList<Finger>(5);
fingers.add(new Finger("Большой", true));
fingers.add(new Finger("Указательный", true));
fingers.add(new Finger("Средний", true));
fingers.add(new Finger("Безымянный", false));
fingers.add(new Finger("Мизинец", true));
System.out.println(fingers.toString());
}
}
/*
ООП - исправь ошибки в наследовании
Исправь метод containsBones и всю связанную с ним логику так, чтобы:
1. Поведение программы осталось прежним.
2. Метод containsBones должен возвращать тип Object и значение «Yes» вместо true, «No» вместо false
Требования:
1. Метод containsBones в классе BodyPart должен иметь тип возвращаемого значения Object.
2. Класс Finger должен быть потомком класса BodyPart.
3. Метод containsBones в классе Finger должен иметь тип возвращаемого значения Object.
4. Метод containsBones в классе BodyPart должен возвращать строку "Yes".
5. Метод containsBones в классе Finger должен возвращать строку "Yes", если метод containsBones в классе BodyPart возвращает "Yes" и флаг isArtificial равен false, если приведенное условие не выполняется - вернуть "No".
6. Метод toString в классе BodyPart должен возвращать строку формата "name(имя части тела) содержит кости", если метод containsBones возвращает "Yes" для этой части тела, а если "No", то строку формата "name(имя части тела) не содержит кости".
package com.javarush.task.task15.task1505;
import java.util.ArrayList;
import java.util.List;
*
ООП - исправь ошибки в наследовании
*
public class Solution {
public static interface Alive {
boolean containsBones();
}
public static class BodyPart implements Alive {
private String name;
public BodyPart(String name) {
this.name = name;
}
public boolean containsBones() {
return true;
}
public String toString() {
return containsBones() ? name + " содержит кости" : name + " не содержит кости";
}
}
public static class Finger extends BodyPart {
private boolean isArtificial;
public Finger(String name, boolean isArtificial) {
super(name);
this.isArtificial = isArtificial;
}
public boolean containsBones() {
return super.containsBones() && !isArtificial;
}
}
public static void main(String[] args) {
printlnFingers();
printlnBodyParts();
printlnAlives();
}
private static void printlnAlives() {
System.out.println(new BodyPart("Рука").containsBones());
}
private static void printlnBodyParts() {
List<BodyPart> bodyParts = new ArrayList<BodyPart>(5);
bodyParts.add(new BodyPart("Рука"));
bodyParts.add(new BodyPart("Нога"));
bodyParts.add(new BodyPart("Голова"));
bodyParts.add(new BodyPart("Тело"));
System.out.println(bodyParts.toString());
}
private static void printlnFingers() {
List<Finger> fingers = new ArrayList<Finger>(5);
fingers.add(new Finger("Большой", true));
fingers.add(new Finger("Указательный", true));
fingers.add(new Finger("Средний", true));
fingers.add(new Finger("Безымянный", false));
fingers.add(new Finger("Мизинец", true));
System.out.println(fingers.toString());
}
}
*/