Report#5

課題


目次

1,問題(1)
2,問題(2)
3,問題(3)
4,問題(4)
5,問題(5)
6,反省・感想

1,問題(1):偶数奇数判定

プログラム
  import java.awt.*;
  import java.awt.event.*;
  
    public class repo7_1 extends Frame {
       Button    b0 = new Button("Even/Odd?");
       Label     x0 = new Label("Type a number and press...");
       TextField t0 = new TextField();
     
       public repo7_1() {
           setLayout(null);
           add(t0); t0.setBounds(10, 40, 90, 30);
           add(b0); b0.setBounds(110, 40, 80, 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 + " is Even");
                   } else {
                       x0.setText(i + " is Odd");
                   }
               }
           });
      }
       public static void main(String[] args) {
           Frame win = new repo7_1();
           win.setSize(200, 150); win.setVisible(true);
           win.addWindowListener(new WindowAdapter() {
               public void windowClosing(WindowEvent evt) {
                   System.exit(0);
               }
           });
       }
   }

○実行結果

  

考察

テキストボックスに入力された文字列を受け取り、それが偶数か奇数か を判定するプログラム。
 テキストボックス、ラベル、ボタンの三つのGUIオブジェクトを使用してい る。
 これらのオブジェクトは、

・ボタンが押された時
 ・テキストボックスに文字が入力された時
 ・マウスが重なった時

  などにイベントを生成する。
  イベントが生成されると登録されたオブジェクト(リスナ)に イベント通 知が送られ、
  それを受け取ったオブジェクトは特定のメソッドを呼び出す。

●GUIについて
簡単に言えばアイコンやボタンなどを使ってコンピュータを操作する方法であり、
絵やアイコンなどで表すため初心者にもわかりやすいといった利点がある。

●ユーザーインターフェースにはGUIの他にCUIがあり、 GUIがグラフィック ベースであるのに対し、
CUIは文字ベースのUIとなっている。

●GUIプログラムはイベントを受けとる事で処理を行う。

●java.awt.*とjava.awt.event.*パッケージについて
java.awt.* : ユーザインターフェースの作成と、グラフィックスとイメージのペイントの為のクラス、インターフェースが含まれている。
AWT(Abstract Window Toolkit)のクラス群であり、ウィンドウオブジェクトの生成、GUIコンポーネントの生成などができる。
java.awt.event.* : イベント処理のクラスとインターフェースが含まれている。

●Listenerについて
Listenerはイベントが発生したときにそのイベントを受け取り、イベント処理 を行うメソッドを呼び出すための インターフェースである。Listenerにはマウスに関するものやウィンドウに関するものなど様々な種類がある。

●addActionListenerとaddWindowListenerについて
これらのadd〜Listenerメソッドはイベントを発生させる部品のインスタンスに Listenerを組み込むような動作を行う。
つまり、このメソッドを使う事により、Listenerとその部品との連結のような ことを行うわけである。

2,問題(2):例外処理

プログラム
public class Repo7test06 {

    public static void main(String[] args) {
        int a = 5;
        int b = 8;
        int c = 0;
        
        try{
        System.out.println("a/b=" + a/b);
        System.out.println("b/c=" + b/c);
	}
	catch(Exception err){
             System.out.println("エラーです");
        }
        finally{
            System.out.println("計算終了");
        }
    }
}

   

結果

a/b=0
エラーです
計算終了
    

考察

例外とはプログラムの実行中に発生した問題を通知するために、実行時に生成さ れるオブジェクトのこと。ユーザーはプログラマーが思いもしないような使い方 をする場合がある。そのようなプログラミングでは解決できない問題を、例外処 理 を使い問題を回避する。

例外処理の基本は、例外オブジェクトをキャッチして処理すること。tryプ ロック内で作られた例外オブジェクトはcatchブロックでキャッチして処理する。
try{
    //ここに例外が発生しそうな処理を書く
   }
   catch(例外クラス1 変数){
    //例外処理1を書く
   }
   catch(例外クラス2 変数){
   //例外処理2を書く
   }
   finally{
   //共通処理を書く
   }
   
例外が発生しなかった場合は共通処理が実行される。
例外1が発生した場合は例外処理1が実行され、共通処理が実行される。
例外2が発生した場合は例外処理2が実行され、共通処理が実行される。
例外が発生した場合はtryブロックの処理は中断され、その例外を処理できる catchブロックを探して、例外を処理できるcatchブロックに例外処理が実行され る。

よく使われる例外クラス
例外クラス 内容
ArrayIndexOutOfBoundsException 定義済みの配列を超えたとき
ClassNotFoundException クラスが見つからないとき
FileNotFoundException ファイルが見つからないとき
IllegalArgumentException 引数が不正なとき
NumberFormatException 数値フォーマットが異なるとき
NullPointerException nullオブジェクトの不正な使用を示すとき

3,問題(3):GUI部品全て含むプログラム

プログラム
import java.awt.*;
import java.awt.event.*;

public class Rep7B extends Frame{
       Button     b0 = new Button("OK");
       Label      x0 = new Label("あなたの名前は");
       Label      x1 = new Label("住所");
       Label      x2 = new Label("都道府県");
       Label      x3 = new Label("市町村");
       Label      x4 = new Label("質問です。該当するものにチェックして下
       さい。");
       Label      x5 = new Label("みんなに何かメッセージがあれば書いて下
       さい。");
       Label      x6 = new Label("Thank you!!");
       TextField  t0 = new TextField();
       TextArea  t1 = new TextArea("some text ...");
       Choice     c0 = new Choice();
       Checkbox  c1 = new Checkbox("毎年年賀状を書いている。");
       Checkbox  c2 = new Checkbox("近頃年賀状を貰った覚えがない。");
       Checkbox  c3 = new Checkbox("郵便受けに年賀状の束を見つけると、自
       分宛のものがないか必ず探す?
。");
       List      l1 = new List();
       public Rep7B() {
              setLayout(null);
                add(b0); b0.setBounds(100, 600, 40, 20);
                         add(x0); x0.setBounds(30, 40, 100, 20);
                add(x1); x1.setBounds(30, 80, 40, 20);
                         add(x2); x2.setBounds(40, 110, 80, 20);
                add(x3); x3.setBounds(210, 110, 100, 20);
                         add(x4); x4.setBounds(30, 240, 120, 20);
                add(x5); x5.setBounds(30, 380, 430, 20);
                         add(x6); x6.setBounds(30, 640, 430, 20);
                add(t0); t0.setBounds(140, 40, 100, 20);
                         add(t1); t1.setBounds(50, 420, 350, 150);
                add(c0); c0.setBounds(105, 110, 80, 20);
                         c0.add("沖縄県"); c0.add("その他");
                add(c1); c1.setBounds(60, 270, 300, 20);
                         add(c2); c2.setBounds(60, 300, 300, 20);
                add(c3); c3.setBounds(60, 330, 430, 20);
                         add(l1); l1.setBounds(280, 110, 70, 120);
                         l1.add("北谷町"); l1.add("北中城村"); l1.add("
       宜野湾市");
                         l1.add("中城村"); l1.add("浦添市"); l1.add("那
       覇市");
                         l1.add("西原町"); l1.add("沖縄市"); l1.add("与
       那原町");
                         l1.add("その他");

         b0.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                 x6.setText("何で毎年家族で俺にだけ年賀状が来ないんだろ
             う?");
            }
        });
        }
    public static void main(String[] args) {
        Frame win = new Rep7B();
        win.setSize(600, 900);
        win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    }
}

実行結果

実行結果その1

4,問題(4):温度変換プログラム

import java.awt.*;
import java.awt.event.*;

public class GUIcc extends Frame{
   Button b0 = new Button("摂氏→華氏");
   Button b1 = new Button("華氏→摂氏");
   Label x0 = new Label("変換のボタンを押して下さい");
   Label x1 = new Label("→");
   TextField[] t0 = new TextField[]{new TextField(), new TextField()};

    public GUIcc(){
        setLayout(null);
        add(x0); x0.setBounds(10, 30, 100, 30);
        add(t0[0]); t0[0].setBounds(10, 70, 70, 30);
        add(t0[1]); t0[1].setBounds(100, 70, 70, 30);
        add(x1); x1.setBounds(80, 70, 20, 30);
        add(b0); b0.setBounds(5, 110, 160, 30);
        add(b1); b1.setBounds(5, 150, 160, 30);

        b0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                try{
                    t0[1].setText("" +((new
            Float(t0[0].getText()).floatValue()*1.8+32)));
                }catch(Exception ex){t0[1].setText(ex.toString());}
            }
        });
        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                try{
                    t0[1].setText("" +Math.round(((new
        Float(t0[0].getText()).floatValue()*(0.555)-(17.777)))));
                }catch(Exception ex){t0[1].setText(ex.toString());}
            }
        });
    }

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

実行結果

考察

1〜2行目でパッケージのインポート 。5〜10行目で GUi 部品の生成。 ボタン2つと、ラベル、テキストフィールドが1つ。
13〜19行目がレイアウトの設定。13行目でレイアウトマネージャを解除し、14、 16行目でラベルの設置、 18,19行目でボタンの設置、15行目でテキストフィールドの設置をしている。
21〜34行目でボタンを押した時のイベント処理。21〜27行目では摂氏→華氏変換 をしている。 計算式は華氏温度=1.8 X 摂氏温度(入力した数字)+32とした。28〜34行目では華 氏→摂氏の変換をしている。
計算式は摂氏温度=0.555 X 華氏温度(入力した数字)-17.777とした。Math.round は値を四捨五入するメソッド。

5,問題(5):「電卓」プログラム

プログラム
import java.awt.*;
import java.awt.event.*;

public class Rep7D extends Frame {
    Label     x0 = new Label();
    Label     x1 = new Label();
    TextField t0 = new TextField("0");
    Button    b0 = new Button("0");
    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    ba = new Button("+");
    Button    bb = new Button("-");
    Button    bc = new Button("*");
    Button    bd = new Button("/");
    Button    be = new Button("AC");
    Button    bf = new Button("=");

    int i = 0, y = 0;

    public Rep7D() {
        setLayout(null);
        add(b0); b0.setBounds(50,300,50,40);
        add(bf); bf.setBounds(100,300,100,40);
        add(bd); bd.setBounds(200,300,50,40);
        add(b1); b1.setBounds(50,250,50,40);
        add(b2); b2.setBounds(100,250,50,40);
        add(b3); b3.setBounds(150,250,50,40);
        add(bc); bc.setBounds(200,250,50,40);
        add(b4); b4.setBounds(50,200,50,40);
        add(b5); b5.setBounds(100,200,50,40);
        add(b6); b6.setBounds(150,200,50,40);
        add(bb); bb.setBounds(200,200,50,40);
        add(b7); b7.setBounds(50,150,50,40);
        add(b8); b8.setBounds(100,150,50,40);
        add(b9); b9.setBounds(150,150,50,40);
        add(ba); ba.setBounds(200,150,50,40);
        add(be); be.setBounds(250,150,70,190);
        add(t0); t0.setBounds(60,100,150,30);
        add(x0); x0.setBounds(60,60,100,40);
        add(x1); x1.setBounds(150,60,150,40);

        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                   i = i * 10 + 1;
                   String s1 = Integer.toString(i);
                   t0.setText(s1);
            }
        });

        b2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                      i = i * 10 + 2;
                         String s1 = Integer.toString(i);
                            t0.setText(s1);
            }
        });
        b3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                i = i * 10 + 3;
                String s1 = Integer.toString(i);
                t0.setText(s1);
            }
        });

        b4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                i = i * 10 + 4;
                String s1 = Integer.toString(i);
                t0.setText(s1);
            }
        });

        b5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                i = i * 10 + 5;
                String s1 = Integer.toString(i);
                t0.setText(s1);
            }
        });

        b6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                      i = i * 10 + 6;
                String s1 = Integer.toString(i);
                t0.setText(s1);
            }
        });

        b7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                      i = i * 10 + 7;
                String s1 = Integer.toString(i);
                t0.setText(s1);
            }
        });

        b8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                      i = i * 10 + 8;
                String s1 = Integer.toString(i);
                t0.setText(s1);
            }
        });

        b9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                i = i * 10 + 9;
                String s1 = Integer.toString(i);
                t0.setText(s1);
            }
        });

        ba.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent evt) {
                i = i * 10 + 9;
                String s1 = Integer.toString(i);
                t0.setText(s1);
            }
        });

        ba.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
                      y = y + i;
                         String s2 = Integer.toString(y);
                            x0.setText(s2 + "+");
                t0.setText("0");
                i = 0;
                bf.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        y = y + i;
                           String s2 = Integer.toString(y);
                              x0.setText(s2);
                                 t0.setText("0");

                    }
                });
                           }
        });

        bb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                   if (y == 0) {
                      y = i - y;
                      } else {
                         y = y - i;
                         }
                            String s2 = Integer.toString(y);
                               x0.setText(s2 + "-");
                t0.setText("0");
                i = 0;
                bf.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        y = y - i;
                           String s2 = Integer.toString(y);
                              x0.setText(s2);
                                 t0.setText("0");
                    }
                });
                           }
        });
        bc.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                   if (y == 0) {
                      y = 1 * i;
                      } else { 
                         y = y * i;
                         }      
                            String s2 = Integer.toString(y);
                               x0.setText(s2 + "*");
                t0.setText("0");
                i = 0;
                bf.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        y = y * i;
                           String s2 = Integer.toString(y);
                              x0.setText(s2);
                                 t0.setText("0");
                    }
                });
                           }
        });
 
        bd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                   if (y == 0) {
                      y = i / 1;
                      } else { 
                         y = y / i;
                         }      
                            String s2 = Integer.toString(y);
                               x0.setText(s2 + "/");
                t0.setText("0");
                i = 0;
                bf.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        y = y / i;
                           String s2 = Integer.toString(y);
                              x0.setText(s2);
                                 t0.setText("0");
                    }
                });
                           }
        });

        be.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                      i = 0; y = 0;
                         String s2 = Integer.toString(y);
                x0.setText(s2);
                t0.setText("0");
                }
        });
    }
    public static void main(String[] args) {
        Frame win = new Rep7D();
        win.setSize(500,500); win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    System.exit(0);
                }
            });
      }
}

実行結果

実行結果その1

考察

○まず数字を出力するボタンについて
・『123』をボタンで入力したい場合、『1』、『2』、『3』と順にボタンを押 すが、これは、1*100+2*10+3*10という計算からなりたっている。
・よって入力の際には前の桁に10を掛けて、それからその下の桁の数字を入 力できるようにしなければならない。
      ・
          i = 0;
      ・
      ・
     i = i * 10 + 1;
      ・


○演算子について
・足し算の場合、int型変数yを準備し、yに値を足して、求めた値をまたyに代 入という作業を繰り返している。
・出力の際にはyをString型変数に変換してからでないとsetTextは使えない。
・『AC』を押し、始めから計算をし直す時は、変数i、yを0に初期化する。
・iは演算子を選択して新しく値を入力する際、初期値に戻す。yは『=』が押さ れるまで計算途中の値を保存する。
・他の演算子でif文が使われているのは、一回目の計算の際、i=0では値が、符 号 が逆になったり、0にしかならなかったりとするので、状況に応じてiの初期値 を1にしなければならない。
            public void actionPerformed(ActionEvent evt) {
                   if (y == 0) {
                      y = i / 1;
                      } else { 
                         y = y / i;
                         }      
                   String s2 = Integer.toString(y);
                   x0.setText(s2 + "/");
                   t0.setText("0");
                   i = 0;

6,反省・感想

今回のレポートはとても時間がかかりました。
いつもながら次のレポートは早めに終わらせたいです。