• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

import: Chapter 1, Review Question 7 OCA Study Guide I

 
Greenhorn
Posts: 2
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

My apologies for dumping such a lengthy code here.
I completed chapter one of OCA Study Guide I  J. Boyarsky and S. Selikoff and took chapter one review questions. Question 7 answer is not clear to me. The question is below.



The Question

Given the following classes, which of the following snippets can be inserted in place of INSERT IMPORTS HERE and have the code compile? (Choose all that apply)



A. import aquarium.*;
B. import acquarium.Water; import acquarium.jellies.*;
C. import aquarium.*; import acquarium.jellies.Water;
D. import aquarium.*; import acquarium.jellies.*;
E. import aquarium.Water; import acquarium.jellies.Water;
F. None of these imports can make the code compile.

My Answer: I chose A. The book said it is A, B, C.

My Questions & confusion: Why are B and C included in the answer? I know that the package acquarium contains Water and Jellies classes and possibly employees classes. Hence the wild card used in choice A makes sense.
In choice B a wild card is used after the jellies class (import acquarium.jellies.*;), isn't that an attempt at importing a method which is wrong?

In choice C, can you import two classes in one import (import acquarium.jellies.Water;)?  If File and Paths are in the same package java.nio.file and your options are: import java.nio.file.*, import java.nio.file.Files; ,import java.nio.file.Paths but you cannot do import java.nio.file.Files.Paths, so why is this acceptable in choice C above?
 
Marshal
Posts: 64666
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think both are wrong; I tried compiling all three classes, and employee.Water compiled with A and D. Have you checked the errata for the book? Did you try to compile the code yourself?
 
Saloon Keeper
Posts: 3295
146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For all those thinking why on earth anything should be filled in, the excersize is as follows (at least in my book):

The rules are: if there is only one import of the class Water, then that is fine. So. option A is correct. If there are two imports of the class Water, then the import that is most specific is chosen. This is the case for options B and C, where the import ...Water is more specific than import ...*
If the imports are equally specific, a compiler error is generated, i.e. for options D and E. So, the book is correct.

Do you have an older book?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to resurrect this topic, but it is hurting my brain.

I understand the logic for answers B and C but not A.

This is mainly because earlier in the chapter it states that wildcards don't import subdirectories.  

Is aquarium.jellies not a subdirectory? I'm feeling extremely dumb right now.

Thanks,
Reg
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The resulting code for answer A is this:



As you can see, the Water class in that code refers to the aquarium.Water which is the first of the three classes in the question.

As you said, wildcards don't include subdirectories, and as you also believe (correctly), aquarium.jellies IS a subdirectory. So those two things imply the import statement there does not import aquarium.jellies. Therefore the import statement in answer A's code only refers to one version of the Water class and therefore it compiles successfully.

So I dunno... everything you thought was correct was indeed correct, but somehow your thought process went off the rails after that. Anyway, does that clarify things for you?
 
Reginald Copound
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul, I get it now.
For some reason my brain wanted imports for both classes.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alrighty then!

And by the way, welcome to the Ranch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!