In a program write a method that accepts two arguments an ar

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. (Jcreator Java)

Solution

GreaterValues.java

import java.util.Scanner;


public class GreaterValues {

   public static void main(String[] args) {
       int a[] = new int[5];
       Scanner scan = new Scanner(System.in);
       for(int i=0; i<5; i++){
           System.out.print(\"Enter a value: \");
           a[i]= scan.nextInt();
       }
       System.out.print(\"Enter n value: \");
       int n = scan.nextInt();
       displayValuesGreaterThanN(a,n);
   }
   public static void displayValuesGreaterThanN(int array[], int n){
       System.out.println(\"Greater tah \"+n+\" values: \");
       for(int i=0; i<array.length; i++){
           if(array[i] > n){
               System.out.println(array[i]);
           }
       }
   }

}

Output:

Enter a value: 4
Enter a value: 5
Enter a value: 6
Enter a value: 7
Enter a value: 8
Enter n value: 6
Greater tah 6 values:
7
8

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site