7 Replies - 628 Views - Last Post: 01 June 2009 - 02:50 AM Rate Topic: -----

#1 choboko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-April 09

Class method problems

Posted 29 May 2009 - 06:32 AM

I'm trying to make my boolean checker check if my "character" is dead or not.
Designing an RPG character class where they have certain attributes.

import java.util.Random;
public class Character
{
	private String name;
	private int strength;
	private int dexterity;
	private int intelligence;
	private int hp;
	private Random randomiser;
	private int stat = 16;
	
	public Character(String name)
	{
	this.name = name;
	randomiser = new Random();
//random stats between 3 and 18
	strength = 3 + randomiser.nextInt(stat);
	dexterity = 3 + randomiser.nextInt(stat);
	intelligence = 3 + randomiser.nextInt(stat);
	
	hp = strength;
	
	}

	public String getName()
	{
	return name;
	}
...
//Does damage to the character's hp
	public void gotHit(int dmg)
	{
	 hp = hp - dmg;
	}
//checks if hp is less than or equal to 0, returns true if dead; false if not dead.
//resets hp back to 0
	public boolean isDead()
	{
	if(hp < 0)
	{
	System.out.print("Character is dead");
	hp = 0;
	return true;
	}
	else
	{
	return false;
	}
	}
	public int getHp()
	{
	return hp;
	}
	
	public String toString()
	{
		String info = (name + ", Strength: " + strength + ", Dexterity: " + dexterity 
		+ ", Intelligence: " + intelligence + ", Hit Points: " + hp );
		return info;
	}


driver
...
		Character Chicken = new Character("Chicken");
		Chicken.gotHit(100);
		System.out.println(Chicken.toString());
...


The problem is that when I do "damage" it subtracts it from hp, but it's not checked by the isDead() method when it's printed in the toString() method. So I get a negative number instead of 0 being printed.

Hope you can help.

Is This A Good Question/Topic? 0
  • +

Replies To: Class method problems

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,513
  • Joined: 20-September 08

Re: Class method problems

Posted 29 May 2009 - 07:29 AM

I'd do

public void gotHit(int dmg)
{
	hp = Math.max(hp - dmg, 0);
}

public boolean isDead()
{
	return hp == 0;
}


Was This Post Helpful? 1
  • +
  • -

#3 choboko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-April 09

Re: Class method problems

Posted 29 May 2009 - 07:13 PM

View Postg00se, on 29 May, 2009 - 06:29 AM, said:

I'd do

public void gotHit(int dmg)
{
	hp = Math.max(hp - dmg, 0);
}

public boolean isDead()
{
	return hp == 0;
}



I thought that in boolean methods I needed to return either true or false?
Edit:
Thanks it works, but I want to print the statement as well. Or do I need to put that somewhere else, because it's not printing in that method.

This post has been edited by choboko: 29 May 2009 - 07:18 PM

Was This Post Helpful? 0
  • +
  • -

#4 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 664
  • Joined: 20-March 09

Re: Class method problems

Posted 29 May 2009 - 08:19 PM

I'm assuming that when you say you want to print the statement you mean a message saying "You Died" or something similar. In your gotHit() method add the print line.
public void gotHit(int dmg) {
	 hp = Math.max(hp-dmg, 0);
	 if (isDead()) {
		  System.out.println("You Died");
	 }
}


Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Class method problems

Posted 29 May 2009 - 10:13 PM

View Postchoboko, on 29 May, 2009 - 06:13 PM, said:

I thought that in boolean methods I needed to return either true or false?


return hp == 0;



That will return true if hp == 0 and false otherwise
Was This Post Helpful? 1
  • +
  • -

#6 choboko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-April 09

Re: Class method problems

Posted 01 June 2009 - 02:32 AM

Hey I'm back again with another problem @.@
I'm having trouble with overriding methods.

public class Barbarian extends Character
{
	protected String name;
	protected boolean rage = false;
	public Barbarian(String name)
	{
	super(name);
	}
	
	public String getName()
	{
	return name;
	}
	
	public void setRage()
	{
			if(rage == false)
		{
			rage = true;
			System.out.println("Rage activated.");
			strength = strength + 6;
		}	
	}
	
	public int getStrength()
	{
		return strength;
	}
	
	public String toString()
		{
			String info = (name + ", Strength: " + strength + ", Dexterity: " + dexterity 
			+ ", Intelligence: " + intelligence + ", Hit Points: " + hp +", Rage: " + rage);
			return info;
		}



Which is extended from the character class above. Only I changed from the Character class was
private -> protected.

Anyway my problem is that when I print the toString method it prints out name = null instead of the name of my character.

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#7 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: Class method problems

Posted 01 June 2009 - 02:39 AM

private variables are not inherited by a subclass, thus all your variables
private String name;
private int strength;
private int dexterity;
private int intelligence;
private int hp;
private Random randomiser;
private int stat = 16;
are not accessible by the barbarian class. To fix this and keep your program secure use protected.
so for name you would
remove the line
protected String name;
from the barbarian class
and change private String name in the character class to
protected String name;

what happens in your program is you pass the string name to the constructor and then call super(name). This sets the name in the super class, not in the subclass. thus none of the methods you create in the new class will have access to this variable, only the methods in the superclass. Towards the end of you program you over rid the method toString(). This created a new method in the Barbarian class that did not have access to the private variable name. Only the protected one that you had created that has not been set yet. Thus another way to fix this would be to remove the new toString() method. However, it would make more sense to allow you subclasses to simply inherit variables like name and dexterity.

This post has been edited by gl3thr0: 01 June 2009 - 02:52 AM

Was This Post Helpful? 1
  • +
  • -

#8 choboko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-April 09

Re: Class method problems

Posted 01 June 2009 - 02:50 AM

Cool thanks it's working now. I keep making small errors -_-
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1