Creating a switch-case statement - propagating and catching


EXERCISE 6- I

Creating a switch-case Statement
Try creating a switch statement using a char value as the case, Include a default behavior if none of the char values match.
Creating Loops Constructs (OCA Objectives 5. I , 5.2, 5.3, 5.4, and 5.5) 3 2 I
 Make sure a char variable is declared before the switch statement.
 Each case statement should be followed by a break.
 The default case can be located at the end, middle, or top.

EXERCISE 6-2

Creating a Labeled while Loop
Try creating a labeled while loop. Make the label outer and provide a condition to check whether a variable age is lcss than or equal to 21, Within the loop, increment age by 1, Every time the program goes through the loop, check whether age is 16. If it is, print the message 'get your driver's license' and continue to the outer loop. If
not, print 'Another year."
 The outer label should appear just before the whil e loop begins.
 Make sure age is declared outside of the while loop,

EXERCISE 6-3

Propagating and Catching an Exception
In this exercise you're going to create two methods that deal with exceptions. One of the methods is the main ( ) method, which will call another method. If an exception is thrown in the other method, main c) must deal with it, A final ly statement will be included to indicate that the program has completed. The method that main ( will call will be named reverse, and it will reverse the order of the characters in a string. If the string contains no characters, reverse will propagate an exception up to the math ( ) method,

1. Create a class called Prepagat e and a main ( ) method, which will remain empty for now.

2. Create a method called reverse. It Lakes an argument of a String and returns a string.

3. In reverse, check whether the string has a length of 0 by using the string. length () method, If the length is 0, the reverse method will throw an exception,

4. Now include the code to reverse the order of the string, Because this isn't the main topic of this chapter, the reversal code has been provided, but feel free to try it on your own,
String reverseStr = mm;
for(int i=s,length()-1;i>=0;--i)
reverseStr += s.charAt(i);
return reverseStri

5. Now in the main ( ) method you will attempt to call this method and deal with any potential exceptions. Additionally, you will include a finally statement that displays when main ( ) has finished.

EXERCISE 6-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 Except ion), 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 okay.

1. Let's first create our exception. We will call it BadFoodExcept ion. This exception will be thrown when a bad food is encountered.

2. Create an enclosing class called MyException and a main ( ) method, which will remain empty for now.

3. 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,

4. 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 check Food method. Because it's a checked exception, the check Food U method must declare it, and the main method must handle it (using a t ry/catch). Do not have main ( ) declare the exception, because if main ( } ducks the exception, who else is back there to catch it? (Actually, main ( ) can legally declare excep¬tions, but don't do that in this exercise.)
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.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Creating a switch-case statement - propagating and catching
Reference No:- TGS02384476

Now Priced at $25 (50% Discount)

Recommended (94%)

Rated (4.6/5)