report#7
ProgII/e045732
Report#1
Report#2
Report#3
Report#4
Report#5
Report#6
Report#7
Report#8
Belonging-----Information Engineering Section
Student ID---045732K
Name----------Shingo Tamaki
Date------------2006/01/16(MON)
1.課題の紹介
2.解答
1.偶数奇数判定プログラム(GUIaa)をタイプし、その動作を考察せよ。
- 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, 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);
}
});
}
}
実行結果
→
→
考察
- java.awt.の中身とjava.awt.event.の中身を読み込み、
Frameクラスから継承されたGUIaaクラスで、Even/Oddと書か
れたボタンType a number and press...と書かれたラベル、
何も書かれていないテキストフィールドを作って、GUIaaメソ
ッドでそれらの配置と大きさを決め、ボタンが押された時にテ
キストフィールドに書かれているのを数値化し、iに入れ、偶
数か奇数かで表示する内容を変えるようにしてます。
mainメソッドではGUIaaクラスをオブジェクト化して、ウィン
ドウの大きさを指定して表示、さらに、左上の終了ボタンが押
された時にプログラムが終わるようになっています。
2.例外処理について考察せよ。
- GUIaa.java
import java.awt.*;
import java.awt.event.*;
public class GUIaa extends Frame {
Button b0 = new Button("奇 or 偶");
Label x0 = new Label("↑ 数字を入力してね"
);
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) {
try{
int i = (new
Integer(t0.getText())).intValue();
t0.setText("数字を入力");
if(i % 2 == 0) {
x0.setText(i + "は偶数でしょ");
} else {
throw new ArithmeticException();
}
}catch (ArithmeticException ex) {
x0.setText("奇数でしょ");
}
catch (NumberFormatException ex) {
x0.setText("認識できない文字です");
}
finally{
t0.setText("");
}
}
});
}
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{1}
catch(2){3}
finally{4}
という形の文章でします。
tryの1の部分で例外の発生しうる部分を書いておきます。
catchの2の部分でこのcatchが処理できるタイプを指定
します。そして、3の部分に処理する内容が入っています。
例外が複数ある時はcatchの数を増やせます。
finallyの4では、例外が発生してもしなくても実行する内容
が入ってます。また、このfinallyは省略しても良いそうです。
例外が発見された場合、処理するcatchを検索しますが、
見つからない場合、プログラムを停止するそうです。
3.サンプルプログラムに出てきたGUI部分を、すべて使ったプログラムを作
成せよ。
- Original.java
import java.awt.*;
import java.awt.event.*;
public class Original extends Frame {
Button b0 = new Button("四角形の面積を求め
る");
Button b1 = new Button("リセット");
Label x0 = new Label("縦");
Label x1 = new Label("横");
Label x2 = new Label("面積");
TextField t0 = new TextField();
TextField t1 = new TextField();
TextField t2 = new TextField();
public Original() {
setLayout(null);
add(t0); t0.setBounds(50, 40, 150, 25);
add(t1); t1.setBounds(50, 80, 150, 25);
add(t2); t2.setBounds(50, 160, 150, 55);
add(b0); b0.setBounds(10, 120, 180, 25);
add(b1); b1.setBounds(10, 250, 100, 25);
add(x0); x0.setBounds(10, 40, 150, 25);
add(x1); x1.setBounds(10, 80, 150, 25);
add(x2); x2.setBounds(10, 160, 150, 25);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{
int x = (new Integer(t0.getText())).
intValue();
int y = (new Integer(t1.getText())).
intValue();
int s=x*y;
t2.setText(""+s);
}
//NumberFormatExceptionという例外が発生した場合の処理
catch(NumberFormatException e){
t2.setText("数字を入力して下さい");
}
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
t0.setText("");
t1.setText("");
t2.setText("");
}
});
}
public static void main(String[] args) {
Frame win = new Original();
win.setSize(250, 300); win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
実行結果
4.摂氏から華氏、華氏から摂氏への温度変換ができるプログラムを作成せよ。
- SeshiKashi.java
import java.awt.*;
import java.awt.event.*;
public class SeshiKashi extends Frame {
Button b0 = new Button("摂氏から華氏へ");
Button b1 = new Button("華氏から摂氏へ");
Button b2 = new Button("リセット");
Label x0 = new
Label("数値を入力してボタンを押す");
Label x1 = new Label("");
Label x2 = new Label("");
Label x3 = new Label("");
TextField t0 = new TextField();
TextField t1 = new TextField();
public SeshiKashi() {
setLayout(null);
add(t0); t0.setBounds(10, 120, 90, 30);
add(b0); b0.setBounds(110, 100, 130, 30);
add(b1); b1.setBounds(110, 130, 130, 30);
add(t1); t1.setBounds(250, 120, 90, 30);
add(x0); x0.setBounds(40, 40, 180, 30);
add(x1); x1.setBounds(10, 90, 90, 30);
add(x2); x2.setBounds(250, 90, 180, 30);
add(x3); x3.setBounds(50, 230, 200, 100);
add(b2); b2.setBounds(280, 150, 100, 30);
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{
int s0 = (newInteger(t0
.getText())).intValue();
int kkekka=s0*9/5+32;
t1.setText(" "+kkekka);
x1.setText("摂氏");
x2.setText("華氏");
}
//NumberFormatExceptionという例外が発生した場合の処理
x3.setText
("リセットして数字を入力して下さい");
}
//何か例外が発生した場合の処理
catch(Exception e){
x3.setText("何らかのエラーが発生");
}
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try{
int k0 =(newInteger
(t0.getText())).intValue();
int skekka=(k0-32)*5/9;
t1.setText(" "+skekka);
x1.setText("華氏");
x2.setText("摂氏");
}
//NumberFormatExceptionという例外が発生した場合の処理
catch(NumberFormatException e){
x3.setText
("リセットして数字を入力して下さい");
}
//何か例外が発生した場合の処理
catch(Exception e){
x3.setText("何らかのエラーが発生");
}
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
t0.setText("");
t1.setText("");
x1.setText("");
x2.setText("");
x3.setText("");
}
});
}
public static void main(String[] args) {
Frame win = new SeshiKashi();
win.setSize(400, 300); win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
実行結果
5.「電卓」プログラム、中身は自分の思うように。
- Dentaku.java
import java.awt.*;
import java.awt.event.*;
public class Dentaku extends Frame {
int k = 0;
Button b0 = new Button("0");
Button b1 = new Button("1");
Button b2 = new Button("2");
Button b3 = new Button("3");
Button b4 = new Button("4");
Button b5 = new Button("5");
Button b6 = new Button("6");
Button b7 = new Button("7");
Button b8 = new Button("8");
Button b9 = new Button("9");
Button bt = new Button("+");
Button bh = new Button("-");
Button bk = new Button("*");
Button bw = new Button("/");
Button br = new Button("+/-");
Button be = new Button("=");
Button bac = new Button("AC");
Button bc = new Button("C");
Label l0 = new Label("");
Label l1 = new Label("0");
Label l2 = new Label("");
TextField t0 = new TextField("0");
public Dentaku() {
setLayout(null);
add(l0); l0.setBounds(90, 30, 80, 30);
add(l1); l1.setBounds(10, 30, 80, 30);
add(t0); t0.setBounds(180, 30, 100, 30);
add(b0); b0.setBounds(40, 230, 60, 30);
add(b1); b1.setBounds(40, 190, 60, 30);
add(b2); b2.setBounds(100, 190, 60, 30);
add(b3); b3.setBounds(160, 190, 60, 30);
add(b4); b4.setBounds(40, 150, 60, 30);
add(b5); b5.setBounds(100, 150, 60, 30);
add(b6); b6.setBounds(160, 150, 60, 30);
add(b7); b7.setBounds(40, 110, 60, 30);
add(b8); b8.setBounds(100, 110, 60, 30);
add(b9); b9.setBounds(160, 110, 60, 30);
add(bt); bt.setBounds(220, 190, 60, 30);
add(bh); bh.setBounds(220, 150, 60, 30);
add(bk); bk.setBounds(220, 110, 60, 30);
add(bw); bw.setBounds(220, 70, 60, 30);
add(br); br.setBounds(100, 230, 60, 30);
add(be); be.setBounds(160, 230, 120, 30);
add(bc); bc.setBounds(160, 70, 60, 30);
add(bac); bac.setBounds(40, 70, 120, 30);
bt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
l0.setText(" + ");
l2.setText("1");
if(k == 0){
l1.setText(t0.getText());
k = k + 1;
}
else {
int i = Integer.parseInt(l1
.getText());
int j = Integer.parseInt(t0
.getText());
int sum = i + j;
String s = Integer.toString(sum);
l1.setText(s);
}
t0.setText("0");
}
});
bh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
l0.setText(" - ");
l2.setText("2");
if(k == 0){
l1.setText(t0.getText());
k = k + 1;
}
else {
int i = Integer.parseInt(l1
.getText());
int j = Integer.parseInt(t0
.getText());
int sum = i - j;
String s = Integer.toString(sum);
l1.setText(s);
}
t0.setText("0");
}
});
bk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
l0.setText(" * ");
l2.setText("3");
if(k == 0){
l1.setText(t0.getText());
k = k + 1;
}
else {
int i = Integer.parseInt(l1
.getText());
int j = Integer.parseInt(t0
.getText());
int sum = i * j;
String s = Integer.toString(sum);
l1.setText(s);
}
t0.setText("0");
}
});
bw.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
l0.setText(" / ");
l2.setText("4");
if(k == 0){
l1.setText(t0.getText());
k = k + 1;
}
else {
int i = Integer.parseInt(l1
.getText());
int j = Integer.parseInt(t0
.getText());
int sum = i / j;
String s = Integer.toString(sum);
l1.setText(s);
}
t0.setText("0");
}
});
br.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i * (-1);
String s = Integer.toString(sum);
t0.setText(s);
}
});
be.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(l1.getText());
int j = Integer.parseInt(t0.getText());
String t = l2.getText();
if(t == "1") {
int sum = i + j;
String s = Integer.toString(sum);
l1.setText(s);
l0.setText("");
t0.setText("0");
}
else if(t == "2") {
int sum = i - j;
String s = Integer.toString(sum);
l1.setText(s);
l0.setText("");
t0.setText("0");
}
else if(t == "3") {
int sum = i * j;
String s = Integer.toString(sum);
l1.setText(s);
l0.setText("");
t0.setText("0");
}
else if(t == "4") {
int sum = i / j;
String s = Integer.toString(sum);
l1.setText(s);
l0.setText("");
t0.setText("0");
}
else {
String s = Integer.toString(i)
t0.setText(s);
}
}
});
bc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
t0.setText("0");
}
});
bac.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
l0.setText("");
l1.setText("0");
t0.setText("0");
k = 0;
}
});
b0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i * 10;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i * 10 + 1;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i * 10 + 2;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i * 10 + 3;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i * 10 + 4;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i * 10 + 5;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i * 10 + 6;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i * 10 + 7;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i * 10 + 8;
String s = Integer.toString(sum);
t0.setText(s);
}
});
b9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(t0.getText());
int sum = i * 10 + 9;
String s = Integer.toString(sum);
t0.setText(s);
}
});
}
public static void main(String[] args) {
Dentaku win = new Dentaku();
win.setSize(320, 270);
win.setVisible(true);
win.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
}
}
実行結果
3.感想
- これはまあ、思ったよりは早く終わらせることができました。
まあ、自分で作るのは苦労しましたけど。
あと、実行結果を別のウィンドウで上手く起動させることができなかったので、
単に貼付けることにしました。ご了承を。
ProgII/e045732
Report#1
Report#2
Report#3
Report#4
Report#5
Report#6
Report#7
Report#8