平成17年 春期 基本情報技術者 午後 問08
問08 Java次の Java プログラムの説明及びプログラムを読んで,設問1,2に答えよ。
〔プログラムの説明〕 次の図のような長方形領域内を移動する点を描画するプログラムである。
点はクラス Point で表され,点の位置を表す座標(x,y)と速さ(speed)を保持する。 速さは,正の値で単位時間に x 軸方向及び y 軸方向に移動する距離を表す。 図の長方形領域はクラス Space で与えられ,次のクラスメソッドを呼び出すことができる。 (1) public static int getMaxX() 長方形領域の x 座標の最大値(正の値)を返す。 (2) public static int getMaxY() 長方形領域の y 座標の最大値(正の値)を返す。 (3) public static void draw(Point) 引数で指定された点をその座標位置に描画する。 (4) public static void erase(Point) 引数で指定された点を消去する。 抽象クラス Motion は,点が移動する様子を示すために,コンストラクタに Point で与えられた点を初期値とし,点の描画,移動及び消去をこの順番で繰り返す。 点の移動後の座標位置は抽象メソッド update で与えられる。 Motion のサブクラス SimpleMotion は,メソッド update と main を実装する。 メソッド update は,長方形内では,一定の速さで直線運動し, 長方形の境界で反射するような点の動きを表す。 メソッド main はプログラムをテストする。 なお,メソッド main で生成される Point の座標の初期値は長方形領域内にあるものとし, 点同士の衝突は考えないものとする。 〔プログラム 1〕
public class Point {
private int x, y, speed;
public Point(int x, int y, int speed) {
this.x = x; this.y = y;
this.speed = speed;
}
public int getX() { return x; }
public int getY() { return y; }
public int getSpeed() { return speed; }
}
〔プログラム 2〕
public abstract class Motion implements Runnable {
private Point point;
public Motion(Point point) {
this.point = point;
}
public void run() {
while (true) {
Space.draw(point);
try {
Thread.sleep(40);
} catch (InterruptedException e) {}
Point current = point;
〔プログラム 3〕
public class SimpleMotion extends Motion {
private int directionX = 1, directionY = 1;
public SimpleMotion(Point point) {
super(point);
}
public Point update(Point point) {
int speed = point.getSpeed();
int x = point.getX() + directionX * speed;
int y = point.getY() + directionY * speed;
if (x <= 0) {
x = -x;
directionX *= -1;
}
x %= 2 * Space.getMaxX();
if (x >= Space.getMaxX()) {
x = 2 * Space.getMaxX() - x;
directionX *= -1;
}
if (y <= 0) {
y = -y;
directionY *= -1;
}
y %= 2 * Space.getMaxY();
if (y >= Space.getMaxY()) {
y = 2 * Space.getMaxY() - y;
directionY *= -1;
}
return new Point(x, y, speed);
}
public static void main(String[] args) {
Point[] points = {
new Point(10, 20, 3),
new Point(50, 10, 5),
new Point(150, 60, 2)
};
for (int i = 0; i < points.length; i++) {
new Thread(
設問1 プログラム中の
a に関する解答群 ア current = update(current) イ current = update(point) ウ point = update(point) エ update(current) オ update(point) b に関する解答群 ア new Motion(points[i]) イ new Point(points[i]) ウ new Runnable(points[i]) エ new SimpleMotion(points[i]) オ points[i]
設問2 クラス SimpleMotion のコンストラクタに与えられた Point が次の図の 点 P で speed の値が1のとき,メソッド run を実行したときの点 P の動きとして
正しい答えを,解答群の中から選べ。
ただし,プログラム中の
解答群 ア 矢印 @ のように進む。 イ 矢印 A のように進む。 ウ 矢印 B のように進む。 エ 矢印 C のように進む。 [←前の問題] [次の問題→] [問題一覧表] [分野別] [基本情報技術者試験TOP ]
©2004-2025 情報処理試験.jp
|
プライバシーポリシー・著作権・リンク
|
お問合わせ
|
