(本日の課題)
アンケート調査のみ
アンケート調査
(期末試験の模範解答)
import java.lang.*; public class Q1_1{ public static void main( String args[] ){ // ----- キーボードからのデータ入力 ----- System.out.print( "1〜10までの整数を入力して下さい " ); int number = KeyboardScan.readLine().intValue(); // ----- 倍数の判定と表示 ----- System.out.print( "倍数は " ); for( int i = 0; i < 50; i++ ){ if( ( ( i + 1 ) % number ) == 0 ){ System.out.print( ( i + 1 ) + " " ); } } System.out.println( "です." ); } }
import java.lang.*; public class Q1_2{ public static void main( String args[] ){ // ----- コンソールパネルの準備 ----- ConsolePanel panel = new ConsolePanel(); // ----- 電球の取り付け ----- Light light[] = new Light[ 50 ]; for( int i = 0; i < 50; i++ ){ light[ i ] = new Lamp( i + 1 ); panel.add( light[ i ] ); } // ----- キーボードからのデータ入力 ----- System.out.print( "1〜10までの整数を入力して下さい " ); int number = KeyboardScan.readLine().intValue(); // ----- 倍数の判定と表示 ----- for( int i = 0; i < 50; i++ ){ if( ( ( i + 1 ) % number ) == 0 ){ light[ i ].on(); } } panel.paint(); } }
public class ColorBall{ private int color; public ColorBall( int color ){ this.color = color; } public int getColor(){ return this.color; } }
import java.lang.*; public class Q2{ public static void main( String args[] ){ // ----- 福引の準備 ----- Box box = new Box(); // ----- ボールの準備 ----- box.add( new ColorBall( 1 ) ); // 赤色のボール for( int i = 0; i < 5; i++ ){ // 黄色のボール box.add( new ColorBall( 2 ) ); } for( int i = 0; i < 20; i++ ){ // 白色のボール box.add( new ColorBall( 3 ) ); } // ----- 福引の回数の入力 ----- System.out.println( "福引の回数は,何回ですか? " ); int number = KeyboardScan.readLine().intValue(); // ----- 福引の実施 ----- for( int i = 0; i < number; i++ ){ ColorBall colorBall = (ColorBall)box.remove(); System.out.print( ( i + 1 ) + "回目 ボールの色 " + colorBall.getColor() ); if( colorBall.getColor() == 1 ){ System.out.println( " 金賞です." ); } else{ if( colorBall.getColor() == 2 ){ System.out.println( " 銀賞です." ); } else{ System.out.println( " 参加賞です." ); } } } } }
public interface Value{ public int getValue(); } public class Item implements Value{ private int i; private int value; private String s; public Item(String s, int value){ this.s = s; this.value = value; } public int getValue(){ return this.value; } public String toString(){ return this.s + " " + this.value; } } import java.util.*; public class Sum implements Value{ private List values = null; public Sum(List values){ this.values = values; } public int getValue(){ if(this.values == null){ return 0; } int sum = 0; try{ for(int i = 0; i < this.values.size(); i++){ Value value = (Value)this.values.get(i); sum = sum + value.getValue(); } }catch(ClassCastException e){ System.out.println(e); } return sum; } } import java.util.*; public class test{ public static void main(String args[]){ List values1 = new ArrayList(); List values2 = new ArrayList(); Sum sum1 = new Sum(values1); Sum sum2 = new Sum(values2); List list = new ArrayList(); while(true){ System.out.println("項目番号(入金:0、出金:1、リスト表示:2、終了:3) 項目名 金額 で入力して下さい。"); Line line = KeyboardScan.readLine(); int k = line.intValue(0); if(k == 0){ Item item = new Item(line.toString(1), line.intValue(2)); list.add(item); values1.add((Value)item); }else if(k == 1){ Item item = new Item(line.toString(1), line.intValue(2)); list.add(item); values2.add((Value)item); }else if(k == 2){ System.out.println("-------------------------------"); for(int i = 0; i < list.size(); i++){ System.out.println((i+1) + " " + ((Item)list.get(i)).toString()); } }else{ break; } System.out.println("-------------------------------"); System.out.println("入金合計" + " " + sum1.getValue()); System.out.println("出金合計" + " " + sum2.getValue()); System.out.println("残金" + " " + (sum1.getValue() - sum2.getValue())); } } }