Advertisement
Azazavr

com.javarush.test.level05.lesson05.task05

Mar 25th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. package com.javarush.test.level05.lesson05.task05;
  2.  
  3. /* Провести три боя  попарно между котами
  4. Создать три кота используя класс Cat.
  5. Провести три боя попарно между котами.
  6. Класс Cat создавать не надо. Для боя использовать метод boolean fight(Cat anotherCat).
  7. Результат каждого боя вывести на экран.
  8. */
  9.  
  10. public class Solution {
  11.     public static void main(String[] args) {
  12.         Cat cat1 = new Cat("Васька", 3, 4, 6);
  13.         Cat cat2 = new Cat("Мурзик", 2, 3, 5);
  14.         Cat cat3 = new Cat("Барсик", 4, 6, 6);
  15.  
  16.         System.out.println(cat1.fight(cat2));
  17.         System.out.println(cat2.fight(cat3));
  18.         System.out.println(cat1.fight(cat3));
  19.     }
  20.  
  21.     public static class Cat {
  22.  
  23.         public static int count = 0;
  24.         public static int fightCount = 0;
  25.  
  26.         protected String name;
  27.         protected int age;
  28.         protected int weight;
  29.         protected int strength;
  30.  
  31.         public Cat(String name, int age, int weight, int strength) {
  32.             count++;
  33.  
  34.             this.name = name;
  35.             this.age = age;
  36.             this.weight = weight;
  37.             this.strength = strength;
  38.         }
  39.  
  40.         public boolean fight(Cat anotherCat) {
  41.             fightCount++;
  42.  
  43.             int agePlus = this.age > anotherCat.age ? 1 : 0;
  44.             int weightPlus = this.weight > anotherCat.weight ? 1 : 0;
  45.             int strengthPlus = this.strength > anotherCat.strength ? 1 : 0;
  46.  
  47.             int score = agePlus + weightPlus + strengthPlus;
  48.             return score > 2; //эквивалентно return score > 2 ? true : false;
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement