• Post Reply Bookmark Topic Watch Topic
  • New Topic

Polymorphism and Inheritance CSC275  RSS feed

 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again, I need a little help to see if I did it correctly.  I have GameDriver class (professor created and we aren't supposed to change), Entity class which holds are the common traits between the Ogre, Archer, and Swordsman class and all getters and setters for those traits.  There is also a playable class, but it just consists of four String methods allowing movement of an Archer and Swordsman (up, down, left, right).  We don't have to track movement. Ogre extends Entity, Playable also extends Entity.  Archer and Swordsman extend playable.  Ogre, Archer, and Swordsman all contain an attack method. To save some space, I'll just show you my driver, Ogre, and Archer.  In the Entity class I set infLife = 100 because otherwise the health is also 0.  I added the print lines as well so I could check if my attack methods were working.





Sometimes it seems to work like this:
Player 1: Ogre1
Player 2: Archer2
Attacking
Archer2 started with 100 health and now has 98 health remaining.

but then other times I get:

Player 1: Ogre2
Player 2: Ogre4
Attacking
Ogre4 started with 1 health and now has 1 health remaining.

The attack method seems to only work when its an Ogre attacking a Swordsman or Archer (they each have 100 health minus the damage inflicted), but not Ogre vs Ogre (Doesn't start with 100 health and no damage is inflicted when attacked).  Any ideas? Or am I overlooking something in the driver that doesn't allow an Ogre to attack an Ogre?

P.S. I plan to get back to my previous thread to complete what has been challenged to me there.  I've just been really busy and haven't been able to get back to it yet.




 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.P.S. I always try to contact my professor first before I resort to asking you fine folks, but unfortunately, he takes forever to get back to you (you'd think 2 days would be enough time to get back to a student, but I don't know, I'm not a teacher).
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The more I run the code the more it seems like only an Ogre can attack. Can someone please tell me where this is in the GameDriver class?  I must be over-looking it.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Wages wrote:The more I run the code the more it seems like only an Ogre can attack?  Can someone tell me where this is in the GameDriver class?  I must be over-looking it.


Yes, that's true. Because of lines 44 and 71.

As for the code, there's way too much casting. I think your attack() methods have three times as much code as necessary and they shouldn't have any casts at all. (Also one of the three versions has a line of code missing. And all of the three have a logic error.) However that's based on some assumptions about the Entity class; your professor's code is full of unnecessary casting too so maybe I'm too optimistic about the Entity class.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Yes, that's true. Because of lines 44 and 71.

As for the code, there's way too much casting.


Hm.. I'm not sure why randomInt will be 0 everytime.  I'll try to figure it out by looking at the Random class.  As for the casting, I got that from the professors code and another student in the class (we concluded that is how it should be done), but since its either an instance of Swordsman/Ogre/Archer I don't have to cast throughout the method, right?


(Also one of the three versions has a line of code missing. And all of the three have a logic error.) However that's based on some assumptions about the Entity class; your professor's code is full of unnecessary casting too so maybe I'm too optimistic about the Entity class.


The Entity class is just the variables strLife, intLife, intEnergy (we don't have to do anything with energy in this assignment) and getters and setters for each variable.  I'll try  to figure out my logic error.  I also wrote the methods like that because it was the simplest way for me to test if they were working.  i'll re-write one now the way I intend to turn it in.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example of what I  would have done:

 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I was laying in bed,  I was thinking.. do I even need to check if they are instanceof  (Ogre, archer, swordsman) at all?  At this point, at least, since the driver doesn't seem fully functional and it wouldn't seem to matter which Entity it is because regardless of the Entity, it attacks the same way.  I could see checking intanceof if an ogre attacked a swordsman a different way than an archer.  Am I on the right path?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a Swordsman or an Archer attacks a Soldier, what is the difference to the victim?
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If a Swordsman or an Archer attacks a Soldier, what is the difference to the victim?


There wouldn't be a difference.  it would still be health - damageToInflict = healthLeft. I had to turn it in because I didn't want it to be late, but I still want it to be correct.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case, why would you want instanceof? I get suspicious when I see instanceof. You can have a damagePotential field in your Archer or Swordsman class; whenever you attack an opponent, you can inflict that amount of damage on the opponent.
Once you start doing things like the following, you are in dangerous waters:-Now, all you have to do is create a Slinger class which implements the Opponent interface and inflicts 456 of damage, and your method will cease to work correctly. You would have to go through all the methods with that sort of code in and add Slinger options to everything.

You are right to hand in the application as it is when you reach the deadline. You would lose far more marks for being late than you would gain by enhancing the game.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:In which case, why would you want instanceof? I get suspicious when I see instanceof. You can have a damagePotential field in your Archer or Swordsman class; whenever you attack an opponent, you can inflict that amount of damage on the opponent.


This is what I originally turned in to my professor:


but then after talking with a classmate, he convinced me that the computer needed to know which player to attack, thus, checking to see if playerToAttack was an ogre, archer, or swordsman.  Why is health -= damageToInflict entering dangerous waters?  I don't quite understand your example with a Slinger class.


You are right to hand in the application as it is when you reach the deadline. You would lose far more marks for being late than you would gain by enhancing the game.


Actually,  I learned the hard way that turning in a project that doesn't work correctly is worse than turning it in a few days late that works correctly.  He gives you a 0 if it doesn't work properly.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Wages wrote:...I have GameDriver class (professor created and we aren't supposed to change) ... To save some space, I'll just show you my driver ...

So, did your professor write this GameDriver class code that you posted or did you write it?
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
So, did your professor write this GameDriver class code that you posted or did you write it?


My professor wrote the GameDriver class code and I wrote everything else.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If -= is dangerous waters should I just do:

 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Wages wrote:If -= is dangerous waters:

You've missed the point entirely, like saying it's dangerous to wear pajamas while cleaning a loaded gun. If I'm not mistaken, Campbell was probably referring to your use of instanceof and hard-coded values.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Jeremy Wages wrote:If -= is dangerous waters:

You've missed the point entirely, like saying it's dangerous to wear pajamas while cleaning a loaded gun. If I'm not mistaken, Campbell was probably referring to your use of instanceof and hard-coded values.


Hard coded values = all the casting? I haven't come across that term yet.

Are any of the corrections I posted right? Or close?
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using instanceof is not casting. Hard-coded values are things like 234 and 345.

No, the corrections you posted are not good. You really should be using polymorphism instead of checking types with instanceof. Also, those changes you posted are not object-oriented. You are pulling information out of the objects and updating their state instead of telling them to do something (by calling a method) that will trigger some behavior in the object that changes its state. The object itself should be the one to manage its own health levels.

The difference is like me taking your wallet from you and counting the money in it versus telling you to say how much money you have on you. The former violates your personal space, just as pulling out information from your objects violates their encapsulation.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Using instanceof is not casting. Hard-coded values are things like 234 and 345.


So:


lines 2 and 3 are hard-coding?


No, the corrections you posted are not good. You really should be using polymorphism instead of checking types with instanceof. Also, those changes you posted are not object-oriented. You are pulling information out of the objects and updating their state instead of telling them to do something (by calling a method) that will trigger some behavior in the object that changes its state. The object itself should be the one to manage its own health levels.

The difference is like me taking your wallet from you and counting the money in it versus telling you to say how much money you have on you. The former violates your personal space, just as pulling out information from your objects violates their encapsulation.


I was feeling good about my understanding of this assignment, but now that all goes out the window .  I'm not sure how I'm supposed to call a method to attack when I'm creating the method to attack.  Does Entity need an attack method?  His instructions don't tell us to do that though.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and I guess I could do a damageTaken() method in Entity, but again, not in the instructions.

Class Name: Entity
Class Level (global) Variables:
strName - String
intLife - Integer
intEnergy - Integer

Method Name: getStrName
Parameters: None
Desired Result: Accessor
Data Returned: strName

Method Name: setStrName
Parameters: name
Desired Result: Mutator
Data Returned: none

Method Name: getIntLife
Parameters: None
Desired Result: Accessor
Data Returned: intLife

Method Name: setIntLife
Parameters: life
Desired Result: Mutator
Data Returned: none

Method Name: getIntEnergy
Parameters: None
Desired Result: Accessor
Data Returned: intEnergy

Method Name: setIntEnergy
Parameters: energy
Desired Result: Mutator
Data Returned: none


Class Name: Playable
Method Name: moveUp
Parameters: None
Data Returned: “ has moved up one space”

Method Name: moveDown
Parameters: None
Data Returned: “ has moved down one space”

Method Name: moveLeft
Parameters: None
Data Returned: “ has moved left one space”

Method Name: moveRight
Parameters: None
Data Returned: “ has moved right one space”


Class Name: Ogre
Method Name: Attack
Parameters: damageToInflict – Integer, playerToAttack - Entity
Desired Result: Attack an entity for x amount of damage
Data Returned: None

Class Name: Archer
Method Name: Attack
Parameters: damageToInflict - Integer, playerToAttack - Entity
Desired Result: Attack an entity for x amount of damage
Data Returned: None

Class Name: Swordsman
Method Name: Attack
Parameters: damageToInflict – Integer, playerToAttack - Entity
Desired Result: Attack an entity for x amount of damage
Data Returned: None

 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Wages wrote:Does Entity need an attack method?  His instructions don't tell us to do that though.


No, Entity needs a method which is called when something (another Entity) attacks that Entity. I guess "damageTaken" is a pretty good name for it actually (or "damageInflicted"), its signature would be "public void damageTaken(int damage)".

Actually, yes, Entity could use an "attack(int damageToInflict, Entity playerToAttack)" method. Even though it would only have one line of code. There's no point in duplicating that method and its code in each subclass of Entity. In many games the action of attacking would be different depending on who was attacking whom, in which case it would be more complicated than that, but I don't see that feature in what you posted so far.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure why "Polymorphism and Inheritance" is the title of your post -- even the professor's code fails to use polymorphism and inheritance where it should be used. But perhaps their idea is to write code without inheritance and then go through why inheritance should be used instead of all that casting?
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming Jeremy is taking a course that follows the typical US university term schedule, they would be past midterms by now. I think it might be a little generous to think that the code given was meant to be an example of how not to do inheritance and polymorphism. If it was, then I have to question the method, seeing as there doesn't appear to be any counter-example of "the right way" to do it. If that omission was by design with the intent to show the proper way to do it later, then I also have to question that approach and the efficacy of having students learn from a bad example. Students already have enough trouble writing good code for good requirements as it is. It makes about as much sense as trying to teach new drivers how to drive by putting them behind the wheel of a car that has a gas pedal that sticks and brakes that don't work so they can learn how NOT to drive.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Wages wrote:. . . [a classmate] convinced me that the computer needed to know which player to attack, thus, checking to see if playerToAttack was an ogre, archer, or swordsman.
I am afraid he is mistaken there.
  Why is health -= damageToInflict entering dangerous waters?
As people have already told you, it is not -= that is dangerous waters. It is instanceof.
  I don't quite understand your example with a Slinger class.
. . .
That example was added to show how awkward it would be to add another class. The hard‑coded values were not intended as something for you to emulate. They were simply numbers picked out of thin air. As if 123 234 345 and 456 would be real values
Actually,  I learned the hard way that turning in a project that doesn't work correctly is worse than turning it in a few days late that works correctly.  He gives you a 0 if it doesn't work properly.
I think that is unreasonably stringent. It means you can make a little mistake somewhere and have a project which almost works and still get 0. That might be because of automated marking; the machine tests the output and the only people to read the code are here on the Ranch.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:. . . No, Entity needs a method which is called when something (another Entity) attacks that Entity. I guess "damageTaken" is a pretty good name for it actually (or "damageInflicted"), its signature would be "public void damageTaken(int damage)". . . .
Surely the method would be called sufferDamage or sufferAttack or takeAttack or similar?

I think I would go one step further and suggest that any Entity subject to attack shou‍ld implement a Vulnerable interface:-I have added /** stubs */ to remind you that the documentation comments are the biggest and bestest part of an interface's code. Now the attack method can take Vulnerable as a parameter rather than Entity. There might then be Entities which can do some things, e.g. move around, but don't implement the Vulnerable interface, in which case they cannot be attacked.

The code originally posted with the instanceof tests had three blocks of identical code; it is a bad idea to repeat yourself. Adding a Slinger class would have changed that to four identical blocks. Adding a JavelinThrower class changes that to five identical blocks.

I would suggest it is not object‑oriented to go through Junilu's wallet. Nor is it normal to ask how much money somebody has, unless you are a M*** taxi driver at 2.00am on Sunday. What you might ask is, “It costs £123.45. Have you got that much?” What you normally say is, “That's £123.45 please,” and wait for the money. So Junilu's Person class might not have a howMuchMoney() method but would have a pay(amount) method.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I'm not sure why "Polymorphism and Inheritance" is the title of your post -- even the professor's code fails to use polymorphism and inheritance where it should be used. But perhaps their idea is to write code without inheritance and then go through why inheritance should be used instead of all that casting?


That was the chapter we had to read in order to do this Lab.  My professor is a java developer (who I guess teaches on the side).  He takes forever to reply to emails and in general, I'm not happy.  If a project that you've turned in doesn't work correctly he doesn't give any feedback other than, "I couldn't do this/that".  Ex: We had to make a program that could add a flower to a flowerPack.  My first submission's feedback was, "Can't add a flower." and he gave me a 0 (he did allow me to correct it, though).
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Assuming Jeremy is taking a course that follows the typical US university term schedule, they would be past midterms by now. I think it might be a little generous to think that the code given was meant to be an example of how not to do inheritance and polymorphism. If it was, then I have to question the method, seeing as there doesn't appear to be any counter-example of "the right way" to do it. If that omission was by design with the intent to show the proper way to do it later, then I also have to question that approach and the efficacy of having students learn from a bad example. Students already have enough trouble writing good code for good requirements as it is. It makes about as much sense as trying to teach new drivers how to drive by putting them behind the wheel of a car that has a gas pedal that sticks and brakes that don't work so they can learn how NOT to drive.


My mid-term is due next week, but yes, mid-term grades have been posted (kinda silly to have the mid-term after in my opinion).  I agree with the approach.  We probably won't get to see an example of a good way to do it either.  At least we haven't for other projects this semester.  I think my code satisfies his requirements for the lab, though.  I'm going to start the mid-term tonight, but it makes me nervous that I can't complete this correctly (in code ranch's eyes).  It might be the right way in my professors eyes.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Wages wrote:. . . complete this correctly (in code ranch's eyes). . . .
We do know what we are talking about.

If you are having problems with teaching, I suggest you find somebody in authority to discuss the problem with. It is probably better not to say any more here, even though we are curious to know what happens.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: . . . We do know what we are talking about. . . .


I agree.




I think I'm going to ask him to grade that one, because I think it satisfies his requirements.  I just don't get how we're supposed to go from this lab to the midterm which will actually need to use polymorphism (at least, I think).   I'm pretty sure.

I guess I'll try to google a beginner java product so  I can get more examples of polymorphism and inheritance. 
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
program** not product** these late nights are taking its toll
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Wages wrote:. . . I'm going to ask him to grade that one . . . 
Don't ask me to grade that code because I wouldn't give it a very good mark.

There is usually no need to quote the whole of a preceding post; I have removed part of that you quoted.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, the proper solution would be to create a takeDamage()  method in Entity? Since all Entities could be attacked?  I'm having trouble figuring out how the attack method in each  class of ogre, archer, and swordsman would use the takeDamage method in Entity.  Would I create a new object in the method takeDamage() and then my attackMethod would simply be object.takeDamage()?
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I started reading "Head First Java" as well, which ironically directs you to come here (so this is used to be javaranch.com eh?).  Maybe I need to re-read the polymorphism and inheritance from my school text book as well. 
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Wages wrote:I guess I'll try to google a beginner java product so  I can get more examples of polymorphism and inheritance.

The only hitch to that is that you don't really know good from bad.

Polymorphism (meaning "many forms") can be seen almost anywhere you look. To understand polymorphism, you need to also consider the idea of abstraction. A phone's interface is polymorphic and abstract. So is a car's interface. On the phone, all you see are buttons and numbers. You don't know what's going on underneath. If you have a smart phone, your phone OS could conceivably be upgraded and made to work differently under the covers (that is, a different program is installed to control the function of dialing out to another phone) but your interface remains the same set of buttons and numbers. Cars have pretty much the same interface: a steering wheel and some pedals to control speed. Each different make of car will have a different implementation of the steering, gas, and brake system but you're still using the same wheel and pedals that you're familiar with.

Likewise, when it comes to programming, abstraction is about being able to abstract away details and just get the "essence" or intent of a behavior.  When you want something in your program to attack another thing, you shouldn't have to tell it in excruciating detail, just generalities: "I want you to attack with full power!" or "Attack with all you have left!" not "I want you to attack with 362 units!" or "How much strength do you have? (Ans. 248) Then, attack with 248 units!" You're not supposed to micromanage your objects like that. Abstraction is about not caring for the details, just the general. Polymorphism is one way to achieve abstraction.

Polymorphism allows you to not care what specifically and how specifically something carries out an action/behavior, as long as it does it according to its specialization. So, if you had an Archer, a Swordsman, and a SpearBearer, all of which were Combatant objects (their general classification), the general idea would be for these objects to "attack" other Combatant objects. Specifically, each one of those types of Combatant has its own interpretation of what "attack" means to them: an Archer would shoot an arrow at the enemy, a Swordsman would try to cut its enemy, and a SpearBearer would try to throw its spear at the enemy. You (i.e. all code that is external to these classes) would only know that whatever the object is, as long as it's a Combatant, it's going to attack. You also know that a Combatant is able to defendAgainst() an attack and that it knows whether or not it isAlive().

So, proper polymorphism would look something like this:

Notice that each implementation of Combatant has its own way of attacking the enemy that's only known to that class itself.  Enemy Combatant objects would only know that they're being attacked by a Weapon, which has a range, strength, and speed. They will then decide how to respond accordingly based on their own capabilities (strength, health, agility, etc.) and calculate any damage, loss of strength, speed, and agility resulting from dealing with that particular attack.

Given the above, all you have to do in your main program is create Combatants and let them loose on each other:

Do you see how that code tells a story?
 
rome pandit
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic.

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind inheritance in java is that you can create new classes that are built upon existing classes.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar, I can semi-follow along with the story.  We aren't to interfaces and abstracts yet.  Where does your code actually attack?  In my mind, your attack methods just defend off an attack.  What am I missing?  Where does "fightOneRound" and "removeDeadFrov" come from?
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Wages wrote:Junilu Lacar, I can semi-follow along with the story.  We aren't to interfaces and abstracts yet.  Where does your code actually attack?  In my mind, your attack methods just defend off an attack.  What am I missing?  Where does "fightOneRound" and "removeDeadFrov" come from?

Keep in mind that what I gave you is just an outline -- a storyboard, if you will. The idea behind the fightOneRound() method is that it takes a list of Combatant pairs, maybe using a class called MatchUp. You'd pass the fightOneRound() method a list of Combatant objects, the list would get shuffled Rather, you'd pass the faceOffByPairs() method a list of Combatants, which would be shuffled and iterated over to create another list of MatchUp objects from each consecutive pair of Combatants. Something like this maybe:

The point of this example is to show that you're not micromanaging the behavior from the outside.  The outside code is just invoking methods that represent general behaviors. It's up to each subclass to implement the specific details of what an action like attack() really involves. Unlike the code you posted, everything is high-level and abstract until you get down to the specific implementation itself, then you can see what the details are. The code you have has the nitty-gritty details at the high level. There's no abstraction or polymorphism at all in that code you gave.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Wages wrote:In my mind, your attack methods just defend off an attack.  What am I missing?

You're missing what Campbell already pointed out before.  For every action here, there should be a corresponding reaction.  If one Combatant attacks another, there has to be some way of telling the other Combatant that it's being attacked. The method pair in this example is comprised of the attack and defendAgainst method.  If I attack you, you have to defend against my attack.  If you attack me, I have to defend against your attack. Get it?

So, the code I gave above in the engage() method does this:  It randomly picks which of the two Combatants gets to attack first. The first to strike will have the advantage because its attack could kill the opponent. If, however, the attack doesn't kill the opponent, the opponent is given a chance to make a counterattack, to be fair. That would be one round of battle.  After each round, you'd call the removeDeadFrom(field) method to clear away all the dead combatants before the next round of matchups and engagements.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see you still want to go down into the details of these methods I'm suggesting in the example. The details are not that important. What I wanted you to concentrate on is how the high-level code and relationships between objects are defined by the API (the set of methods) that I have defined. That's really what you should focus on getting right. If you're trying to build a car, think about the overall picture before you figure out the nitty-gritty details of which bolt goes into which hole. Right now, it seems like you're focused on the bolts and holes.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Wages wrote:We aren't to interfaces and abstracts yet.

Doesn't matter. You can do the same thing with what you know right now.

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