10/30 14:05

This commit is contained in:
2026-10-30 14:05:34 +09:00
parent 43f53da2f2
commit f62bfc6297
13 changed files with 178 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
public class Character {
// 保持するデータ(完全非公開)
private String name = "キャラクター";
private int hp;
private int power;
private int defense;
// 生成方法:生成時に体力・力・防御力の初期値を指定できる
public Character(int hp, int power, int defense) {
this.hp = hp;
this.power = power;
this.defense = defense;
}
// 名前のアクセサ(ゲッター / セッター)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
// 攻撃する:自身の「力」の値をそのまま返す
public int attack() {
return this.power;
}
// ダメージを受ける(相手の攻撃値を受け取る)
public boolean receiveDamage(int attackerPower) {
// 1. ダメージ量 = 受け取った攻撃値 − 自身の防御力(0以下の場合は1とする)
int damage = attackerPower - this.defense;
if (damage <= 0) {
damage = 1;
}
// 2. 自身の体力からダメージ量を引く
this.hp -= damage;
// 3. 体力が0以下になった場合は、体力を0に補正し、「戦闘不能」の状態とする
boolean isDown = false;
if (this.hp <= 0) {
this.hp = 0;
isDown = true;
}
// 4. 次の内容をこの順番で出力する
System.out.println(this.name + "は" + damage + "のダメージを受けた。");
System.out.println(this.name + "の残り体力は" + this.hp + "。");
if (isDown) {
System.out.println(this.name + "はちからつきた。");
}
System.out.println("↓");
// 5. 戦闘不能になったかどうかを真偽値で返す
return isDown;
}
// パラメーター表示:指定の形式の複数行の文字列を返す
public String showStatus() {
return "名 前:" + this.name + "\n" +
"体 力:" + this.hp + "\n" +
" 力 :" + this.power + "\n" +
"耐 久:" + this.defense;
}
}

View File

@@ -0,0 +1,5 @@
class Hero extends Character {
public Hero() {
super(25, 10, 7);
}
}

View File

@@ -0,0 +1,11 @@
public class Q13_3 {
public static void main(String[] args) {
Hero hero = new Hero();
hero.setName("勇者");
String status = hero.showStatus();
System.out.println(status);
}
}

View File

@@ -0,0 +1,13 @@
public class Q13_4 {
public static void main(String[] args) {
Slime slime = new Slime();
slime.setName("スライム");
String status = slime.showStatus();
System.out.println(status);
}
}

View File

@@ -0,0 +1,5 @@
class Slime extends Character {
public Slime() {
super(10, 5, 2);
}
}

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Question14-Interface</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
org.eclipse.jdt.core.compiler.compliance=21
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=21

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="java" version="21"/>
</faceted-project>

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Question15-Polymorphism</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
org.eclipse.jdt.core.compiler.compliance=21
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=21

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="java" version="21"/>
</faceted-project>