java 程序 动物奥运会
初二 记叙文 3348字 179人浏览 处女座好运来了

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<Animal,String> tm1 = new HashMap<Animal,String>();

private TreeMap<Animal,String> tm2 = new TreeMap<Animal,String>(new Comparator<Animal>(){

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<Animal> 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<Animal> 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;

/**

* 游水类

* @author liuwei

*

*/

//游水类

public class Swimming extends Animal {

private double factor = 0.4;//运动系数

public Swimming(String name, double value) { super(name, value);

this.speed = this.factor * value; }

public double getSpeed() {

return this.speed;

}

public double time(Runway r) {

double n;

n = r.getLength()/this.speed; return n;

}

}

package com.animalMatch;

/**

* 定义爬行类,继承动物类

* @author liuwei

*

*/

//爬行类

public class Reptiles extends Animal {

private double factor = 0.8;//运动系数

public Reptiles(String name, double value) { super(name, value);

this.speed = this.factor * value; }

public double getSpeed() {

return this.speed;

}

public double time(Runway r) {

double n;

n = r.getLength()/this.speed; return n;

}

}

package com.animalMatch;

/**

* 定义飞行类,继承动物类

* @author liuwei

*

*/

public class Flight extends Animal {

private double factor = 0.98;//运动系数 public Flight(String name, double value) { super(name, value);

this.speed = this.factor * value; }

public double getSpeed() {

return this.speed;

}

public double time(Runway r) { double n;

n = r.getLength()/this.speed; return n;

}

}

package com.animalMatch;

public class Runway {

private double length;

public Runway(double d) {

this.length = d;

}

public double getLength() {

return this.length;

}

}

实验结果:

第1名: 龙骑士 31.25秒

第2名: 冥界亚龙 102.04秒

第3名: 影魔 208.33秒

第4名: 鱼人守卫 277.78秒

第5名: 炸弹人 1250.00秒