|
| 1 | +package com.javarush.task.task15.task1505; |
| 2 | + |
| 3 | +//It is good to not eat meat, drink wine, nor do anything by which your brother stumbles, |
| 4 | +//is offended, or is made weak. (Romans 14:21) |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +/* |
| 10 | +ООП - исправь ошибки в наследовании |
| 11 | +*/ |
| 12 | + |
| 13 | +public class Solution { |
| 14 | + public static interface Alive { |
| 15 | + Object containsBones(); |
| 16 | + } |
| 17 | + |
| 18 | + public static class BodyPart implements Alive { |
| 19 | + private String name; |
| 20 | + |
| 21 | + public BodyPart(String name) { |
| 22 | + this.name = name; |
| 23 | + } |
| 24 | + |
| 25 | + public Object containsBones() { |
| 26 | + return "Yes"; |
| 27 | + } |
| 28 | + |
| 29 | + public String toString() { |
| 30 | + return containsBones().equals("Yes") ? name + " содержит кости" : name + " не содержит кости"; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public static class Finger extends BodyPart { |
| 35 | + private boolean isArtificial; |
| 36 | + |
| 37 | + public Finger(String name, boolean isArtificial) { |
| 38 | + super(name); |
| 39 | + this.isArtificial = isArtificial; |
| 40 | + } |
| 41 | + |
| 42 | + public Object containsBones() { |
| 43 | + return super.containsBones().equals("Yes") && !isArtificial ? "Yes" : "No"; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public static void main(String[] args) { |
| 48 | + printlnFingers(); |
| 49 | + printlnBodyParts(); |
| 50 | + printlnAlives(); |
| 51 | + } |
| 52 | + |
| 53 | + private static void printlnAlives() { |
| 54 | + System.out.println(new BodyPart("Рука").containsBones()); |
| 55 | + } |
| 56 | + |
| 57 | + private static void printlnBodyParts() { |
| 58 | + List<BodyPart> bodyParts = new ArrayList<BodyPart>(5); |
| 59 | + bodyParts.add(new BodyPart("Рука")); |
| 60 | + bodyParts.add(new BodyPart("Нога")); |
| 61 | + bodyParts.add(new BodyPart("Голова")); |
| 62 | + bodyParts.add(new BodyPart("Тело")); |
| 63 | + System.out.println(bodyParts.toString()); |
| 64 | + } |
| 65 | + |
| 66 | + private static void printlnFingers() { |
| 67 | + List<Finger> fingers = new ArrayList<Finger>(5); |
| 68 | + fingers.add(new Finger("Большой", true)); |
| 69 | + fingers.add(new Finger("Указательный", true)); |
| 70 | + fingers.add(new Finger("Средний", true)); |
| 71 | + fingers.add(new Finger("Безымянный", false)); |
| 72 | + fingers.add(new Finger("Мизинец", true)); |
| 73 | + System.out.println(fingers.toString()); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +/* |
| 81 | +ООП - исправь ошибки в наследовании |
| 82 | +Исправь метод containsBones и всю связанную с ним логику так, чтобы: |
| 83 | +1. Поведение программы осталось прежним. |
| 84 | +2. Метод containsBones должен возвращать тип Object и значение «Yes» вместо true, «No» вместо false |
| 85 | +
|
| 86 | +
|
| 87 | +Требования: |
| 88 | +1. Метод containsBones в классе BodyPart должен иметь тип возвращаемого значения Object. |
| 89 | +2. Класс Finger должен быть потомком класса BodyPart. |
| 90 | +3. Метод containsBones в классе Finger должен иметь тип возвращаемого значения Object. |
| 91 | +4. Метод containsBones в классе BodyPart должен возвращать строку "Yes". |
| 92 | +5. Метод containsBones в классе Finger должен возвращать строку "Yes", если метод containsBones в классе BodyPart возвращает "Yes" и флаг isArtificial равен false, если приведенное условие не выполняется - вернуть "No". |
| 93 | +6. Метод toString в классе BodyPart должен возвращать строку формата "name(имя части тела) содержит кости", если метод containsBones возвращает "Yes" для этой части тела, а если "No", то строку формата "name(имя части тела) не содержит кости". |
| 94 | +
|
| 95 | +package com.javarush.task.task15.task1505; |
| 96 | +
|
| 97 | +import java.util.ArrayList; |
| 98 | +import java.util.List; |
| 99 | +
|
| 100 | +* |
| 101 | +ООП - исправь ошибки в наследовании |
| 102 | +* |
| 103 | +
|
| 104 | +public class Solution { |
| 105 | + public static interface Alive { |
| 106 | + boolean containsBones(); |
| 107 | + } |
| 108 | +
|
| 109 | + public static class BodyPart implements Alive { |
| 110 | + private String name; |
| 111 | +
|
| 112 | + public BodyPart(String name) { |
| 113 | + this.name = name; |
| 114 | + } |
| 115 | +
|
| 116 | + public boolean containsBones() { |
| 117 | + return true; |
| 118 | + } |
| 119 | +
|
| 120 | + public String toString() { |
| 121 | + return containsBones() ? name + " содержит кости" : name + " не содержит кости"; |
| 122 | + } |
| 123 | + } |
| 124 | +
|
| 125 | + public static class Finger extends BodyPart { |
| 126 | + private boolean isArtificial; |
| 127 | +
|
| 128 | + public Finger(String name, boolean isArtificial) { |
| 129 | + super(name); |
| 130 | + this.isArtificial = isArtificial; |
| 131 | + } |
| 132 | +
|
| 133 | + public boolean containsBones() { |
| 134 | + return super.containsBones() && !isArtificial; |
| 135 | + } |
| 136 | + } |
| 137 | +
|
| 138 | + public static void main(String[] args) { |
| 139 | + printlnFingers(); |
| 140 | + printlnBodyParts(); |
| 141 | + printlnAlives(); |
| 142 | + } |
| 143 | +
|
| 144 | + private static void printlnAlives() { |
| 145 | + System.out.println(new BodyPart("Рука").containsBones()); |
| 146 | + } |
| 147 | +
|
| 148 | + private static void printlnBodyParts() { |
| 149 | + List<BodyPart> bodyParts = new ArrayList<BodyPart>(5); |
| 150 | + bodyParts.add(new BodyPart("Рука")); |
| 151 | + bodyParts.add(new BodyPart("Нога")); |
| 152 | + bodyParts.add(new BodyPart("Голова")); |
| 153 | + bodyParts.add(new BodyPart("Тело")); |
| 154 | + System.out.println(bodyParts.toString()); |
| 155 | + } |
| 156 | +
|
| 157 | + private static void printlnFingers() { |
| 158 | + List<Finger> fingers = new ArrayList<Finger>(5); |
| 159 | + fingers.add(new Finger("Большой", true)); |
| 160 | + fingers.add(new Finger("Указательный", true)); |
| 161 | + fingers.add(new Finger("Средний", true)); |
| 162 | + fingers.add(new Finger("Безымянный", false)); |
| 163 | + fingers.add(new Finger("Мизинец", true)); |
| 164 | + System.out.println(fingers.toString()); |
| 165 | + } |
| 166 | +} |
| 167 | +
|
| 168 | +
|
| 169 | +
|
| 170 | +*/ |
0 commit comments