ЗАДАЧА 14.04.03.

package com.javarush.test.level14.lesson04.task03;

/* Food
1. Реализовать интерфейс Selectable в классе Food.
2. Метод onSelect() должен писать в консоль "food is selected".
3. Подумай, какие методы можно вызвать для переменной food и какие для selectable.
4. В методе foodMethods вызови методы onSelect, eat, если это возможно.
5. В методе selectableMethods вызови методы onSelect, eat, если это возможно.
6. Явное приведение типов не использовать.
*/

public class Solution
{
    public static void main(String[] args)
    {
        Food food = new Food();
        Selectable selectable = new Food();
        Food newFood = (Food) selectable;
        selectableMethods(selectable);
        foodMethods(food);

    }

    public static void foodMethods(Food food)
    {
        //тут добавьте вызов методов для переменной food
        food.onSelect();
        food.eat();
    }

    public static void selectableMethods(Selectable selectable)
    {
        //тут добавьте вызов методов для переменной selectable
        selectable.onSelect();
    }

    interface Selectable
    {
        void onSelect();
    }

    static class Food implements Selectable
    {
        public void eat()
        {
            System.out.println("food is eaten");
        }

        @Override
        public void onSelect()
        {
            System.out.println("food is selected");
        }
    }
}

 

ЗАДАЧА 14.04.04.

package com.javarush.test.level14.lesson04.task04;

/* Без ошибок
Инициализировать объект obj таким классом, чтобы метод main выполнился без ошибок.
*/

public class Solution
{
    public static void main(String[] args)
    {
        Object obj = new Jerry();

        Mouse mouse = (Mouse) obj;
        GreyMouse greyMouse = (GreyMouse) mouse;
        Jerry jerry = (Jerry) greyMouse;

        printClasses(obj, mouse, greyMouse, jerry);

    }

    public static void printClasses(Object obj, Mouse mouse, GreyMouse greyMouse, Jerry jerry)
    {
        System.out.println(jerry.getClass().getSimpleName());
        System.out.println(greyMouse.getClass().getSimpleName());
        System.out.println(mouse.getClass().getSimpleName());
        System.out.println(obj.getClass().getSimpleName());
    }

    static class Mouse
    {
    }

    static class GreyMouse extends Mouse
    {
    }

    static class Jerry extends GreyMouse
    {
    }
}

 

ЗАДАЧА 14.04.05.

package com.javarush.test.level14.lesson04.task05;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/* Player and Dancer
1. Подумать, что делает программа.
2. Изменить метод haveRest так, чтобы он вызывал метод
- play, если person имеет тип Player
- dance, если person имеет тип Dancer
*/

public class Solution
{
    public static void main(String[] args) throws Exception
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        Person person = null;
        String key;
        while (!(key = reader.readLine()).equals("exit"))
        {
            if ("player".equals(key))
            {
                person = new Player();
            } else if ("dancer".equals(key))
            {
                person = new Dancer();
            }
            haveRest(person);
        }
    }

    public static void haveRest(Person person)
    {
        //Add your code here
        if (person instanceof Player)
        {
            Player player = (Player) person;
            player.play();
        }
        else if (person instanceof Dancer)
        {
            Dancer dancer = (Dancer) person;
            dancer.dance();
        }

    }

    interface Person
    {
    }

    static class Player implements Person
    {
        void play()
        {
            System.out.println("playing");
        }
    }

    static class Dancer implements Person
    {
        void dance()
        {
            System.out.println("dancing");
        }
    }
}