Javaで政治家を操作する
概要
コンストラクタ、メソッドの使い方を学ぶ。
class Human_01 { String name; int age; String address; int birth; static int numbers_of_humans; public Human_01() { // TODO Auto-generated constructor stub } Human_01(String name, int age, String address, int birth) { this.name = name; this.age = age; this.address = address; this.birth = birth; numbers_of_humans++; } void display() { System.out .println(name + " " + age + " " + address + " " + birth); } boolean isOld(Human_01 obj) { if (this.age > obj.age) { return true; } else { return false; } } void setAgeInSpecifiedYear(int year) { this.age = year - this.birth; } String getName() { return this.name; } } public class HumanTest_1 { public static void main(String[] args) { Human_01 mori = new Human_01("森", 72, "石川県能美市", 1937); Human_01 koizumi = new Human_01("小泉", 67, "神奈川県横須賀市", 1942); Human_01 abe = new Human_01("安倍", 54, "山口県長門市", 1954); Human_01 fukuda = new Human_01("福田", 73, "群馬県高崎市", 1936); Human_01 asou = new Human_01("麻生", 68, "福岡県飯塚市", 1940); System.out.println("会員数:" + Human_01.numbers_of_humans); mori.display(); koizumi.display(); abe.display(); fukuda.display(); asou.display(); System.out.println("年齢を比較します"); if (mori.isOld(koizumi)) { System.out.println(mori.getName() + "さんは" + koizumi.getName() + "さんよりも先輩です"); } else { System.out.println(mori.getName() + "さんは" + koizumi.getName() + "さんよりも後輩もしくは同輩です"); } if (mori.isOld(abe)) { System.out.println(mori.getName() + "さんは" + abe.getName() + "さんよりも先輩です"); } else { System.out.println(mori.getName() + "さんは" + abe.getName() + "さんよりも後輩もしくは同輩です"); } if (mori.isOld(fukuda)) { System.out.println(mori.getName() + "さんは" + fukuda.getName() + "さんよりも先輩です"); } else { System.out.println(mori.getName() + "さんは" + fukuda.getName() + "さんよりも後輩もしくは同輩です"); } if (mori.isOld(asou)) { System.out.println(mori.getName() + "さんは" + asou.getName() + "さんよりも先輩です"); } else { System.out.println(mori.getName() + "さんは" + asou.getName() + "さんよりも後輩もしくは同輩です"); } int curent_year = 2010; System.out.println("西暦を" + curent_year + "年とします"); mori.setAgeInSpecifiedYear(curent_year); koizumi.setAgeInSpecifiedYear(curent_year); abe.setAgeInSpecifiedYear(curent_year); fukuda.setAgeInSpecifiedYear(curent_year); asou.setAgeInSpecifiedYear(curent_year); mori.display(); koizumi.display(); abe.display(); fukuda.display(); asou.display(); } }
実行結果
会員数:5
森 72 石川県能美市 1937
小泉 67 神奈川県横須賀市 1942
安倍 54 山口県長門市 1954
福田 73 群馬県高崎市 1936
麻生 68 福岡県飯塚市 1940
年齢を比較します
森さんは小泉さんよりも先輩です
森さんは安倍さんよりも先輩です
森さんは福田さんよりも後輩もしくは同輩です
森さんは麻生さんよりも先輩です
西暦を2010年とします
森 73 石川県能美市 1937
小泉 68 神奈川県横須賀市 1942
安倍 56 山口県長門市 1954
福田 74 群馬県高崎市 1936
麻生 70 福岡県飯塚市 1940
- 作者: 佐々淳行
- 出版社/メーカー: 文藝春秋
- 発売日: 2014/09/12
- メディア: Kindle版
- この商品を含むブログを見る