【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《java 程序 动物奥运会》,欢迎阅读!

package com.animalMatch; /**
* 动物奥运会
* @author liuwei
* @date 2010-04-29 */
public class Client {
public static void main(String[] args) {
Runway r = new Runway(100.00) ;//100米赛道
Animal a1 = new Flight("冥界亚龙",1.0);
Animal a2 = new Reptiles("影魔",0.6);
Animal a3 = new Swimming("鱼人守卫",0.9);
Animal a4 = new Reptiles("龙骑士",4.0);
Animal a5 = new Reptiles("炸弹人",0.1);
Committee o = new Committee(r);
o.put(a1);
o.put(a2);
o.put(a3);
o.put(a4);
o.put(a5);
o.game();
o.output();
} }
package com.animalMatch; /**
* 抽象动物类
* @author liuwei *
*/
public abstract class Animal {
private String name;//动物的名字
private double value;//动物的翅膀长度,四肢强壮度,尾巴长度
double speed;
public Animal(String name, double value) {
this.name = name;
this.value = value;
}
public String getName() {
return this.name;
}
public double getValue() {
return this.value; }
public abstract double getSpeed();
public abstract double time(Runway r); }
package com.animalMatch;
import java.util.*;
import java.text.DecimalFormat; public class Committee { private Runway r;
private Map tm1 = new HashMap<Animal,String>(); private TreeMap
tm2
=
new TreeMap(new
Comparator(){
public int compare(Animal o1, Animal o2) { if(o1.time(r) > o2.time(r)) return 1; else
return -1; } } );
public Committee(Runway r){ this.r = r ; }
public void put(Animal o1) {//报名
this.tm1.put(o1, o1.getName()); }
public void game(){//组织比赛
Set keySet = this.tm1.keySet(); for(Animal o:keySet)
this.tm2.put(o, o.getName()); }
public void output() {//输出
DecimalFormat df1=new DecimalFormat("0"); String as=df1.format(r.getLength()); System.out.println(as+"米赛跑成绩"); int i = 1;
Set keySet = this.tm2.keySet(); for(Animal o:keySet){
DecimalFormat df=new DecimalFormat("0.00"); as=df.format(o.time(r));
System.out.println("第"+i+"名: "+o.getName()+ " "+as+"秒"); i++; } } }
package com.animalMatch; /**
本文来源:https://www.wddqxz.cn/fd714817866fb84ae45c8d00.html