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

Problem 3 (Arrays & Methods) Create a method that accepts an integer array and a

ID: 3742700 • Letter: P

Question

Problem 3 (Arrays & Methods)
Create a method that accepts an integer array and another integer value. The method will replace
all instances of the integer number in the array with zeros and returns the new array. Incorporate
the method into a working program that tests the validity of your method. Your program will
display the elements of a given array before the method is called and it will display the elements
in the array again right after the method is invoked.

The method’s header is specified as follows:
public static int[] magicReplace(int[] theArray, int magicNumber)

Name your class as Q3ArrayReplace.java

Explanation / Answer

import java.util.Arrays;

class Main {

public static int[] magicReplace(int[] theArray, int magicNumber)

{

for(int i=0; i<theArray.length; i++)

{

if(theArray[i] == magicNumber)

theArray[i] = 0;

}

return theArray;

}

public static void main(String[] args) {

int[] a = new int[]{3, 6, 5, 47, 5, 3, 8, 9, 3};

System.out.println(Arrays.toString(a));

a = (magicReplace(a, 3));

System.out.println(Arrays.toString(a));

}

}

/*SAMPLE OUTPUT

[3, 6, 5, 47, 5, 3, 8, 9, 3]

[0, 6, 5, 47, 5, 0, 8, 9, 0]

*/

// Hit the thumbs up if you are fine with the answer. Happy Learning!

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