Код: Выделить всё
import java.util.Random;
class Monster2 {
private String type, name;
private int level, size, strength, hitPoints; //* these were set by the professor *//
public Monster2() {
this.name = "none";
this.level = 1;
this.size = 1;
this.strength = 1;
this.hitPoints = 1;
}
public Monster2(String inType, String inName, int inLevel) {
this.type = inType;
this.name = inName;
this.level = inLevel;
this.size = 1;
this.strength = calcSTR();
this.hitPoints = calcHP();
}
public static void main(String[] args) {
Monster monster = new Monster();
}
public void setType (String inType) { //* set by the professor
type = inType;
}
public void setName (String inName) {
name = inName;
}
public void setLevel (int inLevel) { //* set by the professor
level = inLevel;
}
public void setSize (int size) {
size = 1;
}
public void setStrength (int calcSTR) {
strength = calcSTR;
}
public void setHitPoints (int calcHP) {
hitPoints = calcHP;
}
public String getType() {
return type;
}
public String getName() {
return name;
}
public int getLevel() {
return level;
}
public int getSize() {
return size;
}
public int getStrength() {
return strength;
}
public int getHitPoints() {
return hitPoints;
}
public int calcSTR() {
Random rand = new Random();
int randInt = rand.nextInt(8) + 3;
return strength = randInt + (level * size);
}
public int calcHP() {
return hitPoints = strength * level / 2;
}
public String toString() { //* all of this was set by professor
String outStr = "";
outStr += name + " (";
outStr += type + ")\n";
outStr += "LVL: " + level + ", ";
outStr += "SZ: " + size + ", ";
outStr += "STR: " + strength + ", ";
outStr += "HP: " + hitPoints + "\n\n";
return outStr;
}
}
Создайте класс с именем «Dragon» в файле с именем Dragon. Java, который расширяет Monster и выполняет следующие действия: Объявляет целочисленную константу BASE_STRENGTH, для которой установлено значение 50.
Создает метод переопределения CalcSTR, который возвращает целое число, определяющее силу Дракона как значение BASE_STRENGTH плюс 1/3. размера, умноженного на уровень
Имеет конструктор для Dragon, которому требуются три параметра: строка для имени, целое число для уровня и целое число для размера
Использует конструктор перегрузки родительского Monster вместе со значениями параметров как передается и использует значение «Дракон» в качестве типа.
Использует метод переопределения CalcSTR класса Дракона для определения и установки силы Дракона.
Устанавливает очки жизни на основе CalcHP из родительского класса.
Оно оставляет открытым вопрос о том, что делать с размером, поэтому я просто установил его на 3.
Это то, что я есть для класса Дракон
Код: Выделить всё
public class Dragon extends Monster2 {
public static final int BASE_STRENGTH = 50;
public Dragon (String inName, int inLevel, int size) {
super("Dragon", inName, inLevel);
this.size = 3;
this.setStrength((int)(BASE_STRENGTH + (inLevel * size) / 3));
this.setHitPoints(calcHP());
}
@Override
public int calcSTR () {
return strength = BASE_STRENGTH + (inLevel * size) / 3;
}
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... es-in-java