第15回 課題解答

1 : エラー・インデント修正

public class Main {
  public static void main(String[] args) {
    Student student = new Student();
    // 自己紹介
    student.introduce();
    // 情報の変更
    student.changeInfo("和島茂", "青森", 2, 3124000);
    // 自己紹介
    student.introduce();
    // 卒業予定年度の表示
    System.out.println("卒業予定年度は" + student.getGraduationYear() + "年度です。");
    // 情報の変更
    student.changeInfo("和島茂", "青森", 4, 3124000);
    // 卒業予定年度の表示
    System.out.println("卒業予定年度は" + student.getGraduationYear() + "年度です。");
  }
}

// 学生のクラス
class Student {
  // フィールド
  String name; // 名前
  String campus; // 所属キャンパス
  int grade; // 学年
  int id; // 学籍番号

  // 引数なしコンストラクタ
  Student() {
    name = "未定";
    campus = "未定";
    grade = 0;
    id = 0;
  }

  // 自己紹介
  void introduce() {
    System.out.print("私は" + name + "です。");
    System.out.print(campus + "キャンパスの" + grade + "年生です。");
    System.out.println("学籍番号は" + id + "です。");
  }

  // 情報の変更
  void changeInfo(String name, String campus, int grade, int id) {
    this.name = name;
    this.campus = campus;
    this.grade = grade;
    this.id = id;
  }

  // 卒業予定年度を返す
  int getGraduationYear() {
    return 2025 + (4 - grade);
  }
}
実行結果