Report#7

偶数奇数判定プログラム
例外処理
GUI
温度換算
電卓
考察


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


プログラム
import java.awt.*; // GUI関係のパッケージクラス群をインポート
import java.awt.event.*;                         // Frameクラスの継承
         // インスタンス変数としてボタン、表示欄、入力欄を生成・初期化

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); // 部品の自動配置機能をoffにする
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");
} else {
x0.setText(i + " is Odd");
}
}
});
}
public static void main(String[] args) { // プログラムを動かすために、当然main()メソッドが必要!
Frame win = new GUIaa(); // 前述のオブジェクトを、変数winへ生成・初期化
win.setSize(200, 150); // ウィンドウの大きさを設定
win.setVisible(true); // ディスプレイへ表示
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}



考察
このプログラムは入力した数が偶数であれば "(入力した数) is Even"
偶数でなければ"(入力した数) is Odd" を出力するプログラム。
このプログラムで分かりにくかった部分は


a.	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");
}
}
});

b.        win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});

の2つで、2つとも複数の行に記述されているが、()で囲まれている引数で、無名クラスが使われている。

a.は、ボタンが押されたときの処理で,
入力された数を整数に直して i に代入。
入力欄を空白にする。
iを2で割った数が0(偶数)であれば i + " is Even"を、それ以外(奇数)なら i + " is Odd"を表示する。
を実行する。

b.は、左上のボタンを押した時の処理で、
プログラムを終了できるようにしてある。

○無名クラス
無名クラスとは、定義とインスタンス化を同時に行う手法です。
プログラムの中の一か所でしかインスタンスを生成しないクラス。
わざわざきちんとしたクラスを宣言するのが面倒な時に使うクラスです。
しかし、複数のオブジェクトがある処理には向きません

宣言方法はnewを使って宣言しなければなりません。
具体的には→new 名前() { 変数定義  メソッド定義 }



トップへ

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


例外処理とはプログラム実行中に問題が発生した場合、それを知らせるために例外というオブジェクトが生成されるので、
例外オブジェクトが生成されたときにどのように対応するのかを定めたもの

記述方法
例外が起こりうるプログラムをtryステートメントの中括弧で囲み,例外の発生を監視します。
このtry部分に問題が起こった場合、例外オブジェクトが生成されます。
例外はtryプロックの直後にあるcatchブロックで処理されます。
いずれの catch にも該当しない例外を扱うために、finally を用います。

try {
:
} catch (TestAException e) {
:
} catch (TestBException e) {
:
} finally {
:
}


トップへ

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


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

public class sanban extends Frame {
Button b9 = new Button("BMI");
Label x9 = new Label("小数点にも対応");
Label x0 = new Label("身長m");
Label x1 = new Label("体重kg");
TextField t0 = new TextField();
TextField t1 = new TextField();

public sanban() {
setLayout(null);
add(b9); b9.setBounds(10, 100, 60, 30);
add(x9); x9.setBounds(80, 100, 60, 30);
add(x0); x0.setBounds(60, 30, 50, 20);
add(x1); x1.setBounds(60, 60, 50, 20);
add(t0); t0.setBounds(10, 30, 50, 20);
add(t1); t1.setBounds(10, 60, 50, 20);
b9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{
float i = (new Float(t0.getText())).floatValue();
float j = (new Float(t1.getText())).floatValue();
t0.setText("");t1.setText("");
float bmi =(new Float(j/i/i)).floatValue();
if(bmi<20.0){x9.setText("痩せ");}
if((20.0 if((24.0 if(26.5 }catch(NumberFormatException e){
x9.setText("数字を入力してください");
}
}
});
}
public static void main(String[] args) {
Frame win = new sanban();
win.setSize(200, 150); win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}

実行結果
pic pic
考察
このプログラムでBMI(体格指数)を計算して痩せ、普通、太り気味、太り過ぎの4段階で評価できるようにした。
GUI.javaをもとに作成したのであまりGUIaa.javaと構造的にはかわらない。
float i = (new Float(t0.getText())).floatValue();
とすることで、小数点にも対応できるようにした。
例外処理も加えた。


トップへ

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



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

public class yonban extends Frame {
Button b0 = new Button("華氏F→摂氏C");
Button b1 = new Button("摂氏C→華氏F");
Label x0 = new Label("温度を入力してください");
TextField t0 = new TextField();

public yonban() {
setLayout(null);
add(t0); t0.setBounds(10, 30, 50, 20);
add(b0); b0.setBounds(80, 30, 120, 30);
add(b1); b1.setBounds(80, 60, 120, 30);
add(x0); x0.setBounds(10, 100, 180, 60);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{
float i = (new Float(t0.getText())).floatValue();
t0.setText("");
float ond =(new Float((i-32)*5/9)).floatValue();
x0.setText("華氏"+i+"?n摂氏"+ond);
}catch(NumberFormatException e){
x0.setText("数字を入力してください");
}
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{
float i = (new Float(t0.getText())).floatValue();
t0.setText("");
float ond =(new Float((i*9/5)+32)).floatValue();
x0.setText("華氏"+ond+"?n摂氏"+i);
}catch(NumberFormatException e){
x0.setText("数字を入力してください");
}
}
});
}
public static void main(String[] args) {
Frame win = new yonban();
win.setSize(200, 150); win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}

実行結果
pic pic
考察
F(華氏温度)→C(摂氏温度)への変換計算方法
C(摂氏温度) = (指定した数 - 32) * 5 / 9

C(摂氏温度)→F(華氏温度)への変換計算方法
F(華氏温度) = (指定した数 * 9 / 5) + 32




トップへ

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


プログラム

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


public class goban extends Frame implements ActionListener{
Button b00 = new Button("0"); //数字0
Button b01 = new Button("1"); //数字1
Button b02 = new Button("2"); //数字2
Button b03 = new Button("3"); //数字3
Button b04 = new Button("4"); //数字4
Button b05 = new Button("5"); //数字5
Button b06 = new Button("6"); //数字6
Button b07 = new Button("7"); //数字7
Button b08 = new Button("8"); //数字8
Button b09 = new Button("9"); //数字9
Button b10 = new Button("."); //小数点
Button b11 = new Button("="); // =
Button b12 = new Button("+"); // +
Button b13 = new Button("-"); // -
Button b14 = new Button("*"); // *
Button b15 = new Button("/"); // /
Button b16 = new Button("+-"); // +-
Button b17 = new Button("C"); // クリア C
Label x0 = new Label("",Label.RIGHT);

long tokoro = 0; //before 入力途中の数値保存
long joji = 0; //after 計算後の数値保存用
int op = 0;


public goban() {
int x=10,y=30; ///開始位置
int th=180,tt=20; //テキストフィールドの大きさ th=幅,tt=高さ
int h=60,t=30; //ボタンの大きさ h=幅,t=高さ
setLayout(null);
add(b00); b00.setBounds(x, y+tt+(t*4), h*2, t);
add(b01); b01.setBounds(x, y+tt+(t*3), h, t);
add(b02); b02.setBounds(x+h, y+tt+(t*3), h, t);
add(b03); b03.setBounds(x+(h*2), y+tt+(t*3), h, t);
add(b04); b04.setBounds(x, y+tt+(t*2), h, t);
add(b05); b05.setBounds(x+h, y+tt+(t*2), h, t);
add(b06); b06.setBounds(x+(h*2), y+tt+(t*2), h, t);
add(b07); b07.setBounds(x, y+tt+t, h, t);
add(b08); b08.setBounds(x+h, y+tt+t, h, t);
add(b09); b09.setBounds(x+(h*2), y+tt+t, h, t);
add(b10); b10.setBounds(x+(h*2), y+tt+(t*4), h, t);
add(b11); b11.setBounds(x+(h*3), y+tt+(t*3), h, t*2);
add(b12); b12.setBounds(x+(h*3), y+tt+(t*2), h, t);
add(b13); b13.setBounds(x+(h*3), y+tt+t, h, t);
add(b14); b14.setBounds(x+(h*3), y+tt, h, t);
add(b15); b15.setBounds(x+(h*2), y+tt, h, t);
add(b16); b16.setBounds(x+h, y+tt, h, t);
add(b17); b17.setBounds(x, y+tt, h, t);
add(x0); x0.setBounds(x, y, th, tt);


b00.addActionListener(this);
b01.addActionListener(this);
b02.addActionListener(this);
b03.addActionListener(this);
b04.addActionListener(this);
b05.addActionListener(this);
b06.addActionListener(this);
b07.addActionListener(this);
b08.addActionListener(this);
b09.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
}

// ActionListener インタフェースの実装
public void actionPerformed(ActionEvent e) {

if (e.getSource() == b00) { tokoro *= 10; }
if (e.getSource() == b01) { tokoro = tokoro * 10 + 1; }
if (e.getSource() == b02) { tokoro = tokoro * 10 + 2; }
if (e.getSource() == b03) { tokoro = tokoro * 10 + 3; }
if (e.getSource() == b04) { tokoro = tokoro * 10 + 4; }
if (e.getSource() == b05) { tokoro = tokoro * 10 + 5; }
if (e.getSource() == b06) { tokoro = tokoro * 10 + 6; }
if (e.getSource() == b07) { tokoro = tokoro * 10 + 7; }
if (e.getSource() == b08) { tokoro = tokoro * 10 + 8; }
if (e.getSource() == b09) { tokoro = tokoro * 10 + 9; }
if (e.getSource() == b17) { tokoro = 0; joji = 0; op = 0; }
if (e.getSource() == b16) {tokoro = tokoro*-1; }

x0.setText("" + tokoro); // 入力途中の数字を画面に出力

if (e.getSource() == b12) { keisan(); op = 1; }
if (e.getSource() == b13) { keisan(); op = 2; }
if (e.getSource() == b14) { keisan(); op = 3; }
if (e.getSource() == b15) { keisan(); op = 4; }
if (e.getSource() == b11) { keisan(); op = 0; }

}
// 演算子の種類を判別して計算をするメソッド
public void keisan() {
if (op == 1) { tokoro = joji + tokoro; }
else if (op == 2) { tokoro = joji - tokoro; }
else if (op == 3) { tokoro = joji * tokoro; }
else if (op == 4) { tokoro = joji / tokoro; }
x0.setText("" + tokoro);
joji = tokoro;
tokoro = 0;
}

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


実行結果
pic
考察
j04018のオナガさんの電卓プログラムを参考にさせていただきました。
機能はmacのアプリケーションの計算機からメモリ機能が抜けたものになっています。


トップへ

考察


今回のプログラムは難しくて大変だったが、記述する部分がほとんど同じだったので後半は数字や式を組み替えるだけだった。
GUIの表示にすると日本語表示ができなくなったが、
javac -encoding EUCJIS MyClass.java
とすることで日本語表示できるようになる事を知れてよかった。

参考文献
とほほのjava入門
http://www.tohoho-web.com/java/index.htm

JavaTM 2 Platform Standard Edition 5.0API 仕様
http://java.sun.com/j2se/1.5.0/ja/docs/ja/api/index.html

トップへ