完了済

This commit is contained in:
2026-10-19 13:50:29 +09:00
parent 55aebdc8fa
commit aa48e98dae
20 changed files with 357 additions and 0 deletions

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>

1
Question13-Extends/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build/

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Question13-Extends</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>

View File

@@ -0,0 +1,22 @@
public class Animal {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void Run() {
System.out.println(this.getName()+"走りました。");
}
public void Eat() {
System.out.println(this.getName()+"は食事をしました。");
}
}

View File

@@ -0,0 +1,4 @@
public class Cat extends Animal {
}

View File

@@ -0,0 +1,84 @@
public class Character {
private String name;
private int hp;
private int power;
private int defense;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHP() {
return hp;
}
public void setHP(int HP) {
hp = HP;
}
public int getPower() {
return power;
}
public void setPower(int power) {
this.power = power;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
public Character() {
}
public Character(int HP, int power, int defense) {
this.hp = HP;
this.power = power;
this.defense = defense;
}
public int attack() {
return this.getPower();
}
public boolean dameged(int enemypower) {
boolean isDead = false;
int damage = 0;
if (enemypower-this.getDefense()<0) {
damage = 1;
} else {
damage = enemypower-this.getDefense();
}
if (this.getHP()-damage<0) {
this.setHP(0);
isDead = true;
} else {
this.setHP(this.getHP()-damage);
}
System.out.println(this.getName()+"は"+damage+"のダメージを受けた。");
System.out.println(this.getName()+"の残り体力は"+this.getHP()+"。");
if(isDead) {
System.out.println(this.getName()+"はちからつきた。");
}
System.out.println("↓");
return isDead;
}
public String display() {
String str = "名 前:"+this.getName()+"\n"
+"体 力:"+this.getHP()+"\n"
+" 力 :"+this.getPower()+"\n"
+"耐 久:"+this.getDefense();
return str;
}
}

View File

@@ -0,0 +1,6 @@
public class Dog extends Animal {
public void Eat() {
System.out.println(this.getName()+"はドッグフードを食べました。");
}
}

View File

@@ -0,0 +1,9 @@
public class Hero extends Character {
public Hero() {
this.setName("");
this.setHP(25);
this.setPower(10);
this.setDefense(7);
}
}

View File

@@ -0,0 +1,25 @@
public class Item {
private String name;
private int add;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAdd() {
return add;
}
public void setAdd(int add) {
this.add = add;
}
public Item(String name,int add) {
this.setName(name);
this.setAdd(add);
}
}

View File

@@ -0,0 +1,14 @@
public class Question13_1 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Cat cat = new Cat();
cat.setName("タマ");
cat.Run();
cat.Eat();
}
}

View File

@@ -0,0 +1,14 @@
public class Question13_2 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Dog dog = new Dog();
dog.setName("ポチ");
dog.Run();
dog.Eat();
}
}

View File

@@ -0,0 +1,13 @@
public class Question13_3 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Hero hero = new Hero();
hero.setName("勇者");
System.out.println(hero.display());
}
}

View File

@@ -0,0 +1,13 @@
public class Question13_4 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Slime slime = new Slime();
slime.setName("スライム");
System.out.println(slime.display());
}
}

View File

@@ -0,0 +1,28 @@
public class Question13_5 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Hero hero = new Hero();
hero.setName("勇者");
// System.out.println(hero.display());
Slime slime = new Slime();
slime.setName("スライム");
// System.out.println(slime.display());
while (true) {
if ( slime.dameged(hero.attack()) ) {
System.out.println(hero.getName()+"は"+slime.getName()+"との戦闘に勝利した");
break;
}
if ( hero.dameged(slime.attack()) ) {
System.out.println(slime.getName()+"は"+hero.getName()+"との戦闘に勝利した");
break;
}
}
}
}

View File

@@ -0,0 +1,21 @@
public class Question13_6 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
SuperHero superhero = new SuperHero();
superhero.setName("勇者(装備あり)");
System.out.println(superhero.display());
Slime slime = new Slime();
slime.setName("スライム");
Item item = new Item("こんぼう",5);
superhero.setItem(item);
System.out.println(superhero.attack());
}
}

View File

@@ -0,0 +1,29 @@
public class Question13_7 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
SuperHero superhero = new SuperHero();
superhero.setName("勇者(装備あり)");
Slime slime = new Slime();
slime.setName("スライム");
Item item = new Item("ロングソード",20);
superhero.setItem(item);
while (true) {
if ( slime.dameged(superhero.attack()) ) {
System.out.println(superhero.getName()+"は"+slime.getName()+"との戦闘に勝利した");
break;
}
if ( superhero.dameged(slime.attack()) ) {
System.out.println(slime.getName()+"は"+superhero.getName()+"との戦闘に勝利した");
break;
}
}
}
}

View File

@@ -0,0 +1,9 @@
public class Slime extends Character {
public Slime() {
this.setName("");
this.setHP(10);
this.setPower(5);
this.setDefense(2);
}
}

View File

@@ -0,0 +1,23 @@
public class SuperHero extends Hero {
private Item item;
public SuperHero() {
super();
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
public int attack() {
this.setPower(this.getPower()+this.getItem().getAdd());
return this.getPower();
}
}