CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Ankita Patra
Level 5
Bhubaneshwar
  • 02.07.2019
  • 1065views
  • 2comments

Plzz help me out. Actually I'm not getting the question!!

Question about the task Implement the fight method
Java Syntax,  Level 5,  Lesson 5
Resolved


Implement the boolean fight(Cat anotherCat) method:
implement a mechanism for cats to fight based on their weight, age and strength.
You can decide for yourself how a cat's characteristics factor into combat.
The method should determine whether we (this) won the fight, i.e. return true if we won and false if not.

The following condition must be met:
if cat1.fight(cat2) returns true,
then cat2.fight(cat1) must return false

Requirements:
  • The Cat class must have a constructor with no parameters.
  • The Cat class must contain public fields age, weight, and strength.
  • The Cat class must have a fight method.
  • In the fight method, implement a mechanism for cats to fight based on their weight, age and strength.
  • The method should return the same value every time we fight the same cat.
  • If cat1 beats cat2, then cat2 must lose to cat1.
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.weight>anotherCat.weight)||(this.age<anotherCat.age)||(this.strength>anotherCat.strength)) return true; else return false; } public static void main(String[] args) { Cat cat1=new Cat(); cat1.weight=5; cat1.age=2; cat1.strength=50; Cat cat2=new Cat(); cat2.weight=4; cat2.age=3; cat2.strength=30; System.out.println(cat1.fight(cat2)); System.out.println(cat2.fight(cat1)); } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Dmitriy Bursa
Level 27 , Kyiv, Ukraine
2 July 2019, 14:42
try use
>= and &&
for all parameters
0
Ankita Patra
Level 5 , Bhubaneshwar, India
3 July 2019, 15:17
thank you
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.