Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings.

Similar presentations


Presentation on theme: "CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings."— Presentation transcript:

1 CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings

2 CS1101X Recitation #52 Ch8AgeInputMain.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_OPTION); if (answer == JOptionPane.NO_OPTION) bornYr--; JOptionPane.showMessageDialog(null, "You are born in " + bornYr); }

3 CS1101X Recitation #53 AgeInputVer1.java 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; } No check.

4 CS1101X Recitation #54 AgeInputVer2.java 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(inputStr); return age; // input okay so return the value } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "'" + inputStr + "' is invalid\n" + "Please enter digits only"); } What if user enters “nine”?

5 CS1101X Recitation #55 AgeInputVer3.java 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(inputStr); if (age < 0) throw new Exception("Negative age is invalid"); return age; // input okay so return the value } catch (NumberFormatException e) { JOptionPane.showMessageDialog(null, "'" + inputStr + "' is invalid\n" + "Please enter digits only"); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error: " + e.getMessage()); } What if user enters negative value?

6 CS1101X Recitation #56 AgeInputVer4.java (1/3) 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 > high) { throw new IllegalArgumentException( "Low (" + low + ") was " + "larger than high(" + high + ")"); } else setBounds(low, high); } Adding a valid range for age.

7 CS1101X Recitation #57 AgeInputVer4.java (2/3) 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 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"); } Adding a valid range for age.

8 CS1101X Recitation #58 AgeInputVer4.java (3/3) // Sets the lower and upper bounds of the input private void setBounds(int low, int high) { lowerBound = low; upperBound = high; }

9 CS1101X Recitation #59 AgeInputVer5.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 upperBound) throw new AgeInputException("Input out of bound", lowerBound, upperBound, age); return age; // input okay so return the value } // code omitted }

10 CS1101X Recitation #510 AgeInputException.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 > high) { throw new IllegalArgumentException(); } lowerBound = low; upperBound = high; value = input; }

11 CS1101X Recitation #511 AgeInputException.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; }

12 CS1101X Recitation #512 Characters 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

13 CS1101X Recitation #513 Characters 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.

14 CS1101X Recitation #514 End of Recitation #5


Download ppt "CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings."

Similar presentations


Ads by Google