Title: CS1101X: Programming Methodology Recitation 5 Exceptions
1CS1101X Programming MethodologyRecitation 5
Exceptions Characters and Strings
2Ch8AgeInputMain.java
import javax.swing. import java.util. class
Ch8AgeInputMain public static void main(
String args ) GregorianCalendar
today int age, thisYear,
bornYr, answer AgeInputVer1 input new
AgeInputVer1( ) //no exception handling //
AgeInputVer2 input new AgeInputVer2( ) //first
level exception handling // AgeInputVer3
input new AgeInputVer3( ) //more exeption
handling age input.getAge("How old
are you?") today new
GregorianCalendar( ) thisYear
today.get(Calendar.YEAR) bornYr
thisYear - age answer
JOptionPane.showConfirmDialog(null,
"Already had your birthday this
year?", "",
JOptionPane.YES_NO_OPTIO
N) if (answer JOptionPane.NO_OPTION)
bornYr-- JOptionPane.showMes
sageDialog(null, "You are born in " bornYr)
3AgeInputVer1.java
No check.
import javax.swing. class AgeInputVer1
private static final String DEFAULT_MESSAGE
"Your age" public AgeInputVer1( )
public int getAge() return
getAge(DEFAULT_MESSAGE) public int
getAge(String prompt) String inputStr
JOptionPane.showInputDialog(null, prompt)
int age Integer.parseInt(inputStr)
return age
4AgeInputVer2.java
What if user enters nine?
import javax.swing. class AgeInputVer2 //
constant, constructor and getAge() omitted same
as version 1 public int getAge(String prompt)
String inputStr int age
while (true) inputStr
JOptionPane.showInputDialog(null, prompt)
try age Integer.parseInt(inputS
tr) return age // input okay so
return the value catch
(NumberFormatException e)
JOptionPane.showMessageDialog(null, "'"
inputStr "' is
invalid\n" "Please enter digits only")
5AgeInputVer3.java
What if user enters negative value?
import javax.swing. class AgeInputVer3 //
constant, constructor and getAge() omitted same
as version 1 public int getAge(String prompt)
String inputStr int age
while (true) inputStr
JOptionPane.showInputDialog(null, prompt)
try age Integer.parseInt(inputS
tr) if (age lt 0)
throw new Exception("Negative age is invalid")
return age // input okay so return
the value catch (NumberFormatException
e) JOptionPane.showMessageDialog(n
ull, "'" inputStr
"' is invalid\n" "Please enter digits only")
catch (Exception e)
JOptionPane.showMessageDialog(null, "Error "
e.getMessage())
6AgeInputVer4.java (1/3)
Adding a valid range for age.
import javax.swing. class AgeInputVer4
private static final String DEFAULT_MESSAGE
"Your age" private static final int
DEFAULT_LOWER_BOUND 0 private static final
int DEFAULT_UPPER_BOUND 99 private int
lowerBound private int upperBound //
default constructor public AgeInputVer4( )
throws IllegalArgumentException
setBounds(DEFAULT_LOWER_BOUND, DEFAULT_UPPER_BOUND
) // Contructs an age input with the
specified lower and upper bounds. public
AgeInputVer4(int low, int high) throws
IllegalArgumentException if (low gt high)
throw new IllegalArgumentException(
"Low (" low ") was "
"larger than high(" high
")") else setBounds(low,
high)
7AgeInputVer4.java (2/3)
Adding a valid range for age.
public int getAge() throws Exception
return getAge(DEFAULT_MESSAGE) //
Inputs the age from an input dialog with the
designated prompt public int getAge(String
prompt) throws Exception String
inputStr int age while (true)
inputStr JOptionPane.showInputDialog
(null, prompt) try
age Integer.parseInt(inputStr)
if (age lt lowerBound age gt upperBound)
throw new Exception("Input out of
bound") return age // input
okay so return the value catch
(NumberFormatException e)
JOptionPane.showMessageDialog(null, "'"
inputStr "' is
invalid\n" "Please enter digits only")
8AgeInputVer4.java (3/3)
// Sets the lower and upper bounds of the
input private void setBounds(int low, int
high) lowerBound low upperBound
high
9AgeInputVer5.java
Programmer-defined exceptions.
- Essentially the same as AgeInputVer4.java except
that now the getAge method throws an
AgeInputException. - The AgeInputException is defined by the
programmer.
// Inputs the age from an input dialog with
the designated prompt public int getAge(String
prompt) throws AgeInputException String
inputStr int age while (true)
inputStr JOptionPane.showInputDialog
(null, prompt) try
age Integer.parseInt(inputStr)
if (age lt lowerBound age gt upperBound)
throw new AgeInputException("Input
out of bound",
lowerBound, upperBound, age)
return age // input okay so return the value
// code omitted
10AgeInputException.java (1/2)
class AgeInputException extends Exception
private static final String DEFAULT_MESSAGE
"Input out of bounds" private int lowerBound
// lower bound of age input private int
upperBound // upper bound of age input
private int value // entered value
public AgeInputException(int low, int high, int
input) this(DEFAULT_MESSAGE, low, high,
input) public AgeInputException(String
msg, int low, int high, int input)
super(msg) if (low gt high)
throw new IllegalArgumentException()
lowerBound low upperBound
high value input
11AgeInputException.java (2/2)
// Returns the lower bound of the age input
public int lowerBound() return
lowerBound // Returns the upper bound
of the age input public int upperBound()
return upperBound // Returns the
entered value public int value()
return value
12Characters and Strings
- Exercise 17 (page 537)
- Write an application NumVowels.java that reads in
a sentence and displays the count of individual
vowels in the sentence. Use any output routine of
your choice to display the result in this format.
Count both the lowercase and uppercase vowels.
Vowel counts for the sentence Mary Had A Little
Lamb. Number of 'a' 4 Number of 'e' 1 Number
of 'i' 1 Number of 'o' 0 Number of 'u' 0
13Characters and Strings
- Exercise 11 (page 536)
- Write a program ReverseCase.java that reads a
sentence and prints out the sentence with all
uppercase letters changed to lowercase and all
lowercase letters changed to uppercase.
Enter sentence Mary Had A LiTTle LaMb. mARY hAD
a lIttLE lAmB.
14End of Recitation 5