JAVA Lab 5 Problem-2 答案与解析

在这里插入图片描述

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();
			}
		}
	}

}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HCIE-R&S Lab-Option-C1是华为公司为网络工程师设计的一项实验室选项。该实验室选项是华为高级认证网络工程师(HCIE)路由与交换方向的实验室考试之一。 HCIE-R&S Lab-Option-C1的主要目的是测试网络工程师在实际网络环境中配置和故障排除的能力。实验室选项C1主要涉及路由器、交换机、网络安全、IPv6等方面的知识与技能。通过这一实验室选项的考试,网络工程师需要证明他们具备在各种复杂网络情境下解决实际问题的能力。 在实验室选项C1中,考试的内容主要包括网络规划与设计、网络配置与部署、网络故障排除与恢复等方面。考生需要使用华为的设备和平台,在虚拟化环境中完成一系列的实验任务。考试会在一个特定的时间内进行,考生需要在规定的时间内完成实验任务并提交相应的实验报告。 通过HCIE-R&S Lab-Option-C1的考试,网络工程师可以获得HCIE认证,并证明他们具备高级路由与交换方面的技能和知识。这个认证可以有效提升网络工程师的职业竞争力,使他们在网络行业中脱颖而出。 总体而言,HCIE-R&S Lab-Option-C1是华为为网络工程师提供的一项实验室选项,旨在测试他们在实际网络环境中的能力。这个选项涵盖了路由器、交换机、网络安全等方面的知识与技能,并通过考试来评估网络工程师的实际运用能力。它对网络工程师职业发展有着积极的意义。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值