This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java 1.5 test Exercise 5-4 Sun Certified Programmer for Java 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Java 1.5 test Exercise 5-4 Sun Certified Programmer for Java 5" Watch "Java 1.5 test Exercise 5-4 Sun Certified Programmer for Java 5" New topic
Author

Java 1.5 test Exercise 5-4 Sun Certified Programmer for Java 5

Kevin Mayer
Greenhorn

Joined: Jan 28, 2009
Posts: 7
1) Is there sample sample code for this exercise(BadFoodException) in the book?

2) Is it OK to have more than one class in a file if neither one is declared public? It looks like that is what they are asking for in the instructions.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Kevin you'll get better replies if you include the question.
Is it OK to have more than one class in a file if neither one is declared public?

Yes it is allowed. Only one public class is allowed per .java file and the name of the public class and .java file must be the same...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Kevin Mayer
Greenhorn

Joined: Jan 28, 2009
Posts: 7
thank you for answering the second question.

Concerning the first question..., what I asking is if there is sample code in the book for the exercise 5-4? Here is the exercise.

Page 365
exercise 5-4
Creating an exception
In this exercise we attempt to create a custom exception. We won't put in any new
methods (it will have only those inherited from Exception), and because it extends
Exception, the compiler considers it a checked exception. The goal of the program
is to determine whether a command-line argument, representing a particular food
(as a string), is considered bad or OK.
* Let's first create our exception. We will call it BadFoodException. This excep-
tion will be thrown when a bad food is encountered.
* Create an enclosing class called MyException and a main() method,
which will remain empty for now.
* Create a method called checkFood(). It takes a String argument and throws
our exception if it doesn't like the food it was given. Otherwise, it tells us it likes
the food. You can add any foods you aren't particularly fond of to the list.
* Now in the main() method, you'll get the command-line argument out of the
String array, and then pass that String on to the checkFood() method.
Because it's a checked exception, the checkFood() method must declare it, and
the main() method must handle it (using a try/catch). Do not have main()
declare the exception, because if main() ducks the exception, who else is back
there to catch it?
* As nifty as exception handling is, it's still up to the developer to make proper
use of it. Exception handling makes organizing our code and signaling problems
easy, but the exception handlers still have to be written. You'll find that even
the most complex situations can be handled, and your code will be reusable,
readable, and maintainable.
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
I did a quick look through of my book (having the same one) and didn't see any example code of this. However, I don't think it is too complicated, and I think if you googled "java extend Exception" you should find some examples.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Since it is an exercise, I think its meant for you to do (a guess as I don't have the book but there are unsolved exercises in K&B book too)...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java 1.5 test Exercise 5-4 Sun Certified Programmer for Java 5
 
Similar Threads
Sample chapters of this book?
OO design question?
need help with java algorithm
I require some Help!!
Superclass that requires any of its subclasses to create a no-arg constructor?