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

This lab is to understand the concept of using array with methods in JAVA 1) Cre

ID: 3715875 • Letter: T

Question

This lab is to understand the concept of using array with methods in JAVA 1) Create a text file Lab91.txt with following data: 10 12 15 32 86 35 72 91 -9999 Here -9999 is the sentinel value 2) Create a class named Lab8_1 that has following methods: a. readArray method that takes three parameters: an array, scanner object and sentinel value. This method reads value from file until the sentinel value is reached This method returns an int value, which is the total number of array elements you have read. b. printData method that takes two parameters: an array and size of the array. This method prints all the elements of the array as shown in sample output. This method does not return any value. c. findMax method that takes two parameters: an array and size of the array This method finds the maximum value among the array elements This method returns an int value, which is the maximum number among array elements. d. findMin method that takes two parameters: an array and size of the array This method finds the minimum value among the array elements. This method returns an int value, which is the minimum number among array elements. e. findSum method that takes two parameters: an array and size of the array This method finds the sum of all the array elements. This method returns an int value, which is the sum of all array elements.

Explanation / Answer

ArrayProcessing .java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ArrayProcessing {
   public static void main(String[] args) {
       Scanner input = new java.util.Scanner(System.in);
           final int SENTINAL_VALUE=-9999;
           int SIZE=30;
           //Create Array
           int [] iArr=new int[SIZE];

           //Read Array Data
       int numberOfValues= readArray(iArr,input,SENTINAL_VALUE);

       // Print Array
       printData(iArr, numberOfValues);
  
       int max = findMax(iArr, numberOfValues);
       System.out.println("The Maximum Value in the Array Is = " + max);

       int min = findMin(iArr, numberOfValues);
       System.out.println("The Minimum Value in the Array Is = " + min);
      

       int sum = findSum(iArr, numberOfValues);

       System.out.println("The Sum of Values in the Array Is = " + sum);
      
       System.out.println("Number of Values Read from the file is = "+numberOfValues);

   }
   /**
   * @param iArr
   * @param input
   */
   private static int readArray(int[] iArr, Scanner input, int sentinal) {
       // Create a File Object
       File file = new File("./Lab9_1.txt");
       int i = 0;
       try {
           input = new Scanner(file);

           while (input.hasNext()) {

               int value = input.nextInt();
               if (value == sentinal) {
                   // break the loop
                   break;
               } else {
                   // add to array
                   iArr[i] = value;
                   i++;
               }
           }
       } catch (FileNotFoundException e) {
           System.out.println("File is not found ");
       }
       return i;

   }
   // Find minimum
   private static int findMin(int[] array, int size) {
       int min = array[0];
       for (int i = 1; i < size; i++) {
           if (array[i] < min)
               min = array[i];
       }
       return min;
   }

   // Method to calculate Max
   private static int findMax(int[] array, int size) {
       int max = array[0];
       for (int i = 1; i < size; i++) {
           if (array[i] > max)
               max = array[i];
       }
       return max;
   }

   // Calculate Sum
   private static int findSum(int[] array, int size) {
       // loop to calculate the sum of all array elements.
       // Then print the sum after the loop
       int total = 0;
       for (int i = 0; i < array.length; i++) {
           total += array[i];
       }
       return total;
   }

   // This method will be used to print the array elements
   private static void printData(int[] array, int size) {
       for (int i = 0; i < size; i++)
           System.out.println("iAarr["+i + "] = "+array[i]);

   }

}// end of class ArrayProcessing

Output:

iAarr[0] = 10
iAarr[1] = 12
iAarr[2] = 15
iAarr[3] = -95
iAarr[4] = 32
iAarr[5] = 86
iAarr[6] = 35
iAarr[7] = 72
iAarr[8] = 91
The Maximum Value in the Array Is = 91
The Minimum Value in the Array Is = -95
The Sum of Values in the Array Is = 258
Number of Values Read from the file is = 9

input File :

10
12
15
-95
32
86
35
72
91
-9999

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote