(maxwell@gamba.130) java Newton 2 Step: 1 x:4.18504 Value:-0.864144 Step: 2 x:2.46789 Value:0.623881 Step: 3 x:3.26619 Value:-0.124272 Step: 4 x:3.14094 Value:0.000648741 Step: 5 x:3.14159 Value:-9.10111e-11 Zero found at x=3.14159 (maxwell@gamba.131)
The number 2 plays the role of a command line parameter.
The arguments to the main method provide a mechanism for
telling a Java program what the command line parameters are. You will
remember that the main method has as its argument an
array of Strings called argv. There are as many elements
in the array as there are command line parameters, and our method checks
to see if there are any such parameters. If you have an array called
my_array, the length of the array is kept in the variable
my_array.length. So, we can see if there are command line
arguments by checking the length of argv.
Like any programming language, Java provides a way of testing
values. In this case, it is the
The keyword if construction:
if(argv.length==1) {
x= Double.valueOf(argv[0]).doubleValue();
}
Here, we test if there is exactly one command line argument. The
test uses the boolean operator == which returns
true if two numbers have the same numeric value,
and false if they are different. Be sure not
to confuse the boolean operator == with the assignment
operator = - I make this mistake all the time! Some other
useful boolean operators are: greater than (>), less
than (<), and not equal (!=).
if takes either true or
false as its argument. If its argument is true,
the Java interpreter executes the commands in the following curly braces;
otherwise it skips them.
|
5.7.3 Calculating in the Main Loop |
David Maxwell,
who is still writing this, would like to
hear your comments and suggestions.