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

CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings

Embed Size (px)

Citation preview

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

CS1101X: Programming Methodology

Recitation 5 Exceptions &

Characters and Strings

Page 2: CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings

CS1101X Recitation #5 2

Ch8AgeInputMain.javaimport 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); }}

Page 3: CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings

CS1101X Recitation #5 3

AgeInputVer1.javaimport 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.

Page 4: CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings

CS1101X Recitation #5 4

AgeInputVer2.javaimport 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”?

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

CS1101X Recitation #5 5

AgeInputVer3.javaimport 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?

Page 6: CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings

CS1101X Recitation #5 6

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.

Page 7: CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings

CS1101X Recitation #5 7

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 < lowerBound || 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.

Page 8: CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings

CS1101X Recitation #5 8

AgeInputVer4.java (3/3)

// Sets the lower and upper bounds of the input private void setBounds(int low, int high) { lowerBound = low; upperBound = high; }}

Page 9: CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings

CS1101X Recitation #5 9

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 < lowerBound || age > upperBound) throw new AgeInputException("Input out of bound", lowerBound, upperBound, age);

return age; // input okay so return the value

} // code omitted } }

Page 10: CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings

CS1101X Recitation #5 10

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; }

Page 11: CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings

CS1101X Recitation #5 11

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; }

}

Page 12: CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings

CS1101X Recitation #5 12

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 sentenceMary Had A Little Lamb.

Number of 'a': 4Number of 'e': 1Number of 'i': 1Number of 'o': 0Number of 'u': 0

Page 13: CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings

CS1101X Recitation #5 13

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.

Page 14: CS1101X: Programming Methodology Recitation 5 Exceptions & Characters and Strings

CS1101X Recitation #5 14

End of Recitation #5