You are being forwarded to the lastest updates ot his page!
Or you can Click Here if it doesn't work or you don't wish to wait.

having problem with ninja fighter class

trystonlee
Hey guys I'm writing a little fighter program and have gotten stuck. Here's the setup:

1) abstract fighter class

package example;

public abstract class fighter {

public int healthBar;

public abstract void taunt();
public abstract void deathmove();
public abstract int getHealth();

}//end fighter


2) abstract life class

package example;

public abstract class life {

public int takeLife(fighter f){
f.healthBar -= 1;
return f.healthBar;
}//end takelife()

}//end life

3) ninja class

package example;

public class ninja extends fighter {

public int healthBar = 20;

public void taunt(){
System.out.println("Feel my cold steel!");
}//end taunt()

public void deathmove(){
System.out.println("Ninja has chopped off your head");
}//end deathmove()

public int getHealth(){
return this.healthBar;
}//end getHealth

public void punch(life l){
System.out.println("Pow!!Ninja has just punched you");
l.takeLife(sumo);
}//end punch()

}//end ninja

4) sumo class

package example;

public class sumo {

public int healthBar = 20;

public void taunt(){
System.out.println("I will crush you!");
}//end taunt()

public int getHealth(){
return this.healthBar;
}//end getHealth()

public void deathmove(){
System.out.println("Sumo has snapped your neck");
}//end deathmove()

}//end sumo

Whenever I try to compile the ninja class I get this error:

example/ninja.java cannot resolve symbol
symbol: variable sumo
location: class example.ninja
l.takeLife(sumo);

1 error

It doesn't seem to like me passing the sumo object into that method. Any ideas would be greatly appreciated.

Thanks,

Chris


MVergouwen
You havent made a sumo object, you've just coded the class.

You need something along the lines of:

code:


sumo mySumoWrestler = new sumo();




then you can execute a method with you sumo object as as argument

code:


thingy.takelife(mySumoWrestler)



ejfried
Hey -- we've got a Java Game Forum now! Although this is, indeed, appropriate here in Java Beginner, I think maybe it's a good idea to move it over to here now that it's been answered.


trystonlee
Hey thanks for the response. I changed a few things, it compiles and runs but doesn't work right


1) fighter class

package example;

public abstract class fighter {

public int healthBar;

public abstract void taunt();
public abstract void deathmove();
public abstract int getHealth();

}//end fighter


2) life class

package example;

public class life {

public int takeLife(fighter f){
f.healthBar = f.healthBar - 1;
return f.healthBar;
}//end takelife()

}//end life

3) ninja class

package example;

public class ninja extends fighter {

public int healthBar = 20;

life l = new life();

public void taunt(){
System.out.println("Feel my cold steel!");
}//end taunt()

public void deathmove(){
System.out.println("Ninja has chopped off your head");
}//end deathmove()

public int getHealth(){
return this.healthBar;
}//end getHealth

public void punch(fighter f){
System.out.println("Pow!!Ninja has just punched you");
l.takeLife(f);
}//end punch()

}//end ninja

4) sumo class

package example;

public class sumo extends fighter {

public int healthBar = 20;

public void taunt(){
System.out.println("I will crush you!");
}//end taunt()

public int getHealth(){
return this.healthBar;
}//end getHealth()

public void deathmove(){
System.out.println("Sumo has snapped your neck");
}//end deathmove()

}//end sumo

5) areana class

package example;

public class areana{

public static void main(String[] args){

ninja n = new ninja();
sumo s = new sumo();

System.out.println("Sumo's life " + s.getHealth());

n.punch(s);

System.out.println("Sumo's life " + s.getHealth());

}//end main()


}//end areana


It compiles with no error, however the output is this:

--------------------
Sumo's life is 20
Pow!! Ninja has just punched you
Sumo's life is 20

----------------------

It doesn't subtract 1 from sumo's healthbar

Any ideas



Thanks,

Chris


Jessica Bradley
hey Chris-
So you actually found a tricky situation that can arise with inheritance. Check this out:

code:

public abstract class fighter {
public int healthBar;
public abstract void taunt();
public abstract void deathmove();
public abstract int getHealth();
}//end fighter


Then, the fighter class is extended by Ninja and Sumo
code:

public class ninja extends fighter {
public int healthBar = 20;
life l = new life();

public void taunt() { ... }
public void deathmove() { ... }
public int getHealth() { ... }
public void punch( fighter f ) {
System.out.println( "Pow!!Ninja has just punched you" );
l.takeLife( f );
}
}

public class sumo extends fighter {
public int healthBar = 20;
public void taunt() { ... }
public int getHealth() { ... }
public void deathmove() { ... }
}//end sumo

public class life {
public int takeLife( fighter f ) {
// notice you're subtracting the healthbar defined in the fighter class
f.healthBar = f.healthBar - 1;
return f.healthBar;
}
}



Notice that you've defined the attribute healthBar in both the super class "fighter" and in the sub-classes, "ninja" and "sumo".

But then in ninja.punch() you decrease the value of the healthbar in the fighter class, but in the arena class you're printing the value of healthbar in the sumo class. It's a bit confusing, but the point is, the "healthbar" you defined in the sumo class is hiding the "healthbar" attribute in the fighter class.

(ok, I'll get to the point already) Basically you don't want to re-declare the attribute again, All you want to do is initialize the attribute you already have.

So, in your ninja and sumo sub-classes, you just need to create a constructor that will initialize the variable how you want:
code:

public class sumo extends fighter {
public sumo(){
healthBar = 20;
}
...
}

public class ninja extends fighter {
public ninja(){
healthBar = 20;
}
...
}



and then you get a result like this:
--------------
Sumo's life 20
Pow!!Ninja has just punched you
Sumo's life 19
--------------

[ July 30, 2004: Message edited by: Jessica Sant ]


trystonlee
Hey thanks for all your help, I really appreciate it.

Take care,

Chris [beerchug]