1030自主学習

This commit is contained in:
2026-10-30 17:49:49 +09:00
parent d9529e2971
commit c0101fc96d
186 changed files with 2507 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 void run() {
System.out.println(this.name + "は走りました。");
}
public void eat() {
System.out.println(this.name + "は食事をしました。");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

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

View File

@@ -0,0 +1,53 @@
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 name;
}
public void setName(String name) {
this.name = name;
}
public int attack() {
return this.power;
}
public boolean damage(int enemyAttack) {
int damage = enemyAttack - this.defense; //ダメージ量の計算
if(damage <= 0) {
damage = 1; //計算結果が0以下ならダメージ量1にする
}
this.hp -= damage; //hpからダメージ量引いて残りをhpにする
boolean dead= false; //戦闘不能かどうかを真偽地を使って判断
if(this.hp <= 0) { //hpが0以下なら
this.hp = 0; //hpを0に補正
dead = true;
}
System.out.println(this.name + "は" + damage + "のダメージを受けた");
System.out.println(this.name + "の残りの体力は" + this.hp + "。");
if(dead) { //戦闘不能の時
System.out.println(this.name + "はちからつきた。");
}
return dead;
}
public String Parameters() { //パラメーターを文字列で返す
return "名前:" + this.name + "\n" + "体力:" + this.hp + "\n" +
"力" + this.power + "\n" + "耐久" + this.defense;
}
}

View File

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

View File

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

View File

@@ -0,0 +1,28 @@
public class Item {
private String eName;
private int addDamage;
public Item(String eNmae, int addDamage) {
this.eName = eNmae;
this.addDamage = addDamage;
}
public String geteNmae() {
return eName;
}
public void seteNmae(String eNmae) {
this.eName = eNmae;
}
public int getAddDamage() {
return addDamage;
}
public void setAddDamage(int addDamage) {
this.addDamage = addDamage;
}
}

View File

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

View File

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

View File

@@ -0,0 +1,26 @@
public class Question13_5 {
public static void main(String[] args) {
// TODO 自動生成されたメソッド・スタブ
Hero hero = new Hero();
hero.setName("勇者");
Slime slime = new Slime();
slime.setName("スライム");
while(true) {
//勇者の攻撃
boolean slimedead = slime.damage(10); //スライムがダメージを受ける
//スライムの戦闘不能判断
if(slimedead == true) {
System.out.println("勇者はスライムとの戦闘に勝利した");
break;
}
boolean herodead = hero.damage(5);
if(herodead == true) {
System.out.println("勇者はスライムとの戦闘に敗北した");
break;
}
}
}
}

View File

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

View File

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

View File

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

View File

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