In a program, write a method that accepts two arguments: an array of integers an
ID: 3857809 • Letter: I
Question
In a program, write a method that accepts two arguments: an array of integers
and a number n. The method should print all of the numbers in the array
that are greater than the number n (in the order that they appear in the
array, each on their own line).
In the same file, create a main method and call your function using the following
data sets:
The array {1, 5, 10, 2, 4, -3, 6} and the number 3.
The array {10, 12, 15, 24} and the number 12.
Explanation / Answer
Hi,
Please see beow the class. Please comment for any queries/feedbacks
Thanks!
PrintNumbers.java
public class PrintNumbers {
public static void main(String[] args) {
//main method to call the function printNumbers
//data set 1:
int [] array1= {1, 5, 10, 2, 4, -3, 6};
int n1 = 3;
printNumbers(array1,n1);
//data set 2:
int [] array2= {10, 12, 15, 24};
int n2 = 12;
printNumbers(array2,n2);
}
/**
* a method that accepts two arguments: an array of integers
* and a number n.
* @param numArray
* @param n
*/
public static void printNumbers(int[] numArray,int n){
System.out.println("Printing elements greater than "+n);
for(int i :numArray){
//Checking if the current number is greater than n
if(i>n){
// The method should print all of the numbers in the array
//that are greater than the number n (in the order that they appear in the array, each on their own line).
System.out.println(i);
}
}
}
}
Sample output:
Printing elements greater than 3
5
10
4
6
Printing elements greater than 12
15
24
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.