Report6:

  1. 偶奇判定プログラム(GUIaa)をタイプし、その動作について考察せよ。
  2. 例外処理について考察せよ。
  3. 講義資料に示されたGUI部品を全て使用したプログラムを作成せよ。
  4. 摂氏から華氏、華氏から摂氏への温度変換ができるプログラムを作成せよ。
  5. 「電卓」プログラムを作成せよ。中身は自由。


課題その1:ソースから先に載せます。講義資料に少し手を加えました.

[administrator@ib03 ~/Javaworks]% cat GUIaa.java

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

public class GUIaa extends Frame {
    Button    b0 = new Button("Even/Odd?");
    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) {
                    try{
                    int i = Integer.parseInt(t0.getText()); 
                    t0.setText("");
                    if(i % 2 == 0) {
                        x0.setText(i + " is Even");
                    } else {
                        x0.setText(i + " is Odd");
                    }
                    }catch(NumberFormatException e){
                        x0.setText("Error! prease input number");
                        t0.setText("");
                    }
                }
            });
    }
    public static void main(String[] args) {
        Frame win = new GUIaa();
        win.setSize(220, 150); win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    System.exit(0);
                }
            });
    }
}
実行結果は次の通り。

課題1の考察:

次に考察する課題2の例外処理をソースの中に加えました。あとは実行時にクリックするボタンのサイズの変更ぐらいです。偶奇判定自体はif文の簡単な式ですが、
x0.setText(i);と、 t0.setText("");は、int値等の数値のみの受け渡しはできません.これは、setText();メソッドが、String値を扱うためです.ただし、
setText( i + "");

というように、文字列と数値を連結して表示することは可能です。どうしても数字だけ出力させたいときは、ラッパークラスのtoStringメソッド等を使ってString値に変換するという手もあります。

課題その2:例外処理についての考察

講義資料中で述べられた例外処理の書式は以下の通り.
             try{                             //以下のブロックが例外処理の対象
                    int i = Integer.parseInt(t0.getText()); 
                    t0.setText("");
                    if(i % 2 == 0) {
                        x0.setText(i + " is Even");
                    } else {
                        x0.setText(i + " is Odd");
                    }
                 }catch(NumberFormatException e){ 
                        x0.setText("Error! prease input number");
                        t0.setText("");
                 }      //catch以下より、例外発生時に行う処理の記述
このコードでは、catch節が一つしか無いが、例外の種類が複数ある場合は、try以下のブロックに複数のcatch文を続けて記述することが出来ます。そうすることで、1つ目のcatch文で示された種類の例外で無いときは別のcatch文へ、というように、複数の例外時それぞれに別々の処理を行うことが出来ます。
例外自身も実はクラスで、上のコード中の
catch(NumberFormatException e)
は、NumberformatExceptionクラス型の例外が、try以下のブロックで発生した時に、そのオブジェクトeを生成して、catch以下の処理を行うことを示しています。この例外クラスは全て、Exceptionというクラスのサブクラスなので、
catch(Exception e)
とすれば、全ての例外を捕えることができます。

課題その3:サンプルGUIを全て使ったプログラムの作成

ソースと実行結果は以下の通りです.
[administrator@ib03 ~/Javaworks]% cat GUId.java
import java.awt.*;
import java.awt.event.*;

public class GUId 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]");
    Checkbox  c0 = new Checkbox("Check");
    List      l1 = new List();
    TextArea  t1 = new TextArea("some text ...");
    Choice    c1 = new Choice();
    
    public GUId() {
        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, 60, 30);
        add(x0); x0.setBounds(200,30, 180, 30);
        add(c0); c0.setBounds(200,50 , 60, 30);
        add(l1); l1.setBounds(200,100 , 80, 100);
        l1.add("Hello!"); l1.add("How are you?");
        add(t1); t1.setBounds(300,300, 80, 100);
        add(c1); c1.setBounds(300,120 , 60, 30);
        c1.add("Red"); c1.add("Green"); c1.add("Blue");

        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) {
           GUId win = new GUId();
        win.setSize(400, 450); win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    System.exit(0);
                }
            });
    }
}

実行結果(こんなしょっぱいのですいません..)

課題その4:摂氏・華氏変換プログラムの作成

作成したソースは以下の通りです。
[administrator@ib03 ~/Javaworks]% cat FtoC.java
import java.awt.*;
import java.awt.event.*;

public class FtoC extends Frame {
    Button    b0 = new Button("FtoC");
    Button    b1 = new Button("CtoF");
    Label     x0 = new Label("temperature conversion");
    Label     x1 = new Label(":Input number");
    TextField t0 = new TextField();
    
    public FtoC() {
        setLayout(null);
        add(t0); t0.setBounds(10, 40, 80, 30);
        add(x1); x1.setBounds(100,40, 200,20);
        add(b0); b0.setBounds(10, 80, 60, 30);
        add(b1); b1.setBounds(100,80, 60,30);

            b0.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent evt) {
                        try{
                        int c = Integer.parseInt(t0.getText());
                        int f = (c*9/5) + 32;
                        String s = Integer.toString(f);
                        t0.setText(s);
                        x1.setText("Converted :CtoF");
                        }catch(NumberFormatException ex1){
                            x1.setText("Error! Prease input number");
                            t0.setText("");
                        }
                    }
                });

            b1.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent evt) {
                        try{
                        int f = Integer.parseInt(t0.getText());
                        int c = (f-32)*5/9;
                        String s = Integer.toString(c);
                        t0.setText(s);
                        x1.setText("Converted :FtoC");
                        }catch(NumberFormatException ex2){
                            x1.setText("Error! Prease input number");
                            t0.setText("");
                        }
                    }
                });

        add(x0); x0.setBounds(10, 120, 180, 30);
    }
    
    public static void main(String[] args) {
        Frame win = new FtoC();
        win.setSize(300, 160);
        win.setVisible(true);
        win.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent ext){
                    System.exit(0);
                }
            });
    }
}

実行結果

サンプルプログラムの、GUIc.javaを改良して作りました。よく見てみると、"FtoC"やら、"temperature conversion"やらと、「これを使って作れ!」とソースコードの痛烈な叫び声が響いてきたので....。割と良く作れたソースだと思います.

課題その5:電卓プログラムの作成