• 検索結果がありません。

IT プロジェクト

N/A
N/A
Protected

Academic year: 2021

シェア "IT プロジェクト"

Copied!
42
0
0

読み込み中.... (全文を見る)

全文

(1)

オブジェクト指向設計による卓球

ゲームの試作

(2)

劉 少英 情報科学部コンピュータ科学科 Email:sliu@hosei.ac.jp ホームページ: http://cis.k.hosei.ac.jp/~sliu/

(2)

講義内容

1.卓球ボールをテーブルの上に移動させる。 2.関連しているクラスにメソッドを加える。

(3)

8 9

1.卓球ボールをテーブルの上に

移動させる。

(4)
(5)
(6)
(7)
(8)
(9)
(10)

既存のクラスに適切なメソッドを加える。 卓球ボール

(11)

2.関連しているクラスにメソッドを加える

1.卓球ボールクラス:

package exe1;

import java.awt.*;

public class Ball {

private int radius; /* ボールの半径 */ private Color color; /* ボールの色 */ private int x; /* ボールのx座標 */

(12)

/* 二つのコンストラクタ */

public Ball() {

}

public Ball(int radius, Color color, int x, int y) { this.radius = radius; this.color = color; this.x = x; this.y = y; }

(13)

/*ほかのメソッド*/ /*get メソッド*/

public Color getColor() {

return this.color; }

public int getRadius() {

return this.radius; }

(14)

public int getX() {

return this.x; }

public int getY() {

return this.y; }

(15)

/*set メソッド*/

public void setRadius(int radius) {

this.radius = radius; }

public void setColor(Color color) {

this.color = color; }

(16)

public void setX(int x) {

this.x = x; }

public void setY(int y) {

this.y = y; }

(17)

public void move(int x1, int y1, int x2, int

y2) {

卓球ボールを(x1, y1)の点から(x2, y2)の点へ 移動させる。

(18)

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.*;

(19)

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 */

(20)

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);

(21)

(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(); }

(22)

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) { } } }

(23)
(24)

3. プログラムの文書化技術

システム要求仕様 作成されたプログラム 変換 静的な コード検査 (1)データ変数の役割を文書化 (2)命令文の機能を文書化 (3)クラスのメソッドの機能の文書化 コードの読み 正しいか? (自己確認)

(25)

3.1

データ変数の役割を文書化

(1) 変数の名前の解釈 (2) 変数を使う目的 (3) 変数の型を選択理由 (4) 変数の初期値が変数の宣言のところに定義さ れていない場合、その変数の初期値を定義。 (5) 変数の不変性質 (6) 配列型の変数の最大要素数とその意味

(26)

(1)変数の名前の解釈

事例:

(27)

(2) 変数を使う目的

事例:

String stu_no; //stu_no = student_number //学生番号を表示

(28)

(3)変数の型を選択理由

事例:

String stu_no; //stu_no = student_number //学生番号を表示

(29)

(4)変数の初期値が変数の宣言のとこ

ろに定義されていない場合、その変数の

初期値を定義

事例:

String stu_no; //stu_no = student_number //学生番号を表示

//学生番号は、数字と文字の並び //初期値は、空文字列””

(30)

(5)変数の不変性質

事例:

String stu_no; //stu_no = student_number //学生番号を表示

//学生番号は、数字と文字の並び //初期値は、空文字列””

(31)

(6)

配列型の変数の最大要素数と

その意味

事例:

int groupA[]; //groupAの最大要素数は、30。 //30は、groupAが表した学生

(32)

3.2 命令文の機能を文書化

(1)命令文の目的 (2)命令文の機能

(3)条件文の条件の意味

(33)

(1)命令文の目的

事例:

average = total_score / number;

(34)

(2)命令文の機能

事例:

average = total_score / number;

// クラスの平均成績は、クラスの総成績

//total_scoreをクラスの人数numberで割って

(35)

(3)条件文の条件の意味

事例: if (stu.score >= 60) qualified = qualified + 1; //条件:学生stuの成績scoreは、60点以上である。 //機能:条件が満たせば、合格人数qualifiedに1を //増加する。

(36)

(4)while文の継続変数

事例:

while (number < class.total) { if (stu.score >= 60) qualified = qualified + 1; number++; } //numberは、継続変数である。 //条件:学生の人数は、クラスの総人数より小さい //機能:継続条件が満たせば、学生人数numberに1を増加 //する。もし学生stuの成績scoreは60点以上であれば、合格 //学生数qualifiedに1を増加する。

(37)

3.3 クラスのメソッドの機能の文書化

クラスのメソッドは、次の通りである。 返す値の型 メソッド名(parameters) { アルゴリズム }

(38)

事例:

double Average(int total_score, int number) {

return total_score / number; }

(39)

事例:

//機能:学生の総成績total_scoreの平均値を

//求める。

double Average(int total_score, int number)

// 事前条件:numberは、0でない。

{

return total_score / number; }

(40)

事例: //機能:整数xの平方根をインスタンス変数 total //に加える。 double Add_Sqrt(int x) // 事前条件:xは、正整数または0である。 {

return Math.sqrt(x) + total; }

(41)

事例:

int Search(int j, int a[]) { int i = 0; while (i < a.length) { if (a[i] = j) return i; } return -1; }

(42)

事例:

//機能:入力整数 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; }

参照

関連したドキュメント

自動運転ユニット リーダー:菅沼 直樹  准教授 市 街 地での自動 運 転が可 能な,高度な運転知能を持 つ自動 運 転自動 車を開 発

WAV/AIFF ファイルから BR シリーズのデータへの変換(Import)において、サンプリング周波 数が 44.1kHz 以外の WAV ファイルが選択されました。.

剣道部 柔道部 硬式野球部 卓球部 水泳部 ラグビー部 ソフトテニス部 テニス部 ハンドボール部 サッカー部 バドミントン部

Robust families of exponential attractors (that is, both upper- and lower-semicontinuous with explicit control over semidistances in terms of the perturbation parameter) of the

In this diagram, there are the following objects: myFrame of the Frame class, myVal of the Validator class, factory of the VerifierFactory class, out of the PrintStream class,

In what follows, everything is stated in terms of color digraphs; color graphs can be modelled as color digraphs by replacing each edge of color k by a digon (arcs in both

The existence of a global attractor and its properties In this section we finally prove Theorem 1.6 on the existence of a global attractor, which will be denoted by A , for

If the above mentioned goods, exempted from customs duty and internal tax, are offered for use other than the personal use of yourself or your family, within 2 years after the