• Post Reply Bookmark Topic Watch Topic
  • New Topic

Just a quick question on this code  RSS feed

 
Jake Augustine
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone. I just started learning (or at least attempting to learn) Java a few days ago. I'm using Java Head First and I had a quick question about this code from the book:



My question is concerning the method frighten. It is a boolean type method with an integer argument but I don't understand why they would use it. How does an integer affect this method? Can the integer change the boolean logic (true to false and vice versa). I guess I'm just confused why the book would use this type of method, i.e. are there any real world applications for it? I also noticed that the Vampire class has a return value of false, but that also appears to have no effect on the output. Is the return value there just to satisfy the requirement of needing a return? I apologize if these questions aren't the type that I should be asking, but I hate moving forward without fully understanding what's going on and I didn't find anything with google. Thanks for your time.
Jake

Edit: Sorry about the formatting. It seemed to have changed slightly after I submitted it.
 
Gautam Joshi
Greenhorn
Posts: 18
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jake,

Use the "Code" button, and paste your code inside the "[code=java]your code will goes here..[/code]"
(just check out the attachement image.., it will format your code)
read first How to ask questions on the forum because, It will not only help you
but answer of your question will come in a great speeedy way..!!



and in java boolean is either "true" or "false" it is not compare with any integer value. int will be consider as a int only.

Now, your question about frighten method..
it seems like there is a printing mistake..because, int variable x is not being used in any method.
So, try to access a blog of the author which is very common for any book where printing mistakes has been discussed..including some other good topics of the partiular book.



Sorry, I think the Code button is not working properly..we should ask this to mentor..
code-button.PNG
[Thumbnail for code-button.PNG]
 
Jake Augustine
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the update on the code.
As for the integer x, it's used in the for loop, but that was it. That was why I was asking if there was any reason to use a method like this. It seems more logical to just make it a void vice boolean method and not worry about a return. But based on your answer, it doesn't seem like there is any logic to it. Thanks!
 
Gautam Joshi
Greenhorn
Posts: 18
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jake,

Yes, it is used in the for loop, and that is the only reason why I said that just try to look at the authors blog about the book... you are using.. because, it seems like printing mistake..

In the method x is not used..
 
Jake Augustine
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I misunderstood what you stated earlier. I thought you meant a printing error on my part when I copied it over. Whew, maybe it's time for me to take a break and relax a bit now, haha. Thanks for the help!
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Was there any more explanation in the book? Which page is it on? It might simply be to show that you can pass values to a method and you can return values from it. You don't have to use those values anywhere, but you there is no limit to how you can change the method. Let's imagine that each Monster has a scariness index (int), then we can change the method to use that. Two possible examples:-And you can use the return value
if (myMonster.scare(999))...
The book simply misses out all those uses of the parameter and the return value. Maybe missed out for simplicity's sake.
 
Jake Augustine
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm....looking at it that way, it makes a lot more sense. Now that I've looked at it again with a fresh set of eyes, although I think a different expression would have made more sense in the context, I understand why it was used. It was part of one of the puzzles demonstrating the ability of a subclass to override or overload the methods of the superclass and it looks like the book used examples that would be fairly obvious to see. The book wanted you to find the correct ones (in this case the code for the vampire and monster classes) to get the proper printout. The other examples used cases where they would change number of method arguments ( from ( int x ) to () ) or change the name used ( .frighten vs .scare ) or even the type of argument ( (byte x) vs ( int x ) ).

Well I guess I learned not to stay up late trying to learn this when I've been up all day. Guess I was a little too tired and became too focused on the code rather than the lesson trying to be taught.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to be careful about overridings. People think they are overriding a method when they have changed its name slightly or changed the parameters, and neither of those constitutes overriding.
For overriding: same name and same parameters. Not even the tiniest difference.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote: . . . For overriding: same name and same parameters. . . .
If you have a method
public void foo(int i, int j, int k)…
and you override it with
public void foo(int x, int y, int z)…
the compiler will regard those parameters as the same.

If the method returns a Foo, then the overridden method must also return a Foo. In Java5 (nearly ten years ago) they refined that so the return type can be a subtype, so the overridden method can return a Bar if Bar is a subtype of Foo. That is called covariant return types.

If you find that most excellent book Horstmann and Cornell, it tells you not to use XXXAdapters in Swing because there is a risk of not overriding their methods properly. For years people complained about that sort of thing. If you try to override mouseMoved and write mousemoved by mistake, the compiler will take it happily and your program won't work. So in Java5 they introduced the @Override annotation. If you think you are overriding a method always write @Override[newline] before the method and the compiler will notice any spelling mistakes and complain bitterly.
 
Jake Augustine
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tips. I didn't know about @override, but I can definitely see it being useful. And thanks for the recommendation. Already added it to my to read list.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

But it isn't @override. Look very carefully.
 
Jake Augustine
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha, point taken
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of errors occur because people forget that method and Method are different.
The annotations have capital letters in because an annotation is a special kind of interface and interface names always start with capital letters (like class names).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!