java Lab5

在这里插入图片描述
在这里插入图片描述

package dome;

// import java.util.*;

public class Monster {
    protected String name;

    public Monster() {
        this.name = "lfz";
    }

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

    public int attack() {
        System.out.print("NAME," + this.name + " of type CLASS_TYPE, " + this.getClass() + " attacks generically: ");
        int ret = 0;
        while (true) {
            ret = (int) (Math.random() * 10);
            if (ret >= 1 && ret <= 5) {
                System.out.println(ret + " points damage caused.");
                break;
            }
        }
        return ret;

    }

    public void move(int direction) {
        switch (direction) {
        case 1:
            System.out.println(this.name + "is moving 1 step NORTH.");
            break;
        case 2:
            System.out.println(this.name + "is moving 1 step EAST.");
            break;
        case 3:
            System.out.println(this.name + "is moving 1 step SOUTH.");
            break;
        default:
            System.out.println(this.name + "is moving 1 step WEST.");
            break;
        }
    }

    public static void main(String[] args) {
        Monster m = new Monster();
        int x = m.attack();
        Dragon d= new Dragon();
        d.attack();
        // double x=Math.random()*10;
        // System.out.println(x);
        // if(x>=3){
        //     System.out.println("good");
        // }
    }
}

第二小问

package dome;
//第二问
public class Dragon extends Monster {

    public Dragon() {

    }

    @Override
    public int attack() {
        int ret = 0;
        int random_num = 0;
        random_num = (int) (Math.random() * 10);
        if (random_num >= 3) {
            System.out
                    .print("NAME," + this.name + " of type CLASS_TYPE, " + this.getClass() + " attacks generically: ");

            while (true) {
                ret = (int) (Math.random() * 100);
                if (ret >= 1 && ret <= 50) {
                    System.out.println(ret + " points damage caused.");
                    return ret;
                }
            }
        } else {
            System.out
                    .print("NAME," + super.name + " of type CLASS_TYPE, " + this.getClass() + " attacks fire: ");

            ret = 0;
            while (true) {
                ret = (int) (Math.random() * 10);
                if (ret <= 50 && ret >= 1) {
                    System.out.println(ret+" points damage caused.");
                    return ret;
                }
            }
        }
    }

    // @Override
    // public int attack(){
    // System.out.println("Scratching");
    // return 1;
    // }
}

第三小问

package dome;

public class TestingMonsters {
    public static void main(String[] args){
        Dragon d= new Dragon();
        d.attack();
        Troll t=new Troll("Saul");
        System.out.println(t.getName());
    }
}

题目给的测试案例

package dome;

import java.util.*;

/**
 * Title : TestingMonsters.java Description: This class is the test class for
 * Monsters.
 * 
 * @author Laurissa Tokarchuk
 * @version 1.0
 * @author Paula Fonseca
 * @version 1.1
 */
public class TestingMonsters {
  public static void main(String[] args) {
    ArrayList<Monster> monsters = new ArrayList<Monster>();
    monsters.add(new Monster("Tom"));
    monsters.add(new Monster("George"));
    monsters.add(new Dragon("Smaug"));
    monsters.add(new Dragon("Jabosh"));
    monsters.add(new Troll("Salomon"));
    monsters.add(new Troll("Bender"));
    int damageDone = 0;
    while (damageDone < 100) {
      for (Monster m : monsters) {
        m.move((int) (Math.random() * 4) + 1);
        damageDone += m.attack();
      }
    }
  }
}

在这里插入图片描述
Monster类


// import java.util.*;

public abstract class Monster {
    public double spAttackProbability = 0.2;
    protected String name;

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

    public Monster(String name,double spAttackProbability){
        this.name=name;
        this.spAttackProbability=spAttackProbability;
    }



    public abstract int specialAttack();

    final public int attack() {
        System.out.print("NAME," + this.name + " of type CLASS_TYPE, " + this.getClass() + " attacks generically: ");
        int ret = 0;
        while (true) {
            ret = (int) (Math.random() * 10);
            if (ret >= 1 && ret <= 5) {
                System.out.println(ret + " points damage caused.");
                break;
            }
        }
        return ret;

    }

    final public void move(int direction) {
        switch (direction) {
        case 1:
            System.out.println(this.name + "is moving 1 step NORTH.");
            break;
        case 2:
            System.out.println(this.name + "is moving 1 step EAST.");
            break;
        case 3:
            System.out.println(this.name + "is moving 1 step SOUTH.");
            break;
        default:
            System.out.println(this.name + "is moving 1 step WEST.");
            break;
        }
    }

    public static void main(String[] args) {

        // Monster m = new Monster();
        // int x = m.attack();
        Dragon d= new Dragon("lfz");
        d.attack();
        // // double x=Math.random()*10;
        // // System.out.println(x);
        // // if(x>=3){
        // // System.out.println("good");
        // // }
    }
}

testingMonster


import java.util.*;

/**
 * Title : TestingMonsters.java Description: This class is the test class for
 * Monsters.
 * 
 * @author Laurissa Tokarchuk
 * @version 1.0
 * @author Paula Fonseca
 * @version 1.1
 */
public class TestingMonsters {
    public static void main(String[] args) {
        ArrayList<Monster> monsters = new ArrayList<Monster>();
        // monsters.add(new Monster("Tom"));
        // monsters.add(new Monster("George"));
        monsters.add(new Dragon("Smaug"));
        monsters.add(new Dragon("Jabosh"));
        monsters.add(new Troll("Salomon"));
        monsters.add(new Troll("Bender"));
        monsters.add(new Dragon("LFZ", 0.1));
        int damageDone = 0;
        while (damageDone < 1000) {
            for (Monster m : monsters) {
                m.move((int) (Math.random() * 4) + 1);
                double ret = (double) Math.random();
                if (ret > m.spAttackProbability) {
                    damageDone += m.specialAttack();
                } else {
                    damageDone += m.attack();
                }
            }
        }
    }
}

Troll类

// // 第三问
public class Troll extends Monster {

    public Troll(String name) {
        super(name);
        if (name.equals("Saul") || name.equals("Salomon")) {
            this.name = "Detritus";
            System.out.println("error !!! ");
        } else {
            this.name = name;
        }
    }


    @Override
    public int specialAttack() {
        System.out.print("NAME," + this.name + " of type CLASS_TYPE, " + this.getClass() + " special Attack: ");

        int ret = 0;
        while (true) {
            ret = (int) (Math.random() * 100);
            if (ret >= 1 && ret <= 15) {
                System.out.println("stick "+ret+" points damage caused.");
                break;
            }
        }
        return ret;
    }

    // public String getName() {
    // return this.name;
    // }
}

Dragon类

// // // package dome;

public class Dragon extends Monster {

    public Dragon(String name) {
        super(name);
    }

    public Dragon(String name, double spAttackProbability) {
        super(name, spAttackProbability);
    }

    @Override
    public int specialAttack() {
        System.out.print("NAME," + this.name + " of type CLASS_TYPE, " + this.getClass() + " special Attack: ");

        int ret = 0;
        while (true) {
            ret = (int) (Math.random() * 100);
            if (ret >= 1 && ret <= 50) {
                System.out.println("breath fire "+ret+" points damage caused.");
                break;
            }
        }
        return ret;
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值