Question
Write a complete Java program to do the following: (a) Write a method called readdata() which receives two parameters, an array of integers and an
Write a complete Java program to do the following:
(a) Write a method called readdata() which receives two parameters, an array of integers and an integer n, both of which are changed in the method, with the changes reflected in the calling method. From the file, the method reads a value into n, and then it reads n integers into the array. For the example if the file would contain 6 66 0 -4 0 4 31 22 0 49, the array would end up with 66 0 -4 0 4 31. (b) Write a method called countzeros() which receives two parameters, an array of integers and an integer n. The method counts and returns how many of the first n elements of the array are 0. Main will print the number of 0 values. For example, if the array holds 66 0 -4 0 4 31 with n = 6, it has two 0 values. (c) Write a method called append() which reads in new values until input failure (i.e. it reads the rest of the values starting from where readdata() left off, not re- reading the values used already) from the file, adding the new values to end of the array and incrementing n. append() must change both the array and the value of n. If the array initially holds 66 0 -4 0 4 31 with n = 6; after the method call, the array will hold 66 0 -4 0 4 31 22 0 49 with n= 9. (d) Write a method called printdata()which receives two parameters, an array of integers and an integer n. The method prints the values of the first n elements of the array. (e) Write a main() method to do the following: Main() calls readdata() to read in a set of data containing n elements into an array, which contains no more than 100 integers. Main() calls printdata() to print the array. Main() calls countzeros() to count how many of the n array elements are 0. Main() prints the count (along with an appropriate message, of course). Main() calls append() to modify the array and n. Main() calls printdata() to print the modified array. Main() calls countzeros() again to determine how many elements in the new array are 0. Main() prints the new count (along with an appropriate message, of course). Please choose your data so that this count changes sometimes.
(f) Run your program at least twice. Read in all data from a file. There should be no keyboard entry. Each time use a different file of carefully chosen data. For the example above, the file would contain 6 66 0 -4 0 4 31 22 0 49. You do not have to use the values in my example. Store the data in two files named file1.txt and file2.txt. Run your program once on each file. The first will change n from 3 to 5. This you will trace. The second will change n from 10 to 15. This you will not trace. No handcheck to submit, but look at your output and make sure its correct before you submit. Make sure you check if only the first array has 0s, only the second array, neither, and both. Your program will be run against test files. Dont write it to fit your data.
(g) Please submit one program listing (the same program must work for all data), two outputs, one trace, and two input files.
Step 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