report#5

課題

GUIプログラム:講義資料を参考に、以下の課題を行え。
1. 偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。
2. 例外処理について、考察せよ。
3. 上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。
4. 摂氏から華氏、華氏から摂氏への温度換算ができるプログラムを作成せよ。
5. 「電卓」プログラム。中身は自分の思うように。


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


GUIaa.javaのソース

01:import java.awt.*;
02:import java.awt.event.*;
03:
04:public class GUIaa 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 GUIaa() {
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 GUIaa();
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:}

GUIaa.javaの実行結果



GUIaa.javaの考察

  • GUIとは、Graphical User Interfaceの頭文字を取って略したもので、文字が並んだだけのCUIの逆。これは、キーボード以外の操作も可能である。
  • 5行目で出てくるButtonは、ユーザーがボタン境界の内側でマウスをクリックすると、ユーザーにフィードバックを提供するようにボタンの外観が変化する。
  • 6行目で出てくるLabel(String str, int align)のstrはラベルテキストで、alignは、ラベルを左揃え、中央揃え、右揃えのうちどれにするかを示す定数を表している。定数LEFT,CENTER,RIGHTとして位置合わせの値を設定する。GUI上に表示される文字列で、プログラムでは変更できるが、ユーザーが変更することはできない。
  • 7行目で出てくるTextField(String str, int cols)のstrはフィールドに入力されたテキストで、引数colsでは、フィールドの幅を字数で指定している。TextFieldでは、1行のテキストを入力することができる。文字を入力すると、テキストイベントが生成され、ENTERキーを押したときにアクションイベントが生成される。
  • 10行目で出てくるsetLayout()は、要素を配置する座標を計算する必要がないので、時間の節約にとなり、ユーザーは間違いを起こしやすい作業を行わずにすむ。サイズを変更すると、要素の配置を動的に調整する。コンポーネントは一般的にプラットフォームによってサイズがことなる。そのため、すべての環境に適した座標を一度に指定することはできない。ここではsetLayout(null);になっているのでこれらの機能は失っている。
  • 15行目で出てくるActionEventは、ボタンを押したり、リスト項目をダブルクリックしたとき、メニュー項目を選択したときに生成される。
  • 16行目で出てくるint i = (new Integer(t0.getText())).intValue();は、t0.getText()でt0のテキストを読み込みIntegerで生成後のオブジェクトを不変なものとする。
  • 28行目で出てくるsetSize();は、要素の数をsizeに変更する。sizeからはみ出した要素は破棄される。
  • 30行目で出てくるwindowClosingは、ユーザーがウィンドウのクローズを要求するとwindowClosing()メソッドが呼び出される。
  • 30行目で出てくるWindowEventは、ユーザーがウィンドウを活動化したとき、閉じたとき、非活動化したとき、アイコン化解除したとき、アイコン化したとき、開いたとき、終了したときに生成される。

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


    例外処理の考察

  • 例外処理の例外とは、コンパイル成功後そのプログラムの実行時に起こる予期しないエラーのこと。プログラマーは、ユーザの思いがけない操作などに、プログラミングでは回避できないエラーがあることを想定したプログラミングを心がけなければならない。例外はオブジェクトとして扱われ、その種類に応じたクラス型のオブジェクトが生成され、そのオブジェクトを捕まえて処理することを例外処理と言う。
    .... try{      例外が起こりそうな処理 }catch(例外1クラス 変数){      例外処理1 }catch(例外2クラス 変数){      例外処理2 }finally{      共通処理 } ....

  • tryのブロック内でcatchで指定した例外が発生するとcatchブロックに処理が移ります。finallyブロックに記述された処理は、エラーが発生しても、しなくても必ず実行されます。なので、必ず行わなければならない事後処理を記述するのに適しています。
  • (例:オープンしたファイルのクローズ処理)
    メソッド内をすべてtry,catch,finallyのいずれかのブロックにしなければならないということはありません。また、finallyを必ず使わなければならないということもありません。

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


    GUI.javaのソース

    01:import java.awt.*;
    02:import java.awt.event.*;
    03:
    04:public class GUI extends Frame{
    05: Button b0 = new Button();
    06: TextField t0 = new TextField();
    07: Label l0 = new Label("");
    08: TextArea ta0 = new TextArea("");
    09: Checkbox cb0 = new Checkbox();
    10: Choice ch0 = new Choice();
    11: List ls0 = new List();
    12:
    13: public GUI(){
    14:  setLayout(null);
    15:  add( b0); b0.setBounds(130, 30, 70, 30);
    16:  add( t0); t0.setBounds( 50, 80,150, 15);
    17:  add( l0); l0.setBounds( 50, 60,150, 30);
    18:  add(ta0); ta0.setBounds( 50, 110,150, 80);
    19:  add(cb0); cb0.setBounds(110, 30, 30, 30);
    20:
    21:  ch0.add("1"); ch0.add("2"); ch0.add("3");
    22:  ch0.add("4"); ch0.add("5"); ch0.add("6");
    23:  add(ch0); ch0.setBounds( 50, 30, 30, 30);
    24:
    25:  ls0.add("1"); ls0.add("2"); ls0.add("3");
    26:  ls0.add("4"); ls0.add("5"); ls0.add("6");
    27:  ls0.add("7"); ls0.add("8"); ls0.add("9");
    28:  add(ls0); ls0.setBounds( 10, 30, 30, 160);
    29:
    30:  b0.addActionListener(new ActionListener() {
    31:   public void actionPerformed(ActionEvent evt){
    32:    if(cb0.getState()){
    33:     t0.setText(ls0.getSelectedItem() + "年連続" + ch0.getSelectedItem() + "回目の出場");
    34:     l0.setText(ls0.getSelectedItem() + "年連続" + ch0.getSelectedItem() + "回目の出場");
    35:     ta0.setText(ls0.getSelectedItem() + "年連続" + ch0.getSelectedItem() + "回目の出場");
    36:    } else {
    37:     t0.setText(ls0.getSelectedItem() + "年ぶり" + ch0.getSelectedItem() + "回目の出場");
    38:     l0.setText(ls0.getSelectedItem() + "年ぶり" + ch0.getSelectedItem() + "回目の出場");
    39:     ta0.setText(ls0.getSelectedItem() + "年ぶり" + ch0.getSelectedItem() + "回目の出場");
    40:    }
    41:   }
    42:  });
    43: }
    44:
    45: public static void main(String argv[]){
    46:  Frame win = new GUI();
    47:  win.setSize(400, 320);
    48:  win.setVisible(true);
    49: }
    50:}

    GUI.javaの実行結果



    GUI.javaの考察

  • これまでに出てきたGUI部品、Button,TextField,Label,TextArea,Checkbox,Choice,Listクラスのインスタンスを作り、それぞれに代入している。
  • 10行目では、Choiceコンポーネントに1から6までの数字を格納している。
  • 11行目では、Listコンポーネントに1から9までの数字を格納している。
  • 32行目では、getState()メソッドを使い、cb0がチェックされているかどうかで処理を分岐させている。チェックされていれば〜年連続〜回目の出場、チェックされていなければ〜年ぶり〜回目の出場と表示される。
  • 33、34、35、36、37、38、39行目では、getSelectedItem()メソッドを使い、年数にはls0で、回数にはch0で選択された項目がそれぞれ代入している。

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


    kansan.javaのソース

    01:import java.awt.*;
    02:import java.awt.event.*;
    03:
    04:public class kansan extends Frame {
    05:
    06: Button b0 = new Button("摂氏→華氏");
    07: Button b1 = new Button("華氏→摂氏");
    08: Button b2 = new Button("Clear");
    09: Label x0 = new Label("入力");
    10: Label x1 = new Label("結果");
    11: TextField t0 = new TextField();
    12: TextField t1 = new TextField();
    13:
    14: public kansan() {
    15: setLayout(null);
    16: add(x0); x0.setBounds(10, 25, 30, 30);
    17: add(t0); t0.setBounds(10, 55, 100, 30);
    18: add(x1); x1.setBounds(10, 95, 30, 30);
    19: add(t1); t1.setBounds(10, 125, 100, 30);
    20: add(b0); b0.setBounds(120, 30, 100, 30);
    21: add(b1); b1.setBounds(120, 60, 100, 30);
    22: add(b2); b2.setBounds(120, 150, 100, 30);
    23:
    24: b0.addActionListener(new ActionListener() {
    25: public void actionPerformed(ActionEvent evt) {
    26: try {
    27: int i = (new Integer(t0.getText())).intValue();
    28: t1.setText("" + (i*1.8+32));
    29: } catch(NumberFormatException ee){
    30: t1.setText("Error!");}
    31: }
    32: });
    33:
    34: b1.addActionListener(new ActionListener() {
    35: public void actionPerformed(ActionEvent evt) {
    36: try {
    37: int i = (new Integer(t0.getText())).intValue();
    38: t1.setText("" + ((i-32)/1.8));
    39: } catch(NumberFormatException ee){
    40: t1.setText("Error!");}
    41: }
    42: });
    43: b2.addActionListener(new ActionListener(){
    44: public void actionPerformed(ActionEvent evt) {
    45: try{
    46: t0.setText(""); t1.setText("");
    47: } catch(NumberFormatException ee){}
    48: }
    49: });
    50: }
    51:
    52: public static void main(String[] args) {
    53: Frame win = new kansan();
    54: win.setSize(250, 200); win.setVisible(true);
    55: win.addWindowListener(new WindowAdapter() {
    56: public void windowClosing(WindowEvent evt) {
    57: System.exit(0);
    58: }
    59: });
    60: }
    61:}

    kansan.javaの実行結果




    数値を入力(摂氏→華氏)


    数値を入力(華氏→摂氏)

    kansan.javaの考察

  • 24行目のbutton b0は、押されたときの処理を定義している。
  • 28行目では、入力された数字 i に対して(i×1.8+32)を行って、摂氏から華氏へ変換している。
  • 34行目のbutton b1は、押されたときの処理を定義している。
  • 38行目では、入力された数字 i に対して((iー32)/1.8)を行って、華氏から摂氏へ変換している。
  • 43行目のbutton b2は、処理が実行すると、入力、出力の文字列が初期化される。
  • 数字以外が入力されると、例外処理より、TextField t1に「Error!」が出力される。

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


    dentaku.javaのソース

    001: import java.awt.*;
    002: import java.awt.event.*;
    003:
    004: public class dentaku extends Frame{
    005: int a = 0;
    006: Button b0 = new Button("Clear");
    007: Button b1 = new Button("+");
    008: Button b2 = new Button("-");
    009: Button b3 = new Button("*");
    010: Button b4 = new Button("/");
    011: Button b5 = new Button("%");
    012: Button b6 = new Button("=");
    013: Button b7 = new Button("continue?");
    014: TextField t0 = new TextField();
    015: TextField t1 = new TextField();
    016: TextField t2 = new TextField();
    017: Label x0 = new Label();
    018: Label x1 = new Label("=");
    019:
    020: public dentaku(){
    021: setLayout(null);
    022: add(b0); b0.setBounds(275,100, 100, 30);
    023: add(b1); b1.setBounds( 10, 30, 50, 30);
    024: add(b2); b2.setBounds( 60, 30, 50, 30);
    025: add(b3); b3.setBounds(110, 30, 50, 30);
    026: add(b4); b4.setBounds(160, 30, 50, 30);
    027: add(b5); b5.setBounds(210, 30, 50, 30);
    028: add(b6); b6.setBounds(275, 30, 100, 30);
    029: add(b7); b7.setBounds( 20,100, 100, 30);
    030: add(t0); t0.setBounds( 20, 60, 100, 30);
    031: add(t1); t1.setBounds(150, 60, 100, 30);
    032: add(t2); t2.setBounds(280, 60, 150, 30);
    033: add(x0); x0.setBounds(128, 65, 50, 30);
    034: add(x1); x1.setBounds(258, 65, 50, 30);
    035: b0.addActionListener(new ActionListener(){
    036: public void actionPerformed(ActionEvent evt) {
    037: try{
    038: a = 0;
    039: t0.setText("");
    040: t1.setText("");
    041: t2.setText("");
    042: x0.setText("");
    043: }catch(Exception ex){}
    044: }
    045: });
    046: b1.addActionListener(new ActionListener(){
    047: public void actionPerformed(ActionEvent evt) {
    048: x0.setText(b1.getLabel());
    049: a = 1;
    050: }
    051: });
    052: b2.addActionListener(new ActionListener(){
    053: public void actionPerformed(ActionEvent evt) {
    054: x0.setText(b2.getLabel());
    055: a = 2;
    056: }
    057: });
    058: b3.addActionListener(new ActionListener(){
    059: public void actionPerformed(ActionEvent evt) {
    060: x0.setText(b3.getLabel());
    061: a = 3;
    062: }
    063: });
    064: b4.addActionListener(new ActionListener(){
    065: public void actionPerformed(ActionEvent evt) {
    066: x0.setText(b4.getLabel());
    067: a = 4;
    068: }
    069: });
    070: b5.addActionListener(new ActionListener(){
    071: public void actionPerformed(ActionEvent evt) {
    072: x0.setText(b5.getLabel());
    073: a = 5;
    074: }
    075: });
    076: b6.addActionListener(new ActionListener(){
    077: public void actionPerformed(ActionEvent evt) {
    078: try{
    079: int x = (new Integer(t0.getText())).intValue();
    080: int y = (new Integer(t1.getText())).intValue();
    081: if(a==1)
    082: t2.setText("" + ( x + y ));
    083: if(a==2)
    084: t2.setText("" + ( x - y ));
    085: if(a==3)
    086: t2.setText("" + ( x * y ));
    087: if(a==4)
    088: t2.setText("" + ( x / y ));
    089: if(a==5)
    090: t2.setText("" + ( x % y ));
    091: if(a==0)
    092: t2.setText("Choose Button!!");
    093: }catch(NumberFormatException ee){
    094: t2.setText("error");
    095: }
    096: }
    097: });
    098: b7.addActionListener(new ActionListener(){
    099: public void actionPerformed(ActionEvent evt) {
    100: try{
    101: int z = (new Integer(t2.getText())).intValue();
    102: t0.setText("" + z);
    103: t1.setText("");
    104: t2.setText("");
    105: x0.setText("");
    106: }catch(NumberFormatException ee){
    107: t0.setText("Error!!");
    108: t1.setText("Push");
    109: t2.setText("Clear");
    110: }
    111: }
    112: });
    113: }
    114:
    115:
    116: public static void main(String[] args) {
    117: Frame win = new dentaku();
    118: win.setSize(450, 150); win.setVisible(true);
    119: win.addWindowListener(new WindowAdapter() {
    120: public void windowClosing(WindowEvent evt) {
    121: System.exit(0);
    122: }
    123: });
    124: }
    125: }

    dentaku.javaの実行結果


    開始時 数値入力 演算子ボタン選択忘れ 計算後 続けて計算 例外処理1 例外処理2

    dentaku.javaの考察

  • 5〜18行目では、部品の作成&初期化がされている。
  • 21〜34行目では、部品の配置がされている。
  • 35〜112行目では、各ボタンが押されたときの処理がされている。ボタンb1〜b5の押したボタンによってx0ラベルに演算子を表示し、変数aの値を決定する。78〜87行で計算し、計算結果を表示する。また、演算子が選ばれていない場合の処理も含めている。
  • 98〜112行目では、1度だけの計算でなく、本物の電卓のように続けて計算する場合のボタンも付け加えた。

    反省と感想


  • 難しかった。