Write a Java program that asks the user to enter an array of integers. The program should prompt the user for the number of elements in the array and then the elements of the array. Theprogram should then call a method named minGap that accepts the array entered by the user as a parameter and returns the minimum 'gap' between adjacent values in the array. The main method should then print the value returned by the method. The gap between two adjacent values in an array is defined as the difference in value between the first element and the secondelement. For example, suppose the user entered the following array of integers {1, 3, 6, 5, 12}The first gap is 2 (3 - 1), the second gap is 3 (6 - 3), the third gap is 1 (6 - 5) and the fourth gap is 5 (12 - 6). Thus, the call of minGap(array) should return 1 because that is the smallest gap in the array. If you are passed an array with fewer than 2 elements, you should return 0.

Respuesta :

Answer:

Follows are the code to this question:

import java.util.*;//import package for user input

public class Main//defining a class Main

{

public static int minGap(int[] arr)//defining a static method minGap that holds an array in its parameters

{

int m,i,gap;//defining integer variables

if(arr.length <2)//use if block to check array element value

return (0); // return value 0

else//defining else block

{

m= 100;//use m to assign value    

for (i=0;i<arr.length-1;i++)//use for loop to calculate difference

{

gap= arr[i]-arr[i+1];//holding difference values

if(gap<0)//use if to check gap less then 0

gap = gap * -1; //  convert value into positive .

if( gap < m) // Finding the minimum difference

m = gap;//holding gap value

}

}

return (m);//return gaps

}    

public static void main(String[] as)//main method

{

int n,i,diff;//defining integer variable

Scanner ox=new Scanner (System.in);//creating Scanner class object  

System.out.println(" Enter the total value you want to insert in the array: ");//print message

n=ox.nextInt();//input value

int arr[]=new int[n];//defining an integer array  

System.out.println("Enter array elements: ");//print message

for (i=0;i<arr.length;i++)//use for loop for input value

{

arr[i]=ox.nextInt();//input value in array

}

diff= minGap(arr);//calling a method minGap

System.out.println("Minimum Gap: "+diff);

}

}

Output:

Enter the total value you want to insert in the array:  

5

Enter array elements:  

1

3

6

5

12

Minimum Gap: 1

Explanation:

In this code, a static method "minGap" is declared, that accepts an array in its parameter and uses a conditional statement to check its minimum difference that can be defined as follows:  

In the first if it checks the length of the array, that is less than 2 so, it will return the value 0, otherwise, it will go in else block, in this, it uses the loop to count the difference of array and use if block to check and return its minimum difference value.

In the main method, an integer variable and an array are declared, which is used for input value from the user end, and pass the value into the method and call and print its return value.