Report5


名前→久貝卓矢

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


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


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

public class hantei extends Frame {
    Button    b0 = new Button("Even/odd?");
    Label     x0 = new Label("Type a number and press");
    TextField t0 = new TextField();

    public hantei() {
        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 + " kisu-");
                    } else {
                        x0.setText(i + " gu-su-");
                    }
                    }
                }
            });
    }
    public static void main(String[] args) {
        Frame win = new hantei();
        win.setSize(200, 150); win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    System.exit(0);
                }
            });
    }
}



考察
入力された値を奇数か偶数か判断するプログラムである。
・プログラムの動作としては、TextFieldに入力された値を取得し、 ifステートメントでその値が奇数であるか偶数であるか判別する。
コンポーネントを提供する主なクラ ス
クラスコンポーネント機能
Labalラベル文字列を表示する
TextFieldテキストフィールド一行の文字列を入 力
TextAreaテキストエリア複数行の文字列を入 力
Buttonボタンボタンを表示している
Checkboxチェックボックスチェックボックスを表 示する
CheckboxGroupチェックボックスグループ 複数のチェックボックスをグループ化する
Choiceチョイス ドロップダウン形式のリストを表示する
Scrollbarスクロールバー スクロールバーを表示する


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


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

public class reigai extends Frame {
    Button    b0 = new Button("Even/Odd?");
    Label     x0 = new Label("Type a number and press...");
    TextField t0 = new TextField();

    public reigai() {
        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) {
                    try{
                        int i = (new Integer(t0.getText())).intValue();
                        t0.setText("");
                        if(i % 2 == 0) {
                            x0.setText(i + " is Even");
                        } else {
                            x0.setText(i + " is Odd");
                        }
                    }catch(NumberFormatException ee){
                        x0.setText("error");}
                }
            });
    }

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


考察
try{}で囲まれた部分に例外が発生するであろう箇所を記述し、この範囲で問題
が発生するとその発生した問題に関する情報をもった例外オブジェクトが作成さ
れる。catch(){...}で実行中に起こった問題の処理が行われる。

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



考察


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


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

public class ondo extends Frame {
    Button    b0 = new Button("kasi -> ssesi");
    Button    b1 = new Button("ssesi -> kasi");


    TextField t0 = new TextField();
    TextField t1 = new TextField();

    public ondo() {
        setLayout(null);
        add(t0); t0.setBounds(10, 40, 80, 30);
        add(t1); t1.setBounds(120, 40, 80, 30);
        add(b0); b0.setBounds(10,  80, 110, 30);
        add(b1); b1.setBounds(120, 80, 110, 30);


        b0.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    try{
                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 ondo();
        win.setSize(300, 160);
        win.setVisible(true);
        win.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent evt){
                    System.exit(0);
                }
            });
    }
}
考察
・これは華氏を摂氏、摂氏を華氏にかえるプログラムである。
・華氏がi*1.8+32で、摂氏が(i-32)/1.8である

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


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

public class dentaku 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 dentaku() {
        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 dentaku();
        win.setSize(210, 200); win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    System.exit(0);
                }
            });
    }
}



考察
・ボタンを四則演算に対応させるように4つ作り、入力欄を 2つ作り、この2
つを計算する。入力された数字をそれぞれ i,y に代入し計算する。