public class TookClay {
public static void main(String[] args){
Student[] class1 = {
new Student(10,10,2,"Jill"),
new Student(5,3,10,"Will"),
new Student(5,5,10,"Bill")
};
Student[] class2 = {
new Student(2,4,10,"Cam"),
new Student(4,3,7,"Sam"),
new Student(8,11,1,"Graham"),
new Student(6,2,7,"Pam")
};
System.out.println(tookTo(class1)+" took clay from "+tookFrom(class1)+".");
System.out.println(tookTo(class2)+" took clay from "+tookFrom(class2)+".");
}
public static int ave(Student[] cla){
int sum = 0;
for(Student stu:cla){
sum += stu.value();
}
return sum / cla.length;
}
public static String tookTo(Student[] cla){
for(int i=0;i<cla.length;i++){
if(cla[i].value()>ave(cla))
return cla[i].name;
}
return null;
}
public static String tookFrom(Student[] cla){
for(int i=0;i<cla.length;i++){
if(cla[i].value()<ave(cla))
return cla[i].name;
}
return null;
}
}
class Student {
int l;
int b;
int h;
String name;
public Student(int l,int b,int h,String name) {
this.l = l;
this.b = b;
this.h = h;
if(name.length()>8){
this.name = name.substring(0, 8);
} else {
this.name = name;
}
}
public int value(){
return l*b*h;
}
} |
|