Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

having problem with ninja fighter class  RSS feed

 
chris le
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maarten Vergouwen
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You havent made a sumo object, you've just coded the class.

You need something along the lines of:



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

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chris le
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey Chris-
So you actually found a tricky situation that can arise with inheritance. Check this out:


Then, the fighter class is extended by Ninja and Sumo


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:


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 ]
 
chris le
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks for all your help, I really appreciate it.

Take care,

Chris
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!