実験課題2

Exercise 2(課題2) See the section 2.1.2. "printf Debugging". Explain the reason why the following method can not execute by following instructions.

  1. create the Java application project named chapter2kadai2.
  2. create calcDivisors method and main method in the same Main class.
  3. add some codes for the printf debugging to the class.
  4. take a screenshot of the execution result.
public static void calcDivisors(int n) {

	for (int i = 0; i <= n; i = i + 1) {

		if (n % i == 0) {

			System.out.print(i + " ");
		}
	}
	System.out.println();
}
Figure:The class method calcDivisors to enumerate all divisors of n

The assumed correct result of calcDivisor method

Figure:The assumed correct result of calcDivisors method with n = 10

(2.1.2.節をよく読み、chapter2kadai2プロジェクトを作成し、与えられたnの約数をすべて表示するcalcDivisorsメソッドと、calcDivisorsメソッドを実行するmainメソッドを作成しなさい。そしてprintfデバッグを行ってiやnがどの値のときに例外を発生するのか、エラーの原因を説明しなさい。)