question 1) Write a function that joins two arrays together. Use your function i
ID: 3629367 • Letter: Q
Question
question 1) Write a function that joins two arrays together. Use your function in question 8 to generate random arrays and use these to test your function. Print the inputs to the function and the output.
// example output
{2, 4, 5, 1, 2} + {3, 1, 2}
= {2, 4, 5, 1, 2, 3, 1, 2}
{1, 2, 1} + {1}
= {1, 2, 1, 1}
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
I tried using your skeletal code as a reference, I would have made a different call just to print and one to generate numbers (see the bottom after ////////////////////////////////)
public static void question8()
{
System.out.println("**************************");
System.out.println("* QUESTION 8 *");
System.out.println("**************************");
int[] arr1=printArray(new int[5]);
System.out.print( " + ");
int[] arr2=printArray(new int[3]);
int[] combo=new int[arr1.length+arr2.length];
int position=0;
for(int x =0; x<arr1.length;x++){
combo[position++]=arr1[x];
}
for(int x =0; x<arr2.length;x++){
combo[position++]=arr2[x];
}
System.out.print(" = { ");
for (int i=0; i<combo.length; i++)
{
System.out.print(combo[i] + " ");
}
System.out.print("}");
}
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;
}
//////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//This is a little cleaner and you could even encapsilate the combine method.
public static void question8()
{
System.out.println("**************************");
System.out.println("* QUESTION 8 *");
System.out.println("**************************");
int[] arr1=makeRandArr(5);
printArr(arr1);
System.out.print( " + ");
int[] arr2=makeRandArr(3);
printArr(arr2);
int[] combo=new int[arr1.length+arr2.length];
int position=0;
for(int x =0; x<arr1.length;x++){
combo[position++]=arr1[x];
}
for(int x =0; x<arr2.length;x++){
combo[position++]=arr2[x];
}
System.out.print(" = ");
printArr(combo);
}
public static void printArr(int[] arr){
System.out.print("{ ");
for (int i=0; i<arr.length; i++)
{
System.out.print(arr[i] + " ");
}
System.out.print("}");
}
public static int[] makeRandArr(int count){
int tmp[]= new int[count];
Random rand = new Random();
for(int x= 0 ; x<count;x++){
tmp[x]=rand.nextInt(tmp.length) + 1;
}
return tmp;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.