//He who observes the day, observes it to the Lord; and he who does not observe the day, to the Lord he does not observe it. // who eats, eats to the Lord, for he gives God thanks. He who doesn't eat, to the Lord he doesn't eat, //d gives God thanks. (Romans 14:6) ------------------------------------------------Solution.java----------------------------------------------------------------- package com.javarush.task.task13.task1328; /* ÐиÑва ÑобоÑов */ public class Solution { public static void main(String[] args) { Robot amigo = new Robot("Ðмиго"); Robot enemy = new Robot("СгибалÑÑик-02"); doMove(amigo, enemy); doMove(amigo, enemy); doMove(enemy, amigo); doMove(amigo, enemy); doMove(enemy, amigo); doMove(amigo, enemy); doMove(enemy, amigo); doMove(amigo, enemy); } public static void doMove(AbstractRobot robotFirst, AbstractRobot robotSecond) { BodyPart attacked = robotFirst.attack(); BodyPart defenced = robotFirst.defense(); System.out.println(String.format("%s аÑаковал ÑобоÑа %s, аÑакована %s, заÑиÑена %s", robotFirst.getName(), robotSecond.getName(), attacked, defenced)); } } ------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------AbstractRobot.java------------------------------------------------------------ package com.javarush.task.task13.task1328; public abstract class AbstractRobot implements Attackable, Defensable{ private static int hitCount; public abstract String getName(); public BodyPart attack() { BodyPart attackedBodyPart = null; hitCount = hitCount + 1; if (hitCount == 1) { attackedBodyPart = BodyPart.ARM; } else if (hitCount == 2) { attackedBodyPart = BodyPart.HEAD; } else if (hitCount == 3) { attackedBodyPart = BodyPart.LEG; } else if (hitCount > 3) { hitCount = 0; attackedBodyPart = BodyPart.CHEST; } return attackedBodyPart; } public BodyPart defense() { BodyPart defencedBodyPart = null; hitCount = hitCount + 2; if (hitCount == 1) { defencedBodyPart = BodyPart.HEAD; } else if (hitCount == 2) { defencedBodyPart = BodyPart.LEG; } else if (hitCount == 3) { defencedBodyPart = BodyPart.ARM; } else if (hitCount > 3) { hitCount = 0; defencedBodyPart = BodyPart.CHEST; } return defencedBodyPart; } } ------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------BodyPart.java---------------------------------------------------------------- package com.javarush.task.task13.task1328; public final class BodyPart { final static BodyPart LEG = new BodyPart("нога"); final static BodyPart HEAD = new BodyPart("голова"); final static BodyPart ARM = new BodyPart("ÑÑка"); final static BodyPart CHEST = new BodyPart("гÑÑдÑ"); private String bodyPart; private BodyPart(String bodyPart) { this.bodyPart = bodyPart; } @Override public String toString() { return this.bodyPart; } } ------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------Defensable.java-------------------------------------------------------------- package com.javarush.task.task13.task1328; public interface Defensable { BodyPart defense(); } ------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------Attackable.java-------------------------------------------------------------- package com.javarush.task.task13.task1328; public interface Attackable { BodyPart attack(); } ------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------Robot.java------------------------------------------------------------------- package com.javarush.task.task13.task1328; public class Robot extends AbstractRobot { private String name; public Robot(String name) { this.name = name; } @Override public String getName() { return name; } } ------------------------------------------------------------------------------------------------------------------------------ /* ÐиÑва ÑобоÑов 1. РазобÑаÑÑÑÑ Ð² Ñом, ÑÑо ÑÑÑ Ð½Ð°Ð¿Ð¸Ñано. 2. http://info.javarush.ru/uploads/images/00/00/07/2014/08/12/50f3e37f94.png 3. СмиÑиÑÑÑÑ Ñо Ñвоей ÑÑаÑÑÑÑ Ð¸ пÑодолжиÑÑ ÑазбиÑаÑÑÑÑ Ð² коде. 4. ⦠5. ÐоÑадоваÑÑÑÑ, ÑÑо Ð¼Ñ Ð²Ñе понÑли. 6. ÐзмениÑÑ ÐºÐ¾Ð´ ÑоглаÑно новой аÑÑ Ð¸ÑекÑÑÑе и добавиÑÑ Ð½Ð¾Ð²ÑÑ Ð»Ð¾Ð³Ð¸ÐºÑ: 6.1. СделаÑÑ ÐºÐ»Ð°ÑÑ AbstractRobot абÑÑÑакÑнÑм, вÑнеÑÑи Ð»Ð¾Ð³Ð¸ÐºÑ Ð°Ñаки и заÑиÑÑ Ð¸Ð· Robot в AbstractRobot. 6.2. ÐÑÑедакÑиÑоваÑÑ ÐºÐ»Ð°ÑÑ Robot ÑÑиÑÑÐ²Ð°Ñ AbstractRobot. 6.3. РаÑÑиÑиÑÑ ÐºÐ»Ð°ÑÑ BodyPart новой ÑаÑÑÑÑ Ñела BodyPart.CHEST(«гÑÑдÑ«). 6.4. ÐобавиÑÑ Ð½Ð¾Ð²ÑÑ ÑаÑÑÑ Ñела в ÑеализаÑÐ¸Ñ Ð¸Ð½ÑеÑÑейÑов Attackable и Defensable (в клаÑÑе AbstractRobot). 7. http://info.javarush.ru/uploads/images/00/00/07/2014/08/12/3b9c65580b.png ТÑебованиÑ: 1.âÐлаÑÑ AbstractRobot должен бÑÑÑ Ð°Ð±ÑÑÑакÑнÑм. 2.âÐлаÑÑ AbstractRobot должен ÑеализовÑваÑÑ Ð¸Ð½ÑеÑÑейÑÑ Attackable и Defensable. 3.âÐлаÑÑ Robot должен наÑледоваÑÑÑÑ Ð¾Ñ ÐºÐ»Ð°ÑÑа AbstractRobot. 4.âÐогика Ð¿Ð¾Ð²ÐµÐ´ÐµÐ½Ð¸Ñ ÑобоÑов должна бÑÑÑ Ð²ÑнеÑена в клаÑÑ AbstractRobot. 5.âРклаÑÑе BodyPart должна ÑодеÑжаÑÑÑÑ Ð¸ бÑÑÑ Ð¸Ð½Ð¸ÑиализиÑована final static пеÑÐµÐ¼ÐµÐ½Ð½Ð°Ñ CHEST Ñипа BodyPart. 6.âÐÐ¾Ð²Ð°Ñ ÑаÑÑÑ Ñела(BodyPart) должна бÑÑÑ Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ð° в Ð»Ð¾Ð³Ð¸ÐºÑ Ð¼ÐµÑодов attack и defense в клаÑÑе AbstractRobot. ------------------------------------------------Solution.java----------------------------------------------------------------- package com.javarush.task.task13.task1328; /* ÐиÑва ÑобоÑов */ public class Solution { public static void main(String[] args) { Robot amigo = new Robot("Ðмиго"); Robot enemy = new Robot("СгибалÑÑик-02"); doMove(amigo, enemy); doMove(amigo, enemy); doMove(enemy, amigo); doMove(amigo, enemy); doMove(enemy, amigo); doMove(amigo, enemy); doMove(enemy, amigo); doMove(amigo, enemy); } public static void doMove(AbstractRobot robotFirst, AbstractRobot robotSecond) { BodyPart attacked = robotFirst.attack(); BodyPart defenced = robotFirst.defense(); System.out.println(String.format("%s аÑаковал ÑобоÑа %s, аÑакована %s, заÑиÑена %s", robotFirst.getName(), robotSecond.getName(), attacked, defenced)); } } ------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------------AbstractRobot.java------------------------------------------------------------ package com.javarush.task.task13.task1328; public class AbstractRobot { } ------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------BodyPart.java---------------------------------------------------------------- package com.javarush.task.task13.task1328; public final class BodyPart { final static BodyPart LEG = new BodyPart("нога"); final static BodyPart HEAD = new BodyPart("голова"); final static BodyPart ARM = new BodyPart("ÑÑка"); private String bodyPart; private BodyPart(String bodyPart) { this.bodyPart = bodyPart; } @Override public String toString() { return this.bodyPart; } } ------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------Defensable.java-------------------------------------------------------------- package com.javarush.task.task13.task1328; public interface Defensable { BodyPart defense(); } ------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------Attackable.java-------------------------------------------------------------- package com.javarush.task.task13.task1328; public interface Attackable { BodyPart attack(); } ------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------Robot.java------------------------------------------------------------------- package com.javarush.task.task13.task1328; public class Robot implements Attackable, Defensable { private static int hitCount; private String name; public Robot(String name) { this.name = name; } public String getName() { return name; } public BodyPart attack() { BodyPart attackedBodyPart = null; hitCount = hitCount + 1; if (hitCount == 1) { attackedBodyPart = BodyPart.ARM; } else if (hitCount == 2) { attackedBodyPart = BodyPart.HEAD; } else if (hitCount == 3) { hitCount = 0; attackedBodyPart = BodyPart.LEG; } return attackedBodyPart; } public BodyPart defense() { BodyPart defencedBodyPart = null; hitCount = hitCount + 2; if (hitCount == 1) { defencedBodyPart = BodyPart.HEAD; } else if (hitCount == 2) { defencedBodyPart = BodyPart.LEG; } else if (hitCount == 3) { hitCount = 0; defencedBodyPart = BodyPart.ARM; } return defencedBodyPart; } } ------------------------------------------------------------------------------------------------------------------------------ */