Advertisement
jaVer404

level15.lesson02.task05

Jul 5th, 2015
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. package com.javarush.test.level15.lesson02.task05;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /* ООП - исправь ошибки в наследовании
  7. Исправь метод containsBones и всю связанную с ним логику так, чтобы:
  8. 1. Поведение программы осталось прежним, т.е. она должна выдавать то же самое, что и выдает сейчас
  9. 2. Метод containsBones должен возвращать тип Object и значение "Yes" вместо true, "No" вместо false
  10. */
  11.  
  12. public class Solution {
  13.     public static interface Alive {
  14.         Object containsBones();
  15.     }
  16.  
  17.     public static class BodyPart implements Alive {
  18.         private String name;
  19.  
  20.         public BodyPart(String name) {
  21.             this.name = name;
  22.         }
  23.  
  24.         public Object containsBones() {
  25.             return "Yes";
  26.         }
  27.  
  28.         public String toString() {
  29.             return containsBones().equals("Yes") ? name + " содержит кости" : name + " не содержит кости";
  30.         }
  31.     }
  32.  
  33.     public static class Finger extends BodyPart {
  34.         private boolean isFoot;
  35.         public Finger(String name, boolean isFoot) {
  36.             super(name);
  37.             this.isFoot = isFoot;
  38.         }
  39. /*Только тут может возращаться false - (No)*/
  40.         public Object containsBones() {
  41.             if (super.containsBones().equals("Yes")&& !isFoot)
  42.                 return "Yes";
  43.             else
  44.                 return "No";
  45.         }
  46.     }
  47.     public static void main(String[] args)
  48.     {
  49.         printlnFingers();
  50.         printlnBodyParts();
  51.         printlnAlives();
  52.     }
  53.  
  54.     private static void printlnAlives() {
  55.         System.out.println(new BodyPart("Рука").containsBones());
  56.     }
  57.  
  58.     private static void printlnBodyParts() {
  59.         List<BodyPart> bodyParts = new ArrayList<BodyPart>(5);
  60.         bodyParts.add(new BodyPart("Рука"));
  61.         bodyParts.add(new BodyPart("Нога"));
  62.         bodyParts.add(new BodyPart("Голова"));
  63.         bodyParts.add(new BodyPart("Тело"));
  64.         System.out.println(bodyParts.toString());
  65.     }
  66.  
  67.     private static void printlnFingers() {
  68.         List<Finger> fingers = new ArrayList<Finger>(5);
  69.         fingers.add(new Finger("Большой", true));
  70.         fingers.add(new Finger("Указательный", true));
  71.         fingers.add(new Finger("Средний", true));
  72.         fingers.add(new Finger("Безымянный", false));
  73.         fingers.add(new Finger("Мизинец", true));
  74.         System.out.println(fingers.toString());
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement