オブジェクト指向設計による卓球
ゲームの試作
(2)
劉 少英 情報科学部コンピュータ科学科 Email:sliu@hosei.ac.jp ホームページ: http://cis.k.hosei.ac.jp/~sliu/講義内容
1.卓球ボールをテーブルの上に移動させる。 2.関連しているクラスにメソッドを加える。
8 9
1.卓球ボールをテーブルの上に
移動させる。
既存のクラスに適切なメソッドを加える。 卓球ボール
2.関連しているクラスにメソッドを加える
1.卓球ボールクラス:
package exe1;
import java.awt.*;
public class Ball {
private int radius; /* ボールの半径 */ private Color color; /* ボールの色 */ private int x; /* ボールのx座標 */
/* 二つのコンストラクタ */
public Ball() {
}
public Ball(int radius, Color color, int x, int y) { this.radius = radius; this.color = color; this.x = x; this.y = y; }
/*ほかのメソッド*/ /*get メソッド*/
public Color getColor() {
return this.color; }
public int getRadius() {
return this.radius; }
public int getX() {
return this.x; }
public int getY() {
return this.y; }
/*set メソッド*/
public void setRadius(int radius) {
this.radius = radius; }
public void setColor(Color color) {
this.color = color; }
public void setX(int x) {
this.x = x; }
public void setY(int y) {
this.y = y; }
public void move(int x1, int y1, int x2, int
y2) {
卓球ボールを(x1, y1)の点から(x2, y2)の点へ 移動させる。
5. 卓球ゲームクラス package exe1; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.image.BufferedImage; import javax.swing.*;
public class Game extends JFrame {
private static final long serialVersionUID = 1L;
private Ball myBall; /* the ball instance in the frame */
private Table myTable; /* the table instance in the frame */
private ScoreDisplay myScoreDisplay = new ScoreDisplay();
private MainCanvas myCanvas; /* the canvas instance in the frame */
public Game() { (1)全てのオブジェクトの描画のために必要な データを入力する; 例えば、 String input1 = JOptionPane.showInputDialog(this, "please set the radius of the ball:");
int ballRadius = Integer.parseInt(input1); (2)必要なオブジェクトを生成する;
例えば、
myBall = new Ball(ballRadius, ballColor, ballX, ballY);
(3)全てのオブジェクトをカンバスに描画する. import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.image.BufferedImage; new Thread(this).start(); }
public void run() { while (true) {
SwingUtilities.invokeLater(new Runnable() { public void run() {
myCanvas.repaint();
} });
myBall.move(x1, y1, x2, y2);
try { Thread.sleep(100); } catch (InterruptedException e) { } } }
3. プログラムの文書化技術
システム要求仕様 作成されたプログラム 変換 静的な コード検査 (1)データ変数の役割を文書化 (2)命令文の機能を文書化 (3)クラスのメソッドの機能の文書化 コードの読み 正しいか? (自己確認)3.1
データ変数の役割を文書化
(1) 変数の名前の解釈 (2) 変数を使う目的 (3) 変数の型を選択理由 (4) 変数の初期値が変数の宣言のところに定義さ れていない場合、その変数の初期値を定義。 (5) 変数の不変性質 (6) 配列型の変数の最大要素数とその意味(1)変数の名前の解釈
事例:
(2) 変数を使う目的
事例:
String stu_no; //stu_no = student_number //学生番号を表示
(3)変数の型を選択理由
事例:
String stu_no; //stu_no = student_number //学生番号を表示
(4)変数の初期値が変数の宣言のとこ
ろに定義されていない場合、その変数の
初期値を定義
事例:
String stu_no; //stu_no = student_number //学生番号を表示
//学生番号は、数字と文字の並び //初期値は、空文字列””
(5)変数の不変性質
事例:
String stu_no; //stu_no = student_number //学生番号を表示
//学生番号は、数字と文字の並び //初期値は、空文字列””
(6)
配列型の変数の最大要素数と
その意味
事例:
int groupA[]; //groupAの最大要素数は、30。 //30は、groupAが表した学生
3.2 命令文の機能を文書化
(1)命令文の目的 (2)命令文の機能
(3)条件文の条件の意味
(1)命令文の目的
事例:
average = total_score / number;
(2)命令文の機能
事例:
average = total_score / number;
// クラスの平均成績は、クラスの総成績
//total_scoreをクラスの人数numberで割って
(3)条件文の条件の意味
事例: if (stu.score >= 60) qualified = qualified + 1; //条件:学生stuの成績scoreは、60点以上である。 //機能:条件が満たせば、合格人数qualifiedに1を //増加する。(4)while文の継続変数
事例:
while (number < class.total) { if (stu.score >= 60) qualified = qualified + 1; number++; } //numberは、継続変数である。 //条件:学生の人数は、クラスの総人数より小さい //機能:継続条件が満たせば、学生人数numberに1を増加 //する。もし学生stuの成績scoreは60点以上であれば、合格 //学生数qualifiedに1を増加する。
3.3 クラスのメソッドの機能の文書化
クラスのメソッドは、次の通りである。 返す値の型 メソッド名(parameters) { アルゴリズム }事例:
double Average(int total_score, int number) {
return total_score / number; }
事例:
//機能:学生の総成績total_scoreの平均値を
//求める。
double Average(int total_score, int number)
// 事前条件:numberは、0でない。
{
return total_score / number; }
事例: //機能:整数xの平方根をインスタンス変数 total //に加える。 double Add_Sqrt(int x) // 事前条件:xは、正整数または0である。 {
return Math.sqrt(x) + total; }
事例:
int Search(int j, int a[]) { int i = 0; while (i < a.length) { if (a[i] = j) return i; } return -1; }
事例:
//機能:入力整数 jは、入力配列aにあるかどうかを
//検索する。
int Search(int j, int a[])
//事前条件: true (つまり、j と aに付加制限がない) { int i = 0; while (i < a.length) { if (a[i] = j) return i; } return -1; }