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