this code is running without errors and it also meets the condition if cat1.fight(cat2) = true, then cat2.fight(cat1) but not passing the test

package com.javarush.test.level05.lesson05.task02;

/ Implement method fight Implement method boolean fight(Cat anotherCat): implement a fight mechanism depending on cats’ weight, age and strength. Make up a dependency by yourself. The method should determine, whether current cat (the object whose fight method was called) or anotherCat won fight, i.e. return true if current cat won and false, if it didn’t win. The following condition must be met: if cat1.fight(cat2) = true, then cat2.fight(cat1) = false /

public class Cat
{
public String name;
public int age;
public int weight;
public int strength;
public static int score;

public Cat(String name, int age , int weight, int strength)
{
     this.name=name;
     this.age=age;
     this.weight=weight;
     this.strength=strength;
}

public boolean fight(Cat anotherCat)
{
if(this.age+this.weight+this.strength> anotherCat.age+anotherCat.weight+anotherCat.strength)
return true;
else
return false;

}
public static void main(String args[])
{
Cat cat1=new Cat("bob",2,3,7);
Cat cat2=new Cat("tom",1,4,5);
cat1.fight(cat2);
cat2.fight(cat1);
System.out.println(cat1.fight(cat2));
System.out.println(cat2.fight(cat1));

}

}

asked 28 Apr '15, 23:19

rahul's gravatar image

rahul
7916
accept rate: 15%


Too many useless code for this task

link

answered 13 May '15, 03:09

dimashaman's gravatar image

dimashaman
262
accept rate: 20%

package com.javarush.test.level05.lesson05.task02;

/ Implement method fight Implement method boolean fight(Cat anotherCat): implement a fight mechanism depending on cats’ weight, age and strength. Make up a dependency by yourself. The method should determine, whether current cat (the object whose fight method was called) or anotherCat won fight, i.e. return true if current cat won and false, if it didn’t win. The following condition must be met: if cat1.fight(cat2) = true, then cat2.fight(cat1) = false /

public class Cat { public String name; public int age; public int weight; public int strength;

public Cat(String na,int ag,int we,int st)
{
    name=na;
    age=ag;
    weight=we;
    strength=st;
}

public boolean fight(Cat anotherCat)
{
    int ageP = this.age > anotherCat.age ? 1 : 0;
    int weightp = this.weight > anotherCat.weight ? 1 : 0;
    int strengthp = this.strength > anotherCat.strength ? 1 : 0;

    int score1 = ageP + weightp + strengthp;
    return score1 > 2;

}
link

answered 28 Sep '15, 08:29

nisar's gravatar image

nisar
111
accept rate: 0%

but not pass testing

(28 Sep '15, 08:30) nisar

package com.codegym.task.task05.task0502;

/* Implement the fight method

*/

public class Cat { public int age; public int weight; public int strength;

public Cat() {
}

public boolean fight(Cat anotherCat) {
    //write your code here
    if(this.age > anotherCat.age && this.weight > anotherCat.weight && this.strength > anotherCat.strength)
        return true;
    return false;
}

public static void main(String[] args) {
    Cat cat1 = new Cat();
    Cat cat2 = new Cat();
    cat1.age = 22;
    cat1.weight = 88;
    cat1.strength = 100;
    cat2.age = 19;
    cat2.weight = 78;
    cat2.strength = 50;
    System.out.println(cat1.fight(cat2));
    System.out.println(cat2.fight(cat1));
}

}

This has passed the testing!

link

answered 27 Aug, 15:11

arkosarkar's gravatar image

arkosarkar
1
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×15

Asked: 28 Apr '15, 23:19

Seen: 3,004 times

Last updated: 27 Aug, 15:11