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.
Paul Clapham wrote:
Yes, that's true. Because of lines 44 and 71.
As for the code, there's way too much casting.
(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.
Campbell Ritchie wrote:If a Swordsman or an Archer attacks a Soldier, what is the difference to the victim?
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.
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 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 ...
Junilu Lacar wrote:
So, did your professor write this GameDriver class code that you posted or did you write it?
Jeremy Wages wrote:If -= is dangerous waters:
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.
Junilu Lacar wrote: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 wrote:Does Entity need an attack method? His instructions don't tell us to do that though.
I am afraid he is mistaken there.
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.
As people have already told you, it is not -= that is dangerous waters. It is instanceof.
Why is health -= damageToInflict entering dangerous waters?
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
I don't quite understand your example with a Slinger class.
. . .
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.
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.
Surely the method would be called sufferDamage or sufferAttack or takeAttack or similar?
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)". . . .
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?
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.
We do know what we are talking about.
Jeremy Wages wrote:. . . complete this correctly (in code ranch's eyes). . . .
Campbell Ritchie wrote: . . . We do know what we are talking about. . . .
Don't ask me to grade that code because I wouldn't give it a very good mark.
Jeremy Wages wrote:. . . I'm going to ask him to grade that one . . .
Jeremy Wages wrote:I guess I'll try to google a beginner java product so I can get more examples of polymorphism and inheritance.
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?
Jeremy Wages wrote:In my mind, your attack methods just defend off an attack. What am I missing?
Jeremy Wages wrote:We aren't to interfaces and abstracts yet.
He's my best friend. Not yours. Mine. You can have this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Appshttps://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous