Win a copy of Getting started with Java programming language this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Logic Error / OCA Java SE 8 Study Guide  RSS feed

 
Dwight Webber
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Mouse class on pages 33 - 34 :



The statement on line 5 leaves open the possibility that the instance variable length will be increased to a value greater than the class variable MAX_LENGTH which would be a logic error.

One possible fix would be:

 
Liutauras Vilda
Marshal
Posts: 3656
163
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I agree with the point you're making here, it is a logical error it seems, however, the book's particular chapter you're refering to or more precisely the particular code snippet you're refering to, is trying to teach the concept of variables scoping rather than how to write logically correct programs. I'd personally consider this as more of a dummy or random code rather than anything else.

But have a cow for analysing code snippets in depth.
 
Liutauras Vilda
Marshal
Posts: 3656
163
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And probably would be fair enough to say: "let's wait and see, as book authors could comment better about their actual intention" (if they see this post ).
 
Dwight Webber
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Liutauras, and thanks for welcoming me to the ranch.

I'd like to add (for the sake of thoroughness) that the MAX_LENGTH class variable should have been declared as final since in this context it is meant to be a constant.


 
Roel De Nijs
Sheriff
Posts: 10900
155
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dwight Webber wrote:which would be a logic error

Although likely to be a logic error, it's impossible to tell without knowing the business requirements for the Mouse class.

And if you have some experience with the Pokemon Go app, you know the bag can have a maximum number of items (by default: 350). If you run out of space in your "Pokemon Go" bag, you will no longer be able to collect any items. But if you have for example 348 items in your bag, you can spin a PokeStop and if you get 3 items, you will have 351 items in your bag, although the maximum of items is 350. So that's very similar to the current implementation of the grow() method
 
Roel De Nijs
Sheriff
Posts: 10900
155
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dwight Webber wrote:I'd like to add (for the sake of thoroughness) that the MAX_LENGTH class variable should have been declared as final since in this context it is meant to be a constant.

Agreed! (Although it's not really an errata item)
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36171
439
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree it would have been better to be final. For the logic error, that wasn't what we were trying to show. I imagine there are lots of things like that. That said, not all the code on the exam makes sense either so try not to let it distract you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!