主题:[讨论]请教一道题目,大家帮帮忙!急!!!
xiaoTT
[专家分:10] 发布于 2008-06-26 14:37:00
设名为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姓名。
回复列表 (共4个回复)
沙发
risky1223 [专家分:0] 发布于 2008-06-26 17:44:00
你的那个平均业绩要怎么算,是不是加个权数???要不然都不好算平均业绩!!
板凳
xiaoTT [专家分:10] 发布于 2008-06-26 18:55:00
用不用都可以。题目里也没说。
3 楼
happyboy2007 [专家分:3900] 发布于 2008-06-27 11:33:00
import java.util.Random;
class Employee
{
String name; //雇员姓名
int age; //年龄
char grade; //业绩评等,从高到低为A,B,C,D四等。
}
class Manager extends Employee
{
String department; //Manager所管辖部门
Employee[] subm; //下属雇员
}
public class TopManager
{
private Random r = new Random();
int nemp ;
int nman ;
Manager [] man ;
Employee [] emp;
public TopManager()
{
this.nemp = r.nextInt(51)+50;
this.nman = r.nextInt(3)+3;
System.out.println("经理:"+nman+"人。雇员:"+nemp+"人。");
this.man = new Manager[nman];
this.emp = new Employee[nemp];
Employee [][] eemp = this.splitEmpToMan();
for(int i=0;i<eemp.length;i++)
{
for(int j=0;j<eemp[i].length;j++)
{
eemp[i][j] = new Employee();
eemp[i][j].name = "雇员"+i;
eemp[i][j].age = r.nextInt(13)+18;
eemp[i][j].grade = (char)(r.nextInt(4)+65);
}
}
for(int i=0;i<eemp.length;i++)
{
System.out.print("经理"+i+"下所有雇员的级别:");
for(int j=0;j<eemp[i].length;j++)
{
System.out.print(eemp[i][j].grade+"\t");
}
System.out.println();
}
for(int i=0;i<man.length;i++)
{
man[i] = new Manager();
man[i].name = "经理"+i;
man[i].age = r.nextInt(21)+30;
man[i].grade = (char)(r.nextInt(2)+65);
man[i].subm = eemp[i];
}
}
public Employee[][] splitEmpToMan()
{
int per = nemp/nman;
int rest = nemp-nman*per;
Employee [][] emp = new Employee[nman][];
for(int i=0;i<emp.length;i++)
{
emp[i] = new Employee[per];
if(i==emp.length-1)
emp[i] = new Employee[per+rest];
}
return emp;
}
public String getMaxGradeName()
{
int [] totalgrade = new int [nman];
double [] dg = new double[nman];
for(int i=0;i<nman;i++)
{
int total = 0;
Employee [] emp = this.man[i].subm;
for(int j=0;j<emp.length;j++)
{
total += emp[i].grade;
}
totalgrade[i] = total;
dg[i] = total*1.0/emp.length;
}
double max = dg[0];
for(int i=1;i<dg.length;i++)
{
if(max>dg[i])
max = dg[i];
}
for(int i=0;i<dg.length;i++)
{
if(max==dg[i])
return man[i].name;
}
return "";
}
}
class Test
{
public static void main(String[] args)
{
TopManager topman = new TopManager();
System.out.println("属下雇员平均级别最高的经理名称:"+topman.getMaxGradeName());
}
}
4 楼
xiaoTT [专家分:10] 发布于 2008-06-27 19:25:00
谢谢楼上那位大哥了!非常感谢!
我来回复