/** * AssertDemoApp.java * * * Created: Fri Jan 10 16:01:40 2003 * * @author Stuart C. Shapiro * * Demonstrates the usefulness of Java assertions and exceptions. */ public class AssertDemoApp { /** * Prints n, formatted as a 10-digit phone number, * with the comment that it is a possible phone number. *

* pre: n.length == 10. *

* post: n is printed as a 10-digit phone number, * formatted as a phone number, * with the comment that it is a possible phone number. * * @param n a 10-digit String. */ public static void CheckPhoneNumber (String n) { assert n.length() == 10: "Illegal phone number"; System.out.println(n.substring(0,3) + "-" + n.substring(3,6) + "-" + n.substring(6) + " is a possible phone number."); } /** * Driver for AssertDemoApp, * demonstrating a failed assertion. * */ public static void main (String[] args) { CheckPhoneNumber("7165551234"); CheckPhoneNumber("201467"); } // end of main () }// AssertDemoApp