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

Using the file attached, read all 30 integers into an array of ints. Next, write

ID: 654178 • Letter: U

Question

Using the file attached, read all 30 integers into an array of ints. Next, write a sequential search to find the highest, the lowest, and the sum and average of all the numbers.

You must create a static method(s) to do these functions (highest, lowest, sum, average), that will accept the array as an input parameter, and then the method can print the answer. Print out all of the answers, with a description before them.    Call the method(s) from main.

Make sure to document the functionality the program.

14
87
65
24
3
19
39
54
12
16
2
6
4
5
23
56
34
77
88
65
34
15
18
21
34
56
78
90
87
01

  

Explanation / Answer

/**
* The java program that reads integer values from a text file called
* "integers.txt" and then calls the static methods
* to find maximum, minimum , total sum and average sum value
* and print the results
*
* */
//Driver.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Driver
{
   public static void main(String[] args)
   {
       //Declare variables of type File and Scanner class
       File fileName=null;
       Scanner fileReader=null;

       //array of integer of size 30
       int[] values=new int[30];
       int index=0;

       try {

           //create instances of File and Scanner class with file name
           fileName=new File("integers.txt");
           fileReader=new Scanner(fileName);

           //read integers from the file scanner object
           while(fileReader.hasNextInt())
           {
               //read next integer value
               values[index]=fileReader.nextInt();
               //increment index by one
               index++;
           }

          
           //calling static methods to print maximum, minimum, sum and average
           //values
           findMax(values);
           findMin(values);
           findSum(values);
           findAverage(values);


           //close the input file stream reader
           fileReader.close();

       }
       catch (FileNotFoundException e)
       {
           System.out.println("File not found");
       }
   }


   //The method findMin that accepts the integer array and prints
   //the minimum value from the integer values
   private static void findMin(int[] values)
   {
       //assume first value is minimum
       int min=values[0];

       //start for loop from index =1
       for (int index = 1; index < values.length; index++)
       {
           if(values[index]<min)
               min=values[index];
       }

       System.out.println("Minimum integer value :"+min);

   }

   //The method findMax that accepts the integer array and prints
   //the maximum value from the integer values
   private static void findMax(int[] values)
   {
       //assume first value is maximum
       int max=values[0];

       //start for loop from index =1
       for (int index = 1; index < values.length; index++)
       {
           if(values[index]>max)
               max=values[index];
       }

       System.out.println("Maximum integer value :"+max);
   }


   //The method findSum that accepts the integer array and prints
   //the sum of the values from the integer values
   private static void findSum(int[] values)
   {
       //set totalSum to zero
       int totalSum=0;

       //start for loop from index =0
       for (int index = 0; index < values.length; index++)
       {
           totalSum+=values[index];
       }

       System.out.println("Total sum of integers :"+totalSum);

   }
   //The method findAverage that accepts the integer array and prints
   //the average value of the integer sum of the values from the integer values
   private static void findAverage(int[] values)
   {
       //set totalSum to zero
       double totalSum=0;

       //start for loop from index =0
       for (int index = 0; index < values.length; index++)
       {
           totalSum+=values[index];
       }

       System.out.printf("Average : %.2f",totalSum/values.length);
   }

}//end of the class Driver


Sample output:
Sample input file name : integers.txt
14
87
65
24
3
19
39
54
12
16
2
6
4
5
23
56
34
77
88
65
34
15
18
21
34
56
78
90
87
01

sample output :
Maximum integer value :90
Minimum integer value :1
Total sum of integers :1127
Average : 37.57

Note: program is assumed to be java.

Input file integers.txt must be in the current working directory.
Hope this helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote