Advertisement
jaVer404

level14.lesson04.task05

Jun 6th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. package com.javarush.test.level14.lesson04.task05;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5.  
  6. /* Player and Dancer
  7. 1. Подумать, что делает программа.
  8. 2. Изменить метод haveRest так, чтобы он вызывал метод
  9. - play, если person имеет тип Player
  10. - dance, если person имеет тип Dancer
  11. */
  12.  
  13. public class Solution
  14. {
  15.     public static void main(String[] args) throws Exception
  16.     {
  17.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  18.  
  19.         Person person = null;
  20.         String key;
  21.         while ((key = reader.readLine()) != null)
  22.         {
  23.             if ("player".equals(key))
  24.             {
  25.                 person = new Player();
  26.             } else if ("dancer".equals(key))
  27.             {
  28.                 person = new Dancer();
  29.             }
  30.             haveRest(person);
  31.         }
  32.     }
  33.  
  34.     public static void haveRest(Person person)
  35.     {
  36.         //Add your code here
  37.         if (person instanceof Player) {
  38.             ((Player) person).play();
  39.         }
  40.         else if (person instanceof Dancer) {
  41.             ((Dancer) person).dance();
  42.         }
  43.     }
  44.  
  45.     interface Person
  46.     {
  47.     }
  48.  
  49.     static class Player implements Person
  50.     {
  51.         void play()
  52.         {
  53.             System.out.println("playing");
  54.         }
  55.     }
  56.  
  57.     static class Dancer implements Person
  58.     {
  59.         void dance()
  60.         {
  61.             System.out.println("dancing");
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement