report5

ProgrammingII report#5

TOP 情報工HP ProgrammingII
report#01
report#02
report#03
report#04
report#05
report#06
動作環境はsafariでやったので、safariを推奨します。

【Report#5】 GUIプログラム:講義資料を参考に、以下の課題を行え。



1.偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。


ソース
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
import java.awt.*;
import java.awt.event.*;

public class GUIaa extends Frame {
    Button    b0 = new Button("偶数/奇数?");
    Label     x0 = new Label("Type a number and press...");
    TextField t0 = new TextField();
    
    public GUIaa() {
        setLayout(null);
        add(t0); t0.setBounds(10, 40, 90, 30);
        add(b0); b0.setBounds(110, 40, 100, 30);
        add(x0); x0.setBounds(10, 80, 180, 30);
        b0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                int i = (new Integer(t0.getText())).intValue();
                t0.setText("");
                if(i % 2 == 0) {
                    x0.setText(i + "は偶数です");
                } else {
                    x0.setText(i + "は奇数です");
                }
            }
        });
    }
    public static void main(String[] args) {
        Frame win = new GUIaa();
        win.setSize(200, 150); win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    }
}

実行結果

20を入力した場合
     
35を入力した場合
     

解説:
  1~2行目では、GUI関係のパッケージクラス群をインポートしている。4行目では、Frameクラスの継承している。
5~7行目では、インスタンス変数としてボタン、表示欄、入力欄を生成・初期化している。
15~23行目では、actionPerformed() というメソッドである。これは具体的にボタンが押されると呼び出される。
18~22行目では、if文を使って、偶数か奇数かを判断して、表示までさせている。
また、11~13行目ではそれぞれの配置位置をしていして、幅や高さを指定している。
29行目ではwindowClosing() というメソッドである。これはウィンドウを閉じると呼び出される。

2.例外処理について、考察せよ。

ソース
01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
import java.awt.*;
import java.awt.event.*;

public class ColorRGBa extends Frame {
    Button      b0 = new Button("Display");
    Label[]     la = new Label[]{new Label ("Red"),
                                 new Label ("Green"),
                                 new Label ("Blue")  };
    TextField[] ta = new TextField[]{new TextField(),
                                     new TextField(),
                                     new TextField()};
    Label       x0 = new Label("Input RGB values [0..255]");
    
    public ColorRGBa() {
        setLayout(null);
        for(int i = 0; i < la.length; i++) {
            add(la[i]); la[i].setBounds(10, 40 + i*40, 60, 30);
            add(ta[i]); ta[i].setBounds(80, 40 + i*40, 60, 30);
        }
        add(b0); b0.setBounds(10, 160, 65, 30);
        add(x0); x0.setBounds(10, 200, 180, 30);
        b0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                try {
                    x0.setBackground(new Color(
                        (new Integer(ta[0].getText())).intValue(),
                        (new Integer(ta[1].getText())).intValue(),
                        (new Integer(ta[2].getText())).intValue()));
                } catch(Exception ex) { x0.setText(ex.toString()); }
            }
        });
    }
    public static void main(String[] args) {
        Frame win = new ColorRGBa();
        win.setSize(200, 250); win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    }
}

実行結果


Red 140
Green 60
Blue 200
を入力した場合



指定された範囲外の数字を入力した場合

解説
  22~31行目では、ボタンを押したときの具体的な動作を指定している。
24~28行目では例外が発生する可能性のある処理を実行して、29行目では例外時の処理をしています。

考察
Red 140
Green 60
Blue 200
を入力した場合にはうまく紫系統の色が表示されている。
また、範囲外の数値を入力した場合にはエラーメッセージが表示されるようになった。例外処理がうまくいったことになる。

3.上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。

01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
import java.awt.*;
import java.awt.event.*;

public class ColorRGBa extends Frame {
    Button      b0 = new Button("Display");
    Label[]     la = new Label[]{new Label ("Red"),
                                 new Label ("Green"),
                                 new Label ("Blue")  };
    TextField[] ta = new TextField[]{new TextField(),
                                     new TextField(),
                                     new TextField()};
    Label       x0 = new Label("Input RGB values [0..255]");
    Label       x1 = new Label("");
    Choice      c0 = new Choice();
    
    public ColorRGBa() {
        setLayout(null);
        for(int i = 0; i < la.length; i++) {
            add(la[i]); la[i].setBounds(10, 40 + i*40, 60, 30);
            add(ta[i]); ta[i].setBounds(80, 40 + i*40, 60, 30);
        }
        add(b0); b0.setBounds(10, 160, 65, 30);
        add(x0); x0.setBounds(10, 200, 400, 30);
        add(x1); x1.setBounds(410,200, 400, 30);
        add(c0); c0.setBounds(150,80,200,30);
        c0.add("左に色を付ける");c0.add("右に色を付ける");c0.add("両方色を付ける");
        b0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                try {
                    int a =c0.getSelectedIndex();
                    if(a==0){
                      x0.setBackground(new Color(
                         (new Integer(ta[0].getText())).intValue(),
                         (new Integer(ta[1].getText())).intValue(),
                         (new Integer(ta[2].getText())).intValue()));
                    }
                    else if(a==1){
                      x1.setBackground(new Color(
                         (new Integer(ta[0].getText())).intValue(),
                         (new Integer(ta[1].getText())).intValue(),
                         (new Integer(ta[2].getText())).intValue()));
                    }
                    else{
                      x0.setBackground(new Color(
                         (new Integer(ta[0].getText())).intValue(),
                         (new Integer(ta[1].getText())).intValue(),
                         (new Integer(ta[2].getText())).intValue()));
                      x1.setBackground(new Color(
                         (new Integer(ta[0].getText())).intValue(),
                         (new Integer(ta[1].getText())).intValue(),
                         (new Integer(ta[2].getText())).intValue()));
                    
                    }
                } 
                catch(Exception ex) { x0.setText(ex.toString()); }
            }
        });
    }
    public static void main(String[] args) {
        Frame win = new ColorRGBa();
        win.setSize(900, 250); win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    }
}



実行結果


左に色を付けるを選択した場合


右に色を付けるを選択した場合


両方に色をつけるを選択した場合

4.摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。

01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
import java.awt.*;
import java.awt.event.*;

public class Seshi extends Frame {
    Button    b0 = new Button("温度換算");
    Label     x0 = new Label("摂氏から華氏、華氏から摂氏に換算します。");
    Label     x1 = new Label("左の欄に入力してボタンを押してください。");
    TextField t0 = new TextField();
    TextField t1 = new TextField();
    Choice    c0 = new Choice();
    
    public Seshi() {
        setLayout(null);
        add(t0); t0.setBounds(10, 40, 80, 30);
        add(c0); c0.setBounds(100, 40, 80, 30);
        c0.add("華→摂"); c0.add("摂→華");
        add(t1); t1.setBounds(200,40,80,30);
        add(b0); b0.setBounds(290, 40, 90, 30);
        add(x0); x0.setBounds(10, 100, 400, 30);
        add(x1); x1.setBounds(10,120,400,30);
        b0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                double i = (new Double(t0.getText())).doubleValue();
                int a =c0.getSelectedIndex();
                try{
                	if(a == 0 ){
                        if(i<-273.15){
                        	throw new Exception();
                        }
                        double b = (5*(i-32)/9);
                		t1.setText(b + "°C");
                    }
                	else{
                		if(i<-459.67){
                			throw new Exception();
                		}
                		double b = (9*i/5+32);
                		t1.setText(b + "°F");
                    }
         
                }
                catch(Exception ex){
                	t1.setText("残念");
                	x0.setText("絶対零度以下は存在しません。");
                	x1.setText("");
                }
            }
    });
    }

    public static void main(String[] args) {
        Frame win = new Seshi();
        win.setSize(400, 160);
        win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    }
}

解説


実行結果


具体的な数値を入力した場合



絶対零度より低い数値を入力した場合

5.「電卓」プログラム。中身は自分の思うように。

01:
02:
03:
04:
05:
06:
07:
08:
09:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
import java.awt.*;
import java.awt.event.*;

public class Dentaku extends Frame {
	Button	b0 = new Button("0");
	Button	b00 = new Button("00");
	Button	bt = new Button(".");
	Button	b1 = new Button("1");
	Button	b2 = new Button("2");
	Button	b3 = new Button("3");
	Button	b4 = new Button("4");
	Button	b5 = new Button("5");
	Button	b6 = new Button("6");
	Button	b7 = new Button("7");
	Button	b8 = new Button("8");
	Button	b9 = new Button("9");
	Button	bta = new Button("+");
	Button	bh = new Button("ー");
	Button	bk = new Button("×");
	Button	bw = new Button("÷");
	Button	be = new Button("=");
	Button	bc = new Button("C/CE");
	Button	bp = new Button("%");
	Button	br = new Button("√");
	Label la0 = new Label("0",Label.RIGHT);
	Label la1 = new Label();
	Label la2 = new Label();
	double old = 0;
	double pre = 0;
	double num;
	int en2;
   	int ten = 0;
   	int ten2;
   	int a;
   	int equal;
	
    public Dentaku() {
        setLayout(null);           //ボタンの配置や、デザイン
        la0.setBackground(new Color(0xFFFFFF));
        add(la0); la0.setBounds(51,31,213,23);
        la2.setBackground(new Color(0xFFFFFF));
        add(la2); la2.setBounds(21,31,30,23);
        la1.setBackground(new Color(0x000000));
        add(la1); la1.setBounds(20,30,245,25);
        add(b7); b7.setBounds(20,70,40,20);
        add(b8); b8.setBounds(70,70,40,20);
        add(b9); b9.setBounds(120,70,40,20);
        add(b4); b4.setBounds(20,100,40,20);
        add(b5); b5.setBounds(70,100,40,20);
        add(b6); b6.setBounds(120,100,40,20);
        add(b1); b1.setBounds(20,130,40,20);
        add(b2); b2.setBounds(70,130,40,20);
        add(b3); b3.setBounds(120,130,40,20);
        add(b0); b0.setBounds(20,160,40,20);
        add(b00); b00.setBounds(70,160,40,20);
        add(bt); bt.setBounds(120,160,40,20);
        add(bc); bc.setBounds(170,70,90,20);
        add(bta); bta.setBounds(170,130,40,55);
        add(bh); bh.setBounds(220,130,40,20);
        add(bw); bw.setBounds(220,100,40,20);
        add(bk); bk.setBounds(170,100,40,20);
        add(be); be.setBounds(220,160,40,20);
        b0.addActionListener(new ActionListener() {       //0のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputNumber(0);
            }
        });
        b00.addActionListener(new ActionListener() {     //00のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputNumber(0);
            	inputNumber(0);
            }
        });
        b1.addActionListener(new ActionListener() {     //1のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputNumber(1);
            }
        });
        b2.addActionListener(new ActionListener() {    //2のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputNumber(2);
            }
        });
        b3.addActionListener(new ActionListener() {   //3のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputNumber(3);
            }
        });
        b4.addActionListener(new ActionListener() {  //4のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputNumber(4);
            }
        });
        b5.addActionListener(new ActionListener() {  //5のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputNumber(5);
            }
        });
        b6.addActionListener(new ActionListener() {  //6のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputNumber(6);
            }
        });
        b7.addActionListener(new ActionListener() {  //7のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputNumber(7);
            }
        });
        b8.addActionListener(new ActionListener() {  //8のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputNumber(8);
            }
        });
        b9.addActionListener(new ActionListener() {  //9のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputNumber(9);
            }
        });
        bt.addActionListener(new ActionListener() {  //.のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	ten = 1;
    			int a = (int)pre;
    			la0.setText(Integer.toString(a) + ".");
            }
        });
        bc.addActionListener(new ActionListener() {//cのボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	la0.setText("0");
				la2.setText("");
            	pre = 0;
            	old = 0;
            }
        });
        bta.addActionListener(new ActionListener() {  //+のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputEnzan(0);
            }
        });
        bh.addActionListener(new ActionListener() {  //−のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputEnzan(1);
            }
        });
        bk.addActionListener(new ActionListener() {  //×のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputEnzan(2);
            }
        });
        bw.addActionListener(new ActionListener() {//÷のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputEnzan(3);
            }
        });
        be.addActionListener(new ActionListener() {  //=のボタンが押されたとき
            public void actionPerformed(ActionEvent evt) {
            	inputEnzan(4);
            }
        });
    }

    public void inputNumber(double num){ //数字が入力された場合
    		if(ten == 1){  //小数点が入力された場合
    			pre = pre + num / 10;
    			la0.setText(Double.toString(pre));
    			ten2 = ten;
    			ten = 0;
    		}
    		else{   //数字が続けて入力された場合
    			pre = pre * 10 + num;
    			a = (int)pre;
    			la0.setText(Integer.toString(a));
    		}
    	}
    
    
    public void inputEnzan(int en){   //0:+  1:ー  2:×  3:÷  4:= とする
    	switch(en){
    		case 0:
    			if(old == 0){  //前に入力された数字がない場合
    	    		la2.setText("+");
    	    		old = pre;
    	    		en2 = en;
    	    	}
    	    	else       {   //前に入力された数字がある場合
       			
    	    		la2.setText("+");
    	    		old = old + pre;
    	    		if(ten2 == 1){
    	    			la0.setText(Double.toString(old));
    	    		}
    	    		else{
    	    			int a = (int)old;
    	    			la0.setText(Integer.toString(a));
    	    		}
    	    		en2 = en;
    	    	}
    			pre = 0;
    			break;
    		case 1:
    			if(old == 0){  //前に入力された数字がない場合
    	    		la2.setText("ー");
    	    		old = pre;
    	    		en2 = en;
    			}
    	    	else       {   //前に入力された数字がある場合
    	    		la2.setText("ー");
    	    		old = old - pre;
    	    		if(ten2 == 1){
    	    			la0.setText(Double.toString(old));
    	    		}
    	    		else{
    	    			int a = (int)old;
    	    			la0.setText(Integer.toString(a));
    	    		}
    	    		en2 = en;
    	    	}
    			pre = 0;
        		break;
    		case 2:
    			if(old == 0){  //前に入力された数字がない場合
    	    		la2.setText("×");
    	    		old = pre;
    	    		en2 = en;
    			}
    	    	else       {   //前に入力された数字がある場合
    	    		la2.setText("×");
    	    		old = old * pre;
    	    		if(ten2 == 1){
    	    			la0.setText(Double.toString(old));
    	    		}
    	    		else{
    	    			int a = (int)old;
    	    			la0.setText(Integer.toString(a));
    	    		}
    	    		en2 = en;
    	    	}
    			pre = 0;
        		break;
    		case 3:
    			if(old == 0){  //前に入力された数字がない場合
    	    		la2.setText("÷");
    	    		old = pre;
    	    		en2 = en;
    			}
    	    	else       {   //前に入力された数字がある場合
    	    		la2.setText("÷");
    	    		old = old / pre;
    	    		if(ten2 == 1){
    	    			la0.setText(Double.toString(old));
    	    		}
    	    		else{
    	    			int a = (int)old;
    	    			la0.setText(Integer.toString(a));
    	    		}
    	    		en2 = en;
    	    	}
    			pre = 0;
        		break;
    		case 4:
    			if(old == 0){  //前に入力された数字がない場合
    	    		old = pre;
    	    		en2 = en;
    			}
    	    	else       {   //前に入力された数字がある場合
    	    		la2.setText("");
    	    		switch(en2){
    	    			case 0:
    	    				old = old + pre;
    	    				break;
    	    			case 1:
    	    				old = old - pre;
    	    				break;
    	    			case 2:
    	    				old = old * pre;
    	    				break;
    	    			case 3:
    	    				old = old / pre;
    	    				break;
    	    			default:
    	    		}
    	    		if(ten2 == 1){            //答えに小数点がある場合
    	    			la0.setText(Double.toString(old));
    	    		}
    	    		else{
    	    			a = (int)old;
    	    			la0.setText(Integer.toString(a));
    	    		}
    	    	}
    			pre = 0;
        		
        		break;
        	default:
        		la2.setText("");
    	}
    }
	public static void main(String[] args) {
		Frame win = new Dentaku();
		win.setSize(280, 200); win.setVisible(true);
		win.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				System.exit(0);
			}
		});
	}
	
}


実行結果(1233 + 0.6)



1233と入力した場合



+を入力



0.6を入力



=を入力

解説はソース中にコメントとして記述しています。

感想
今回の課題は、とっても難しかったです。作りながら楽しくもなっていったんですけど、やはりこだわればこだわるほど難しくなっていきました。
けど、いい経験になったので、次にいかしたいです。