2011年4月20日水曜日

Java プログラム 基本情報技術者 午後 過去問

 内部整列

 回答と解説を掲載します。

  JAVA の勉強を始めてから今日で きっかり3カ月だ。
下記の過去問を完璧に理解するまでには、それはもう
かなり苦労しました。 この記事に目をとめてくださった
方々も、中途半端なインチキ教本には振り回されない様
ご注意下さい。
 ※やさしいJ○○


































(プログラム1)



















01 public class PriceSort {















02
public static void main(String[ ] args) {









03

Computer[ ] computers = { 












04



new Server("Sv1", 800, 256, 300000),






05



new PersonalComputer("Pc1", 450, 128, 180000),

06



new Server("Sv2", 800, 512, 500000),






07



new PersonalComputer("Pc2", 450, 256, 200000) };

08

for (int i = 0; i < computers.length; i++) {







09


System.out.println(computers[i]);









10

}






















11

System.out.println("\n<sorted by price>");






12

PriceUtility.ComputeDiscountPrice(computers);




13

PriceUtility.sort(computers);











14

for (int i = 0; i < computers.length; i++) {







15


System.out.println(computers[i]);









16

}






















17
}























18 }















































































(プログラム2)



















01 public interface IComp {















02
int compareWith(IComp a);













03 }















































































(プログラム3)



















01 public class Computer implements IComp {








02
String name;


















03
int frequency;


















04
int memory;



















05
int price;




















06
public Computer(String name, int frequency, int memory, int price) {
07

this.name = name;















08

this.frequency = frequency;












09

this.memory = memory;













10

this.price = price;
















11
}























12
public int getPrice() {















13

return price;

















14
}























15
public void setPrice(int price) {











16

this.price = price;
















17
}























18
public String toString() {














19

return name + ", frequency:" + frequency + ",memory:" + memory
20



+ ",price:" + price;













21
}























22
public double getDiscountRate() {










23

return 0.0;


















24
}























25
public int compareWith(IComp a) {










26

Computer computer = (Computer) a;








27

return computer.price - this.price;









28
}























29 }















































































(プログラム4)



















01 public class Server extends Computer {









02
public Server(String name, int frequency, int memory, int price) {
03

super(name, frequency, memory, price);







04
}























05
public double getDiscountRate() {










06

return 0.1;


















07
}























08 }















































































(プログラム5)



















01 public class PersonalComputer extends Computer {




02
public PersonalComputer(String name, int frequency, int memory, int price) {
03

super(name, frequency, memory, price);







04
}























05
public double getDiscountRate() {










06

return 0.2;


















07
}























08 }















































































(プログラム6)



















01 public class PriceUtility {















02
public static void ComputeDiscountPrice(Computer[ ] a) {

03

int newPrice;

















04

for (int i = 0; i < a.length; i++) {











05


newPrice = (int) (a[i].getPrice() * (1 - a[i].getDiscountRate()));
06


a[i].setPrice(newPrice);












07

}






















08
}























09

























10
public static void sort(IComp[ ] a) {










11

int j;





















12

for (int i = 1; i < a.length; i++) {











13


IComp temp = a[i];














14


for (j = i - 1; j >= 0 && temp.compareWith(a[j]) < 0; j--) {
15



a[j + 1] = a[j];















16


}





















17


a[j + 1] = temp;















18

}






















19
}























20 }






















































情報処理再チャレンジブログ ホー ム

0 件のコメント:

コメントを投稿