テーマ:合コン
Gokon.java
import java.io.*; //標準出力に使用
import java.util.Random; //乱数を用いるために使用
class Gokon{
final static int N = 3;
public static void main(String[] args){
//初期化
Man[] man = new Man[N];
Woman[] woman = new Woman[N];
for(int i=0; i<N; i++){
man[i] = new Man();
woman[i] = new Woman();
}
man[0].setInfo("高橋","たくや");
man[1].setInfo("佐藤","しょう");
man[2].setInfo("井上","なおき");
woman[0].setInfo("伊藤","あい");
woman[1].setInfo("三浦","ゆき");
woman[2].setInfo("高木","かな");
//自己紹介
for(int i=0; i<N; i++){ man[i].introduction();}
System.out.println("");
for(int i=0; i<N; i++){ woman[i].introduction();}
//適当にカップル成立
Random rnd = new Random();
Man kare = man[rnd.nextInt(N)];
Woman kano =woman[rnd.nextInt(N)];
System.out.format("\n%sさんと%sさんが付き合うことになりました。\n\n",kare.getName(),kano.getName());
//一定確率で子供ができる
int s = rnd.nextInt(10);
int[] condom = new int[5];
try{
condom[s] = 19;
}
catch(Exception e){
System.out.println("子供ができました。");
Child child = kano.importDna(kare.exportDna());
child.naku();
child.showDna();
}
}
}
//インターフェイス
interface iHuman{
//抽象メソッド :インターフェイスを使用するクラスでは、この関数を実装する必要がある。
void introduction();
}
interface iMan{
Dna exportDna();
}
interface iWoman{
Child importDna(Dna dna);
}
interface iChild{
void naku();
}
//遺伝子情報クラス
class Dna{
private int height; //身長 150 - 190
private int weight; //体重 40 - 80
private int color; //肌の色 0 - 100
//アクセッサ
public int getHeight(){ return height;}
public int getWeight(){ return weight;}
public int getColor(){ return color;}
//コンストラクタ
public Dna(){
Random rnd = new Random();
this.height = rnd.nextInt(40)+150;
this.weight = rnd.nextInt(40)+40;
this.color = rnd.nextInt(100);
}
public Dna(int height, int weight, int color){
this.height = height;
this.weight = weight;
this.color = color;
}
public Dna(Dna d1, Dna d2){
this.height = (d1.height + d2.height)/2;
this.weight=(d1.weight + d2.weight)/2;
this.color=(d1.color + d2.color)/2;
}
}
//人間クラス :これは抽象クラスなので直接インスタンス(オブジェクト)を生成することはできない
abstract class Human implements iHuman{
private String lastName; //名字
private String firstName; //名前
private int age; //年齢 20 - 50
private int nenshu; //年収 0 - 1000
private int miryoku; //魅力 0 - 100
private Dna dna; //遺伝子情報
public Human(){}
public Dna getDna(){return this.dna;}
public String getName(){return (lastName+firstName);}
public void introduction(){
System.out.format("%s%s(年齢:%3d、年収%3d、魅力%3d、身長%3d、体重%3d、肌の色%3d)\n",
lastName,firstName,age,nenshu,miryoku,dna.getHeight(),dna.getWeight(),dna.getColor());
}
public void showDna(){
System.out.format("きっとこんな感じに成長します:身長%3d、体重%3d、肌の色%3d\n",dna.getHeight(),dna.getWeight(),dna.getColor());
}
public void setInfo(String lastName, String firstName){
this.lastName = lastName;
this.firstName = firstName;
Random rnd = new Random();
this.age = rnd.nextInt(30)+20;
this.nenshu = rnd.nextInt(1000);
this.miryoku = rnd.nextInt(100);
this.dna = new Dna();
}
public void setInfo(Dna dna){
this.dna = dna;
}
public void setInfo(String lastName, String firstName, int age, int nenshu, int miryoku, Dna dna){
this.lastName = lastName;
this.firstName = firstName;
this.age = age;
this.nenshu = nenshu;
this.miryoku = miryoku;
this.dna = dna;
}
}
//男性クラス
class Man extends Human implements iMan{
public Dna exportDna(){
return getDna();
}
}
//女性クラス
class Woman extends Human implements iWoman{
public Child importDna(Dna man_dna){
Random rnd = new Random();
boolean isMan = rnd.nextBoolean();
if(isMan) System.out.println("男の子です。");
else System.out.println("女の子です。");
Child child = new Child();
child.setInfo(new Dna(this.getDna(), man_dna));
return child;
}
}
//子供クラス
class Child extends Human implements iChild{
public void naku(){
System.out.println("おぎゃー");
}
}
最終更新:2011年03月28日 11:10