Java Reference
In-Depth Information
char[][] screen = new char[ROWS][];
for (int row = 0; row < ROWS; row++)
screen[row] = new char[COLS];
double scaleX = COLS/360.0;
for (int degree = 0; degree < 360; degree++)
{
int row = ROWS/2+
(int)
Math.round(ROWS/
2*Math.sin(Math.toRadians(degree)));
int col = (int) (degree*scaleX);
screen[row][col] = 'S';
row = ROWS/2+
(int)
Math.round(ROWS/
2*Math.cos(Math.toRadians(degree)));
screen[row][col] = (screen[row][col] == 'S') ? '*'
: 'C';
}
for (int row = ROWS-1; row >= 0; row--)
{
for (int col = 0; col < COLS; col++)
System.out.print(screen[row][col]);
System.out.println();
}
}
}
Listing 4-1 introduces a Graph class that first declares a pair of constants: NROWS
and NCOLS . These constants specify the dimensions of an array on which the graphs
are generated. NROWS must be assigned an odd integer; otherwise, an instance of the
java.lang.ArrayIndexOutOfBoundsException class is thrown.
Tip It'sagoodideatouseconstantswhereverpossible.Thesourcecodeiseasierto
maintainbecauseyouonlyneedtochangetheconstant'svalueinoneplaceinsteadof
having to change each corresponding value throughout the source code.
Graph next declares its main() method, which first creates a two-dimensional
screen arrayofcharacters.Thisarrayisusedtosimulateanold-stylecharacter-based
screen for viewing the graphs.
Search WWH ::




Custom Search