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


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

プログラムソース
01 import java.awt.*;
02 import java.awt.event.*;
03 
04 public class rep7_1 extends Frame {
05     Button    b0 = new Button("Even/Odd?");
06     Label     x0 = new Label("Type a number and press...");
07     TextField t0 = new TextField();
08   
09     public rep7_1() {
10         setLayout(null);
11         add(t0); t0.setBounds(10, 40, 90, 30);
12         add(b0); b0.setBounds(110, 40, 80, 30);
13         add(x0); x0.setBounds(10, 80, 180, 30);
14         b0.addActionListener(new ActionListener() {
15             public void actionPerformed(ActionEvent evt) {
16                 int i = (new Integer(t0.getText())).intValue();
17                t0.setText("");
18                if(i % 2 == 0) {
19                     x0.setText(i + " is Even");
20                 } else {
21                     x0.setText(i + " is Odd");
22                 }
23             }
24         });
25    }
26     public static void main(String[] args) {
27         Frame win = new rep7_1();
28         win.setSize(200, 150); win.setVisible(true);
29         win.addWindowListener(new WindowAdapter() {
30             public void windowClosing(WindowEvent evt) {
31                 System.exit(0);
32             }
33         });
34     }
35 }
 


実行結果
rep7_1_1 rep7_1_2

考察



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

プログラムソース
01 import java.awt.*;
02 import java.awt.event.*;
03 
04 public class rep7_1 extends Frame {
05     Button    b0 = new Button("Even/Odd?");
06     Label     x0 = new Label("Type a number and press...");
07     TextField t0 = new TextField();
08    
09     public rep7_1() {
10         setLayout(null);
11         add(t0); t0.setBounds(10, 40, 90, 30);
12         add(b0); b0.setBounds(110, 40, 80, 30);
13         add(x0); x0.setBounds(10, 80, 180, 30);
14         b0.addActionListener(new ActionListener() {
15            public void actionPerformed(ActionEvent evt) {
16              try{   
17             int i = (new Integer(t0.getText())).intValue();
18                 t0.setText("");
19                 if(i % 2 == 0) {
20                     x0.setText(i + " is Even");
21                 } else {
22                     x0.setText(i + " is Odd");
23                 }
24                 }
25                 catch(NumberFormatException ee)
26  {x0.setText("error");}
27               
28             }
29         });
30     }
31     public static void main(String[] args) {
32         Frame win = new rep7_1();
33         win.setSize(200, 150); win.setVisible(true);
34         win.addWindowListener(new WindowAdapter() {
35             public void windowClosing(WindowEvent evt) {
36                 System.exit(0);
37             }
38         });
39     }
40 }
 


実行結果
rep7_1_3

考察



3.上述のサンプルプログラムに出てきたGUI部品を、全て使ったプログラムを作成せよ。
ソースプログラム
import java.awt.*;
import java.awt.event.*;

public class GUId extends Frame{
 Button b0 = new Button();
 TextField t0 = new TextField();
 Label l0 = new Label("");
 TextArea ta0 = new TextArea("");
 Checkbox cb0 = new Checkbox();       -------------(1)
 Choice ch0 = new Choice();
 List ls0 = new List();

 public GUId(){
  setLayout(null);
  add( b0); b0.setBounds(130, 30, 70, 30);
  add( t0); t0.setBounds( 50, 80,150, 15);
  add( l0); l0.setBounds( 50, 60,150, 30);
  add(ta0); ta0.setBounds( 50, 110,150, 80);
  add(cb0); cb0.setBounds(110, 30, 30, 30);

  ch0.add("1"); ch0.add("2"); ch0.add("3");
  ch0.add("4"); ch0.add("5"); ch0.add("6"); -------------(2)
  add(ch0); ch0.setBounds( 50, 30, 30, 30);

  ls0.add("1"); ls0.add("2"); ls0.add("3");
  ls0.add("4"); ls0.add("5"); ls0.add("6");
  ls0.add("7"); ls0.add("8"); ls0.add("9"); ------------(3)
  add(ls0); ls0.setBounds( 10, 30, 30, 160);

  b0.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent evt){
    if(cb0.getState()){          ------------(4)
     t0.setText(ls0.getSelectedItem() + "年連続" + 
     ch0.getSelectedItem() + "回目の出場");
     l0.setText(ls0.getSelectedItem() + "年連続" + 
     ch0.getSelectedItem() + "回目の出場");
     ta0.setText(ls0.getSelectedItem() + "年連続" + 
     ch0.getSelectedItem() + "回目の出場");
    } else {
     t0.setText(ls0.getSelectedItem() + "年ぶり" +
     ch0.getSelectedItem() + "回目の出場");
     l0.setText(ls0.getSelectedItem() + "年ぶり" + 
     ch0.getSelectedItem() + "回目の出場");
     ta0.setText(ls0.getSelectedItem() + "年ぶり" + 
     ch0.getSelectedItem() + "回目の出場");
    }
   }
  }); 
 }

 public static void main(String argv[]){
  Frame win = new GUId();
  win.setSize(400, 320);
  win.setVisible(true);
 }
}


実行結果
rep7_2

考察



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

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

  public class rep7_3 extends Frame{
    Button b0 = new Button("摂氏から華氏へ");
    Button b1 = new Button("華氏から摂氏へ");
    Label x0 = new Label("変換ボタンを押してください。");
    Label x1 = new Label("変換後");
    TextField t0 = new TextField();
    TextField t1 = new TextField();

  public rep7_3(){
    setLayout(null);
     add(x0); x0.setBounds(50, 40, 150, 20);
     add(x1); x1.setBounds(165, 60, 60, 20);
     add(t0); t0.setBounds(50, 80, 60, 20);
     add(t1); t1.setBounds(150, 80, 60, 20);
     add(b0); b0.setBounds(70, 120, 100, 20);
     add(b1); b1.setBounds(70, 150, 100, 20);

    b0.addActionListener(new ActionListener() {
     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 rep7_3();
     win.setSize(300, 200); win.setVisible(true);
     win.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent evt) {
             System.exit(0);
         }
      });
   }
}
 


実行結果
rep7_3

考察



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

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

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


実行結果
rep7_4_1 rep7_4_2 rep7_4_3 rep7_4_4

考察


感想