diff --git a/Question13-Extends/.classpath b/Question13-Extends/.classpath new file mode 100644 index 0000000..c771657 --- /dev/null +++ b/Question13-Extends/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/Question13-Extends/.gitignore b/Question13-Extends/.gitignore new file mode 100644 index 0000000..84c048a --- /dev/null +++ b/Question13-Extends/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/Question13-Extends/.project b/Question13-Extends/.project new file mode 100644 index 0000000..5c757c4 --- /dev/null +++ b/Question13-Extends/.project @@ -0,0 +1,23 @@ + + + Question13-Extends + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.jdt.core.javanature + + diff --git a/Question13-Extends/.settings/org.eclipse.jdt.core.prefs b/Question13-Extends/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..23fa13b --- /dev/null +++ b/Question13-Extends/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/Question13-Extends/.settings/org.eclipse.wst.common.project.facet.core.xml b/Question13-Extends/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..c1a8d07 --- /dev/null +++ b/Question13-Extends/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,4 @@ + + + + diff --git a/Question13-Extends/src/main/java/Animal.java b/Question13-Extends/src/main/java/Animal.java new file mode 100644 index 0000000..10b1e92 --- /dev/null +++ b/Question13-Extends/src/main/java/Animal.java @@ -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()+"は食事をしました。"); + } + +} diff --git a/Question13-Extends/src/main/java/Cat.java b/Question13-Extends/src/main/java/Cat.java new file mode 100644 index 0000000..a9bab3f --- /dev/null +++ b/Question13-Extends/src/main/java/Cat.java @@ -0,0 +1,4 @@ + +public class Cat extends Animal { + +} diff --git a/Question13-Extends/src/main/java/Character.java b/Question13-Extends/src/main/java/Character.java new file mode 100644 index 0000000..5249ad5 --- /dev/null +++ b/Question13-Extends/src/main/java/Character.java @@ -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; + } + +} diff --git a/Question13-Extends/src/main/java/Dog.java b/Question13-Extends/src/main/java/Dog.java new file mode 100644 index 0000000..bdf1453 --- /dev/null +++ b/Question13-Extends/src/main/java/Dog.java @@ -0,0 +1,6 @@ + +public class Dog extends Animal { + public void Eat() { + System.out.println(this.getName()+"はドッグフードを食べました。"); + } +} diff --git a/Question13-Extends/src/main/java/Hero.java b/Question13-Extends/src/main/java/Hero.java new file mode 100644 index 0000000..3b5086d --- /dev/null +++ b/Question13-Extends/src/main/java/Hero.java @@ -0,0 +1,9 @@ + +public class Hero extends Character { + public Hero() { + this.setName(""); + this.setHP(25); + this.setPower(10); + this.setDefense(7); + } +} diff --git a/Question13-Extends/src/main/java/Item.java b/Question13-Extends/src/main/java/Item.java new file mode 100644 index 0000000..a8d674c --- /dev/null +++ b/Question13-Extends/src/main/java/Item.java @@ -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); + } + +} diff --git a/Question13-Extends/src/main/java/Question13_1.java b/Question13-Extends/src/main/java/Question13_1.java new file mode 100644 index 0000000..ea2d7c7 --- /dev/null +++ b/Question13-Extends/src/main/java/Question13_1.java @@ -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(); + + } + +} diff --git a/Question13-Extends/src/main/java/Question13_2.java b/Question13-Extends/src/main/java/Question13_2.java new file mode 100644 index 0000000..20c3ec8 --- /dev/null +++ b/Question13-Extends/src/main/java/Question13_2.java @@ -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(); + + } + +} diff --git a/Question13-Extends/src/main/java/Question13_3.java b/Question13-Extends/src/main/java/Question13_3.java new file mode 100644 index 0000000..eddd778 --- /dev/null +++ b/Question13-Extends/src/main/java/Question13_3.java @@ -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()); + + } + +} diff --git a/Question13-Extends/src/main/java/Question13_4.java b/Question13-Extends/src/main/java/Question13_4.java new file mode 100644 index 0000000..0eb1fb6 --- /dev/null +++ b/Question13-Extends/src/main/java/Question13_4.java @@ -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()); + + } + +} diff --git a/Question13-Extends/src/main/java/Question13_5.java b/Question13-Extends/src/main/java/Question13_5.java new file mode 100644 index 0000000..d48a543 --- /dev/null +++ b/Question13-Extends/src/main/java/Question13_5.java @@ -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; + } + } + + } + +} diff --git a/Question13-Extends/src/main/java/Question13_6.java b/Question13-Extends/src/main/java/Question13_6.java new file mode 100644 index 0000000..40d4f69 --- /dev/null +++ b/Question13-Extends/src/main/java/Question13_6.java @@ -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()); + + } + +} diff --git a/Question13-Extends/src/main/java/Question13_7.java b/Question13-Extends/src/main/java/Question13_7.java new file mode 100644 index 0000000..cd2344b --- /dev/null +++ b/Question13-Extends/src/main/java/Question13_7.java @@ -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; + } + } + + } + +} diff --git a/Question13-Extends/src/main/java/Slime.java b/Question13-Extends/src/main/java/Slime.java new file mode 100644 index 0000000..301fe5f --- /dev/null +++ b/Question13-Extends/src/main/java/Slime.java @@ -0,0 +1,9 @@ + +public class Slime extends Character { + public Slime() { + this.setName(""); + this.setHP(10); + this.setPower(5); + this.setDefense(2); + } +} diff --git a/Question13-Extends/src/main/java/SuperHero.java b/Question13-Extends/src/main/java/SuperHero.java new file mode 100644 index 0000000..f4cd9b7 --- /dev/null +++ b/Question13-Extends/src/main/java/SuperHero.java @@ -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(); + } + +}