Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In a program, write a method that accepts two arguments: an array of integers an

ID: 3587522 • 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.

My code:

public class LargerThanN
{
  
public static void print(int[] array,int n)
   {
   for(int i=0; i<array.length; i++)
   {
       if (array[i]>n)
           System.out.println(array[i]);
   }
   System.out.println();
}

  
   public static void main(String[] args)
   {
       int[] array1 = {1,5,10,2,4,-3,6};
       int num1=3;
       print(array1,num1);
      
       int[] array2 = {10, 12, 15, 24};
       int num2 = 12;
       print(array2, num2);
   }
}

My programming lab does not want extra lines between the output. What am I doing wrong? How do I fix it?

Failed Test Run The contents of your standard output is incorrect. Attention: the difference between your stdout and the expected stdout is just a question of spacing. Standard Output Hide Invisibles Your Code's Actual Result: Expected Result: 54 104 10 6.4 15 24.4 15. 24

Explanation / Answer

// See the updated code below extra space is now removed in code. Please do thumbs up if you like the soultion

public class LargerThanN
{
  
   public static void print(int[] array,int n)
   {
        for(int i=0; i<array.length; i++)
        {
            if (array[i]>n)
                System.out.println(array[i]);
        }
   }

  
    public static void main(String[] args)
    {
        int[] array1 = {1,5,10,2,4,-3,6};
        int num1=3;
        print(array1,num1);
      
        int[] array2 = {10, 12, 15, 24};
        int num2 = 12;
        print(array2, num2);
    }
}

OUTPUT:

5
10
4
6
15
24

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote