Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write two static methods writeArray and readArray; both methods should accept tw

ID: 3829607 • Letter: W

Question

Write two static methods writeArray and readArray; both methods should accept two arguments, the name of a file, and a reference to an integer array: writeArray: The file should be opened as binary file, the content of the array should be written to the file, and then file should be closed. This method throws an IllegalArgumentException if any of the elements of the array is less than 0 or greater than 100 readArray: The file should be opened, data should be read from the file and stored into the array and then the file should be closed This method should return an ArrayList object (Integer) containing of the Sum, the Average of Even Values, and the mean of the entire array (same as Average; the arithmatic mean is the Sum devided by the size of the set/array) of the array that has been read from the file. This method also throws an IllegalArgumentException if any of the elements of the array is less than 0 or greater than 100 Both writeArray and readArray methods should take of the IO Exception (Hint: Using Try-Catch) The file for both methods MUST be closed in the finally Section (Hint: In the example that I have submitted for you in the class I have provided how it should be done in order to accept your file to get closed in finally block) Main method: generate an integer array (don't ask from the user, make a sample array) between the size 5 to 15 and use those methods and stored the "readArray" function to local variable of ArrayList object, then use a loop to read through the list and display the Sum, Average and the mean.

Explanation / Answer

Driver.java

import java.util.ArrayList;

public class Driver {

   public static void main(String[] args) {
   int[] Array = new int[]{1,2,3,4,5};
      
   ArrayList<Integer> A = new ArrayList<Integer>();
      
   try
   {
ArrayOperations.writeArray(Array, "SomeFile.dat");
A= ArrayOperations.readArray(Array, "SomeFile.dat");

   }
   catch(IllegalArgumentException e)
   {
e.printStackTrace();
   }
   System.out.println("Sum =" +A.get(0));
System.out.println("Average of even nos =" +A.get(0));
           //so on with average of even numbers and mean of entire array   
   }
}

ArrayOperations.java

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class ArrayOperations {
  
public static void writeArray (int[] write_from,String FileName)throws IllegalArgumentException
{
   try
{
DataOutputStream output = new DataOutputStream(new FileOutputStream(FileName));
          
for(int i=0;i<write_from.length;i++)
{
if(write_from[i]< 0 || write_from[i]> 100)
throw new IllegalArgumentException("Values were not legal");
       else
output.writeInt(write_from[i]);      
}
output.close();
   }
         
   catch (IOException e)
{
e.printStackTrace();
   }
}
@SuppressWarnings("finally")
public static ArrayList<Integer> readArray(int[] a, String fileName)throws IllegalArgumentException {

int[] array = new int[a.length]; int even_nums=0; int sum_even_nums=0; int sum=0;
ArrayList<Integer> A = new ArrayList<Integer>();

try
{
DataInputStream input = new DataInputStream (new BufferedInputStream(new FileInputStream(fileName)));

int value; int i=0;
while (input.available()>0)
{
value = input.readInt();
if(value<0 || value>100)
throw new IllegalArgumentException("Values were not legal");

array[i++]=value;

//do operation to the arraylist
sum+=value;
if(value%2==0)
{  
sum_even_nums+=value;
even_nums++;
}              
}
A.add(0,sum);
A.add(1,sum_even_nums/even_nums);
A.add(2,sum/A.size());      
input.close();
}

catch ( IOException e)
{
e.printStackTrace();
}
finally
{
return A;
}          
}  
}

Output:

run:
Sum =15
Average of even nos =15
BUILD SUCCESSFUL (total time: 0 seconds)