2. 演習[1]
class Dog { (A)どのような出力が得られるか?
public static void bark() { (B) bark()メソッドがstatic宣言されて
System.out.print("woof");
} いない場合の出力は?
} (C)メソッドがどのように実行されて
class Bulldog extends Dog { いるか説明せよ
public static void bark() {}
}
public class Bark {
public static void main(String args[]) {
Dog d1 = new Dog();
Dog d2 = new Bulldog();
d1.bark();
d2.bark();
}
} ヒント
Java言語仕様 §15.12 Method Invocation Expressions
2
3. 演習[2]
(A)どのような出力が得られるか?
(B) なぜこのような出力が得られたのか説明せよ.
class Point {
(C)適切な出力が得られるようにコードを修正せよ.
protected final int x, y;
private final String name;
protected String makeName() { return "[" + x + "," + y + "]"; }
public final String toString() { return name; }
Point(int x, int y) {
this.x = x; this.y = y;
this.name = makeName();
}
}
public class ColorPoint extends Point {
private final String color;
protected String makeName() { return super.makeName() + ":" + color; }
ColorPoint(int x, int y, String color) {
super(x, y);
this.color = color;
}
public static void main(String[] args) {
System.out.println(new ColorPoint(4, 2, "purple"));
}
} ヒント: Java言語仕様 §15.12 Method Invocation Expressions
3
4. 演習[3]
class Purse {
private int i;
public Purse(int arg) {
i = arg;
}
public int get_i() { return i; }
public void set_i(int iarg) { i = iarg; }
}
class User {
private Purse p;
(A) User クラスのインスタンスを生成し,
public User(Purse arg) {
p = arg; その private フィールド p が参照する
} Purse インスタンスの持つ値を変更す
public Purse get_p() {
return p; る攻撃コードを書け.
} (B) (A) でつくった攻撃コードが動作しな
}
いように元のコードを修正せよ.
4
5. 演習[4]
class Authlet {
int i;
Authlet(int i0){
if (checkarg(i0)) { this.i = i0; }
}
boolean checkarg(int i) throws IllegalArgumentException {
if (i<0 || 100<i) {
throw
new IllegalArgumentException("arg should be positive < 100.");
}
class Auth {
return true;
private static Authlet a0;
}
}
public static void checkAuth(Authlet a){
if (a0 == null){
if (a == null){
System.out.println("invalid Authlet!");
System.exit(1);
}
a0 = a;
}
}
}
5
6. 演習[4]
class useAuth {
public static void main(String[] args){
Authlet au;
try {
au = new Authlet(Integer.parseInt(args[0]));
} catch(IllegalArgumentException ex){
au = null;
}
Auth.checkAuth(au);
System.out.println("Authenticated!");
}
}
(A) checkarg() による入力値チェックを回避する攻撃
コードを書け.
(B) (A) で書いた攻撃コードに対する対策を行え.
6