• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris
Bartenders:

Chapter 1 - Review Question 6 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there

I'm having trouble understanding the answer to review question 6 in chapter 1 of the book.
Can someone tell me where my thinking is going wrong.
I thought the code would not compile because you have


and according to the "Ordering Elements in a Class" section of chapter 1, Table 1.4 this is not allowed. Packages must be the first statements in the code if they are present. How then can you have a package after a class? Or am I not getting it right?
The correct answer in the book is E.
I thought it was F.

6. Given the following classes, what is the maximum number of imports that can be removed and have the code still compile?


A. 0
B. 1
C. 2
D. 3
E. 4
F. Does not compile.

When I try compiling the code with:
javac Water.java
I get the following errors. (I get similar errors if I try compiling with javac Tank.java, I'm not sure which one I should be doing)

Water.java:2: error: class, interface, or enum expected
package aquarium;
^
Water.java:3: error: class, interface, or enum expected
import java.lang.*;
^
Water.java:4: error: class, interface, or enum expected
import java.lang.System;
^
Water.java:5: error: class, interface, or enum expected
import aquarium.Water;
^
Water.java:6: error: class, interface, or enum expected
import aquarium.*;
^
5 errors

Where am I going wrong? Please help

Thank you

And Jeanne and/or Scott if you happen to come across this post and aren't happy about me posting this question from your book, please tell me. I'm thinking I'll be asking quite quite a few questions in the coming weeks, so I'll refrain from doing it again.
 
Tom Keegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, now that I think of it. I had the same confusion with review question 5.
The package, import and class aren't in the order they're supposed to be according to table 1.4.
Am I being daft?
 
author & internet detective
Posts: 41763
885
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the printed copy of the book, there is a line break before each package statement. This is intended to show that each is defined in a separate file. I checked the electronic version of the book and these line breaks are missing. You can assume when this happens that each package statement is intended to be in a separate file.

I've added this to our errata and credited you with reporting it. This kind of issue is hard to prevent because it was right, when we wrote it, in all the page proofs we reviewed and in the printed book.

And it's fine to post questions from the book as long as you aren't posting all of them . Before posting, check the errata to confirm it hasn't been reported already. If it is still not clear, it is fine to ask (ideally by replying to that thread).
 
Tom Keegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks heaps for the quick reply Jeanne.

Just so I'm clear, can I clarify for myself



Is that right?
And something similar for questions 5 and 7?
 
Jeanne Boyarsky
author & internet detective
Posts: 41763
885
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is correct. There are two files in the example you are using. And the same technique applies elsewhere as well.
 
Tom Keegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help Jeanne
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tom Keegan,

First of all, a warm welcome to CodeRanch!

Tom Keegan wrote:Is that right?
And something similar for questions 5 and 7?


Just a little (useful) tip: if you want to simulate different (seperate) source code files, you can use multiple code-tags, one for every source code file. For each source code file line numbering will start at 1. And if required you can add at the top a comment with the name of the source code file as well. So let's apply this tip to the code snippet and you'll get

Kind regards,
Roel
 
Tom Keegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the welcome Roel.

And thanks for the helpful tip, much appreciated.
 
what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic