next up previous
Next: この文書について... Up: ソフトウェア第三 講義資料 Java入門,宣言,インタフェース,パッケージ,スレッド Previous: 17.3 スレッドの例 3

自習

  1. 次のプログラムの場合,aNumberが 3 であった場合に何を表示するか.プログラムを走らせてみよ.
    if (aNumber >= 0)
        if (aNumber == 0)
           System.out.println("first string");
    else System.out.println("second string");
    System.out.println("third string");
    
  2. プログラムの間違いを指摘して,正しいプログラムにしなさい.
    class Rectangle {
        int width;
        int height;
        int area() {
    	return width*height;
        }
    }
    
    public class SomethingIsWrong {
        public static void main(String[] args) {
            Rectangle myRect;
            myRect.width = 40;
            myRect.height = 50;
            System.out.println(
             "myRect's area is " + myRect.area());
        }
    }
    
  3. 以下のプログラムは何を表示するか.
    
    class StringTest{
        public static void main(String argv[]) {
    	
    	String s1 = "abc";
    	String s2 = "abc";
    	char s3[] = {'a','b','c'};
    	String s4 = new String(s3);
    	System.out.println("s1:" + s1 +
    			   " s2:" + s2 +
    			   " s3:" + s3 +
    			   " s4:" + s4);
    	if (s1 == s2) 
    	    System.out.println("s1 == s2 true");
    	if (s1 == s4) 
    	    System.out.println("s1 == s4 true");
    	if (s2 == s4) 
    	    System.out.println("s2 == s4 true");
        }
    }
    
  4. 以下のプログラムを走らせた時に表示されるものを示せ.
    class A {
        String str="A";
        A() {
            System.out.println("A() " + str);
            System.out.println("A() " + this.str);
        }
        A(String str) {
            System.out.println(str);        
            System.out.println(this.str);
        }
    }
    
    class B extends A {
        String str="B";
        B() {
            System.out.println("B() " + str);
            System.out.println("B() " + this.str);
            System.out.println("B() " + super.str);
        }
        B(String str) {
            System.out.println(str);
            System.out.println(this.str);
            System.out.println(super.str);
        }
    }
    
    class C extends B {
        C() {System.out.println("C() C");}
        C(String str) {
            System.out.println(str);
            System.out.println(this.str);
            System.out.println(super.str);
        }
    }
    
    public class Construct2 {
        public static void main(String str[]) {
            C m = new C();
            System.out.println("--");
            C m2 = new C("C");
        }
    }
    
  5. 以下のプログラムを走らせた時に表示されるものを示せ.
    class SuperShow {
        public String str = "SuperStr";
        public void show() {
            System.out.println("Super.show: " + str);
        }
    }
    
    class ExtendShow extends SuperShow {
        public String str = "ExtendStr";
        public void show() {
            System.out.println("Extend.show: " + str);        
        }
        public static void main(String[] args) {
            ExtendShow ext = new ExtendShow();
            SuperShow sup = ext;
            sup.show();
            ext.show();
            System.out.println("sup.show: " + sup.str);        
            System.out.println("ext.show: " + ext.str);
        }
    }
    


generated through LaTeX2HTML. M.Inaba 平成18年5月7日