oop - Java - Lists and methods -
i have abstract class named spell. spell class suppose have few subclasses, damagespell, changestatspell etc.
the problem technical - in damagespell class - right after declaration of damage method - says can't find getspell (java can't find symbol : method getspell(). how can fix this? i'd appreciate code example describes how fix it. thanks.
abstract public class spell { private string name; private int spelllevel; private int manacost; spell(string name, int spelllevel , int manacost){ this.name = name; this.spelllevel = spelllevel; this.manacost = manacost; } string getspellname(){ return name; } int getspelllevel() {return spelllevel; } int getmanacost(){ return manacost; } }
public class damagespell extends spell { private int n; private int dice; private int base; private int steplevel; private int maxcasterlevel; damagespell(string name, int spelllevel, int manacost, int n, int dice, int base, int steplevel, int maxcasterlevel){ super(name, spelllevel, manacost); this.n = n; this.dice = dice; this.base = base; this.steplevel = steplevel; this.maxcasterlevel = maxcasterlevel; } static list<damagespell> damagespellslist = new arraylist<damagespell>(); static { damagespellslist.add(new damagespell("magic missiles", 1, 2, 1, 4, 1, 2, 9)); // damagespellslist.add(new damagespell("scorching ray ", 2, 4)); damagespellslist.add(new damagespell("fireball", 3, 6, 1, 6, 1, 1, 10)); //damagespellslist.add(new damagespell("ice storm", 4, 8)); } public static void damage(string spellname, character attacker, character target){ damagespell spell = damagespellslist.getspell(spellname); int damage = 0; int casterlevel = math.min(attacker.getlevel(), spell.maxcasterlevel); int totalrolls = (casterlevel-spell.base)%spell.steplevel; (int = 1; <= totalrolls ; i++) { damage += diceroller.roll(spell.n, spell.dice); } target.setcurrenthp(target.getcurrenthp() - damage); system.out.println(" dealt " + damage + " enemy"); } public damagespell getspell(string spellname) { try { (iterator<damagespell> iter = damagespellslist.iterator(); iter.hasnext(); ) { damagespell spell = iter.next(); if (spellname.equals(spell.getspellname())) { return spell; } } } catch (exception e){ system.out.println(spellname + " haven't been found in spells-list"); return null; } return null; } }
your getspell(" ") problem quite simple. doing damagespellslist.getspell(spellname);
, damagespellslist
list<t>
, , lists don't have getspell()
method (or collection in general).
what have access object
list storing @ index of list<t>
using list#get(i)
or iterating on list
entirely using loop or iterator
.
Comments
Post a Comment