実験課題4

Exercise 4(課題4) See the section 2.3.1. "Usage of Debugger". Show the sourcecode, the execution result that has the sort method in the following Figure, and the screenshot of "variable tab" in the debugger just before entering internal for loop at the second time iteration of external for in the program. Further, introduce modified sourcecode to sort correctly. Follow the instructions below to show the result.

  1. create the Java application project named chapter2kadai4.
  2. create main method to sort following contents in double array by using sort method in following figure.
  3. take a screenshot of the execution result.
[2.0, 2.2, 1.5, 5.0, 3.0]
Figure:The array passed to class method `sort`
[5.0, 3.0, 2.2, 2.0, 1.5]
Figure:The correct sorted array by using class method `sort`
/**
 * バブルソートによって配列をソートする。
 * @param n 配列の長さ
 * @param array ソートする配列
 * @return ソートした配列
 */
public static double[] sort(int n, double[] array) {

	for (int j = 0; j < n; j = j + 1) {
		for (int i = j; i < n - 1; i = i + 1) {
			if (array[i] < array[i + 1]) {
				double t = array[i];
				array[i] = array[i + 1];
				array[i + 1] = t;
			}
		}
	}
	return array;
}
Figure:The class method `sort` to sort double-typed array

(2.3.1.節をよく読み、プロジェクトchapter2kadai4を作成し、下図のメソッドを作成して、下に示すdouble型配列の中身を図のsortメソッドに渡して、その結果を表示させるプログラムを作成しなさい。その際、デバッガを使ってデバッグし、外側のfor文のループ毎に、内側のfor文に入る直前の状態の変数タブの内容をスクリーンショットで示しなさい。また、その結果どのような変数の値であれば問題ないのか、プログラムが正しくソートするためにはどのように改変すればよいか説明しなさい。)