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

Problem: Develop a program which allows the user to enter numbers into an array

ID: 3688055 • Letter: P

Question


Problem: Develop a program which allows the user to enter numbers into an array Input will be as follows The user will enter the total number of integers to be entered into the array The user will then enter that number of unique integers (negative or positive). entered to exceed the array sine. Do not allow the number of values Develop methods to: main' method Print the array Sont the array ( YOU MUST DEVELOP YOUR OWN SORT METHOD -don't use someone else's) Determine the highest value Determine the lowest value Calculate the average value (double)

Explanation / Answer

ArrayProgram.java

package com.chegg.test;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
* Class to check the array characteristics
*
* @author yourName
*
*/
public class ArrayProgram {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);

       System.out.println("Enter the size of the array:");
       int arraySize = sc.nextInt();
       boolean unique = false;
       boolean validArray = false;
       int[] array = new int[arraySize];
       Set<Integer> set = new HashSet<Integer>();
       for (int i = 0; i < arraySize; i++) {
           validArray = true;
           do {
               System.out.println("Enter the element into the array:");
               int value = Integer.parseInt(sc.next());
               // As per instruction we are reading only unique values
               if (set.add(value)) {
                   array[i] = value;
                   unique = true;
               } else {
                   System.out.println("Please enter Unique value only");
                   unique = false;
               }
           } while (!unique);
       }
       if (validArray) {
           // Printing the array
           System.out.println(" The Elements in the array are:");
           print(array);

           // Sorting the array
           sort(array);
           // Printing the array
           System.out.println(" The Elements after sorting the array are:");
           print(array);
           // Determine highest value
           int max = getMaxValue(array);
           System.out.println(" The heighest value from the array: " + max);

           // Determine Minimum value
           int min = getMinValue(array);
           System.out.println("The smallest value from the array: " + min);

           // Average
           double average = getAverage(array);
           System.out.println("The average of the elements of an array: "
                   + average);
       }
       sc.close();
   }

   /**
   * Method to find the average of an array
   *
   * @param array
   * @return average
   */
   private static double getAverage(int[] array) {
       double average = 0;
       int sum = 0;
       for (int i = 0; i < array.length; i++) {
           sum += array[i];
       }
       average = sum / array.length;
       return average;
   }

   /**
   * Method to sort given array
   *
   * @param array
   */
   private static void sort(int[] array) {
       int temp = 0;
       // Arrays.sort(array); //it is java API method for sorting no need to
       // write below code
       for (int i = 0; i < array.length; i++) {
           for (int j = i + 1; j < array.length; j++) {
               if (array[i] > array[j]) {
                   temp = array[i];
                   array[i] = array[j];
                   array[j] = temp;
               }
           }
       }

   }

   /**
   * Method to print the array
   *
   * @param arry
   */
   private static void print(int[] arry) {
       for (int i = 0; i < arry.length; i++) {
           System.out.print(arry[i] + " ");
       }
   }

   /**
   * Method to get Maximum value of an array
   *
   * @param array
   * @return maxValue
   */
   private static int getMaxValue(int[] array) {
       int maxValue = array[0];
       for (int i = 1; i < array.length; i++) {
           if (array[i] > maxValue) {
               maxValue = array[i];

           }
       }
       return maxValue;
   }

   /**
   * Method to get Minimum value of an array
   *
   * @param array
   * @return minValue
   */
   private static int getMinValue(int[] array) {
       int minValue = array[0];
       for (int i = 1; i < array.length; i++) {
           if (array[i] < minValue) {
               minValue = array[i];

           }
       }
       return minValue;
   }
}

Output:

Enter the size of the array:
4
Enter the element into the array:
-2
Enter the element into the array:
0
Enter the element into the array:
1
Enter the element into the array:
5

The Elements in the array are:
-2   0   1   5  
The Elements after sorting the array are:
-2   0   1   5  
The heighest value from the array: 5
The smallest value from the array: -2
The average of the elements of an array: 1.0

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