Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

E5 (10 marks)

Focus: Binary I/O

Q1. Writing to / Reading from a binary file:


a) Let's try to create a data file. Develop a method that writes 10 random integers from 0 to
100 into a given file (e.g. named integers.dat) using binary I/O. You don’t have to
handle any exceptions for this example.
b) Let’s now try to read the data in a text editor or viewer. Use your favourite text editor or
viewer to open the data.dat file and notice how the integers are written. Copy/paste the
contents here as an answer to this part.
c) Now let’s try to read the file again using Java binary input stream. Implement a method to
read the integers you stored in the file created above (integers.dat) and return the
maximum only. You don’t have to handle any exceptions at this stage.
d) Test the above methods (a) and (c) by calling them in main using appropriate code.

Q2. Repeat (c) above after changing your method to return four statistical values: maximum, minimum,
sum, and average. You have to think of a way so that your method can return all these values in
one return statement. Call your method in main and print out all the four statistical values.

Q3. One way to track how many times a program is executed is to store an integer value in a file, and
every time the program is executed we read that value, increment it, and then save it back to the
file. Write a method that performs this function. Test your method by writing a simple HelloWorld
program that also prints how many times the program has been executed so far.

You might also like