e055763
安富 順一

Report#5



[1]課題


講義資料オブジェクト指向プログラムJavaIの中のサンプルプログラムのポ イントについて考察せよ。各自オリジナルのサンプルプログラムのポイン ト(全ての講義内容を含んだ、1つのコード)を作成し、考察せよ。

[2]解答・実行結果・考察


ローカル変数とクラス変数


ソースコード

class Display {
    int n;                  //クラス変数
    void Disp() {
        int i;              //ローカル変数
        for (i = 1; i <= n; i++)
            System.out.println(i + " ");
    }
}

class VarSample {
    public static void main(String args[]) {
        Display obj = new Display();     //クラスDisplayのオブジェクトobj生成
        obj.n = 10;                      //オブジェクトobjの変数nに10を代入
        obj.Disp();                      //オブジェクトobjのDisp()実行
    }
}
 

実行結果


1 2 3 4 5 6 7 8 9 10 

考察



static変数


ソースコード

class Sum {
    int total;
    Sum() {                     //コンストラクタ(クラス名と同名))
        total = 0;              //0クリア
    }
    void add(int x) {
        total += x;
    }
}

class SumSample {
    public static void main(String args[]) {
        Sum obj1 = new Sum();    //クラスSumのオブジェクトobj1生成
        Sum obj2 = new Sum();    //クラスSumのオブジェクトobj2生成
        obj1.add(10);
        obj1.add(20);
        obj2.add(30);
        obj2.add(40);
        System.out.println("obj1.total = " + obj1.total);
        System.out.println("obj2.total = " + obj2.total);
    }
}
 

実行結果


obj1.total = 30
obj2.total = 70
 

ソースコード

class Sum2 {
    static int total;
    Sum2() {
        total = 0;
    }
    void add(int x) {
        total += x;
    }
}

class SumSample2 {
    public static void main(String args[]) {
        Sum2 obj1 = new Sum2();
        Sum2 obj2 = new Sum2();
        obj1.add(10);
        obj1.add(20);
        obj2.add(30);
        obj2.add(40);
        System.out.println("obj1.total = " + obj1.total);
        System.out.println("obj2.total = " + obj2.total);
        System.out.println("Sum2.total = " + Sum2.total);
    }
}
 

実行結果


obj1.total = 100
obj2.total = 100
Sum2.total = 100
 

考察


コンストラクタ


ソースコード

class Sum3 {
    int total;
    Sum3() {                      //コンストラクタ 引数無し
        total = 0;
    }
    Sum3(int x) {                 //コンストラクタ 引数 x
        total = x;
    }
    void add(int x) {
        total += x;
    }
}

class ConstrcSample {
    public static void main(String args[]) {
        Sum3 obj1 = new Sum3();     //クラスSumのオブジェクトobj1生成
        obj1.add(10);
        obj1.add(20);
        System.out.println("sum1 = " + obj1.total);
        Sum3 obj2 = new Sum3(100);  //クラスSumのオブジェクトobj2生成
        obj2.add(10);
        obj2.add(20);
        System.out.println("sum2 = " + obj2.total);
    }
}
 

実行結果


sum1 = 30
sum2 = 130
 

考察



staticイニシャライズ


ソースコード

class Disp {
    static {                       //staticイニシャライズ
        System.out.println("initialize");
    }
    Disp() {                       //コンストラクタ
        System.out.println("construct");
    }
}

class StaticSample {
    public static void main(String args[]) {
        Disp obj1 = new Disp();    //オブジェクト生成時にコンストラク
    タを処理
        Disp obj2 = new Disp();    //オブジェクト生成時にコンストラク
    タを処理
    }
}
 

実行結果


initialize
construct
construct
 

考察



オリジナルプログラム


ソースコード

class Sum {
    int x = 4;
    int add(int y) {
        return x+y;
    }
    int sub(int y) {
        return x-y;
    }
}

class Subst {
    int w;
    int x = 800;
    int y = 200;
    void Disp() {
        System.out.println("w = " + w);
        System.out.println("x = " + x);
        System.out.println("y = " + y);
        int x = 3;
        int y = 7;
        int z = 5;
        System.out.println("w = " + w);
        System.out.println("x = " + x);
        System.out.println("y = " + y);
        System.out.println("z = " + z);
    }
}

class Rep5 {
    public static void main(String args[]) {
        int wa,sa;
        Sum obj;
        obj = new Sum();

        wa = obj.add(20);
        System.out.println("wa = " + wa);

        obj.x = 30;
        wa = obj.add(20);
        System.out.println("wa = " + wa);

        sa = obj.sub(30);
        System.out.println("sa = " + sa);
        System.out.println();

        Subst obj2 = new Subst();
        obj2.Disp();
        System.out.println();
        obj2.w = 70;
        obj2.Disp();
    }
}
 

実行結果

wa = 24
wa = 50
sa = 0

w = 0
x = 800
y = 200
w = 0
x = 3
y = 7
z = 5

w = 70
x = 800
y = 200
w = 70
x = 3
y = 7
z = 5
 

考察



[3]感想


 年末にOSのヴァージョンアップをして、ターミナルの調子が悪くなったのを 一人で何とかしようとしていたら、こんなに遅くなってしまいました。この ままReport#6に取りかかります。

[4]参考文献


PukiWiki プログラミングIIへ
とっぷにもどる