15回目 5月28日の授業内容(中間試験の模範解答等)


(前半) (後半)

(本日の課題)

アンケート調査


問題1(Sugoroku.java) (注)プログラムを実行するためには、SuperDieクラス、Dieクラスが必要です
import java.lang.*;

public class Sugoroku{
  public static void main(String args[]){

// ----- サイコロを2個生成 -----

    Die die1 = new Die();
    Die die2 = new Die();

// ----- 初期値の設定 -----

    int goal = 40;
    int total = 0;

// ----- サイコロを5回振る -----

    for(int i = 0; i < 5; i++){

      // ----- 2つのサイコロを振る -----

      die1.roll();
      die2.roll();

      // ----- 2つのサイコロの合計と総和を求める -----

      int sum = die1.getValue() + die2.getValue();
      total = total + sum;

      // ----- 途中結果を表示する -----

      System.out.print((i + 1) + "回目 ");
      System.out.print(die1.getValue() + " " + die2.getValue());
      System.out.println(" " + sum + " " + total);
    }

// ----- ゴールの判定を行う -----

    if(total >= goal){
      System.out.println("ゴールできました.");
    }
    else{
      System.out.println("ゴールまで,残り " + (goal - total) + " でした.");
    }
  }
}

問題2(Coin1.java 〜 Coin500.java)
public class Coin1 implements Value{
  public int getValue(){
    return 1;
  }
}

public class Coin5 implements Value{
  public int getValue(){
    return 5;
  }
}

public class Coin10 implements Value{
  public int getValue(){
    return 10;
  }
}

public class Coin50 implements Value{
  public int getValue(){
    return 50;
  }
}

public class Coin100 implements Value{
  public int getValue(){
    return 100;
  }
}

public class Coin500 implements Value{
  public int getValue(){
    return 500;
  }
}

問題2(Saisen.java)
import java.util.*;

public class Saisen implements Value{

// ----- 賽銭箱 -----

  private List saisen;

// ----- 賽銭箱のコンストラクタ -----

  public Saisen(){
    this.saisen = new ArrayList();
  }

// ----- getValueメソッド -----

  public int getValue(){
    int sum = 0;
    for(int i = 0; i < this.saisen.size(); i++){
      Value value = (Value)this.saisen.get(i);
      sum = sum + value.getValue();
    }
    return sum;
  }

// ----- putCoinメソッド -----

  public void putCoin(Value coin){
    this.saisen.add(coin);
  }

// ----- putCoinNumberメソッド -----

  public int getCoinNumber(Value coin){
    int num = 0;
    for(int i = 0; i < this.saisen.size(); i++){
      Value value = (Value)this.saisen.get(i);
      if(value.getValue() == coin.getValue()){
        num = num + 1;
      }
    }
    return num;
  }
}

問題2(SaisenMain.java) (注)SaisenMainはいろいろな考え方ができます(これは一例です)
import java.util.*;

public class SaisenMain{
  public static void main(String args[]){

// ----- 初期値の設定 -----

    int n = 2;                          // 賽銭の枚数
    int ninzu = 5;                      // 参拝者数

// ----- 硬貨の生成 -----

    List coin = new ArrayList();
    coin.add(new Coin1());
    coin.add(new Coin5());
    coin.add(new Coin10());
    coin.add(new Coin50());
    coin.add(new Coin100());
    coin.add(new Coin500());

// ----- 賽銭箱の生成 -----

    Saisen saisen = new Saisen();

// ----- 参拝する -----

    Random random = new Random();
    for(int j = 0; j < ninzu; j++){
      System.out.print((j + 1) + " 人目 ");

      // ----- 硬貨を2枚選び賽銭箱に入れる -----
      for(int i = 0; i < n; i++){
        Value value = (Value)coin.get(random.nextInt(6));
        saisen.putCoin(value);
        System.out.print(value.getValue() + " 円 ");
      }
      System.out.println();
    }
    System.out.println();

// ----- 賽銭箱に入っている硬貨の枚数の表示 -----

    for(int i = 0; i < coin.size(); i++){
      Value value = (Value)coin.get(i);
      System.out.print(value.getValue() + " 円 ");
      System.out.println(saisen.getCoinNumber(value) + "枚");
    }

// ----- 合計の表示 -----

    System.out.println("合計は " + saisen.getValue() + "円です.");
  }
}