fork(8) download
  1. /*問題1:ChildAP01.java をコンパイルしたときエラーとなるのはどのコードか確かめなさい.ただし,エラーとなるコードは5 カ所ある.また,Base02.java およびChildAP01.java をBase03.java およびChildAP03.java と名前をつけ直した上で,正しく動作するよう書き直しなさい.
  2.  
  3. ChildAP01.java(これはコンパイルエラーが出ます)*/
  4.  
  5. package p052;
  6. import p051.*;
  7. class ChildAP01 extends Base02{
  8. //コンストラクタ
  9. ChildAP01(){
  10. this.w = 111;
  11. this.x = 222;
  12. this.y = 333;
  13. this.z = 444;
  14. }
  15. public static void main(String [] args){
  16. ChildAP01 cs = new ChildAP01();
  17. System.out.println("w = "+cs.w);
  18. System.out.println("x = "+cs.x);
  19. System.out.println("z = "+cs.y);
  20. System.out.println("z = "+cs.z);
  21. }
  22. }
  23.  
  24. //---------------------------------------
  25. //Base02.java
  26.  
  27. package p051;
  28. public class Base02{
  29. public int w;
  30. protected int x;
  31. int y;
  32. private int z;
  33. //setter メソッド(private 変数z に値を他クラスから代入する)
  34. public void setZvalue(int z){
  35. this.z = z;
  36. }
  37. //(private 変数z の値を他クラスから取りだす)getter メソッド
  38. public int getZvalue(){
  39. return this.z;
  40. }
  41. //コンストラクタ
  42. Base02( ){
  43. this.w = 10;
  44. this.x = 20;
  45. this.y = 30;
  46. this.z = 40;
  47. }
  48. }
  49.  
  50.  
  51. /*----------------------------------------
  52. 問題2
  53. 1. BallPropagator.java に,コンストラクタを書き加えなさい.
  54. 2. BallPropPanel.java にコードを加筆して,ボールに色をつけなさい.
  55. 3. BallPropPanel.java にコードを加筆して,反射するボールを2 つに増やしなさい.
  56. 4. BallPropagator.java に,ボールの速度を更新するためのメソッドを書き加えなさい.ただ
  57. し,加速度はどちらのボールもax = 0,ay = 9.8 とせよ.
  58. 5. BallPropagator.java を書き換え,ボールが壁に非弾性衝突するようにせよ.ただし,反発
  59. 係数
  60.  = 0.8(gamma) を用いること.
  61. 6. BallPropagator クラスのインスタンスフィールドr, x, y, vx, vy, ax, ay にはアクセス
  62. 修飾子が付いていない.ここにprivate 修飾子を加えた上で,プログラムが動作するよう
  63. BallPropagator.java およびBallPropPanel.java を書き換えなさい.(アクセサメソッドを
  64. 追加する.)
  65.  
  66. ----
  67. BallPropPanel.java*/
  68.  
  69. import java.awt.*;
  70. import java.awt.event.*;
  71. import javax.swing.*;
  72. public class BallPropPanel extends JPanel implements ActionListener{
  73. //描画パネルのサイズ
  74. private int xPanelSize;
  75. private int yPanelSize;
  76. //BallPropagator クラスのインスタンス
  77. BallPropagator b1;
  78. //ボールの色
  79. Color c1;
  80. //コンストラクタ
  81. BallPropPanel(int xPanelSize, int yPanelSize){
  82. //描画パネルのサイズ
  83. this.xPanelSize = xPanelSize;
  84. this.yPanelSize = yPanelSize;
  85. //BallPropagator クラスのインスタンスの生成(ボールの生成)
  86. //ボールb1 の初期状態(ボールの半径,位置,速度,加速度)
  87. b1 = new BallPropagator(10, 100, 100, 100, -50, 0, 0);
  88. /////////////////////////////////////////////
  89. //ここでボールの色を決める(RGB カラーの生成)
  90. /////////////////////////////////////////////
  91. }
  92. //イベント(タイマーによる呼び出し)が発生したときの処理
  93. public void actionPerformed(ActionEvent e){
  94. //ボールb1 の位置の更新
  95. b1.xUpdate();
  96. b1.yUpdate();
  97. //ボールb1 の速度の更新
  98. b1.vxUpdate();
  99. b1.vyUpdate();
  100. // 再描画
  101. repaint();
  102. }
  103.  
  104. // 描画するときの処理
  105. public void paintComponent(Graphics g){
  106. super.paintComponent(g);
  107. g.fillOval((int)(b1.x-b1.r), (int)(b1.y-b1.r),
  108. (int)(2*b1.r), (int)(2*b1.r));
  109. }
  110. // メイン・メソッド
  111. public static void main(String[] args) {
  112. //微小時間間隔
  113. BallPropagator.dt=0.1;
  114. // フレームを生成する
  115. JFrame frame = new JFrame();
  116. // パネルを生成する
  117. BallPropPanel panel = new BallPropPanel(500,400);
  118. panel.setBackground(Color.white);
  119. panel.setPreferredSize(
  120. new Dimension(panel.xPanelSize, panel.yPanelSize));
  121. //ボールの動く範囲を決定する(境界条件)
  122. BallPropagator.xmin = 0;
  123. BallPropagator.xmax = panel.xPanelSize;
  124. BallPropagator.ymin = 0;
  125. BallPropagator.ymax = panel.yPanelSize;BallPropagator.java
  126. public class BallPropagator{
  127. public static double dt;//プロパゲータの微小時間間隔
  128. public static int xmin; //境界座標
  129. public static int xmax;
  130. public static int ymin;
  131. public static int ymax;
  132. public static final double gamma = 0.8;//反発係数
  133. double r; //ボールの半径
  134. double x, y; //ボールの位置(中心座標)
  135. double vx, vy;//ボールの速度
  136. double ax, ay;//ボールの加速度
  137. ///////////////////////////////////////////////////////////
  138. //
  139. // ここにBallPropagator クラスのコンストラクタを書きなさい
  140. // ( r, x, y, vx, vy, ax, ay を初期化する)
  141. //
  142. ///////////////////////////////////////////////////////////
  143. //x 軸方向の位置の更新
  144. public void xUpdate() {
  145. this.x = this.x + this.vx * dt;
  146. //x 方向の反射(cf. 反発係数)
  147. if(this.x-this.r < (double)xmin ){
  148. this.vx *= -1;
  149. this.x = (double)xmin+this.r;
  150. }else if((double)xmax < this.x+this.r ){
  151. this.vx *= -1;
  152. this.x = (double)xmax-this.r;
  153. }
  154. }
  155. // フレームを設定する
  156. frame.add(panel);
  157. frame.pack();
  158. frame.setTitle("反射するボール");
  159. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  160. frame.setResizable(false);
  161. frame.setVisible(true);
  162.  
  163. // イベントを定期的に発生させるためのタイマー
  164. Timer timer = new Timer(10, panel);
  165. timer.start();
  166. }
  167. }
  168.  
  169. /*------------------------------
  170. BallPropagator.java*/
  171.  
  172. public class BallPropagator{
  173. public static double dt;//プロパゲータの微小時間間隔
  174. public static int xmin; //境界座標
  175. public static int xmax;
  176. public static int ymin;
  177. public static int ymax;
  178. public static final double gamma = 0.8;//反発係数
  179. double r; //ボールの半径
  180. double x, y; //ボールの位置(中心座標)
  181. double vx, vy;//ボールの速度
  182. double ax, ay;//ボールの加速度
  183. ///////////////////////////////////////////////////////////
  184. //
  185. // ここにBallPropagator クラスのコンストラクタを書きなさい
  186. // ( r, x, y, vx, vy, ax, ay を初期化する)
  187. //
  188. ///////////////////////////////////////////////////////////
  189. //x 軸方向の位置の更新
  190. public void xUpdate() {
  191. this.x = this.x + this.vx * dt;
  192. //x 方向の反射(cf. 反発係数)
  193. if(this.x-this.r < (double)xmin ){
  194. this.vx *= -1;
  195. this.x = (double)xmin+this.r;
  196. }else if((double)xmax < this.x+this.r ){
  197. this.vx *= -1;
  198. this.x = (double)xmax-this.r;
  199. }
  200. }
  201.  
  202. //y 軸方向の位置の更新
  203. public void yUpdate() {
  204. this.y = this.y + this.vy * dt;
  205. //y 方向の反射(cf. 反発係数)
  206. if(this.y-this.r < (double)ymin ){
  207. this.vy *= -1;
  208. this.y = (double)ymin+this.r;
  209. }else if((double)ymax < this.y+this.r ){
  210. this.vy *= -1;
  211. this.y = (double)ymax-this.r;
  212. }
  213. }
  214.  
  215. ///////////////////////////////////////////////////////////
  216. //
  217. // ここにx 軸方向の速度を更新するためのメソッドを書きなさい
  218. //
  219. ///////////////////////////////////////////////////////////
  220. ///////////////////////////////////////////////////////////
  221. //
  222. // ここにy 軸方向の速度を更新するためのメソッドを書きなさい
  223. //
  224. ///////////////////////////////////////////////////////////
  225.  
  226. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:27: class, interface, or enum expected
package p051;
^
Main.java:69: class, interface, or enum expected
import java.awt.*;
^
Main.java:70: class, interface, or enum expected
import java.awt.event.*;
^
Main.java:71: class, interface, or enum expected
import javax.swing.*;
^
Main.java:125: not a statement
BallPropagator.ymax = panel.yPanelSize;BallPropagator.java
                                                     ^
Main.java:125: ';' expected
BallPropagator.ymax = panel.yPanelSize;BallPropagator.java
                                                          ^
Main.java:126: illegal start of expression
public class BallPropagator{
^
Main.java:126: ';' expected
public class BallPropagator{
      ^
Main.java:126: not a statement
public class BallPropagator{
             ^
Main.java:126: ';' expected
public class BallPropagator{
                           ^
Main.java:127: illegal start of expression
public static double dt;//?????????????
^
Main.java:127: illegal start of expression
public static double dt;//?????????????
       ^
Main.java:127: ';' expected
public static double dt;//?????????????
             ^
Main.java:127: not a statement
public static double dt;//?????????????
                     ^
Main.java:128: illegal start of expression
public static int xmin; //????
^
Main.java:128: illegal start of expression
public static int xmin; //????
       ^
Main.java:128: ';' expected
public static int xmin; //????
             ^
Main.java:128: not a statement
public static int xmin; //????
                  ^
Main.java:129: illegal start of expression
public static int xmax;
^
Main.java:129: illegal start of expression
public static int xmax;
       ^
Main.java:129: ';' expected
public static int xmax;
             ^
Main.java:129: not a statement
public static int xmax;
                  ^
Main.java:130: illegal start of expression
public static int ymin;
^
Main.java:130: illegal start of expression
public static int ymin;
       ^
Main.java:130: ';' expected
public static int ymin;
             ^
Main.java:130: not a statement
public static int ymin;
                  ^
Main.java:131: illegal start of expression
public static int ymax;
^
Main.java:131: illegal start of expression
public static int ymax;
       ^
Main.java:131: ';' expected
public static int ymax;
             ^
Main.java:131: not a statement
public static int ymax;
                  ^
Main.java:132: illegal start of expression
public static final double gamma = 0.8;//????
^
Main.java:132: illegal start of expression
public static final double gamma = 0.8;//????
       ^
Main.java:132: ';' expected
public static final double gamma = 0.8;//????
             ^
Main.java:144: illegal start of expression
public void xUpdate() {
^
Main.java:144: illegal start of expression
public void xUpdate() {
       ^
Main.java:144: ';' expected
public void xUpdate() {
                   ^
Main.java:226: reached end of file while parsing
}
 ^
37 errors
stdout
Standard output is empty