Please help me with the code

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

    }
}

asked 14 Mar '15, 15:37

Reem's gravatar image

Reem
39118
accept rate: 0%

edited 15 Mar '15, 06:39

Elleonora%20Carry's gravatar image

Elleonora Carry ♦♦
275


write down the compiler result with this code, then it will be easy to see the errors....

link

answered 15 Mar '15, 04:31

alimujtaba's gravatar image

alimujtaba
1115
accept rate: 0%

edited 15 Mar '15, 04:33

there is no error but it does not pass the test

(15 Mar '15, 16:34) Reem
firstCat.age=3
firstCat.weight=3
firstCat.strength=3

secondCat.age=3
secondCat.weight=3
secondCat.strength=4

You have

firstCat.fight(secondCat) = false
secondCat.fight(firstCat) = false

But according to condition

firstCat.fight(secondCat) != secondCat.fight(firstCat)
link

answered 15 Mar '15, 17:15

Elleonora%20Carry's gravatar image

Elleonora Carry ♦♦
275
accept rate: 16%

Program is compiled without errors but not getting a passing test..need help. my code :

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

public Cat(String n, int a, int w, int s)
{
    name = n;
    age = a;
    weight = w;
    strength = s;
}

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

public static void main(String[] args)
{
 Cat cat1 = new Cat("cat1", 10, 10, 10);
 Cat cat2 = new Cat("cat2", 5, 5, 5);

 cat1.fight(cat2);
 cat2.fight(cat1);

}
}
link

answered 12 Apr '15, 15:26

Gautam%20Roy's gravatar image

Gautam Roy
353
accept rate: 14%

Please help, if anyone has passed the test??

link

answered 23 Apr '15, 19:13

Zain%20Mehar's gravatar image

Zain Mehar
13
accept rate: 0%

public Cat(String n, int a, int w, int s) { name = n; age = a; weight = w; strength = s; }

public static void main(String[] args){
    Cat cat1 = new Cat("lo" , 2 , 3, 2);
    Cat cat2 = new Cat("mo" , 1 , 1, 2);
    Cat cat3 = new Cat("so" , 122 , 223, 121);
    System.out.println(cat1.fight(cat2));
    System.out.println(cat1.fight(cat3));
    System.out.println(cat3.fight(cat2));
}
public boolean fight(Cat anotherCat)
{

         int s = this.age+this.weight+this.strength;

        int d = anotherCat.age+anotherCat.weight+anotherCat.strength;
    if(s>d)

        return true;
        else
        return false; 
}

}

This is my code .. it is giving correct output but still not passed testing.. Anyone who solved , please help

link

answered 25 Apr '15, 16:16

Muhammad%20Abu%20Bakr%20Chaudhry's gravatar image

Muhammad Abu...
12
accept rate: 0%

Hi, I have passed the tests writing this code:

   public class Cat
{
    public String name = "Kot";
    public int age = 2;
    public int weight = 2;
    public int strength = 5;

    public Cat()
    {
    }

    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.strength > anotherCat.strength) {
            return true;
        }
        return false;
    }
    public static void main(String[] args) {
        Cat cat = new Cat();
        Cat catTwo = new Cat("Kocur",2,2,10);
        cat.fight(catTwo);
        catTwo.fight(cat);
    }
}
link

answered 17 Apr, 09:40

labamba's gravatar image

labamba
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:

×30
×24
×9

Asked: 14 Mar '15, 15:37

Seen: 2,145 times

Last updated: 17 Apr, 09:40