| 目次 | 索引 |
|---|---|
次回(1月14日)の授業では試験を行います。 今日の演習問題をよく見直しておいてください。
レポートの提出に関するスケジュールは次の通りです。
この授業の成績は、レポートの提出と試験の得点で決まります。
成績に関して次のような事情のある人はメールで連絡してください。 できる限り対処します。
問 1. 次のプログラムは、星印を 10 行 10 列に並べた正方形を出力する (つもりの)
ものです。このプログラムの不具合を修正してください。
/* 1*/ class PrintSquare {
/* 2*/ public static void main (String[] args) {
/* 3*/ int i, SIZE = 10;
/* 4*/ for (i = 0; i < SIZE; i++) {
/* 5*/ for (i = 0; i < SIZE; i++) {
/* 6*/ System.out.print("*");
/* 7*/ }
/* 8*/ System.out.println();
/* 9*/ }
/* 10*/ }
/* 11*/ }
b04a001@AsiaA1:~/comp3b% java PrintSquare
********** // 1 行しかないので間違い
b04a001@AsiaA1:~/comp3b%
問 2. 次のプログラムは、10 進数 100 を 2 進数に変換する (つもりの) ものです。10
進 2 進変換は、以下のように 2 で割り続け、余りを下から上に読むという方法
に基づきます。このプログラムの不具合を修正してください。
100 ÷ 2 = 50 … 0 ↑
50 ÷ 2 = 25 … 0 ↑
25 ÷ 2 = 12 … 1 ↑
12 ÷ 2 = 6 … 0 ↑
6 ÷ 2 = 3 … 0 ↑
3 ÷ 2 = 1 … 1 ↑
1 ÷ 2 = 0 … 1 ↑
/* 1*/ class ToBinary {
/* 2*/ public static void main (String[] args) {
/* 3*/ toBinary(100);
/* 4*/ System.out.println();
/* 5*/ }
/* 6*/ static void toBinary (int n) {
/* 7*/ if (n > 0) {
/* 8*/ System.out.print(n % 2);
/* 9*/ toBinary(n / 2);
/* 10*/ }
/* 11*/ }
/* 12*/ }
b04a001@AsiaA1:~/comp3b% java ToBinary
0010011 // 1100100 でないので間違い
b04a001@AsiaA1:~/comp3b%
問 3. 次のプログラムは、数列 3, 6, 5, 4, 9, 1, 8, 2, 7 を一種のバブルソートで整
列する (つもりの) ものです。この整列法は、左から右に見ていき、逆順を見つ
けたらそこを交換してまた左端から見直すというものです。このプログラムの不
具合を修正してください。
/* 1*/ class BubbleSort {
/* 2*/ public static void main (String[] args) {
/* 3*/ int i = 0, temp;
/* 4*/ int[] a = {3, 6, 5, 4, 9, 1, 8, 2, 7};
/* 5*/ while (i < a.length) {
/* 6*/ if (a[i] > a[i + 1]) {
/* 7*/ temp = a[i];
/* 8*/ a[i] = a[i + 1];
/* 9*/ a[i + 1] = temp;
/* 10*/ i = 0;
/* 11*/ } else {
/* 12*/ i++;
/* 13*/ }
/* 14*/ }
/* 15*/ for (i = 0; i < a.length; i++) {
/* 16*/ System.out.print(" " + a[i]);
/* 17*/ }
/* 18*/ System.out.println();
/* 19*/ }
/* 20*/ }
b04a001@AsiaA1:~/comp3b% java BubbleSort
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at BubbleSort.main(BubbleSort.java:6)
// 1 2 3 4 5 6 7 8 9 でないので間違い
b04a001@AsiaA1:~/comp3b%
問 4. 次のプログラムは、リスト [1, 2, 3, 4, 5] をコピーする (つもりの) もので
す。このプログラムの不具合を修正してください。
/* 1*/ class IntList {
/* 2*/ int first;
/* 3*/ IntList rest;
/* 4*/ IntList (int first, IntList rest) {
/* 5*/ this.first = first;
/* 6*/ this.rest = rest;
/* 7*/ }
/* 8*/ }
/* 1*/ class ListCopy {
/* 2*/ public static void main (String[] args) {
/* 3*/ int DUMMY = 0;
/* 4*/ IntList lst1, lst2, head;
/* 5*/ lst1 = new IntList(DUMMY, null);
/* 6*/ lst1.rest = new IntList(1, null);
/* 7*/ lst1.rest.rest = new IntList(2, null);
/* 8*/ lst1.rest.rest.rest = new IntList(3, null);
/* 9*/ lst1.rest.rest.rest.rest = new IntList(4, null);
/* 10*/ lst1.rest.rest.rest.rest.rest = new IntList(5, null);
/* 11*/ lst2 = new IntList(DUMMY, null);
/* 12*/ head = lst2;
/* 13*/ lst1 = lst1.rest;
/* 14*/ while (lst1 != null) {
/* 15*/ lst2 = new IntList(lst1.first, null);
/* 16*/ lst1 = lst1.rest;
/* 17*/ lst2 = lst2.rest;
/* 18*/ }
/* 19*/ lst2 = head;
/* 20*/ for (lst2 = lst2.rest; lst2 != null; lst2 = lst2.rest) {
/* 21*/ System.out.print(" " + lst2.first);
/* 22*/ }
/* 23*/ System.out.println();
/* 24*/ }
/* 25*/ }
b04a001@AsiaA1:~/comp3b% java ListCopy
// 1 2 3 4 5 でないので間違い
b04a001@AsiaA1:~/comp3b%
問 5. 次のプログラムは、以下の二分木の葉をすべて出力する (つもりの) ものです。
このプログラムの不具合を修正してください。
1
|
+-------+-------+
| |
2 3
| |
+---+---+ +---+
| | |
4 5 6
/* 1*/ class IntTree {
/* 2*/ int node;
/* 3*/ IntTree left, right;
/* 4*/ IntTree (int node, IntTree left, IntTree right) {
/* 5*/ this.node = node;
/* 6*/ this.left = left;
/* 7*/ this.right = right;
/* 8*/ }
/* 9*/ }
/* 1*/ class PrintLeaves {
/* 2*/ public static void main (String[] args) {
/* 3*/ int DUMMY = 0;
/* 4*/ IntTree tr = new IntTree(DUMMY, null, null);
/* 5*/ tr.right = new IntTree(1, null, null);
/* 6*/ tr.right.left = new IntTree(2, null, null);
/* 7*/ tr.right.right = new IntTree(3, null, null);
/* 8*/ tr.right.left.left = new IntTree(4, null, null);
/* 9*/ tr.right.left.right = new IntTree(5, null, null);
/* 10*/ tr.right.right.left = new IntTree(6, null, null);
/* 11*/ printLeaves(tr.right);
/* 12*/ System.out.println();
/* 13*/ }
/* 14*/ static void printLeaves (IntTree tr) {
/* 15*/ if (tr.left == null) {
/* 16*/ if (tr.right == null) {
/* 17*/ System.out.print(" " + tr.node);
/* 18*/ } else {
/* 19*/ printLeaves(tr.right);
/* 20*/ }
/* 21*/ } else {
/* 22*/ printLeaves(tr.left);
/* 23*/ }
/* 24*/ }
/* 25*/ }
b04a001@AsiaA1:~/comp3b% java PrintLeaves
4 // 4 5 6 でないので間違い
b04a001@AsiaA1:~/comp3b%
今日の演習12の答案をkonishi@twcu.ac.jpあてにメールで送ってください。 メールには、学生番号、氏名、科目名、授業日(12月17日)を明記してください。