Java In a program write a method that accepts two arguments
Java
In a program, write a method that accepts two arguments: an array and a number n. Assume that the array contains integers. The method should display all of the numbers in the array that are greater than the number n. Create a driver program that accepts 5 integers into an array and the n number from the user and then calls the method
Solution
LargeNumbers.java
import java.util.Scanner;
public class LargeNumbers {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num[] = new int[5];
for(int i=0; i<num.length; i++){
System.out.print(\"Enter integer \"+(i+1)+\": \");
num[i] = scan.nextInt();
}
System.out.println(\"Enter a number n: \");
int n= scan.nextInt();
displayLargeNumbers(num,n);
}
public static void displayLargeNumbers(int array[], int n){
for(int i=0; i<array.length; i++){
if(n < array[i]){
System.out.print(array[i]+\" \");
}
}
System.out.println();
}
}
Output:
Enter integer 1: 1
Enter integer 2: 2
Enter integer 3: 3
Enter integer 4: 4
Enter integer 5: 5
Enter a number n:
3
4 5
