(1)Javaの使い方
以下のプログラムが与えられたとき,
(a)何というファイル名にすべきか答えよ.
(b)コンパイル方法を答えよ.
(c)実行方法を答えよ.
(d)コンパイラによって作成されたファイルの
ファイル名を答えよ.
public class Example1 {
public static void main(String[] args) {
...内容...
}
}
(2)コンストラクタ
public class Example2 {
int num;
public static void main( String[] args ) {
Example2 ex2 = new Example2( 5 );
int a = ex2.getNum();
System.out.println( a );
}
Example2( int num ) {
this.num = num;
}
int getNum() {
return this.num;
}
} (a)コンストラクタは何行目から
始まっているか答えよ
(3)ローカル変数とインスタンス変数
public class Example2 {
int num;
public static void main( String[] args ) {
Example2 ex2 = new Example2( 5 );
int a = ex2.getNum();
System.out.println( a );
}
Example2( int num ) {
this.num = num;
}
int getNum() {
return this.num;
}
} (a)ローカル変数を全て答えよ.
(b)インスタンス変数を答えよ.
(4)メソッド
public class Example2 {
int num;
public static void main( String[] args ) {
Example2 ex2 = new Example2( 5 );
int a = ex2.getNum();
System.out.println( a );
}
Example2( int num ) {
this.num = num;
}
int getNum() {
return this.num;
}
}
(a)インスタンスメソッド名を答えよ.
(b)そのメソッドを呼び出している行を答えよ.
(5)コンストラクタ その2
public class Example3 {
int num;
public static void main( String[] args ) {
Example3 ex3 = new Example3( 5.6 );
int a = ex3.getNum();
System.out.println( a );
}
※getNumメソッドは省力
Example3( int num ) {
Example2のgetNumと同じ
this.num = num;
}
Example3( double num ) {
this.num = (int)(num + 0.5);
}
} (a)コンストラクタは何行目から始まっているか答えよ.
(b)どちらのコンストラクタが実行されるか答えよ.
(c)表示結果を答えよ.
(6)インスタンスメソッドとインスタンス変数
public class Example4 {
int num;
void plus( int num ) {
this.num = this.num + num;
}
void multiple( int num ) {
this.num = this.num * num;
}
void setNum( int num ) {
this.num = num;
}
int getNum() {
return this.num; (a)this.numとnumの違いを答えよ.
} (b)各メソッド内のthis.numは
} 何行目で宣言されているか答えよ.
(7)パラメータ
public class Example5 {
public static void main( String[] args ) {
String data = "test.";
hyouji( data );
}
static void hyouji( String str ) {
System.out.println( str );
}
}
(a)変数strには何が入っているか答えよ.
(b)dataとstrの関係について答えよ.