Junilu Lacar wrote:Look carefully at this Die constructor:
On line 17, what happens to the value that you passed in to the constructor?
Junilu Lacar wrote:And a nitpick:
What's the point of the local variable value? You can cut the length of this method by half by just having a return statement.
Mike Corona wrote:
sides becomes 1. I take it that should be this.sides = sides;
Campbell Ritchie wrote:Yes, it should. What does the if statement mean? I think it is incorrect.
Mike Corona wrote:sides becomes 1. I take it that should be this.sides = sides;
All things are lawful, but not all things are profitable.
Mike Corona wrote:the if statement was to make sure the sides could not be a negative integer or 0.
Junilu Lacar wrote:
Mike Corona wrote:sides becomes 1. I take it that should be this.sides = sides;
Ok. So you default the Die to be showing a 1 on the next line. That's fine, too. What's your intent for lines 20 to 23? (I know what it is, I just want to see if you can express it in plain English--the code doesn't match the intent, that's why you have a bug)
Junilu Lacar wrote:
Mike Corona wrote:the if statement was to make sure the sides could not be a negative integer or 0.
Let me help you express in plain English, since it's sometimes hard to switch back to regular language when you've been racking your brain for the Java-speak for so long:
"If we're asked to create a die with negative or zero sides, we should ignore the request and just default to a regular 6-sided die."
Mike Corona wrote:
Junilu Lacar wrote:
Mike Corona wrote:the if statement was to make sure the sides could not be a negative integer or 0.
Let me help you express in plain English, since it's sometimes hard to switch back to regular language when you've been racking your brain for the Java-speak for so long:
"If we're asked to create a die with negative or zero sides, we should ignore the request and just default to a regular 6-sided die."
So, are you basically saying that constructor is useless and I should move the if statement to the empty contructor?
Mike Corona wrote:So, are you basically saying that constructor is useless and I should move the if statement to the empty contructor?
Junilu Lacar wrote:
Mike Corona wrote:So, are you basically saying that constructor is useless and I should move the if statement to the empty contructor?
No, I'm saying that this:
is not how you say this: "If we're asked to create a die with negative or zero sides, we should ignore the request and just default to a regular 6-sided die" in Java. It's not useless because the intent is valid; the implementation, however, is invalid. It's really saying, "If we're asked to create a Die that has negative or zero sides, then let this Die have sides number of sides." Since you changed the value of sides prior to executing this code, this section of code becomes pointless.
A more "techie" translation of "default to a regular 6-sided die" is "let the number of sides be 6" -- that's as plain as I can get without actually writing code for you.
Mike Corona wrote:
Junilu Lacar wrote:If you wanted your second constructor to do what the no-argument constructor does first, you can always call it like this:
I don't particularly like to do this kind of thing in general; just giving this to illustrate how you can call other constructors from a constructor.
Junilu Lacar wrote: You can do that but now you've lost the intent of checking for invalid values of sides and defaulting to a reasonable value.
You can't be sure it makes sure unless you have some reasonable proof. I suggest you start writing tests early on or at least tests, in fact, better probably start from them, so such mistakes might wouldn't even occur.Mike Corona wrote:the if statement was to make sure the sides could not be a negative integer or 0.
You: Hey, you there, Die with 6 faces... ( ) Roll yourself four times and give me the highest two rolls you get.
Die: Ok... (turns around, rolls 4 random numbers, sorts those numbers, then picks out the two biggest ones)... (turning back to you) Here you go... the two highest rolls out of four.
Mike Corona wrote:
How about this?
Junilu Lacar wrote:
Well, first of all you should put away that rolling pin because your code looks like you just took one and smushed your nice indentations flat as a pizza. (I guess the expression is "flat as a pancake" but it's past breakfast time and I'm hankering for some melted cheese and anchovies on a hand-tossed crust right now)
Now that code says "If sides is a valid value, then make this die have that many sides, then show the 1 face. Otherwise, make this a one-sided die." Is that what you really want, a one-sided die if you write new Die(0) or new Die(-1)? I thought you wanted a 6-sided Die by default?
And by the way, when sides is not valid, that code will also leave the faceValue at the default value that Java assigns to all int instance variables: 0.
Change that to if‑0‑then‑6‑otherwise‑n.A few minutes ago, I wrote:. . . if‑0‑then‑1‑otherwise‑n . . .
Mike Corona wrote:Now it looks like too much code for a simple method.
Junilu Lacar wrote:
Mike Corona wrote:Now it looks like too much code for a simple method.
That's the funny thing about programming. Very seldom do you write the best code the first time around. I suppose that's reasonable because after all, writers go through many rough drafts before they publish their books. Even great writers like Ernest Hemingway went through reams of paper, looking for just the right words to use in his books.
Programmers are authors, too. There's even an @author tag for JavaDoc comments. As authors, we seldom come up with just the right words or phrases the first time we put pen to paper or fingers to keyboard, as the case may be. We have to read what we wrote, see if it says what we really want to say simply and clearly. The process of cleaning up after the initial mess we make is called refactoring. You should get used to it.
As Campbell said, that constructor doesn't have to be that long. You can boil it down / refactor it to two lines of code.
Mike Corona wrote:There is still the hero class I need help with though. I just need to understand how to integrate it into the battlecode class and I can take it from there
Junilu Lacar wrote:
Mike Corona wrote:There is still the hero class I need help with though. I just need to understand how to integrate it into the battlecode class and I can take it from there
What you have right now is kind of heading in the right direction. What exactly don't you understand about the instructions? If you take them one item at a time and follow each to the letter, it seems like a pretty straightforward translation.
It looks a lot better nowMike Corona wrote:. . . This is the new code and I am sticking with it. . . .
Work out what that expression avaluates to. I don't think the addHealth method should return anything.Mike Corona wrote:. . .. . .
Campbell Ritchie wrote:
Work out what that expression avaluates to. I don't think the addHealth method should return anything.Mike Corona wrote:. . .. . .
Mike Corona wrote:How about this?
Mike Corona wrote:
Mike Corona wrote:
but not I run into a new issue. rest no longer makes sense considering it is also void and I am using the addHealth() method in it.