main method does before it
exits is tell the user the results from the calculation. The
two possible outcomes are either we found a root or we exceeded
the allowed iterations. Again, we use an if construction,
but this time we see also an addition, else.
We test to see if the last value of
Notice that there is no x qualifies as a root
with:
if( Math.abs(f(x)) <= tolerance) {
System.out.println("Zero found at x="+x);
}
All of this should be clear from what you have seen earlier in the
example. If we didn't find a root, we would like to tell the user
this as well. We can tack on to the end of an if
statement an else statement that will be executed if
the if fails. In our case, this is:
else {
System.out.println("Failed to find a zero");
}
All together, then we have:
if( Math.abs(f(x)) <= tolerance) {
System.out.println("Zero found at x="+x);
}
else {
System.out.println("Failed to find a zero");
}
return statement in main,
since main returns type void.
|
6 Beginners' Java Reference Manual |
David Maxwell,
who is still writing this, would like to
hear your comments and suggestions.