A mathematical table.

exercise No. 99

Nicely formatting sine values. Create comment in forum

Q:

We are interested in the following output presenting the sine function rounded to three decimal places:

  x | sin(x)
----+------
  0 | 0.000
  5 | 0.087
 10 | 0.174
 15 | 0.259
 20 | 0.342
----+-------
 25 | 0.423
 30 | 0.500
 35 | 0.574
 40 | 0.643
----+-------
... (left out for brevity's sake)
----+-------
325 |-0.574
330 |-0.500
335 |-0.423
340 |-0.342
----+-------
345 |-0.259
350 |-0.174
355 |-0.087

You may also generate HTML output.

Write a corresponding Java application producing this output. You will have to deal with alignment problems, leading spaces, padding zeros and so on. Though more sophisticated support exists the following hints will fully suffice:

  1. Consider the sin(...) function. The documentation will tell you that the argument to sin(...) is expected to be in radians rather than common degree values from [0,360[ being expected on output. So you will have to transform degree values to radians.

  2. Depending on the angle's value you may want to add one or two leading spaces to keep the first column right aligned.

  3. Depending on the sign you may want to add leading spaces to the second column.

  4. Rounding the sine's value is the crucial part here. The function round() is quite helpful. Consider the following example rounding 23.22365 to four decimal places:

    1. Multiplication by 10000 results in 232236.5

    2. round(232236.5) results in 232237 (long).

    3. Integer division by 10000 yields 23

    4. The remainder (by 10000) is 2237

    5. So you may print 23.2237 this way

  5. You'll need padding zero values to transform e.g. 0.4 to 0.400.

A:

See MathTable.