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




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


import java.awt.*;                                          // GUI関係のパッケージクラス群をインポート
import java.awt.event.*;          

public class GUIaa extends Frame {                           // Frameクラスの継承
    Button    b0 = new Button("Even/Odd?");         //Buttonクラスのオブジェクトを生成し、文字列をボタンにつける。
    Label     x0 = new Label("Type a number and press...");  //Labelクラスのオブジェクトを作成し、文字列を表示する。 
    TextField t0 = new TextField();   //TextFieldクラスのオブジェクトを作成し、入力欄を作る
    
    public GUIaa() {
        setLayout(null);
        add(t0); t0.setBounds(10, 40, 90, 30);            // 入力欄の貼り付け(左上隅のx/y座標、幅、高さ)
        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");                //2で割って余りが0なら"~ is Even"を表示
                } else {
                    x0.setText(i + " is Odd");                  //2で割って余りが0以外なら"~ is Odd"を表示
                }
            }
        });
    }
    public static void main(String[] args) {
        Frame win = new GUIaa();
        win.setSize(200, 150); win.setVisible(true);                        // ウィンドウの大きさを設定
        win.addWindowListener(new WindowAdapter() {     //ウィンドウを閉じるときの動作
            public void windowClosing(WindowEvent evt) {
                System.exit(0);                             //システムを終了させる
            }
        });
    }
}




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


例外処理とはプログラムを実行してエラーが起こったときに、エラーを適切に処理すること 例外処理の基本
try{
例外の発生をしらべる文;
・・・
}
catch(例外のクラス 変数名){
例外が起きたときに処理する分;
・・・
}
1,tryブロック内で例外が起きると、そこで処理を中断する。
2,例外がcatchブロックの()内の例外の種類と一致していれば、そのcatchブロック内の処理を行う。
3,catchブロックが終わったら、そのtry~catchブロックの後から処理が続けられる。

finallyブロックでは、例外の発生に関わらず、最後に行う処理を記述する。
try~catch~finallyが例外処理の基本となっている。
catchブロック内の、例外のオブジェクトを受け取った変数を出力させると、例外の種類を出す。


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


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

public class GUIall extends Frame {
    Button    b0 = new Button("Even/Odd?");
    Label     x0 = new Label("Type a number and press...");
    TextField t0 = new TextField();
    Checkbox c0= new Checkbox("Check");
    List           l1 = new List();
    TextArea t1 =new TextArea("some text ");
    Choice     c1 = new Choice();
    
    public GUIall() {
        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);
        add(c0); c0.setBounds(10, 230, 60, 30);
	add(l1); l1.setBounds(10, 100 , 80, 100);
	l1.add("Hello"); l1.add("color");
	add(t1);t1.setBounds(100, 100, 80,100);
	add(c1); c1.setBounds(10,  250, 60, 30);
	c1.add("red"); c1.add("green"); c1.add("blue");
	
	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 GUIall();
        win.setSize(200, 300); win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    }
}

とりあえず意味はないけど、いろいろとくっつけてみました。

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



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

public class hennkan extends Frame {
    Button   b0 = new Button("C->F");
    Button   b1 = new Button("F->C");
    Label     x0 = new Label("Type a number and press...");
    TextField t0 = new TextField();
    
    public hennkan() {
        setLayout(null);
        add(t0); t0.setBounds(10, 40, 90, 30);
        add(b0); b0.setBounds(110, 30, 80, 30);
	add(b1); b1.setBounds(110, 60, 80, 30);
        add(x0); x0.setBounds(10, 100, 180, 30);
        b0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                  try {
		int i = (new Integer(t0.getText())).intValue();
                t0.setText("");
		{
		x0.setText(i*1.8+32+"C");
		}
		        }catch(Exception ex) {
			    x0.setText("整数を入れて下さい"); 
			    }
            }
        });
	b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                   try{
		int i = (new Integer(t0.getText())).intValue();
                t0.setText("");
		{
		x0.setText((i-32)/1.8+"F");
		}
		       }catch(Exception ex){
		           x0.setText("整数を入れて下さい");
			   }
            }
        });
    }
    public static void main(String[] args) {
        Frame win = new hennkan();
        win.setSize(200, 150); win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    }
}

考察
GUIaaのプログラムに書き足したもの。ボタンを摂氏から華氏、
華氏から摂氏へのそれぞれの変換ボタンを作った。
整数以外では、実行できないので例外処理が起こる。


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


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);
            }
        });
    }
}
考察
これもGUIaaのプログラムを書き換えたもの。ボタン を四則演算に対応させるように4つ作り、入力欄を 2つ作り、この2つを計算する。 入力された数字をそれぞれi,yに代入し計算。 わり算は割り切れないこともあるのでfloat型にした。

反省・感想
今回は結構難しくやりたいプログラムができなかった。 電卓プログラムは例外処理を入れずにしてしまったことが気になる。
参考文献
やさしいjava