Parameter in a method

I am supposed to write a method that will do the following.

attack - receives a parameter of the type Character and attempts to fire weapon at that character. It should:

1. Print the name of attacker and who he/she is trying to attack (the attackee).

2. Make sure the attacker is alive, if not print a message to that effect and do nothing else.

3. Make sure the attackee is alive, if not print a message to that effect and do nothing else.

4. Check to make sure attacker and attackee are on different sides, if not print a message to that effect and do nothing else.

5. Check to see if attacker has ammo, if not print a message to that effect then check to see if attackee has ammo. If the attacker did not have any ammo but the attackee has ammo we now switch--the attacker becomes attackee and vice versa--and print a message to that effect.

6. Attacker attacks (fires a shot at) attackee and inflicts 20 damage. If inflicting 20 damage on the atackee "kills" him/her print a message saying so.

* Parameters: Character object

* Returns: none

I don't think that I will have a problem writing the method, but I don't understand how to apply a parameter of the type, Character to this method. Character is the name of this class that the method will be in

Message was edited by:

darkmonk04

[1379 byte] By [darkmonk04a] at [2007-11-27 3:52:49]
# 1

It's just like any other method, but with a Character for the parameter

public void hereHaveAnInt(int i) {

}

pubilc void hereHaveACharacter(Character ch) {

}

hunter9000a at 2007-7-12 8:56:49 > top of Java-index,Java Essentials,New To Java...
# 2
Ok I think that I possibly understand....let me mess around with it for a little while, and I will let you know how it turns out.
darkmonk04a at 2007-7-12 8:56:49 > top of Java-index,Java Essentials,New To Java...
# 3
ok....is this a valid statement...besides the fact that it has no bodypublic void attackChar(Character charAttacked);{}
darkmonk04a at 2007-7-12 8:56:49 > top of Java-index,Java Essentials,New To Java...
# 4
No, because of that semicolon at the end. Remove the semicolon and you're good.(Actually, the code may be syntactically valid, but if so it almost certainly won't do what you probably think it will.)
paulcwa at 2007-7-12 8:56:49 > top of Java-index,Java Essentials,New To Java...
# 5
Just curious....I need to for it to do what I have written above....I thought that this would work =(
darkmonk04a at 2007-7-12 8:56:49 > top of Java-index,Java Essentials,New To Java...
# 6
What?
paulcwa at 2007-7-12 8:56:50 > top of Java-index,Java Essentials,New To Java...
# 7
I'm sorry lol....I need this method to be able to perform the actions that I have listed in my first post. The way that it is set up now....will it be able to handle it?
darkmonk04a at 2007-7-12 8:56:50 > top of Java-index,Java Essentials,New To Java...
# 8
It will once you write the code inside the method.BTW, if you are to adhere to the specs, the method should be called attack not attackChar.
floundera at 2007-7-12 8:56:50 > top of Java-index,Java Essentials,New To Java...
# 9

Offhand, sure, it seems likely enough.

But keep in mind that some of the functionality may depend on other functionality, which you may or may not have written yet. For example, does Character have anything like a "isAlive" method?

For each of the items in your first post, ask yourself how you would do that given that you have two Character objects, and attacker and an attackee, when you're in the body of that method. Note that in some cases, the stuff you'd be doing may be sufficiently complicated that you may need to break it out into other methods, or to write helper methods.

paulcwa at 2007-7-12 8:56:50 > top of Java-index,Java Essentials,New To Java...
# 10

yes yes...I have other methods already written...such as isAlive, health, armor, damage, side....thats all done....just this one has particularly been giving me an annoying time. I do understand how to write the code for attacking and stuff like that, but I'm confused as how to go about writing it that "this character" is attacking "that character....I mean I only have one parameter....and the Character have not been defined yet...

darkmonk04a at 2007-7-12 8:56:50 > top of Java-index,Java Essentials,New To Java...
# 11

Well, this is a method in the class Character, right? And it takes a parameter of type Character, whose name you've defined as "charAttacked", right?

In that case, you now have two Character objects to play with. One you can refer to via charAttacked, and the other you can refer to via "this". (Read about the "this" reference in the manual if you don't already know about it.)

So the "this" Character object is attacking the "charAttacked" Character object.

Using that, you can call methods like "isAlive" on either the current, attacking Character (as in: "this.isAlive()"), on on the attacked Character (as in: "charAttacked.isAlive()").

paulcwa at 2007-7-12 8:56:50 > top of Java-index,Java Essentials,New To Java...
# 12

public void attack(Character charAttacking)

{

System.out.print(charAttacking.getName() + " is going to attack "

+ this.Character.getName());

}

in the object that i made

public class CharacterDemo

{

public static void main(String[] args)

{

Character CharObj = new Character();

Character CharObj2 = new Character();

CharObj.readInput();

CharObj2.readInput();

System.out.print(CharObj.attack(CharObj CharObj2));

}

}

This seems correct to me, but does not work. Anyone see something obviously wrong with it?

darkmonk04a at 2007-7-12 8:56:50 > top of Java-index,Java Essentials,New To Java...
# 13
The attack method is void so it returns nothing so the println method has nothing to print.
floundera at 2007-7-12 8:56:50 > top of Java-index,Java Essentials,New To Java...
# 14
So what could I replace void with?
darkmonk04a at 2007-7-12 8:56:50 > top of Java-index,Java Essentials,New To Java...
# 15
NOTHING!Just call the method.
floundera at 2007-7-21 20:57:35 > top of Java-index,Java Essentials,New To Java...
# 16
So what about printing the text, "is going to attack", how do I work that into it if I can't just print it right there. Do I need a method for every piece of text that I want to insert, if so things could get kinda lengthy.
darkmonk04a at 2007-7-21 20:57:35 > top of Java-index,Java Essentials,New To Java...
# 17

No, he was referring to the System.out.println in your main() method. There's no need for it there, since attack() returns nothing. The println that's inside your attack method is fine.

But the message in your attack method is backwards. The character doing the attacking is the one whose attack() method is being called. So if you have this:

Character bob = new Character("bob");

Character jim = new Character("jim");

bob.attack(jim);

then the output would be (given normal naming conventions) "bob is going to attack jim".The way you have it, it would be "jim is going to attack bob".

Also, in attack(), there's no need to add the class name after "this".

And you don't pass the type of the argument with the argument. You just do that when you declare a method, not when you invoke it.

And by the way, in Java naming conventions, local variables start with a lower-case letter.

So altogether, your code would be better as:

public void attack(Character charToAttack)

{

System.out.print(this.getName() + " is going to attack "

+ charToAttack.getName());

}

// and

public class CharacterDemo

{

public static void main(String[] args)

{

Character firstCharacter = new Character();

Character secondCharacter = new Character();

firstCharacter.readInput();

secondCharacter.readInput();

firstCharacter.attack(secondCharacter);

}

}

Actually that's still not great, but I think it should at least compile and make a bit more sense.But one problem is that "firstCharacter" and "secondCharacter" are still pretty poor variable names. Something more descriptive is usually better, although for a short test class it's fine. Also, it's better if you never create an object in an incomplete state. When you do "new Character()", that new object is meaningless, a character with no name or other stats. It's better to gather the information from the user, and then create the Character object with the information. But these are less important for now.

paulcwa at 2007-7-21 20:57:36 > top of Java-index,Java Essentials,New To Java...
# 18
lol I understand now about the System.out.println statement...I didn't need to tell it to print, because it was already going to print. All your you guys' info has been AWESOME. So let me go and write that method, and I will tell you guys if it works....
darkmonk04a at 2007-7-21 20:57:36 > top of Java-index,Java Essentials,New To Java...
# 19

public class CharacterDemo

{

public static void main(String[] args)

{

Character charObj = new Character();

Character charObj2 = new Character();

charObj.setName("Michael");

charObj.setHealth(100);

charObj.setArmor(100);

charObj.setSide("evil");

charObj.setAmmo(50);

charObj2.setName("Paul");

charObj2.setHealth(100);

charObj2.setArmor(100);

charObj2.setSide("good");

charObj2.setAmmo(50);

charObj2.attack(charObj);

}

}

if (this.getSide() == charToAttack.getSide())

{

System.out.println(this.getName() + " and "

+ charToAttack.getName() + " are on the same side!");

}

}

This part of the method is supposed to make sure that charObj and charObj2 are on different sides. When it is ran it always says they are on the same side, even when I set one to evil and the other to good.Any ideas?

darkmonk04a at 2007-7-21 20:57:36 > top of Java-index,Java Essentials,New To Java...
# 20
If the getSide method returns a String then you will need to use the equals method. Before you ask, look it up in the API in the String class and try to work it out yourself.
floundera at 2007-7-21 20:57:36 > top of Java-index,Java Essentials,New To Java...
# 21
I see....getSide() when I try to check for equality I am checking the reference location....not the actual value I am wanting to.
darkmonk04a at 2007-7-21 20:57:36 > top of Java-index,Java Essentials,New To Java...
# 22
Yes, A == B checks if A and B have the same reference. That is points to the same object.
floundera at 2007-7-21 20:57:36 > top of Java-index,Java Essentials,New To Java...
# 23
So would this be the proper way to run the equalsIgnoreCase() methodthis.side.equalsIgnoreCase() == charToAttack.side.equalsIgnoreCase()
darkmonk04a at 2007-7-21 20:57:36 > top of Java-index,Java Essentials,New To Java...
# 24
Have you tried it?Did you do as I suggested and read the API on how to use it?
floundera at 2007-7-21 20:57:36 > top of Java-index,Java Essentials,New To Java...
# 25

i'm sorry yes I have tried it. but I am only used to writing things such as

while (!side.equalsIgnoreCase("good") &&

!side.equalsIgnoreCase("evil"));

I have never used two different objects in the same line of code with a test of equality such as this.

darkmonk04a at 2007-7-21 20:57:36 > top of Java-index,Java Essentials,New To Java...
# 26
What is API?
darkmonk04a at 2007-7-21 20:57:36 > top of Java-index,Java Essentials,New To Java...
# 27

But you don't need 2 arguments to your if statement. You only need one call to the equals method. You call it on the first object and the parameter to the equals method is the second object.

if(object1.equals(object2)) {

}

This isn't exactly what you need. Lets see if you can figure it out.

floundera at 2007-7-21 20:57:36 > top of Java-index,Java Essentials,New To Java...
# 28
> What is API?I can never remember what it stands for but it is a collection of html pages that explain all about the classes and their methods. http://java.sun.com/j2se/1.5.0/docs/api/
floundera at 2007-7-21 20:57:36 > top of Java-index,Java Essentials,New To Java...
# 29
API == "Application Programmer's Interface"
paulcwa at 2007-7-21 20:57:36 > top of Java-index,Java Essentials,New To Java...
# 30
> API == "Application Programmer's Interface"How many time do we need to say it? Use the equals method ;)
floundera at 2007-7-21 20:57:41 > top of Java-index,Java Essentials,New To Java...
# 31

oh super nice...that will come in very handy....I do have resources available to me....but my book will not come out and explain something....it runs around it, and it makes it hard to find a meaning for something....thanks for the help I really do appreciate it

this.getSide().equalsIgnoreCase(charToAttack.getSide())))

darkmonk04a at 2007-7-21 20:57:41 > top of Java-index,Java Essentials,New To Java...