一道程序设计题,望高手相助!

zcxwlh 2011-02-23 11:33:44
设名为staff的包中包含两个类,Employee和Manager,其中Manager为Employee的子类,定义如下:

class Employee {
String name; //雇员姓名
int age; //年龄
char grade; //业绩评等,从高到低为A,B,C,D四等。
}

class Manager extends Employee {
String department; //Manager所管辖部门
Employee[] subm; //下属雇员
}

请在包中定义一个名为TopManager的类,在该类中定义若干Employee和Manager的样本值,然后写一方法,找出所有Manager中其下属雇员平均业绩评等最高的Manager姓名。
...全文
94 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
peggy0135 2011-02-24
  • 打赏
  • 举报
回复

public class Employee
{
protected String name;
private int age;
protected char grade;

public Employee( String n, int a, char g )
{
name = n;
age = a;
grade = g;
}

public String getName()
{
return name;
}

public int getAge()
{
return age;
}

public char getGrade()
{
return grade;
}

public void printInfo()
{
System.out.println( "name: " + getName() );
System.out.println( "age: " + getAge() );
System.out.println( "grade: " + getGrade() );
}
}



public class Manager extends Employee
{
private String department;
private double average;
private Employee[] subm = new Employee[10];
private int pos;

public Manager( String name, int age, char grade, String department )
{
super( name, age, grade );
this.department = department;
pos = 0;
}

public void addEmployee( Employee workman )
{
subm[ pos ] = workman;
pos++;
}
public void printManager()
{
System.out.println( "name: " + name );
System.out.println( "department: " + department );
System.out.println( "grade: " + grade );
}
public String getName()
{
return name;
}

public void printInfo()
{
System.out.println( "department: " + department );
System.out.println( "manager: " + name );
System.out.println( "work men: " );
for( int i = 0; i < subm.length; i++ )
{
subm[i].printInfo();
System.out.println();
}

System.out.println( "-----------" );
}

public void countAverage()
{
int sum = 0;

for( int i = 0; i < subm.length; i++ )
{
sum += subm[i].getGrade();
}

average = sum / subm.length;
}

public void sortGrade()
{
boolean change = true;
while( change )
{
change = false;

for( int i = 0; i < subm.length-1; i++ )
{
int g1, g2 = 0;
g1 = subm[i].getGrade();
g2 = subm[i+1].getGrade();
if( g1 > g2 )
{
Employee temp = subm[i+1];
subm[i+1] = subm[i];
subm[i] = temp;
change = true;
}
}
}
}

public char getGrade()
{
int theAverage = (int) average;
switch( theAverage )
{
case 4:
grade = 'A';
break;

case 3:
grade = 'B';
break;

case 2:
grade = 'B';
break;

case 1:
grade = 'B';
break;
}

return grade;
}
}


public class TopManager
{
private String name;
private int pos;
private Manager[] subManager = new Manager[3];

public TopManager( String name )
{
this.name = name;
}

public void addManager( Manager manager )
{
subManager[pos] = manager;
pos++;
}

public void sortGrade()
{
boolean change = true;
while( change )
{
change = false;

for( int i = 0; i < subManager.length-1; i++ )
{
int g1, g2 = 0;
g1 = subManager[i].getGrade();
g2 = subManager[i+1].getGrade();
if( g1 > g2 )
{
Manager temp = subManager[i+1];
subManager[i+1] = subManager[i];
subManager[i] = temp;
change = true;
}
}
}
}

public void printInfo()
{
System.out.println( "generl manager: " + name );
for( int i = 0; i < subManager.length; i++ )
{
subManager[i].printInfo();
System.out.println( "-----------" );
}
System.out.println();
}

public void showBest()
{
subManager[subManager.length-1].printManager();
}
}


public class Company
{
public static void main( String[] args )
{
TopManager topmanager = new TopManager( "Lee" );

Manager manager1 = new Manager( "Iee", 35, 'A', "produce");
Manager manager2 = new Manager( "Jee", 36, 'A', "sale");
Manager manager3 = new Manager( "kee", 39, 'A', "service");

topmanager.addManager( manager1 );
topmanager.addManager( manager2 );
topmanager.addManager( manager3 );

for( int i = 0; i < 10; i++ )
{
manager1.addEmployee( new Employee( "wang", 21+i, 'A' ) );
}

for( int i = 0; i < 10; i++ )
{
manager2.addEmployee( new Employee( "Chang", 19+i, 'B' ) );
}

for( int i = 0; i < 10; i++ )
{
manager3.addEmployee( new Employee( "Liue", 21+i, 'A' ) );
}

manager1.countAverage();
manager1.sortGrade();
manager2.countAverage();
manager2.sortGrade();
manager3.countAverage();
manager3.sortGrade();

topmanager.sortGrade();
topmanager.printInfo();
topmanager.showBest();
}
}


大大要的話可以再加一些讓使用者操作的功能
mucrea 2011-02-23
  • 打赏
  • 举报
回复
这个是设计模式里面的一个经典案例,应该是访问者模式。你可以细细查查!
peggy0135 2011-02-23
  • 打赏
  • 举报
回复
正在思考
請教大大要怎麼PO
java的souce code
peggy0135 2011-02-23
  • 打赏
  • 举报
回复
樓上
請教一下
它的grade是char的型態耶
為什麼可以當float來加????
star95hmz 2011-02-23
  • 打赏
  • 举报
回复
public class Employee {
String name; //雇员姓名
int age; //年龄
char grade; //业绩评等,从高到低为A,B,C,D四等。
public Employee(String name,int age,char grade){
this.name=name;
this.age=age;
this.grade=grade;
}
}


public class Manager extends Employee {
public Manager(String name, int age, char grade,String department,Employee[] subm) {
super(name,age,grade);
this.department=department;
this.subm=subm;

}
String department; //Manager所管辖部门
Employee[] subm; //下属雇员
}


public class TopManager {
Employee Employee1=new Employee("zhangsan1", 21, 'A');
Employee Employee2=new Employee("zhangsan2", 22, 'B');
Employee Employee3=new Employee("zhangsan3", 23, 'C');
Employee Employee4=new Employee("zhangsan4", 24, 'D');
Employee Employee5=new Employee("zhangsan5", 25, 'A');
Employee Employee6=new Employee("zhangsan6", 26, 'B');
Employee Employee7=new Employee("zhangsan7", 27, 'C');
Employee Employee8=new Employee("zhangsan8", 28, 'D');
Employee Employee9=new Employee("zhangsan9", 29, 'A');
Employee[] subm1={Employee1,Employee2,Employee3,Employee4};
Employee[] subm2={Employee5,Employee6,Employee7,Employee8,Employee9};
Manager Manager1=new Manager("Manager1", 31, 'A', "sale", subm1);
Manager Manager2=new Manager("Manager2", 32, 'B', "produce", subm2);

public static void main(String[] args) {
TopManager TopManager1=new TopManager();
Manager ManagerTop=TopManager1.find();
System.out.println("下属雇员平均业绩评等最高的Manager是:"+ManagerTop.department+"的"+ManagerTop.name);
}
/*
* find方法是找出其下属雇员平均业绩评等最高的Manager
* */
public Manager find(){
float avg1,avg2;
int sum1=0,sum2=0;
for(int i=0;i<subm1.length;i++){
sum1=sum1+subm1[i].grade;
}
avg1=(float)sum1/(float)subm1.length;
for(int i=0;i<subm2.length;i++){
sum2=sum2+subm2[i].grade;
}
avg2=(float)sum2/(float)subm2.length;
//由于本方法是用的是字符型的转为整型的然后求的平均值,所以小的值结果等级高
if(avg1<avg2){
return Manager1;
}
if(avg1>=avg2){
return Manager2;
}
return Manager1;
}
}

23,405

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧