ممكن حل لهذا الاب

Write a Java program that takes ten integers from the user and

construct the following methods:

1. Write a method that displays the ten integers.

2. Write a method that calculates the average of the ten numbers and

displays the result.

3. Write a method that returns the max and min numbers.

4. Write a method that finds the maximum value in a range between

the 3

rd number and the 8th number and print it. Note, the 3

rd

number and the 8th number are included in the range.

5. Write a method that finds the duplicate values within the ten

integers. If any duplicate value is found, display it. If there are no

duplicate elements, write the statement “Your numbers are

unique”.

6. Write a method that sorts the ten numbers and displays the result.

7. Implement the insertion sort method (mentioned in the lecture).

Use the method to insert a new number in the middle of the sorted

ten numbers (you already sorted them in step 6).


حاولت شرح كل سطر بقدر المستطاع من البرنامج، لكن حاولي في المرات القادمة أن تجربي حل الكود بيدك ثمّ أسألي عمّا لم تعرفيه لكي تحصّلي الإفادة بقدر المستطاع

import java.util.Arrays;
import java.util.Scanner;


public class Question {
      static double getAverage(int...arr) {
            double sum=0;
            for (int n: arr) sum+=n; // summition all integers
            return sum/arr.length; // ave = summition/number of numbers
      }
      static int[] getMaxMin(int...arr){
            int[] minmax = {Integer.MAX_VALUE, Integer.MIN_VALUE}; // set min max integers to send it
            for (int n: arr){
                  if(n < minmax[0]) minmax[0]=n; // if number less than min set number as min
                  if(n > minmax[1]) minmax[1]=n; // if number greate than max set number as max
            }
            return minmax; // return array of two integer [min, max]
      }
      static int[] getMaxMinInRange3rdAnd8th(int... arr){
            int[] subArr = Arrays.copyOfRange(arr, 2, 8); // get sub array with range from 3rd element to 8th
            return getMaxMin(subArr); // use last function -getMaxMin on new sub array to return min max values
      }
      static void uniquenessTest(int...arr){
            boolean uniqueness = true; // default uniqueness is true before any check
            first: for (int i=0; i<arr.length-1; i++)
            for(int j=i+1; j<arr.length; j++)
                  if(arr[i]==arr[j]) { // check all element with all other elements
                        // if any number is repeated print number
                        System.out.println(arr[i] + " is duplicated");
                        // set uniqueness = false
                        uniqueness = false;
                        // stop searching
                        break first;
                  }
            // if array numbers are unique print "Your numbers are unique"
            if(uniqueness) System.out.println("Your numbers are unique");
      }
      static int[] sortArray(int...arr){
            // sort array with default method in Arrays
            Arrays.sort(arr);
            // return sorted array
            return arr;
      }
      static int[] insertNewNumberWithInsertionSort(int[] array, int num){
            int[] arr = Arrays.copyOf(array, array.length+1); // expend array with one
            arr[arr.length-1] = num; // set new number in last place
            // --insertion soirt--
            int n = arr.length;
            for (int j = 1; j < n; j++) {  
                  int key = arr[j];
                  int i = j-1;
                  while (i>-1 && arr[i]>key){  
                        arr[i+1]=arr[i];  
                        i--;
                  }  
                arr[i+1] = key;  
            }
            return arr;
      }
      public static void main(String[] args) {
             Scanner sc = new Scanner(System.in); // scanner to enter numbers
             int[] inputs = new int[10]; // array to store them
             for (int i=0; i<inputs.length; i++){
                  inputs[i] = sc.nextInt(); // enter process
             }
             sc.close(); // close scanner --you can don't write it--
            double average = getAverage(inputs); // get Average
            System.out.println(average); // print it
            int[] minmax = getMaxMin(inputs); // get min max values in array [min, max]
            System.out.println(Arrays.toString(minmax));
            int[] minmaxInRange = getMaxMinInRange3rdAnd8th(inputs); // get min max between 3rd and 8th number. 
            System.out.println(Arrays.toString(minmaxInRange));
            uniquenessTest(inputs); // test array if its unique
            sortArray(inputs); // sort array
            System.out.println(Arrays.toString(inputs));
            int[] arr1 = insertNewNumberWithInsertionSort(inputs,1); // after sort array set new value [1 here] using insertion sort
            System.out.println(Arrays.toString(arr1));


      }
}