Java Reference
In-Depth Information
E3. Write a function that changes its String argument of the form " first-name
last-name " into the form " last-name , first-name " . Throw an IllegalArgu-
mentException if the argument does not have the right form. Write the function
specification first, and be sure to state what “right form” means to you. Test your
function thoroughly.
E4. Write a function to compute (factorial n) , for int value n≥0 , i.e. the value
1*2*…*n . (Remember, factorial 0 is 1 .) Throw an IllegalArgumentException
if overflow occurs. Specify your function before writing it, and test it thorough-
ly.
E5. Write a function to compute the largest value (factorial n) that can be calcu-
lated using type int . Do this by calculating (factorial 1 ), (factorial 2 ), (factorial
3 ), using the function of Exercise E4, until a function call causes an exception.
This is an inefficient way to calculate the value, but it gives you practice with
exception handling.
E6. Write a function to read in and return an integer that the user types on their
keyboard. (See Sec. 5.7.1 for reading from the keyboard.) When a line is read as
a string from the keyboard, it has to be converted to an int , say, by using func-
tion Integer.parseInt . This function throws an exception if its argument can-
not be converted to an int , and if this happens, your function should again ask
the user to type an integer on their keyboard.
E7. Create an exception class and several subclasses of it. Then write a program
to demonstrate that a catch-clause parameter with the class type actually catches
exceptions with the subclass types. This will require using the catch-clause
parameter in the catch-block.
 
Search WWH ::




Custom Search