(本日の課題)
アンケート調査
import java.lang.*; public class LifeGame{ public static void main(String args[]){ // ----- サイコロを2個生成 ----- Die die1 = new Die(); Die die2 = new Die(); // ----- 初期値の設定 ----- int n = 5; int total = 0; int bonusCount = 0; int bonusWeight = 100; // ----- サイコロを5回振る ----- for(int i = 0; i < n; i++){ // ----- 2つのサイコロを振る ----- die1.roll(); die2.roll(); // ----- サイコロの目を表示 ----- System.out.print((i + 1) + "回目 "); System.out.print(die1.getValue() + " " + die2.getValue()); // ----- 得点の加算とボーナスの加算 ----- int Score = die1.getValue() * die2.getValue(); if (die1.getValue() == die2.getValue()){ System.out.println(" ボーナス獲得"); Score = Score + bonusWeight; bonusCount = bonusCount + 1; }else{ System.out.println(); } // ----- 得点の表示 ----- total = total + Score; System.out.print("得点 " + Score); System.out.println(" 総得点 " + total); System.out.println(); } // ----- 結果の表示 ----- System.out.println("ボーナスで獲得した得点 " + bonusCount * bonusWeight); System.out.println("ボーナスを除いた得点 " + (total - bonusCount * bonusWeight)); } }
public interface Value{ public int getValue(); } public abstract class Cash implements Value{ public abstract int getValue(); } public class Cash1000 extends Cash{ public int getValue(){ return 1000; } } public class Cash2000 extends Cash{ public int getValue(){ return 2000; } } public class Cash5000 extends Cash{ public int getValue(){ return 5000; } } public class Cash10000 extends Cash{ public int getValue(){ return 10000; } }
import java.util.*; public class Atm{ // ----- フィールドの設定 ----- private List list = new ArrayList(); private int person = 0; // ----- コンストラクタ ----- public Atm(){ this.list.add(new Cash1000()); this.list.add(new Cash2000()); this.list.add(new Cash5000()); this.list.add(new Cash10000()); } // ----- getMoneyメソッド ----- public Value getMoney(int valueRequest){ this.person = this.person + 1; System.out.print(this.person + " 人目 "); Value value = null; if(this.list.size() != 0){ for(int i = 0; i < this.list.size(); i++){ value = (Value)this.list.get(i); if(valueRequest == value.getValue()){ this.list.remove(i); System.out.println(value.getValue() + "円札を出金しました。"); return value; } } System.out.println(valueRequest + "円札は紙幣が不足しており出金できません。"); }else{ System.out.println("ATMは空です。出金できません。"); } return value; } // ----- putMoneyメソッド ----- public void putMoney(Value value){ this.person = this.person + 1; System.out.print(this.person + " 人目 "); this.list.add(value); System.out.println(value.getValue() + "円札を入金しました。"); } }
import java.lang.*; public class MainAtm{ public static void main(String args[]){ Value value = null; Atm atm = new Atm(); atm.putMoney(new Cash1000()); value = atm.getMoney(1000); value = atm.getMoney(2000); value = atm.getMoney(5000); value = atm.getMoney(10000); value = atm.getMoney(1000); value = atm.getMoney(1000); } }