CodeGym 错题集 2 LEVEL 5 类,构造函数

L5 类,构造函数

L5L5 实现 fight 方法

实现 boolean fight(Cat anotherCat) 方法:基于猫的体重年龄力量实现一种猫咪作战机制。你可以自行决定猫的性格如何影响战斗。方法应确定我们 (this) 是否赢得战斗,即,如果我们赢了,则返回 true,否则将返回 false

**必须满足以下条件:**如果 cat1.fight(cat2),则返回 true,如果 cat2.fight(cat1),则必须返回 false

Requirements:

    1. Cat 类必须包含不带参数的构造方法。
    1. Cat 类必须包含 public 字段 age、weight 和 strength。
    1. Cat 类必须包含 fight 方法。
    1. 在 fight 方法中,基于猫的体重、年龄和力量实现一种猫咪作战机制。
    1. 每当我们与同一只猫作战时,该方法应返回相同的值。
    1. 如果猫 1 击败猫 2,则猫 2 必须输给猫 1。
public class Cat {
   
    public int age;
    public int weight;
    public int strength;

    public Cat() {
   

    }

    public boolean fight(Cat anotherCat) {
   
        //在此编写你的代码
        int score1 = this.age > anotherCat.age ? 1 : 0;
        int score2 = this.weight > anotherCat.weight ? 1 : 0;
        int score3 = this.strength > anotherCat.strength ? 1 : 0;
        int sum = score1 + score2 + score3;
        return sum > 2;
    }

    public static void main(String[] args) {
   
    }
}

?:的用法

https://blog.csdn.net/qq_32571823/article/details/49929949

L5L7 创建 Friend 类(initialize)

创建包含三个初始化器(三个 initialize 方法)的 Friend 类:- name- name, age- name, age, sex

**注意:**name 为 String,age 为 int,sex 为 char。

Requirements:

    1. Friend 类必须包含 String 变量 name。
    1. Friend 类必须包含 int 变量 age。
    1. Friend 类必须包含 char 变量 sex。
    1. 该类必须包含将 name 作为参数并初始化相应实例变量的 initialize 方法。
    1. 该类必须包含将 name 和 age 作为参数并初始化相应实例变量的 initialize 方法。
    1. 该类必须包含将 name、age 和 sex 作为参数并初始化相应实例变量的 initialize 方法。
public class Friend {
   
    public String name;
    public int age;
    public char sex;

    //在此编写你的代码
    public void initialize(String name) {
   
        this.name = name;
    }

    public void initialize(String name, int age) {
   
        this.name = name;
        this.age = age;
    }

    public void initialize(String name, int age, char sex) {
   
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    public static void main(String[] args) {
   

    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值