Java Reference
In-Depth Information
main()
nextcalculatesahorizontalscalevalueforscalingeachgraphhorizontally
so that 360 horizontal (degree) positions fit into the number of columns specified by
NCOLS
.
Continuing,
main()
enters a for loop that, for each of the sine and cosine graphs,
creates(row,column)coordinatesforeachdegreevalue,andassignsacharactertothe
screen
arrayatthosecoordinates.Thecharacteris
S
forthesinegraph,
C
fortheco-
sine graph, and
*
when the cosine graph intersects the sine graph.
Therowcalculationinvokes
toRadians()
toconvertits
degree
argumenttora-
dians, which is required bythe
sin()
and
cos()
methods. The value returned from
sin()
or
cos()
(-1to1)isthenmultipliedby
ROWS/2
toscalethisvaluetohalfthe
numberofrowsinthe
screen
array.Afterroundingtheresulttothenearestlongin-
tegerviathe
long round(double d)
method,acastisusedtoconvertfromlong
integertointeger,andthisintegerisaddedto
ROW/2
tooffsettherowcoordinatesothat
it'srelativetothearray'smiddlerow.Thecolumncalculationissimpler,multiplyingthe
degree value by the horizontal scale factor.
The
screen
array is dumped to the standard output device via a pair of nested for
loops.Theouterforloopinvertsthescreensothatitappearsrightsideup—rownumber
0 should output last.
Graph
). You observe the following output:
CC SSSS CC
CSSS SS CC
S*C SS CC
S CC SS CC
SS CC SS CC
S CC S CC S
C SS C SS
CC SS CC S
CC SCC SS
CC CSS SSS
CCCCC SSSS
Note
When I created the
screen
array, I took advantage of the fact that every
element is initialized to 0, which is interpreted as the null character. When a
Sys-
tem.out.print()
or
System.out.println()
methoddetectsthischaracter,
it outputs a space character instead.