Java Reference
In-Depth Information
You should then get the following output:
Exception in thread "main" java.lang.AssertionError
at TryAssertions.main(TryAssertions.java:15)
How It Works
Because you have set daysInMonth to an invalid value, the assertion statement is executed, and that res-
ults in the error message. You could try out the other form of the assertion in the example:
assert false : "daysInMonth has the value " + daysInMonth;
Now you should see that the output includes the string resulting from the second expression in the asser-
tion statement:
Exception in thread "main" java.lang.AssertionError: daysInMonth has
the value 32
at TryAssertions.main(TryAssertions.java:16)
I will use assertions from time to time in the examples in subsequent chapters.
SUMMARY
In this chapter you have learned about all of the essential mechanisms for making decisions in Java. You
have also learned all of the looping facilities that you have available when programming in Java. You will
use all of these capabilities in examples throughout the rest of the topic.
EXERCISES
You can download the source code for the examples in the topic and the solutions to the following exer-
cises from www.wrox.com .
1. Write a program to display a random choice from a set of six choices for breakfast (you could use
any set — for example, scrambled eggs, waffles, fruit, cereal, toast, or yogurt).
2. When testing whether an integer is a prime, it is sufficient to try to divide by integers up to and
including the square root of the number being tested. Rewrite the program example from this chapter
to use this approach.
3. A lottery requires that you select six different numbers from the integers 1 to 49. Write a program
to do this for you and generate five sets of entries.
4. Write a program to generate a random sequence of capital letters that does not include vowels.
Search WWH ::




Custom Search