Report#7

戻る

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


No.1
01. import java.awt.*; 02. import java.awt.event.*; 03. public class GUIaa extends Frame{ 04. Button b0 = new Button("Even/Odd?"); 05. Label x0 = new Label("Type a number and press..."); 06. TextField t0 = new TextField(); 07. public GUIaa() { 08. setLayout(null); 09. add(t0); t0.setBounds(10, 40, 90, 30); 10. add(b0); b0.setBounds(110, 40, 80, 30); 11. add(x0); x0.setBounds(10, 80, 180, 30); 12. b0.addActionListener(new ActionListener() { 13. public void actionPerformed(ActionEvent evt) { 14. int i = (new Integer(t0.getText())).intValue(); 15. t0.setText(""); 16. if(i % 2 == 0) { 17. x0.setText(i + " is Even"); 18. } else { 19. x0.setText(i + " is Odd"); 20. } 21. } 22. }); 23. } 24. public static void main(String[] args) { 25. Frame win = new GUIaa(); 26. win.setSize(200, 150); 27. win.setVisible(true); 28. win.addWindowListener(new WindowAdapter() { 29. public void windowClosing(WindowEvent evt) { 30. System.exit(0); 31. } 32. }); 33. } 34. }
 4行目はb0という名のボタンを作り、そのボタンに"Even/Odd?"という文字列を設定する。  5行目はx0という名のLabelを定義し、"Type a number and press..."という文字列を設定する。  6行目はt0という名のTextFieldそ定義する。  9~11行目は、定義したもの開始位置と、長さ、幅を決める。  12行目~22行目は、ボタンを押した時の処理である。 No.2
01. import java.awt.*; 02. import java.awt.event.*; 03. 04. public class ColorRGBa extends Frame { 05. Button b0 = new Button("Display"); 06. Label[] la = new Label[]{new Label ("Red"), 07. new Label ("Green"), 08. new Label ("Blue") }; 09. TextField[] ta = new TextField[]{new TextField(), 10. new TextField(), 11. new TextField()}; 12. Label x0 = new Label("Input RGB values [0..255]"); 13. 14. public ColorRGBa() { 15. setLayout(null); 16. for(int i = 0; i < la.length; i++) { 17. add(la[i]); la[i].setBounds(10, 40 + i*40, 60, 30); 18. add(ta[i]); ta[i].setBounds(80, 40 + i*40, 60, 30); 19. } 20. add(b0); b0.setBounds(10, 160, 60, 30); 21. add(x0); x0.setBounds(10, 200, 180, 30); 22. b0.addActionListener(new ActionListener() { 23. public void actionPerformed(ActionEvent evt) { 24. try { 25. x0.setBackground(new Color( 26. (new Integer(ta[0].getText())).intValue(), 27. (new Integer(ta[1].getText())).intValue(), 28. (new Integer(ta[2].getText())).intValue())); 29. } catch(Exception ex) { x0.setText(ex.toString()); } 30. } 31. }); 32. } 33. public static void main(String[] args) { 34. Frame win = new ColorRGBa(); 35. win.setSize(200, 250); win.setVisible(true); 36. win.addWindowListener(new WindowAdapter() { 37. public void windowClosing(WindowEvent evt) { 38. System.exit(0); 39. } 40. }); 41. } 42. }
 25行目~28行目を実行しようとしてエラーが起こったら、29行目を実行する。 No.3
01. import java.awt.*; 02. import java.awt.event.*; 03. 04. public class original extends Frame { 05. 06. Button a0 = new Button("Reset"); 07. Button a1 = new Button("確認"); 08. Label b0 = new Label("name(数字以外)"); 09. Label b1 = new Label("sex"); 10. Label b2 = new Label("OS"); 11. Label b3 = new Label("desk or lap"); 12. Label b4 = new Label(""); 13. TextField c0 = new TextField(); 14. List d0 = new List(); 15. Choice e0 = new Choice(); 16. Checkbox f0 = new Checkbox("male"); 17. Checkbox f1 = new Checkbox("female"); 18. TextArea g0 = new TextArea("Other and Plural write OS"); 19. 20. public original(){ 21. setLayout(null); 22. add(b0); b0.setBounds(10,40,80,30); 23. add(b1); b1.setBounds(10,75,80,30); 24. add(b2); b2.setBounds(10,110,80,30); 25. add(b3); b3.setBounds(10,185,80,30); 26. add(c0); c0.setBounds(110,40,190,30); 27. add(f0); f0.setBounds(110,75,80,30); 28. add(f1); f1.setBounds(195,75,80,30); 29. add(e0); e0.setBounds(110,110,42,30); 30. e0.add("Windows"); e0.add("Macintosh"); e0.add("Other"); e0.add("Plural"); 31. add(g0); g0.setBounds(220,110,200,70); 32. add(d0); d0.setBounds(110,185,82,60); 33. d0.add("desktop"); d0.add("laptop"); d0.add("desk and lap"); 34. add(a0); a0.setBounds(10,244,80,30); 35. add(a1); a1.setBounds(95,244,80,30); 36. add(b4); b4.setBounds(10,275,190,30); 37. 38. a0.addActionListener(new ActionListener() { 39. public void actionPerformed(ActionEvent evt){ 40. c0.setText(""); 41. g0.setText("Other and Plural write OS"); 42. f0.setState(false); 43. f1.setState(false); 44. } 45. }); 46. a1.addActionListener(new ActionListener() { 47. public void actionPerformed(ActionEvent evt){ 48. try{ 49. (new Integer(c0.getText())).intValue(); 50. b4.setText("間違いが有ります"); 51. } catch(Exception ex) {b4.setText("確認しました");} 52. } 53. }); 54. } 55. 56. public static void main(String[] args) { 57. Frame win = new original(); 58. win.setSize(430,300); win.setVisible(true); 59. win.addWindowListener(new WindowAdapter() { 60. public void windowClosing(WindowEvent evt) { 61. System.exit(0); 62. } 63. }); 64. } 65. }
 06行目~18行目では、Button、Label、TextField、Checkbox、Choice、TextArea、Listを定義している。  46行目~53行目は例外処理であるが、間違った使い方をしている。本来なら、try{}に目的の動作を入れるが、このプログラムでは、catch(){}に入れてい
る。数字とそれ以外を分けるとき、try{}に入力されたものが数字だった時の動作を入れる方がやりやすいから、私は敢えてこのようにした。 No.4
01. import java.awt.*; 02. import java.awt.event.*; 03. 04. public class kanzan extends Frame { 05. TextField a0 = new TextField(); 06. TextField a1 = new TextField(); 07. Button b0 = new Button("->"); 08. Button b1 = new Button("->"); 09. Label c0 = new Label("摂氏"); 10. Label c1 = new Label("華氏"); 11. Label c2 = new Label("華氏"); 12. Label c3 = new Label("摂氏"); 13. Label c4 = new Label(""); 14. Label c5 = new Label(""); 15. 16. public kanzan() { 17. setLayout(null); 18. add(a0); a0.setBounds(75,40,60,30); 19. add(a1); a1.setBounds(75,85,60,30); 20. add(b0); b0.setBounds(140,40,60,30); 21. add(b1); b1.setBounds(140,85,60,30); 22. add(c0); c0.setBounds(10,40,60,30); 23. add(c1); c1.setBounds(10,85,60,30); 24. add(c2); c2.setBounds(205,40,60,30); 25. add(c3); c3.setBounds(205,85,60,30); 26. add(c4); c4.setBounds(270,40,60,30); 27. add(c5); c5.setBounds(270,85,60,30); 28. b0.addActionListener(new ActionListener() { 29. public void actionPerformed(ActionEvent evt) { 30. int x = (new Integer(a0.getText())).intValue(); 31. a0.setText(""); 32. int y = x * 9 / 5 + 32; 33. c4.setText(new Integer (y).toString()); 34. } 35. }); 36. b1.addActionListener(new ActionListener() { 37. public void actionPerformed(ActionEvent evt) { 38. int n = (new Integer(a1.getText())).intValue(); 39. a1.setText(""); 40. int m = (n - 32)* 5 / 9; 41. c5.setText(new Integer (m).toString()); 42. } 43. }); 44. } 45. 46. public static void main(String[] args) { 47. Frame win = new kanzan(); 48. win.setSize(300,150); win.setVisible(true); 49. win.addWindowListener(new WindowAdapter() { 50. public void windowClosing(WindowEvent evt) { 51. System.exit(0); 52. } 53. }); 54. } 55. }
 28行目~35行目が、摂氏から華氏に換算する処理で、36行目~43行目が、華氏から摂氏に換算する処理である。  33行目と41行目は、数字を文字列として扱い、それをLabelに設定する処理である。 No.5
01. import java.awt.*; 02. import java.awt.event.*; 03. 04. public class denntaku extends Frame { 05. 06. Button a0 = new Button("+"); 07. Button b0 = new Button("-"); 08. Button c0 = new Button("*"); 09. Button d0 = new Button("/"); 10. TextField e0 = new TextField(); 11. TextField e1 = new TextField(); 12. Label f0 = new Label("input integer"); 13. Button g0 = new Button("Reset"); 14. 15. public denntaku() { 16. setLayout(null); 17. add(e0); e0.setBounds(60,87,80,30); 18. add(a0); a0.setBounds(150,30,40,30); 19. add(b0); b0.setBounds(150,70,40,30); 20. add(c0); c0.setBounds(150,110,40,30); 21. add(d0); d0.setBounds(150,150,40,30); 22. add(e1); e1.setBounds(200,87,80,30); 23. add(f0); f0.setBounds(60,180,80,30); 24. add(g0); g0.setBounds(60,220,80,30); 25. a0.addActionListener(new ActionListener() { 26. public void actionPerformed(ActionEvent evt) { 27. int i = (new Integer(e0.getText())).intValue(); 28. int j = (new Integer(e1.getText())).intValue(); 29. int k = i+j; 30. f0.setText("answer is "+ new Integer (k).toString()); 31. } 32. }); 33. b0.addActionListener(new ActionListener() { 34. public void actionPerformed(ActionEvent evt) { 35. int i = (new Integer(e0.getText())).intValue(); 36. int j = (new Integer(e1.getText())).intValue(); 37. int k = i-j; 38. f0.setText("answer is "+ new Integer (k).toString()); 39. } 40. }); 41. c0.addActionListener(new ActionListener() { 42. public void actionPerformed(ActionEvent evt) { 43. int i = (new Integer(e0.getText())).intValue(); 44. int j = (new Integer(e1.getText())).intValue(); 45. int k = i*j; 46. f0.setText("answer is "+ new Integer (k).toString()); 47. } 48. }); 49. d0.addActionListener(new ActionListener() { 50. public void actionPerformed(ActionEvent evt) { 51. int i = (new Integer(e0.getText())).intValue(); 52. int j = (new Integer(e1.getText())).intValue(); 53. int k = i/j; 54. f0.setText("answer is "+ new Integer (k).toString()); 55. } 56. }); 57. g0.addActionListener(new ActionListener() { 58. public void actionPerformed(ActionEvent evt) { 59. e0.setText(""); 60. e1.setText(""); 61. f0.setText("input integer"); 62. } 63. }); 64. } 65. public static void main(String[] args) { 66. Frame win = new denntaku(); 67. win.setSize(340, 260); win.setVisible(true); 68. win.addWindowListener(new WindowAdapter() { 69. public void windowClosing(WindowEvent evt) { 70. System.exit(0); 71. } 72. }); 73. } 74. }
 06行目~09行目は、四則演算のボタンを定義している。  10、11行目はTextFieldを、12行目はLabelを定義している。  13行目は、Resetボタンを定義している。  30、38、46、54行目は、int型の変数kをintegerによってカプセル化し、それをさらにStringに直してf0に表示する。  57行目~64行目は、Resetボタンを押した時の動作である。  59、60行目はTextFieldを初期化している。  61行目は、Labelのテキストを"inpout integer"という文字列に設定する。