Answered step by step
Verified Expert Solution
Question
1 Approved Answer
My current program: import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Random; import java.util.Scanner; public class E5 { public static void main(String[] args) throws Exception { Random r
My current program:
import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Random; import java.util.Scanner;
public class E5 { public static void main(String[] args) throws Exception { Random r = new Random(); int randomInt = r.nextInt(100); FileOutputStream out = new FileOutputStream("integers.dat"); for(int i = 1; i max) { max = input; } else if(input
}
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 farStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started