目录
L5 类,构造函数
L5L5 实现 fight 方法
实现 boolean fight(Cat anotherCat) 方法:基于猫的体重、年龄和力量实现一种猫咪作战机制。你可以自行决定猫的性格如何影响战斗。方法应确定我们 (this) 是否赢得战斗,即,如果我们赢了,则返回 true,否则将返回 false。
**必须满足以下条件:**如果 cat1.fight(cat2),则返回 true,如果 cat2.fight(cat1),则必须返回 false
Requirements:
-
- Cat 类必须包含不带参数的构造方法。
-
- Cat 类必须包含 public 字段 age、weight 和 strength。
-
- Cat 类必须包含 fight 方法。
-
- 在 fight 方法中,基于猫的体重、年龄和力量实现一种猫咪作战机制。
-
- 每当我们与同一只猫作战时,该方法应返回相同的值。
-
- 如果猫 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:
-
- Friend 类必须包含 String 变量 name。
-
- Friend 类必须包含 int 变量 age。
-
- Friend 类必须包含 char 变量 sex。
-
- 该类必须包含将 name 作为参数并初始化相应实例变量的 initialize 方法。
-
- 该类必须包含将 name 和 age 作为参数并初始化相应实例变量的 initialize 方法。
-
- 该类必须包含将 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) {
}