Question 10) Write a function that copies the contents of one array into another
ID: 3629368 • Letter: Q
Question
Question 10) Write a function that copies the contents of one array into another array. If the two arrays havedifferent length, copy as much of the first array as you can. Use your function in question 8 to
generate random arrays and use these to test your function. Print both arrays before copying,
and the second array after copying.
// example output
Arrays before copying:
{3, 4, 1, 4, 3}
{1, 3, 1}
Second array after copying:
{3, 4, 1}
Arrays before copying:
{5, 5, 3, 1, 2}
{6, 5, 6, 3, 1, 2, 4}
Second array after copying:
{5, 5, 3, 1, 2, 2, 4}
The coding for question 8 is as follow:
public static void question8()
{
int[] num4 = new int[5];
printArray(num4);
}
public static int[] printArray(int[] num4)
{
Random generator = new Random();
System.out.print("{");
for (int i=0; i<num4.length; i++)
{
num4[i] = generator.nextInt(num4.length) + 1;
System.out.print(num4[i] + " ");
}
System.out.print("" + "}");
System.out.println(" ");
return num4;
}
//Output for question 8:
// {2 2 5 3 2}
// {2 1 4 3 5}
Please make sure the format for question should be like:
public class Exercises1
{
public static void question1()
{
System.out.println("**************************");
System.out.println("* QUESTION 1 *");
System.out.println("**************************");
int x = f(5);
...
}
public static void question2()
{
System.out.println("**************************");
System.out.println("* QUESTION 2 *");
System.out.println("**************************");
...
}
...
public static void main(String[] args)
{
question1();
question2();
...
}
}
where all coding should be done in question function only and NOT in main function.
Explanation / Answer
import java.util.Random; public class Exercise1 { public static void question8() { int[] num4 = new int[5]; printArray(num4); } public static void question10(int arraysize1, int arraysize2) { int[] num1 = new int[arraysize1]; int[] num2 = new int[arraysize2]; System.out.print("Arrays before copying: "); num1 = printArray(num1); num2 = printArray(num2); int[] result= copyArray(num1, num2); System.out.print("Second Array after copying: "); System.out.print("{"); for(int i=0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.