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

I java A

N/A
N/A
Protected

Academic year: 2021

シェア "I java A"

Copied!
13
0
0

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

全文

(1)

情報工学実験

I

java

によるオブジェクト指向プログラミング(赤嶺)

学籍番号:065762A

氏名:与儀涼子

(2)

目 次

1 Level 1 3 1.1 Kouzaクラス . . . . 3 1.2 Kouzaクラス . . . . 4 1.3 実行結果 . . . . 4 2 Level 2 4 2.1 Kouzaクラス . . . . 5 2.2 Testクラス . . . . 5 2.3 実行結果 . . . . 5 3 Level 3 5 3.1 Testクラス . . . . 5 3.2 Shapeクラス . . . . 6 3.3 Triangleクラス . . . . 6 3.4 Circleクラス . . . . 6 3.5 実行結果 . . . . 7 4 Level 4 7 4.1 Squareクラス . . . . 7 4.2 Testクラス . . . . 7 4.3 実行結果 . . . . 7 5 Level 5 8 5.1 CaBaseクラス . . . . 8 5.2 CaTesterクラス . . . . 10 6 Level 5-2 11 6.1 CaLeftクラス . . . . 11 6.2 CaTesterクラス . . . . 11 6.3 実行結果 . . . . 11 7 Level 6 12 7.1 CaTesterクラス . . . . 12 7.2 ic2.txt . . . . 13 7.3 実行結果 . . . . 13

(3)

1

Level 1

実習 1 のソースコード、解説、実行結果

1.1

Kouza

クラス

class Kouza{ int number; //口座番号を格納する変数 int zandaka; //残高を格納する変数 public Kouza(int x){ //コンストラクタ number = x; //numberに引数で指定された値を格納 zandaka = 0; //zandakaに 0 を格納 }

public void azuke(int in){ //azukeメソッド

printnum(); //printnumメソッドを呼び出す

zandaka += in; //zandakaに引数の値を加算

System.out.print(in + " in\t");//引数の値を表示 }

public int hikidashi(int out){ //hikidashiメソッド

int hiki; //実際に引き出した金額を格納する変数 printnum(); //printnumメソッドを呼び出す if(zandaka>=out){ zandaka -= out; hiki = out; } else { //引き出す金額が残高より多い時 hiki=zandaka; //全額引き出す zandaka = 0; } return(hiki); //hikiを返す }

private void printnum(){//実行結果を見やすくするため、number を表示するメソッド System.out.print("Kouza" + number + ":\t");

}

public void printzan(){ //zandakaを表示するメソッド

System.out.println("zandaka:" + zandaka); }

(4)

1.2

Kouza

クラス

Testクラス

import java.io.*;

class Test{

public static void main(String args[]){ //mainメソッド

Kouza kouza1 = new Kouza(1234); //Kouzaクラスのインスタンス kouza1 を生成

//引数 1234 が number(口座番号) になる Kouza kouza2 = new Kouza(1235);

kouza1.azuke(100); //kouza1の azuke メソッドを呼び出す

kouza1.printzan(); //kouza1の printzan メソッドを呼び出す

kouza2.azuke(200); kouza2.printzan();

System.out.print(new Integer(kouza1.hikidashi(50)).toString() + " out\t");

//kouza1の hikidashi メソッドを呼び出し、返り値を表示

kouza1.printzan(); //kouza1の printzan メソッドを呼び出す

System.out.print(new Integer(kouza1.hikidashi(100)).toString() + " out\t"); kouza1.printzan();

System.out.print(new Integer(kouza2.hikidashi(3000)).toString() + " out\t"); kouza2.printzan(); }

1.3

実行結果

% java Test Kouza1234: 100 in zandaka:100 Kouza1235: 200 in zandaka:200

Kouza1234: 50 out zandaka:50

Kouza1234: 50 out zandaka:0

Kouza1235: 200 out zandaka:0

kouza1234に 100 円預け入れ。 kouza1235に 200 円預け入れ。 kouza1234から 50 円引き出し。 kouza1234から 100 円引き出そうとしたが、残高が 50 円しかないので 50 円引き出し。 kouza1235から 3000 円引き出そうとしたが、残高が 200 円しかないので 200 円引き出し。

2

Level 2

実習 2 追加したコードのみ記述する。

(5)

2.1

Kouza

クラス

  private int number; private int zandaka;

口座番号と残高を外部からアクセス禁止にした。 試しに、以下のコードを追加して Test クラスから zandaka にアクセスを試みる。

2.2

Test

クラス

kouza1.zandaka = 1000;

2.3

実行結果

% javac *.java

test.java:8: zandaka は Kouza で private アクセスされます。

kouza1.zandaka = 1000; ^ エラー 1 個 修飾子が private のデータは、外部のクラスからアクセスする事ができない。

3

Level 3

実習 3

3.1

Test

クラス

class Test{

public static void main(String args[]){ //mainメソッド

Shape shape1 = new Triangle(5); //Triangleクラスのインスタンス shape1 を生成

Shape shape2 = new Circle(4);

shape1.draw(); //shape1の draw メソッドを呼び出す

shape2.draw(); }

(6)

3.2

Shape

クラス

abstract class Shape{ //抽象クラス Shape を宣言 public Shape(){ //コンストラクタ

}

abstract public void draw(); //抽象メソッド draw を宣言 }

//抽象クラスは、内容を実装せずに、型だけが宣言されている。 //利用する時は、抽象クラスを継承したクラスに機能を実装して使う。

3.3

Triangle

クラス

import java.io.*;

class Triangle extends Shape{ //Shapeクラスを継承した Triangle クラスを定義

double mSize; //double型の変数 mSize を定義

public Triangle(int size){ //コンストラクタ mSize = size; //引数を mSize に格納

}

public void draw(){ //drawメソッドを定義

System.out.println("Drawing a triangle of size " +new Double(mSize).toString());

} }

3.4

Circle

クラス

class Circle extends Shape{ //Shapeクラスを継承した Circle クラスを定義

double mRadius; //double型の変数 mRadius を定義

public Circle(int r){ //コンストラクタ mRadius = r; //引数を mRadius に格納 }

public void draw(){ //drawメソッドを定義

System.out.println("Drawing a Circle of radius " + new Double(mRadius).toString());

} }

(7)

3.5

実行結果

% java Test

Drawing a triangle of size 5.0 Drawing a Circle of radius 4.0

4

Level 4

追加したコードのみ記述する。

4.1

Square

クラス

class Square extends Shape{ //Shapeクラスを継承したクラス Square

double mSize; //double型の変数 mSize を定義

public Square(int size){//コンストラクタの実装 mSize = size;

}

public void draw(){

System.out.println("Drawing a square of size " + new Double(mSize).toString());

} }

4.2

Test

クラス

class Test{

public static void main(String args[]){ Shape shape1 = new Triangle(5);

Shape shape2 = new Circle(4);

Shape shape3 = new Square(3); //Squareクラスのインスタンス shape3 を生成

shape1.draw(); shape2.draw();

shape3.draw(); //Shape3の draw メソッドを呼び出す

} }

4.3

実行結果

(8)

Drawing a triangle of size 5.0 Drawing a Circle of radius 4.0 Drawing a square of size 3.0

5

Level 5

実習 4 のコードいコメントを付ける

5.1

CaBase

クラス

/* 情報工学実験 1CA シミュレータ実装ファイル 2007/6/2 Y. Akamine */ import java.io.*; class CaBase { int mNumCells; //シュミレートするセル数 int[] mWriteBuf; // int[] mReadBuf;

public CaBase(int ncells) //コンストラクタ {

mNumCells = ncells;

mWriteBuf = new int[ncells]; //mWriteBufに ncells 分の領域を確保 mReadBuf = new int[ncells];

}

//初期状態をファイルから読み込む

public boolean readIC(String ic_filename) {

try{

BufferedReader reader = new BufferedReader(new FileReader(ic_filename));

//ic_filenameファイルを読み込んで reader というオブジェクトを生成 String str = reader.readLine(); //オブジェクト reader の持つ一行分の文字列を String 型変数 str に格納 int i; //文字列をそれぞれのセルの状態として格納する //指定が無い部分は 0 になる

(9)

for(i=0; i<mNumCells; ++i) { if(i < str.length() ) { if(str.charAt(i) == ’0’) writeState(i, 0); else writeState(i, 1); }else{ writeState(i, 0); } } }catch(FileNotFoundException e){ //ic_filenameのファイルが見つからなかったときの例外処理 System.err.println("ファイルが見つかりません:" + ic_filename); return false; }catch(Exception e){ //その他の例外処理 System.err.println(e); return false; } swapBuffers(); //swapBuffersメソッドを呼び出す return true; } //状態遷移を行う

public void updateCA() { int i;

//セルの状態を、i-1,i,i+1 の状態から決定 for(i=0; i<mNumCells; ++i) {

writeState(i, transFunc(readState(i-1), readState(i), readState(i+1))); }

swapBuffers(); }

//セルの状態を出力する public void printStatus() {

int i;

for(i=0; i<mNumCells; ++i) {

System.out.print(new Integer(readState(i)).toString()); }

System.out.print(’\n’); }

(10)

//状態遷移関数

//サブクラスからアクセス可能にするため、protected protected int transFunc(int l, int c, int r) {

return (l+c+r) == 1 ? 1 : 0; }

//以下は非公開関数なので、private //セルを環状に繋ぐ

private int calcPosition(int x) {

return (x+mNumCells) % mNumCells; }

//書き込みバッファmWriteBuf に状態を書き込む private void writeState(int x, int s) {

mWriteBuf[calcPosition(x)] = s; }

//読み込みバッファsReadBuf から状態を読み込む private int readState(int x)

{

return mReadBuf[calcPosition(x)]; }

//書き込みバッファと読み込みバッファを入れ替える private void swapBuffers()

{

int[] temp = mReadBuf; mReadBuf = mWriteBuf; mWriteBuf = temp; } }

5.2

CaTester

クラス

class CaTester {

public static void main(String args[]) {

CaBase ca = new CaBase(8); //セル数 8 のオブジェクト ca を生成 // CaLeft ca = new CaLeft(8); //level4-2で使う

(11)

int i;

for(i=0; i!=8; ++i) { ca.updateCA(); ca.printStatus(); } } }

6

Level 5-2

CaBaseを継承して新たなクラスを作成し、状態遷移関数を変更せよ。

6.1

CaLeft

クラス

class CaLeft extends CaBase{ //CaBaseクラスを継承した CaLeft クラス public CaLeft(int ncells){

super(ncells); //スーパークラスのコンストラクタを呼び出し }

//transFuncメソッドをオーバーライド

public int transFunc(int l, int c, int r){ return(l+c+r) == 0 ? 1 : 0; } } javaは勝手にスーパークラスの引数無しのコンストラクタを呼び出そうとするため、明示的に super(ncells)を記述する必要がある。

6.2

CaTester

クラス

//CaBase ca = new CaBase(8); CaLeft ca = new CaLeft(8);

以上を追加。

6.3

実行結果

% java CaTester 01100000 00001111 01100000 00001111 01100000

(12)

00001111 01100000 00001111

7

Level 6

level4,5の CaBase ではオブジェクトが状態を管理する事を示せ。 そのため、複数の初期状態ファイルから、それぞれのシミュレーションを個別に実行できる。(すな わち一つの初期状態にたいして一つのインスタンスを割り当てる) それがわかるような実験コードを作成せよ。(実習 1.2(その 2) を参照) それを証明するため、CaTester において、インスタンス ca の他に別の初期状態ファイルを読み込 んだ同じクラスのインスタンス ca2 を生成し、同じように処理させてみた。

7.1

CaTester

クラス

class CaTester {

public static void main(String args[]) { // CaBase ca = new CaBase(8);

CaLeft ca = new CaLeft(8);

CaLeft ca2 = new CaLeft(8); //ca2生成

ca.readIC("ic.txt");

int i;

for(i=0; i!=8; ++i) { ca.updateCA(); ca.printStatus(); }

System.out.println(’\n’); //空行

ca2.readIC("ic2.txt"); //ic2.txt読み込み

for(i=0; i!=8; ++i) { //ca2の処理 ca2.updateCA(); ca2.printStatus(); } } } また、以下の様な初期状態ファイル ic2.txt を作って ca2 の初期状態とした。

(13)

7.2

ic2.txt

00110011

7.3

実行結果

% java CaTester 01100000 00001111 01100000 00001111 01100000 00001111 01100000 00001111 00000000 11111111 00000000 11111111 00000000 11111111 00000000 11111111 2行の空白以降が ca2 の状態遷移の様子である。これより、このプログラムではオブジェクトが状 態遷移を管理していることがわかる。

参考文献

[1] 過去に作った自分の java のページ   http://www.ie.u-ryukyu.ac.jp/ e065762/ [2] java入門   http://www.nextindex.net/java/

参照

関連したドキュメント

Intervals graphs (denoted by INT ) are intersection graphs of intervals on a line, circular-arc graphs (CA ) are intersection graphs of intervals (arcs) on a circle, circle graphs (CI

What relates to Offline Turing Machines in the same way that functional programming languages relate to Turing Machines?.. Int Construction.. Understand the transition from

[3] Chari, Vyjayanthi, On the fermionic formula and the Kirillov-Reshetikhin conjecture, Int. and Yamada, Y., Remarks on fermionic formula, Contemp. and Tsuboi, Z., Paths, crystals

These manifolds have strictly negative scalar curvature and the under- lying topological 4-manifolds do not admit any Einstein metrics1. Such 4-manifolds are of particular interest

West, “Generating trees and forbidden subsequences,”

Then the strongly mixed variational-hemivariational inequality SMVHVI is strongly (resp., weakly) well posed in the generalized sense if and only if the corresponding inclusion

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,

Tsouli, Infinitely many solutions for nonlocal elliptic p-Kirchhoff type equation under Neumann boundary condition, Int. Journal