java - I have to write a fighter class to be used with my assignment program and it is returning errors that I cannot figure out -
import java.util.scanner; public class assignment5 { public static void main(string[] arg) { fighter myfighter, enemyfighter; scanner console = new scanner(system.in); int num1, num2, num3; string str, another; system.out.println ("*** fighter game ***"); { system.out.println("create fighter (type 3 integers + name): "); num1 = console.nextint(); num2 = console.nextint(); num3 = console.nextint(); str = console.next(); if (num1 + num2 + num3 == 10) { myfighter = new fighter (num1, num2, num3, str); enemyfighter = new fighter( ); enemyfighter.setname ("enemy"); system.out.print( myfighter.getname()+" ["+myfighter.getpower()+","+myfighter.getspeed()+","+myfighter.getheal()+"] "); system.out.print( enemyfighter.getname()+" ["+enemyfighter.getpower()+","+enemyfighter.getspeed()+","+enemyfighter.getheal()+"] "); system.out.println(); int fights = 0; boolean gameover= false; while (fights < 10 && !gameover){ system.out.print("fight[" + fights + "]: "); myfighter.attack (enemyfighter); myfighter.heal (); enemyfighter.attack(myfighter); enemyfighter.heal(); myfighter.printinfo(); enemyfighter.printinfo(); if (enemyfighter.isdead() || myfighter.isdead()) gameover = true; fights ++; system.out.println(); } if(myfighter.gethealth() > enemyfighter.gethealth()) system.out.println(" win"); else system.out.println("you lost"); system.out.println(); system.out.print("play fight (y/n)? "); = console.next(); } else { system.out.println("invalid inputs. total of 3 numbers should 10."); = console.next(); } } while (another.equalsignorecase("y")); } }
fighter.java class
public class fighter { private int power; private int speed; private int heal; private int health; private string name; public fighter (int num1, int num2, int num3, string str) { } public string getname() { return name; } public int getpower() { return power; } public int getspeed() { return speed; } public int getheal() { return heal; } public int gethealth() { return health; } public void setname(string newname) { } public void setpower(int newpower) { } public void setspeed(int newspeed) { } public void setheal(int newheal) { } public void sethealth(int newhealth) { } public boolean isdead() { if(health <= 0) { return true; } else { return false; } } public void heal() { } public void printinfo() { } public void attack() { } }
the assignment program followed fighter class , post error code assignment below
exception in thread "main" java.lang.error: unresolved compilation problems: constructor fighter() undefined method attack() in type fighter not applicable arguments (fighter) method attack() in type fighter not applicable arguments (fighter) @ assignment5.main(assignment5.java:27)
this error message displayed , have tried fix , i'm not sure else try, not allowed change assignment program, fighter class allow program run way should
you getting issues because fighter doesn't have proper methods. example: fighter has constructor defined public fighter (int num1, int num2, int num3, string str)
, trying instantiate new 1 enemyfighter = new fighter( );
. need create constructor (overload existing one) takes no parameters.
the same thing happens attack method, it's signature public void attack()
calling parameters: myfighter.attack (enemyfighter);
. means you'll need overloaded attack method receives 1 parameter of type fighter: public void attack(fighter fighter)
Comments
Post a Comment