Implement the fight method

  • 4
  • Locked
Theo Baslim, a retired colonel, saw active combat as a young man, but now he is bored. When he learned that his neighbor Sally Johnson had acquired five hundred cats, and he offered to arrange bloodless feline battles (she wouldn't go for bloody battles). There's just one small detail: we need to implement a mechanism for cats to fight based on weight, age and strength.
You can't complete this task, because you're not signed in.
Comments (31)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
ROOPAK PARASHAR
Level 8 , New Delhi, India
11 May 2021, 04:11
why the solution code returns score > 2; can anyone explain this to me?
Anonymous #10783379
Level 7 , China
31 August 2021, 12:55
code returns score > 2;Let Boolean judge whether it's true or false
Jaime Padilla
Level 8 , Tempe, United States
23 April 2021, 23:49
This was pretty difficult for me, but it felt awesome when I completed it! Felt like I made my own little video game. My solution was completely different than what their solution shows. They use the ternary operator and don't even specify the age, weight, or str. Their solution actually confuses me, how does it calculate what's true and false and add the score if there is no specification for the variables?
ImDevin
Level 15 , Old Town, United States
5 April 2021, 00:52
Don't worry about creating objects. That's not the focus of the task, and it could distract you from focusing on the actual work, which is to write the logic for the method. btw, anyone actually working on these tasks currently?
Khongpak Phupkdee
Level 15 , Chiangrai, Thailand
5 February 2021, 07:41
For this exercise, All you need is to use "this" and the weight, age, and strength, variables in comparison as this.age > anotherCat.age if true the method will return true but you must use all variables
Guadalupe Gagnon
Level 37 , Tampa, United States
22 October 2020, 20:36
This task is very simple. The fight() method must return true if the current cat (this) can beat the cat passed in as the argument. Because the method is not static, that means it can only be called on an implemented Cat object. The task has the requirement that if one cat wins against another cat, then if you reverse the order the other cat should lose against the first cat:
Cat cat1 = new Cat();
Cat cat2 = new Cat()

// if this line is true
cat1.fight(cat2);

// then this line MUST be false
cat2.fight(cat1);
I have included test code in the next two replies to this post. Copy+paste all of the code into your main() method. If you coded your fight() method correctly then all the output needs to either show 1 cat wins and the other cat loses (a true and false), or that both cats lose (both false). If any lines of the output are "true true", then your code does not meet the requirements and you will need to revisit the logic. Also, if all the lines show identical output then you hard coded the answer and your code will fail.
Guadalupe Gagnon
Level 37 , Tampa, United States
22 October 2020, 20:37
Cat lowStat = new Test.Cat(); lowStat.age = 1; lowStat.weight = 1; lowStat.strength = 1; Cat highStat = new Test.Cat(); highStat.age = 10; highStat.weight = 10; highStat.strength = 10; Cat midLowAge = new Test.Cat(); midLowAge.age = 5; midLowAge.weight = 1; midLowAge.strength = 1; Cat midLowWgt = new Test.Cat(); midLowWgt.age = 1; midLowWgt.weight = 5; midLowWgt.strength = 1; Cat midLowStr = new Test.Cat(); midLowStr.age = 1; midLowStr.weight = 1; midLowStr.strength = 5; Cat midHighAge = new Test.Cat(); midHighAge.age = 5; midHighAge.weight = 10; midHighAge.strength = 10; Cat midHighwght = new Test.Cat(); midHighwght.age = 10; midHighwght.weight = 5; midHighwght.strength = 10; Cat midHighStr = new Test.Cat(); midHighStr.age = 10; midHighStr.weight = 10; midHighStr.strength = 5; Cat mid2LowAge = new Test.Cat(); mid2LowAge.age = 1; mid2LowAge.weight = 5; mid2LowAge.strength = 5; Cat mid2LowWgt = new Test.Cat(); mid2LowWgt.age = 5; mid2LowWgt.weight = 1; mid2LowWgt.strength = 5; Cat mid2LowStr = new Test.Cat(); mid2LowStr.age = 5; mid2LowStr.weight = 5; mid2LowStr.strength = 1; Cat mid2HighAge = new Test.Cat(); mid2HighAge.age = 10; mid2HighAge.weight = 5; mid2HighAge.strength = 5; Cat mid2HighWght = new Test.Cat(); mid2HighWght.age = 5; mid2HighWght.weight = 10; mid2HighWght.strength = 5; Cat mid2HighStr = new Test.Cat(); mid2HighStr.age = 10; mid2HighStr.weight = 10; mid2HighStr.strength = 5;
Guadalupe Gagnon
Level 37 , Tampa, United States
22 October 2020, 20:37
StringBuilder sb = new StringBuilder(); sb.append(lowStat.fight(highStat) + " "); sb.append(highStat.fight(lowStat) + "\n"); sb.append(highStat.fight(midHighAge) + " "); sb.append(midHighAge.fight(highStat) + "\n"); sb.append(highStat.fight(midHighwght) + " "); sb.append(midHighwght.fight(highStat) + "\n"); sb.append(highStat.fight(midHighStr) + " "); sb.append(midHighStr.fight(highStat) + "\n"); sb.append(highStat.fight(mid2HighAge) + " "); sb.append(mid2HighAge.fight(highStat) + "\n"); sb.append(highStat.fight(mid2HighWght) + " "); sb.append(mid2HighWght.fight(highStat) + "\n"); sb.append(highStat.fight(mid2HighStr) + " "); sb.append(mid2HighStr.fight(highStat) + "\n"); sb.append(lowStat.fight(midLowAge) + " "); sb.append(midLowAge.fight(lowStat) + "\n"); sb.append(lowStat.fight(midLowWgt) + " "); sb.append(midLowWgt.fight(lowStat) + "\n"); sb.append(lowStat.fight(midLowStr) + " "); sb.append(midLowStr.fight(lowStat) + "\n"); sb.append(lowStat.fight(mid2LowAge) + " "); sb.append(mid2LowAge.fight(lowStat) + "\n"); sb.append(lowStat.fight(mid2LowWgt) + " "); sb.append(mid2LowWgt.fight(lowStat) + "\n"); sb.append(lowStat.fight(mid2LowStr) + " "); sb.append(mid2LowStr.fight(lowStat) + "\n"); System.out.println(sb);
Nutty Coder
Level 8 , United States
24 September 2020, 14:47
Probably a little more clarification could be provided about the expectations of the fight mechanism. The method I implemented was similar to others and I couldn't find a test where it should have failed. I'll have to circle back around and test my original answer more later.
andy 6473
Level 9 , Bangalore, India
30 May 2020, 17:19
can any bod explain how objects are affecting each other . How cat1 object becomes local object and cat2 becomes another object.
Joshua Swigut
Level 12 , Buffalo, United States
31 March 2020, 01:03
The only part I didn't really get is how "anotherCat" became a thing.
public boolean fight (Cat anotherCat)
just a space in the identifier of the boolean method? i probably sound challenged here because I'm brand new trying to talk about all this but any explanation would be appreciated.
26 March 2020, 16:10
This task is very confusing. I finally got it by copy and pasting someone else answer, but I don't even know why I couldn't do it.
Nickolas Johnson
Level 8 , St. Louis, United States of America
6 March 2020, 15:32
I thought this one was a bit weird. It definitely seemed much harder than it actually was.