Question: Figure 8.2b has two if statement conditions that contain what are called regular expressions. As indicated, these are explained in the Java API Pattern class.

Figure 8.2b has two if statement conditions that contain what are called regular expressions. As indicated, these are explained in the Java API Pattern class. This exercise is intended to help you get a better feeling for Java’s regular expressions and their usage. Use your Java API documentation on the Pattern class to answer these questions:

a) Write the regular expression for a character string starting with a ‘Z’ and containing any number of additional characters of any kind except for a space or a tab.

b) Write the regular expression for a string that represents a U.S. long- distance telephone number (three digits, a hyphen or space, three digits, a hyphen or space, and four digits).

c) What is the meaning of the regular expression, "[A–Z][a–z]*", which appears in Figure 8.2b?

Figure 8.2b

// This method verifies that first starts with an uppercase // letter and contains lowercase letters

// This method verifies that first starts with an uppercase // letter and contains lowercase letters thereafter. public void setFirst(String first) ( // [A-Z][a-z]* is a regular expression. See API Pattern class. if (first.matches ("[A-Z][a-z]*")) { this.first = first; } else { System.out.println(first + " is an invalid name. " + "Names must start with an uppercase letter and have" + " lowercase letters thereafter."); } } // end setFirst //************** // This method verifies that last starts with an uppercase // letter and contains lowercase letters thereafter. this. last = last; public void setLast(String last) { // [A-Z][a-z]* is a regular expression. See API Pattern class. if (last.matches ("[A-Z][a-z]*")) { } else { ********* } } // end set Last "1 System.out.println(last + is an invalid name. " + "Names must start with an uppercase letter and have" + " lowercase letters thereafter."); //********** *********************** public void print Full Name ( ) { **** // Print the student's first and last names. ******************** System.out.println(this. first + " " + this. last); 3 // end print Full Name } // end class Student

Step by Step Solution

3.46 Rating (166 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

a The regular expression for a character string starting with a Z and cont... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Introduction To Programming With Java A Problem Solving Approach Questions!