レポート7
課題
- 偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。
- 例外処理について、考察せよ。
- 上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。
- 摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。
- 「電卓」プログラム。中身は自分の思うように。
1.偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。
プログラムソース
01 import java.awt.*;
02 import java.awt.event.*;
03
04 public class rep7_1 extends Frame {
05 Button b0 = new Button("Even/Odd?");
06 Label x0 = new Label("Type a number and press...");
07 TextField t0 = new TextField();
08
09 public rep7_1() {
10 setLayout(null);
11 add(t0); t0.setBounds(10, 40, 90, 30);
12 add(b0); b0.setBounds(110, 40, 80, 30);
13 add(x0); x0.setBounds(10, 80, 180, 30);
14 b0.addActionListener(new ActionListener() {
15 public void actionPerformed(ActionEvent evt) {
16 int i = (new Integer(t0.getText())).intValue();
17 t0.setText("");
18 if(i % 2 == 0) {
19 x0.setText(i + " is Even");
20 } else {
21 x0.setText(i + " is Odd");
22 }
23 }
24 });
25 }
26 public static void main(String[] args) {
27 Frame win = new rep7_1();
28 win.setSize(200, 150); win.setVisible(true);
29 win.addWindowListener(new WindowAdapter() {
30 public void windowClosing(WindowEvent evt) {
31 System.exit(0);
32 }
33 });
34 }
35 }
実行結果
考察
- 1,2行目でjava.awtとjava.awt.eventパッケージを読み込む。
- 5行目でButtonクラスのb0を生成する。引数を文字列("Even/Odd")にする事で、Even/Oddラベル付きのボタンを表示する。
- 6行目でLabelクラスのx0を生成する。引数を文字列("Type a number and press...")にする事で、Type a number and press...のラベルを表示する。
- 7行目でTextFieldクラスのt0を生成する。引数を指定していないので、空白のテキスト入力フィールドを表示する。
- 10行目で部品の自動配置機能をoffにする
- 11~13行目でadd(t0),add(b0),add(x0)とする事で、5~7行目で生成したオブジェクトをsetBoundsで配置する事ができる。
- 14~25行目でボタンが押された場合、入力された数字が偶数か奇数か判定する。
- 27行目でrep7_1クラスのオブジェクトをFrameクラス型変数winに生成する。
- 28行目で表示するディスプレイのサイズを指定し、表示する。
- 29~33行目でウィンドウが閉じられた時、作業を終了する。
2. 例外処理について、考察せよ。
プログラムソース
01 import java.awt.*;
02 import java.awt.event.*;
03
04 public class rep7_1 extends Frame {
05 Button b0 = new Button("Even/Odd?");
06 Label x0 = new Label("Type a number and press...");
07 TextField t0 = new TextField();
08
09 public rep7_1() {
10 setLayout(null);
11 add(t0); t0.setBounds(10, 40, 90, 30);
12 add(b0); b0.setBounds(110, 40, 80, 30);
13 add(x0); x0.setBounds(10, 80, 180, 30);
14 b0.addActionListener(new ActionListener() {
15 public void actionPerformed(ActionEvent evt) {
16 try{
17 int i = (new Integer(t0.getText())).intValue();
18 t0.setText("");
19 if(i % 2 == 0) {
20 x0.setText(i + " is Even");
21 } else {
22 x0.setText(i + " is Odd");
23 }
24 }
25 catch(NumberFormatException ee)
26 {x0.setText("error");}
27
28 }
29 });
30 }
31 public static void main(String[] args) {
32 Frame win = new rep7_1();
33 win.setSize(200, 150); win.setVisible(true);
34 win.addWindowListener(new WindowAdapter() {
35 public void windowClosing(WindowEvent evt) {
36 System.exit(0);
37 }
38 });
39 }
40 }
実行結果
考察
- 偶数奇数判定プログラムに少し加えて、数字以外が入力されても作業を続ける。数字以外が入力されるとerrorが出力される
- 例外とは、簡単に言うとコンパイルは成功するのに、そのプログラムの実行時に起こる予期しないエラーのことである。
- Javaでは、基本的に例外が発生する可能性がある箇所では必ず例外処理を記述しなければいけない。
- 例外を呼び出す事もできる。例外を呼び出す事をthrowすると言う。
3.上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。
ソースプログラム
import java.awt.*;
import java.awt.event.*;
public class GUId extends Frame{
Button b0 = new Button();
TextField t0 = new TextField();
Label l0 = new Label("");
TextArea ta0 = new TextArea("");
Checkbox cb0 = new Checkbox(); -------------(1)
Choice ch0 = new Choice();
List ls0 = new List();
public GUId(){
setLayout(null);
add( b0); b0.setBounds(130, 30, 70, 30);
add( t0); t0.setBounds( 50, 80,150, 15);
add( l0); l0.setBounds( 50, 60,150, 30);
add(ta0); ta0.setBounds( 50, 110,150, 80);
add(cb0); cb0.setBounds(110, 30, 30, 30);
ch0.add("1"); ch0.add("2"); ch0.add("3");
ch0.add("4"); ch0.add("5"); ch0.add("6"); -------------(2)
add(ch0); ch0.setBounds( 50, 30, 30, 30);
ls0.add("1"); ls0.add("2"); ls0.add("3");
ls0.add("4"); ls0.add("5"); ls0.add("6");
ls0.add("7"); ls0.add("8"); ls0.add("9"); ------------(3)
add(ls0); ls0.setBounds( 10, 30, 30, 160);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt){
if(cb0.getState()){ ------------(4)
t0.setText(ls0.getSelectedItem() + "年連続" +
ch0.getSelectedItem() + "回目の出場");
l0.setText(ls0.getSelectedItem() + "年連続" +
ch0.getSelectedItem() + "回目の出場");
ta0.setText(ls0.getSelectedItem() + "年連続" +
ch0.getSelectedItem() + "回目の出場");
} else {
t0.setText(ls0.getSelectedItem() + "年ぶり" +
ch0.getSelectedItem() + "回目の出場");
l0.setText(ls0.getSelectedItem() + "年ぶり" +
ch0.getSelectedItem() + "回目の出場");
ta0.setText(ls0.getSelectedItem() + "年ぶり" +
ch0.getSelectedItem() + "回目の出場");
}
}
});
}
public static void main(String argv[]){
Frame win = new GUId();
win.setSize(400, 320);
win.setVisible(true);
}
}
|
実行結果
考察
4.摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。
プログラムソース
import java.awt.*;
import java.awt.event.*;
public class rep7_3 extends Frame{
Button b0 = new Button("摂氏から華氏へ");
Button b1 = new Button("華氏から摂氏へ");
Label x0 = new Label("変換ボタンを押してください。");
Label x1 = new Label("変換後");
TextField t0 = new TextField();
TextField t1 = new TextField();
public rep7_3(){
setLayout(null);
add(x0); x0.setBounds(50, 40, 150, 20);
add(x1); x1.setBounds(165, 60, 60, 20);
add(t0); t0.setBounds(50, 80, 60, 20);
add(t1); t1.setBounds(150, 80, 60, 20);
add(b0); b0.setBounds(70, 120, 100, 20);
add(b1); b1.setBounds(70, 150, 100, 20);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{
int i = (new Integer(t0.getText())).intValue();
t1.setText("" + i*1.8+32);
}catch(Exception ex) {
t1.setText("error");
}
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{
int i = (new Integer(t0.getText())).intValue();
t1.setText("" + (i-32)/1.8);
}catch(Exception ex) {
t1.setText("error");
}
}
});
}
public static void main(String[] args) {
Frame win = new rep7_3();
win.setSize(300, 200); win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
実行結果
考察
- このプログラムもサンプルを参考にした。
- 華氏 = 摂氏*1.8 + 32 により計算を行う。
5.「電卓」プログラム。中身は自分の思うように。
プログラムソース
import java.awt.*;
import java.awt.event.*;
public class rep7_4 extends Frame {
Button b0 = new Button("+");
Button b1 = new Button("-");
Button b2 = new Button("*");
Button b3 = new Button("/");
Label x0 = new Label("Type a number and press...");
TextField t0 = new TextField();
TextField t1 = new TextField();
public rep7_4() {
setLayout(null);
add(t0); t0.setBounds(10, 40, 90, 30);
add(t1); t1.setBounds(110, 40, 90, 30);
add(b0); b0.setBounds(10, 80, 80, 30);
add(b1); b1.setBounds(110, 80, 80,30);
add(b2); b2.setBounds(10,110, 80, 30);
add(b3); b3.setBounds(110,110, 80, 30);
add(x0); x0.setBounds(10, 140, 180, 30);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = (new Integer(t0.getText())).intValue();
int y =(new Integer(t1.getText())).intValue();
{
x0.setText(i+y+"");
}
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = (new Integer(t0.getText())).intValue();
int y =(new Integer(t1.getText())).intValue();
{
x0.setText(i-y+"");
}
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = (new Integer(t0.getText())).intValue();
int y =(new Integer(t1.getText())).intValue();
{
x0.setText(i*y+"");
}
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
float i = (new Integer(t0.getText())).floatValue();
float y =(new Integer(t1.getText())).floatValue();
{
x0.setText(i/y+"");
}
}
});
}
public static void main(String[] args) {
Frame win = new rep7_4();
win.setSize(210, 200); win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
実行結果
考察
- ボタンを四則演算に対応させるように4つ作り、入力欄を 2つ作り、この2つを計算する。入力された数字をそれぞれ i,y に代入し計算する。
- 割り算は割り切れないこともあるので float 型にした。
感想