コンテンツにスキップ

第8章

コード08-01

Main.java

public class Main {
  public static void main(String[] args) {
    // (以下の内容をJavaで記述していく)
    // 勇者よ、この仮想世界に生まれよ!
    // お化けキノコよ、この仮想世界に生まれよ!
    // 勇者よ、戦え!
    // お化けキノコよ、逃げろ!
  }
}

コード08-02

Hero.java

public class Hero {
  String name;
  int hp;
  public void attack() {/* … */}
  public void sleep() {/* … */}
  public void sit(int sec) {/* … */}
  public void slip() {/* … */}
  public void run() {/* … */}
}

コード08-03

Hero.java

public class Hero {
}

コード08-04

Hero.java

public class Hero {
  String name;  // 名前の宣言
  int hp;       // HPの宣言
}

コード08-05

Matango.java

public class Matango {
  int hp;
  int level = 10;
}

コード08-06

Matango.java

public class Matango {
  int hp;
  final int LEVEL = 10;
}

コード08-07

Hero.java

public class Hero {
  String name;
  int hp;

  void sleep() {
    this.hp = 100;
    System.out.println(this.name + "は、眠って回復した!");
  }
}

コード08-08

Hero.java

public class Hero {
  String name;
  int hp;
  public void sleep() {
    this.hp = 100;
    System.out.println(this.name + "は、眠って回復した!");
  }
  public void sit(int sec) {
    this.hp += sec;
    System.out.println(this.name + "は、" + sec + "秒座った!");
    System.out.println("HPが" + sec + "ポイント回復した");
  }
  public void slip() {
    this.hp -= 5;
    System.out.println(this.name + "は、転んだ!");
    System.out.println("5のダメージ!");
  }
  public void run() {
    System.out.println(this.name + "は、逃げ出した!");
    System.out.println("GAMEOVER");
    System.out.println("最終HPは" + this.hp + "でした");
  }
}

コード08-09

Hero.java

public class Hero {
  String name;
  int hp;
  public void sleep() {
    this.hp = 100;
    System.out.println(this.name + "は、眠って回復した!");
  }
  public void sit(int sec) {
    this.hp += sec;
    System.out.println(this.name + "は、" + sec + "秒座った!");
    System.out.println("HPが" + sec + "ポイント回復した");
  }
  public void slip() {
    this.hp -= 5;
    System.out.println(this.name + "は、転んだ!");
    System.out.println("5のダメージ!");
  }
  public void run() {
    System.out.println(this.name + "は、逃げ出した!");
    System.out.println("GAMEOVER");
    System.out.println("最終HPは" + this.hp + "でした");
  }
}

Main.java

public class Main {
  public static void main(String[] args) {
    Hero h;
  }
}

コード08-10

Main.java

public class Main {
  public static void main(String[] args) {
    /* ここに勇者への指示を書いていく */
  }
}

コード08-11

Hero.java

public class Hero {
  String name;
  int hp;
  public void sleep() {
    this.hp = 100;
    System.out.println(this.name + "は、眠って回復した!");
  }
  public void sit(int sec) {
    this.hp += sec;
    System.out.println(this.name + "は、" + sec + "秒座った!");
    System.out.println("HPが" + sec + "ポイント回復した");
  }
  public void slip() {
    this.hp -= 5;
    System.out.println(this.name + "は、転んだ!");
    System.out.println("5のダメージ!");
  }
  public void run() {
    System.out.println(this.name + "は、逃げ出した!");
    System.out.println("GAMEOVER");
    System.out.println("最終HPは" + this.hp + "でした");
  }
}

Main.java

public class Main {
  public static void main(String[] args) {
    // 1.勇者を生成
    Hero h = new Hero();
  }
}

コード08-12

Hero.java

public class Hero {
  String name;
  int hp;
  public void sleep() {
    this.hp = 100;
    System.out.println(this.name + "は、眠って回復した!");
  }
  public void sit(int sec) {
    this.hp += sec;
    System.out.println(this.name + "は、" + sec + "秒座った!");
    System.out.println("HPが" + sec + "ポイント回復した");
  }
  public void slip() {
    this.hp -= 5;
    System.out.println(this.name + "は、転んだ!");
    System.out.println("5のダメージ!");
  }
  public void run() {
    System.out.println(this.name + "は、逃げ出した!");
    System.out.println("GAMEOVER");
    System.out.println("最終HPは" + this.hp + "でした");
  }
}

Main.java

public class Main {
  public static void main(String[] args) {
    // 1.勇者を生成
    Hero h = new Hero();
    // 2.フィールドに初期値をセット
    h.name = "ミナト";
    h.hp = 100;
    System.out.println("勇者" + h.name + "を生み出しました!");
  }
}

コード08-13

Hero.java

public class Hero {
  String name;
  int hp;
  public void sleep() {
    this.hp = 100;
    System.out.println(this.name + "は、眠って回復した!");
  }
  public void sit(int sec) {
    this.hp += sec;
    System.out.println(this.name + "は、" + sec + "秒座った!");
    System.out.println("HPが" + sec + "ポイント回復した");
  }
  public void slip() {
    this.hp -= 5;
    System.out.println(this.name + "は、転んだ!");
    System.out.println("5のダメージ!");
  }
  public void run() {
    System.out.println(this.name + "は、逃げ出した!");
    System.out.println("GAMEOVER");
    System.out.println("最終HPは" + this.hp + "でした");
  }
}

Main.java

public class Main {
  public static void main(String[] args) {
    // 1.勇者を生成
    Hero h = new Hero();
    // 2.フィールドに初期値をセット
    h.name = "ミナト";
    h.hp = 100;
    System.out.println("勇者" + h.name + "を生み出しました!");
    // 3.勇者のメソッドを呼び出してゆく
    h.sit(5);
    h.slip();
    h.sit(25);
    h.run();
  }
}

コード08-14

Main.java

public class Main {
  public static void main(String[] args) {
    int yusha_hp = 100;
    int matango1_hp = 50;
    int matango2_hp = 48;
    String yusha_name = "ミナト";
    int matango1_level = 10;
    int matango2_level = 10;
    System.out.println(yusha_name + "は5秒座った!");
    yusha_hp += 5;
    System.out.println("HPが5ポイント回復した");
    /* : */
    /* : */
  }
}

コード08-15

Matango.java

public class Matango {
  int hp;
  final int LEVEL = 10;
  char suffix;
  public void run() {
    System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
  }
}

コード08-16

Hero.java

public class Hero {
  String name;
  int hp;
  public void sleep() {
    this.hp = 100;
    System.out.println(this.name + "は、眠って回復した!");
  }
  public void sit(int sec) {
    this.hp += sec;
    System.out.println(this.name + "は、" + sec + "秒座った!");
    System.out.println("HPが" + sec + "ポイント回復した");
  }
  public void slip() {
    this.hp -= 5;
    System.out.println(this.name + "は、転んだ!");
    System.out.println("5のダメージ!");
  }
  public void run() {
    System.out.println(this.name + "は、逃げ出した!");
    System.out.println("GAMEOVER");
    System.out.println("最終HPは" + this.hp + "でした");
  }
}

Main.java

public class Main {
  public static void main(String[] args) {
    Hero h = new Hero();
    h.name = "ミナト";
    h.hp = 100;

    Matango m1 = new Matango();
    m1.hp = 50;
    m1.suffix = 'A';

    Matango m2 = new Matango();
    m2.hp = 48;
    m2.suffix = 'B';

    // 冒険のはじまり
    h.slip();
    m1.run();
    m2.run();
    h.run();
  }
}

Matango.java

public class Matango {
  int hp;
  final int LEVEL = 10;
  char suffix;
  public void run() {
    System.out.println("お化けキノコ" + this.suffix + "は逃げ出した!");
  }
}