レポート5



課題


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

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


ソースコード

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();
obj.n = 14;
obj.Disp();
}
}

実行結果


1 2 3 4 5 6 7 8 9 10 11 12 13 14

考察


  • ローカル変数とは、メソッドの内部で宣言した変数。
  • クラス変数とは、メソッドの外部で宣言した変数。
  • クラス変数は一つのクラスに一つしかなく、その値はそのクラスの全て のインスタンスに関わってくる。
  • ローカル変数は宣言されたメソッド実行中のみ存在する。
  • 複数のメソッドで同名のローカル変数を使うことができる。

    static変数


    ソースコード

    class Sum {
    int total;
    Sum() {
    total = 0;
    }
    void add(int x) {
    total += x;
    }
    }
    class Sample {
    public static void main(String args[]) {
    Sum obj1 = new Sum();
    Sum obj2 = new Sum();
    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) {                 
            total = x;
        }
        void add(int x) {
            total += x;
        }
    }
    
    class ConstrcSample {
        public static void main(String args[]) {
            Sum3 obj1 = new Sum3();    
            obj1.add(10);
            obj1.add(20);
            System.out.println("sum1 = " + obj1.total);
            Sum3 obj2 = new Sum3(100);  
            obj2.add(10);
            obj2.add(20);
            System.out.println("sum2 = " + obj2.total);
        }
    }
     

    実行結果


    sum1 = 30
    sum2 = 130
     

    考察



    staticイニシャライズ


    ソースコード

    class Disp {
        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("tasita = " + wa);
    
            obj.x = 30;
            wa = obj.add(20);
            System.out.println("tasita = " + wa);
    
            sa = obj.sub(30);
            System.out.println("hiita = " + sa);
            System.out.println();
    
            Subst obj2 = new Subst();
            obj2.Disp();
            System.out.println();
            obj2.w = 70;
            obj2.Disp();
        }
    }
     

    実行結果

    tasita = 24
    tasita = 50
    hiita = 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
     

    考察



    感想


    とにもかくにも、長い長い!!ホームページにのせる方法なんかも、教えて もらいながら、つくったもんだから、つかれた。。ほとんど人のアイディアっ てのが、くやしい。つぎこそは、完全にオリジナルでつくりたい!!努力が 必要です。。

    参考文献