[オブジェクト指向と情報処理試験 -2001年 秋-]
(基本:午後・問12)
次の Java プログラムの説明及びプログラムを読んで、設問1〜3に答えよ。
[プログラムの説明]
ある会社の情報管理部門で導入するコンピュータの候補としてワークステーション とパーソナルコンピュータが提案されている。これらのコンピュータの値引き後の価 格の計算を行い、コンピュータの仕様を価格の降順に整列して出力するプログラムで ある。コンピュータの仕様は、次の四つからなる。 機種名、動作周波数、メモリ容量、価格 このプログラムは、次の四つのクラスと一つのインタフェースで構成される。 Computer コンピュータに関するクラスであり、価格を返すメソッド getPrice、価格を 格納するメソッド setPrice、仕様を文字列で返すメソッド toString、値引き 率を返すメソッド getDiscountRate、及び整列する際に用いるデータの大小を 比較するメソッド compareWith を定義している. Workstation クラス Computer のサブクラスとして定義されたワークステーションに関するク ラスであり、ワークステーションの値引き率を返すメソッド getDiscountRate を再定義している。 PersonalComputer クラス Computer のサブクラスとして定義されたパーソナルコンピュータに 関するクラスであり、パーソナルコンピュータの値引き率を返すメソッド getDiscountRate を再定義している。 PriceUtility 値引き後の価格の計算と整列をするためのクラスであり、価格の計算を行うメソ ッド computeDiscountPrice、及び整列を行うメソッド sort を定義している。 IComp 大小比較の対象を示すインタフェースであり、比較を行うメソッド computeWith を宣言している。 プログラム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 [ a ] { 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 = ([ b ])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].[ c ]() 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.[ d ](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