created 05/08/00; edits 11/20/2012
Write a program that creates a file
containing TotalCount
random integers
(in character format)
in the range 0 to HighValue-1
.
Write PerLine
integers per line.
Separate each integer with one space.
End each line with the correct line
termination for your computer.
The user is prompted for and
enters HighValue
,
which should be an integer larger than zero.
Then the user is prompted for and
enters PerLine
, which is
an integer greater than zero,
and TotalCount
, which also is
an integer greater than zero.
Finally the user is prompted for and
enters the file name.
Use a BufferedWriter
with a FileWriter
for output.
Construct a Random
object
and use its
method nextInt(int Top)
,
which returns an int
in the range 0..Top-1.
C:\Programs>java RandomIntData Enter HighValue-->100 Enter how many per line-->10 Enter how many integers-->100 Enter Filename-->rdata.dat C:\Programs>type rdata.dat 4 12 54 10 38 97 40 11 80 16 36 41 67 67 93 58 62 12 50 99 18 42 9 28 45 6 68 72 80 28 86 63 22 17 68 18 59 50 6 50 90 8 68 61 9 24 77 34 62 61 63 8 15 17 67 58 34 56 12 50 43 85 39 77 30 68 89 88 65 68 84 29 42 74 48 55 19 82 95 3 39 27 25 96 41 39 18 84 39 88 82 58 84 90 74 35 24 89 85 92
Click here to go back to the main menu.
The previous program used a BufferedWriter
between the program and the FileWriter
stream.
Using a buffer is supposed to increase the efficiency
of the program.
Does it?
Modify the previous program
so that it does not use newLine()
(but still uses BufferedWriter
).
Use it to create a file of 1,000,000 integers.
Time how long this takes with a watch.
Now modify the program by removing the BufferedWriter
.
Again create a file of 1,000,000 integers and time how
long it takes.
Is there a difference?
(On my system there was a sizable difference.)
Be sure that the two versions use exactly the same logic.
That is why the newLine()
method has to be removed.
Click here to go back to the main menu.
Write a program that creates a text file that
contains a power of two table in HTML format.
The file could be called twoPowerTable.html
.
When it is viewed with a browser you will see
something like:
Power of 2 | Value |
---|---|
0 | 1 |
1 | 2 |
2 | 4 |
3 | 8 |
4 | 16 |
5 | 32 |
6 | 64 |
7 | 128 |
8 | 256 |
9 | 512 |
10 | 1024 |
The file should start with something like:
<html><head> <title>Powers of Two</title> </head> <body> <table border cellpadding=5> <tr><th>Power of 2</th><th>Value</th></tr>
And end with:
</table> </body></html>
Each line of the table looks like:
<tr><td>0</td><td>1</td></tr>
Look at the HTML tutorial at the end of these notes (or other on-line HTML tutorial).
Click here to go back to the main menu.
Write a program that creates text files that
contain powers of integers tables for integers 2 through 5.
The files could be called
powerTable2.html
,
powerTable3.html
,
powerTable4.html
,
powerTable5.html
,
and should be formatted similar to the previous exercise.
Do this in a loop rather than in four separate sections of
code that do nearly the same thing.
Your program will have to construct a file name from a base file
name powerTable
and create separate disk files.
Click here to go back to the main menu.
End of Exercises.