課題7

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

http://www.pc-view.net/Help/ 参考文献

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

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, 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 GUIaa();
        win.setSize(200, 150); win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    }
}
考察です

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

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

public class ColorRGBa 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]");
    
    public ColorRGBa() {
        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(10, 200, 180, 30);
        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) {
        Frame win = new ColorRGBa();
        win.setSize(200, 250); win.setVisible(true);
        win.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                System.exit(0);
            }
        });
    }
}

例外とは

例外とはプログラムの実行中に発生した問題を通知するために、
実行時に生成されるオブジェクトの事です。例外が発生する典型的な
ケースは、整数を0で除算した、ファイルが見つからなかった、数値の形式が
不正だった、等の場合です。
try{
処理内容
}

catch(例外1){
例外1が起きたときの処理
}

catch(例外2){
例外2が起きたときの処理
}

finally{
処理内容
}
tryで処理をしているときに問題が発生した場合、上から順にcatchブロックが
検索されます。起きた問題と例外が合致するとcatch処理が行われます。
finallyは必ず最後に行われる処理です。try処理やcatch処理が終わると行われます。

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


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

public class Matome extends Frame{
//コンポーネントの生成
Choice     co = new Choice();
Checkbox[] ce = new Checkbox[]{new Checkbox("man"),
new Checkbox("woman") };
Button b0 = new Button("表示");
Label[]  x0 = new Label[]{new Label("あなたの性別"),
new Label("あなたの好きな色は?"),
new Label("学年")};
TextArea ta = new TextArea();
List      li = new List();

//コンストラクタの設定
public Matome() {
setLayout(null);
//コンポーネントの絶対位置を指定する
add(b0); b0.setBounds(10, 290, 90, 30);
add(x0[0]); x0[0].setBounds(10, 30, 80, 30);
add(x0[1]); x0[1].setBounds(10, 100, 90, 30);
add(x0[2]); x0[2].setBounds(10, 220, 60, 30);
add(ce[0]); ce[0].setBounds(10, 50, 80, 30);
add(ce[1]); ce[1].setBounds(70, 50, 80, 30);
add(co);    co.setBounds(10, 240, 20, 30);
co.add("1"); co.add("2"); co.add("3"); co.add("4");
add(li); li.setBounds(10, 120, 120, 100);
li.add("緑"); li.add("オレンジ"); li.add("青");
li.add("白"); li.add("赤");
add(ta); ta.setBounds(220, 30, 200, 200);

//ボタンが押されたときの処理
b0.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
//テキストエリアに表示する文
if(ce[0].getState())
ta.append("性別は "+ ce[0].getLabel() + "?n");
if(ce[1].getState())
ta.append("性別は "+ ce[1].getLabel() + "?n");
ta.append("好きな色は "+li.getSelectedItem()+"?n");
ta.append("学年は "+co.getSelectedItem()+"?n");
}
});

}

public static void main(String[] args) {
Frame win = new Matome();
win.setSize(500,400); win.setVisible(true);
//ウィンドウが閉じられたときの設定
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
解説 出力結果


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

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

public class Henka extends Frame{
Label[] l0 = new Label[]{new Label("入力フィールド"),
new Label("出力フィールド")};
Choice co = new Choice();
TextField[] tx = new TextField[]{new TextField(""),
new TextField("")};
Button bt = new Button("変換");

public Henka() {
setLayout(null);
add(l0[0]); l0[0].setBounds(240, 40,40,40);
add(l0[1]); l0[1].setBounds(240, 140,40,40);
add(co);    co.setBounds(110, 70, 120, 30);
co.add("摂氏→華氏"); co.add("華氏→摂氏");
add(tx[0]);    tx[0].setBounds(30, 40, 200, 10);
add(tx[1]);    tx[1].setBounds(30, 140, 200, 10);
add(bt);    bt.setBounds(30, 70, 60, 30);

bt.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
if(co.getSelectedIndex()==1){//華氏→摂氏
//tx[0]を整数型に変換
String str = tx[0].getText();
double tx0 = Double.parseDouble(str);
//華氏を摂氏に直す
int tx0kai = (int) ((tx0 - 32) / 1.8);
tx[1].setText("摂氏は "+tx0kai+" です");
}
else{//摂氏→華氏
//tx[0]を整数型に変換
String str = tx[0].getText();
double tx0 = Double.parseDouble(str);
//摂氏を華氏に直す
int tx0kai = (int)(tx0 * 1.8 + 32);
tx[1].setText("華氏は "+tx0kai+" です");
}

}
});

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

}
解説 実行画面


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

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

public class Dentaku extends Frame implements ActionListener{
static int kazu1 = 0;
static int kazu2 = 0;
static int hugou = 0;
Button[] b0 = new Button[]{
new Button("0"),new Button("1"),new Button("2"),
new Button("3"),new Button("4"),new Button("5"),
new Button("6"),new Button("7"),new Button("8"),
new Button("9")
};

Button[] b1 = new Button[]{
new Button("+"),new Button("-"),new Button("*"),
new Button("/"),new Button("C"),new Button("=")
};
TextField tf = new TextField("");


public Dentaku() {

setLayout(null);
for(int i=0; i<10;i++) add(b0[i]);
for(int i=0; i<6;i++) add(b1[i]);
add(tf);					     
int wi = 40;//幅
int hi = 50;//高さ
int ki = 250;					     
tf.setBounds(0,hi,wi*5,hi);
b0[0].setBounds(0,ki,wi,hi);
b0[1].setBounds(0,ki-hi,wi,hi);
b0[2].setBounds(wi,ki-hi,wi,hi);
b0[3].setBounds(wi+wi,ki-hi,wi,hi);
b0[4].setBounds(0,ki-hi*2,wi,hi);
b0[5].setBounds(wi,ki-hi*2,wi,hi);
b0[6].setBounds(wi+wi,ki-hi*2,wi,hi);
b0[7].setBounds(0,ki-hi*3,wi,hi);
b0[8].setBounds(wi,ki-hi*3,wi,hi);
b0[9].setBounds(wi+wi,ki-hi*3,wi,hi);
					     
b1[0].setBounds(wi+wi+wi,ki,wi,hi);
b1[1].setBounds(wi+wi+wi,ki-hi,wi,hi);
b1[2].setBounds(wi+wi+wi,ki-hi*2,wi,hi);
b1[3].setBounds(wi+wi+wi,ki-hi*3,wi,hi);
b1[4].setBounds(wi,ki,wi,hi);
b1[5].setBounds(wi+wi+wi+wi,ki-hi*3,wi,hi*4);
					     
for(int i=0;i<10;i++)
b0[i].addActionListener(this);
							     
for(int i=0;i<6;i++)
b1[i].addActionListener(this);
									     
}
									     
public void actionPerformed (ActionEvent e) {

     try{
									            //数字が押されたときの処理
         String num1 = e.getActionCommand();
	 kazu1 =  kazu1 * 10 + Integer.parseInt(num1);
	 //入力された数字を表示
	 tf.setText(""+kazu1);
      }
      catch(NumberFormatException nu){
	   if (e.getSource() == b1[0]){
		if(kazu1 != 0){
		    keisan();
		    hugou = 1;//hugou に+を代入
		}else{
		    kazu2 = kazu1;
		    kazu1 = 0;
		}
	 }
	 else if (e.getSource() == b1[1]){
	     if(kazu1 != 0) {
		 keisan();
		 hugou = 2;//hugouに-を代入
	     }else{
		 kazu2 = kazu1;
		 kazu1 = 0;
	     }
	  }
	  else if (e.getSource() == b1[2]){
	      if(kazu1 != 0){
		  keisan();
		  hugou = 3;//hugouに*を代入
	      }else{
		  kazu2 = kazu1;
		  kazu1 = 0;
	      }
	   }
	   else if (e.getSource() == b1[3]){
	        if(kazu1 != 0){
		     keisan();
		     hugou = 4;//hugouに/を代入
		}else{
		     kazu2 = kazu1;
		     kazu1 = 0;
		}
	  }
	  else if (e.getSource() == b1[4]){
	       kazu1 = 0; kazu2 = 0; tf.setText(""+0);
	  }
	  else if (e.getSource() == b1[5]){
	      keisan();
	  }
	 tf.setText(""+kazu2);

	 }
  }
public void keisan(){
    switch(hugou) {
     case 0:
	  break;
     case 1:
	  kazu2 = kazu2 + kazu1;
	  kazu1 = 0;
		break;
		case 2:
		kazu2 = kazu2 - kazu1;
		kazu1 = 0;
		break;
		case 3:
		kazu2 = kazu2 * kazu1;
		kazu1 = 0;
		break;
		case 4:
		kazu2 = kazu2 / kazu1;
		kazu1 = 0;
		break;
		}
		
		
		}
		
		public static void main(String[] args) {
		Frame win = new Dentaku();
		win.setSize(300,300); win.setVisible(true);
		win.addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent evt) {
		System.exit(0);
		}
		});
		}

}
説明 実行画面

感想:相変わらず大変だった(汗)