Exercises

  1. (Patterns, easy) Do a web search to find all the pieces that comprise a URL in its full generality. State what these pieces are. Write a pattern-matching program that checks URLs for validity (such as no embedded spaces). Show the output of your program when run on both good and bad URLs.

  2. (Patterns, medium) An email message consists of an arbitrary number of headers, then a blank line, then the body of the text. The headers all have a label followed by a colon, a space, and some optional text all on one line. The "From:" and "To:" headers are required, and come first in that order. The pattern would begin something like this:

    Pattern email = Pattern.compile(
                    "^From: .*$"       // "From" line
                    + "^To: .*$"       // "To" line

    Write the rest of a pattern to match an email message, and use capturing groups to separate the individual parts. Don't forget to make this a multiline pattern.

  3. (Patterns, harder) Write a pattern that matches the Java regular expressions presented in this text. This is several days of work. Run your meta-pattern matcher on all the Java regular expression Strings shown in this chapter. Account for any that it does not accept.

  4. (Math, medium) Convince yourself that the nextGaussian() numbers form a bell curve by writing a program to actually do it. Hint: You'll find it easier to plot a graph using ASCII text if you generate the values first, save them, and sort them into order before plotting the values. Which of the collection classes is a good choice to help with storing and sorting?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.173.199