JAVA Lab 5 Problem-2 答案与解析

这篇博客介绍了如何在Java中创建抽象的'Monster'类,并实现龙(Dragon)和巨魔(Troll)子类,它们分别拥有独特的特殊攻击。通过构造函数和概率判断战斗行为,展示了面向对象编程在怪物角色设计中的应用。
摘要由CSDN通过智能技术生成

在这里插入图片描述

package problem2;

public abstract class Monster
{
	String name;
	double spAttackPropability = 0.2;

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

	}

	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 abstract int specialAttack();

	public final int attack()
	{
		int damage;
		// 题目要求 如果生成的随机数小于spAttackPropability 则使用specialAttack(),否则 使用 常规攻击
		double probability = Math.random();
		if (probability < spAttackPropability)
		{
			damage = specialAttack();
		} else
		{
			damage = (int) (Math.random() * 5 + 1);
			System.out.println(this.name + " ," + "of type" + getClass() + ", attacks generically: " + damage
					+ " points damage caused.");
		}

		return damage;

	}

}

Note: 要注意special 和 常规攻击的关系,把这二者的关系判断都写到 这个Monster(父类)当中是一个非常好的选择!!
为什么不在Monster里建立 constructor? 因为题目中要求:provide two constructor for each kind of monster!

package problem2;

public class Dragon extends Monster
{

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

	public Dragon(String name, double spAttackPropability)
	{
		this.name = name;
		this.spAttackPropability = spAttackPropability;
	}

	@Override
	public int specialAttack()
	{
		int x = (int) (Math.random() * 50 + 1);
		System.out.println(this.name + " ," + "of type" + getClass() + ", attacks by breathing fire: " + x
				+ " points damage caused.");
		return x;
	}

}

package problem2;

public class Troll extends Monster
{
	public Troll(String name)
	{

		if (name.equals("Saul") || name.equals("Salomon"))
		{
			System.out.println("Error: name is illegal.");
			this.name = "Detritus";
		} else
		{
			this.name = name;
		}
	}

	public Troll(String name, double spAttackPropability)
	{
		this(name);
		this.spAttackPropability = spAttackPropability;
	}

	public int specialAttack()
	{
		int x = (int) (Math.random() * 15 + 1);
		System.out.println(this.name + " ," + "of type" + getClass() + ", attacks by hitting with a stick " + x
				+ " points damage caused.");
		return x;
	}

}

Note:在Troll 类中,需要判断 命名是否正确
在 public Troll(String name) 和 public Troll(String name, double spAttackPropability)
都需要判断,但是在 public Troll(String name, double spAttackPropability) 可以通过使用
this(name) 调用public Troll(String name) 判断命名正确与否

package problem2;

import java.util.ArrayList;

public class TestingMonster
{
	public static void main(String[] args)
	{
		ArrayList<Monster> monsters = new ArrayList<>();

		monsters.add(new Dragon("Dragon1"));
		monsters.add(new Dragon("Dragon2"));

		monsters.add(new Troll("Troll1"));
		monsters.add(new Troll("Saul", 20));

//		System.out.println(monsters.get(3).spAttackPropability);
//		System.out.println(monsters.get(3).name);

		int damageDone = 0;
		while (damageDone < 100)
		{
			for (Monster m : monsters)
			{
				m.move((int) (Math.random() * 4) + 1);
				damageDone = damageDone + m.attack();
			}
		}
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值