...

2009/7/14 OOP・同演習 追加演習問題 次のプログラムの説明を呼んで

by user

on
Category: Documents
10

views

Report

Comments

Transcript

2009/7/14 OOP・同演習 追加演習問題 次のプログラムの説明を呼んで
2009/7/14 OOP・同演習 追加演習問題
次のプログラムの説明を呼んでプログラムを完成させよ。
ある会社の従業員は,一般従業員と管理職で構成される。この従業員の新しい給与計算
して,その給与を昇順に整列し,その結果を出力するプログラムである。従業員のデータ
は,次の情報で構成される。
従業員のデータ:従業員名,給与,入社年月日
このプログラムは,次の五つのクラスと一つのインタフェースで構成される。
(1) Date クラス
入社年度を表すクラスであり,入社年月日を文字列で返す toString メソッドを実装し
ている。
(2) Employee クラス
一般従業員を表すクラスであり,従業員のデータを文字列で返す toString メソッド,
一般従業員の新しい給与を計算に用いるベースアップ率を返す getBaseup メソッドと
整列処理でデータの大小関係を比較する compareTo メソッドを実装している。
(3) Manager クラス
管理職を表すクラスであり,Employee クラスのサブクラスである。管理職の新しい
給与を計算に用いるベースアップ率を返す getBaseup メソッドを実装している。
(4) NewSalary クラス
新しい給与を計算のためのクラスであり,給与を計算する computeSalary メソッドを
実装している。
(5) SortableArray クラス
整列処理に関するクラスであり,整列処理を行う sort メソッドを実装している。
(6) Sortable インターフェイス
整列処理において,データの大小関係を比較するための compareTo メソッドを
interface で宣言している。
図1は、main メソッド、図2にはメインメソッドの実行結果を示す。main メソッド処理
の流れは,次の通りである。
(1) 従業員のデータを生成し,配列 employees に設定する。
(2) 従業員のデータを出力する。
(3) 新しい給与計算を行う。
(4) 整列処理を行う。
(5) 整列結果を出力する。
public class OKadai090714_StaffSort {
public static void main(String args[]) {
【***** A *****】
Employee Taro = new Employee("Taro",75000,new Date(1977,11,15));
Employee Tom = new Employee("Tom",50000,new Date(1974,8,20));
Employee Jiro = new Manager("Jiro",90000,new Date(1970,7,15));
Employee Brown = new Manager("Brown",80000,new Date(1977,9,20));
【***** B *****】
Employee[] employees = {Taro,Tom,June,Jiro,Brown};
【***** C *****】
for (int i=0 ; i<employees.length ; i++) {
System.out.println(employees[i]);
}
【***** D *****】
new NewSalary(employees).computeSalary();
【***** E *****】
new SortableArray(employees).sort();
System.out.println(" sorted by salary ");
【***** F *****】
for (int i=0 ; i<employees.length ; i++) {
System.out.println(employees[i]);
}
}
}
以下のコメント文を適切な【A】~【F】に書き入れよ
// 従業員のインスタンスを作成
// 従業員を給与順に並べ替える
// 従業員を配列に格納
// 新しい順序で従業員を表示する
// 従業員一覧を表示
// 従業員の配列を渡し、給与を更新する
図1 main メソッド例
$ java OKadai090714_StaffSort
Taro,salary is 75000.0,hired on 1977/11/15
Tom,salary is 50000.0,hired on 1974/8/20
Jiro,salary is 90000.0,hired on 1970/7/15
Brown,salary is 80000.0,hired on 1977/9/20
sorted by salary
Jiro,salary is 94500.0,hired on 1970/7/15
Taro,salary is 90000.0,hired on 1977/11/15
Brown,salary is 84000.0,hired on 1977/9/20
Tom,salary is 60000.0,hired on 1974/8/20
図2 実行結果例
// 入社年度を表すクラス
class Date{
//入社年度を表すクラスであり,入社年月日を文字列で返す toString メソッドを実装
int y; int m; int d;
Date(int y,int m,int d){
this.y=y; this.m=m; this.d=d;
}
public String toString(){
return y+"/"+m+"/"+d;
}
}
interface Sortable {
// 整列処理において,データの大小関係を比較するための compareTo メソッド
public int compareTo(Sortable b);
}
//
一般従業員を表すクラス
class Employee implements 【*****a*****】{
String name;
double salary;
Date hiredDay;
public Employee(String name, double salary, Date hiredDay) {
this.name = name;
this.salary = salary;
this.hiredDay = hiredDay;
}
public String toString() {
return name+",salary is "+salary+",hired on "+hiredDay;
}
public double getBaseup() {
return 0.2;
}
public int compareTo(Sortable b) {
Employee eb = ( 【***** b ****】) b;
if (salary < eb.salary) return 1 ;
if (salary > eb.salary) return -1 ;
return 0 ;
}
}
// 管理職を表すクラス
class Manager extends Employee {
public Manager(String name,double salary,Date hiredDay) {
super( name,salary,hiredDay);
}
public double getBaseup() {
return 0.05;
}
}
// 新しい給与を計算のためのクラス
class NewSalary {
Employee[] a;
NewSalary(Employee[] a) {
this.a = a;
}
public void computeSalary() {
int i;
for(i=0;i<a.length;i++) a[i].【 **** c ****】 *= 1+a[i].getBaseup();
}
}
// 整列処理に関するクラス
class SortableArray {
Sortable[] a;
SortableArray(Sortable[] a) {
this.a = a;
}
public void sort() {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < i; j++) {
if (a[i].【**** d ****】(a[j]) < 0) {
Sortable temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
}
}
Fly UP