Way Back, You Were Given Two JAVA Programs
Total Page:16
File Type:pdf, Size:1020Kb
Testing Exceptions – due 07/14
Way back, you were given two JAVA programs MyInput which contained two methods readInt( ) returned an int from the keyboard readDouble( ) returned a double from the keyboard
TestMyInput – tested the two methods
Later, you added two methods to MyInput readChar( ) returned a character readString( ) returned a String readFloat() returned a float
You may have stumbled across the fact that your program bombed if an invalid character was entered in readInt( ) or readDouble( )
We will fix that with exception handling
Keep the program names as TestMyInput.java and MyInput.java
Add to the following methods…. readInt( ) & readFloat Add to this method the logic to catch the exception if an invalid character is entered on the keyboard or if no integers are entered on the keyboard. Process the exception in the method.
If an exception(s) occurs, display to the screen ‘invalid data entered’ and the data entered ‘no data entered’ return a 0 or 0.0
the program should not bomb readDouble( ) If an invalid character is entered on the keyboard or if no data is entered on the keyboard, allow the exception to move up the call chain and be captured and processed in the calling method.
So, TestMyInput will catch the exceptions from readDouble( )
If an exception(s) occurs, display to the screen invalid data entered and the data entered no data entered return a 0
the program should not bomb readChar( ) design for yourself one or two of your own Exception classes
in readChar( ) If more than one character is entered on the keyboard, throw one of your exceptions. TestMyInput should catch the Exception and display an error message more than one character entered and the data
If no characters are entered on the keyboard, throw the other exception. TestMyInput should catch the Exception and display an error message no data entered
the program should not bomb