東京理科大学 infoserv[更新日]2001.11.05


問12

 次の Java プログラムの説明及びプログラムを読んで,設問 1 〜 3 に答えよ。

〔プログラムの説明〕

ある会社の情報管理部門で導入するコンピュータの候補としてワークステーションとパーソナルコンピュータが提案されている。 これらのコンピュータの値引き後の価格の計算を行い,コンピュータの仕様を価格の降順に整列して出力するプログラムである。 コンピュータの仕様は,次の四つからなる。

機種名,動作周波数,メモリ容量,価格

このプログラムは,次の四つのクラスと一つのインタフェースで構成される。

Computer

コンピュータに関するクラスであり,価格を返すメソッド getPrice, 価格を格納するメソッド setPrice,仕様を文字列で返すメソッド toString, 値引き率を返すメソッド getDiscountRate,及び整列する際に用いるデータの 大小を比較するメソッド compareWith を定義している。

Workstation

クラス Computer のサブクラスとして定義されたワークステーションに 関するクラスであり,ワークステーションの値引き率を返すメソッド getDiscountRate を再定義している。

PersonalComputer

クラス Computer のサブクラスとして定義されたパーソナルコンピュータに関するクラスであり,パーソナルコンピュータの値引き率を返すメソッド
getDiscountRate を再定義している。

PriceUtility

値引き後の価格の計算と整列をするためのクラスであり,価格の計算を行うメソッド computeDiscountPrice,及び整列を行うメソッド sort を定義している。

IComp

大小比較の対象を表すインタフェースであり,比較を行うメソッド compareWith を宣言している。

プログラム 1 のメソッド main とプログラム 2 〜 6 の実行結果を図に示す。 このとき,メソッド main の処理は,次のとおりである。

(1) 四つのコンピュータの仕様を生成し,配列 computers に格納する。

(2) 各コンピュータの仕様を出力する。

(3) 値引き後の価格の計算を行う。

(4) 整列を行う。

(5) 整列結果を出力する。

Ws1, frequency:800, memory:256, price:300000
Pc1, frequency:450, memory:128, price:180000
Ws2, frequency:800, memory:512, price:500000
Pc2, frequency:450, memory:256, price:200000

<sorted by price>
Ws2, frequency:800, memory:512, price:450000
Ws1, frequency:800, memory:256, price:270000
Pc2, frequency:450, memory:256, price:160000
Pc1, frequency:450, memory:128, price:144000

図 実行結果

〔プログラム 1 〕

(行番号)


 1  public class PriceSort {
 2     public static void main(String[] args) {
 3        Computer[] computers = {
 4           new Workstation("Ws1", 800, 256, 300000),
 5           new PersonalComputer("Pc1", 450, 128, 180000), 
 6           new Workstation("Ws2", 800, 512, 500000), 
 7           new PersonalComputer("Pc2", 450, 256, 200000),
 8        };
 9        for (int i = 0; i < computers.length; i++) {
10           System.out.println(computers[i]);
11        }
12        System.out.println("\n<sorted by price>");
13        PriceUtility.computeDiscountPrice(computers);
14        PriceUtility.sort(computers);
15        for (int i = 0; i < computers.length; i++) {
16           System.out.println(computers[i]);
17        }
18     }
19  }
〔プログラム 2 〕 (行番号)

 1   public interface IComp {
 2      int compareWith(IComp a);
 3   }
〔プログラム 3 〕 (行番号)

 1   public class Computer implements  {
 2      String name;
 3      int frequency;
 4      int memory;
 5      int price;
 6      public Computer(String name, int frequency, 
 7                      int memory, int price) {
 8         this.name = name;
 9         this.frequency = frequency;
10         this.memory = memory;
11         this.price = price;
12      }
13      public int getPrice() {
14         return price;
15      }
16      public void setPrice(int price) {
17         this.price = price;
18      }    
19      public String toString() {
20         return name + ", frequency:" + frequency
21              + ", memory:" + memory + ", price:" + price;
22      }
23      public double getDiscountRate() {
24         return 0.0;
25      }
26      public int compareWith(IComp a) {
27         Computer computer = ()a;
28         return computer.price - this.price;
29      }
30   }
〔プログラム 4 〕 (行番号)

 1   public class Workstation extends Computer {
 2      public Workstation(String name, int frequency, 
 3                         int memory, int price) {
 4         super(name, frequency, memory, price);
 5      }
 6      public double getDiscountRate() {
 7         return 0.1;
 8      }
 9   }
〔プログラム 5 〕 (行番号)

 1   public class PersonalComputer extends Computer {
 2      public PersonalComputer(String name, int frequency,
 3                              int memory, int price) {
 4         super(name, frequency, memory, price);
 5      }
 6      public double getDiscountRate() {
 7         return 0.2;
 8      }
 9   }
〔プログラム 6 〕 (行番号)

 1   public class PriceUtility {
 2      public static void computeDiscountPrice(Computer[] a) {
 3         int newPrice;
 4         for (int i = 0; i < a.length; i++) {
 5            newPrice = (int)(a[i].() 
 6                              * (1 - a[i].getDiscountRate()));
 7            a[i].setPrice(newPrice);
 8         }
 9      }
10      public static void sort(IComp[] a) {
11         // 単純挿入法による整列
12         int j;
13         for (int i = 1; i < a.length; i++) {
14            IComp temp = a[i];
15            for (j = i - 1;
16                 j >= 0 && temp.(a[j]) < 0;
17                 j--) { 
18               a[j + 1] = a[j];
19            }
20            a[j + 1] = temp;
21         }
22      }
23   }

設問1  プログラム 3,6 中の に入れる正しい答えを,解答群の中から選べ。

解答群

ア compareWith    イ computeDiscountPrice
ウ Computer     エ getDiscountRate
オ getPrice     カ IComp
キ PriceUtility    ク sort

設問2  プログラム 3 の行番号 28 を

return computer.frequency - this.frequency;

に変更したときのプログラム 1 の行番号 16 の出力として正しい答えを, 解答群の中から選べ。

解答群

ア Pc1, frequency:450, memory:128, price:144000
  Pc2, frequency:450, memory:256, price:160000
  Ws1, frequency:800, memory:256, price:270000
  Ws2, frequency:800, memory:512, price:450000

イ Pc2, frequency:450, memory:256, price:160000
  Pc1, frequency:450, memory:128, price:144000
  Ws2, frequency:800, memory:512, price:450000
  Ws1, frequency:800, memory:256, price:270000

ウ Ws1, frequency:800, memory:256, price:270000
  Ws2, frequency:800, memory:512, price:450000
  Pc1, frequency:450, memory:128, price:144000
  Pc2, frequency:450, memory:256, price:160000

エ Ws2, frequency:800, memory:512, price:450000
  Ws1, frequency:800, memory:256, price:270000
  Pc2, frequency:450, memory:256, price:160000
  Pc1, frequency:450, memory:128, price:144000

設問3  プログラム 5 の行番号 7 を

return 0.4;

に変更したとき,各コンピュータの値引き後の価格として正しい答えを,解答群の中から選べ。

解答群

 

Ws1

Pc1

Ws2

Pc2

180000

108000

300000

120000

180000

144000

300000

160000

270000

108000

450000

120000

270000

144000

450000

160000


東京理科大学 infoserv 戻る 次頁:問13