Final Exam- version 1 5/5: 120 pts] Write a method called logicand that accepts
ID: 3907570 • Letter: F
Question
Final Exam- version 1 5/5: 120 pts] Write a method called logicand that accepts as parameters two arrays o another array of integer (C) as follows: c14) -1 if B[i) divides A(il, otherwise CtA) integer (A and B) and returns 0 Example: 20 6 24 ? 1010110 Use the following method header public static intlJ logicAnd (intl 1 A, intl 1 B) b- Write a void method called printArray that accepts as parameters an array of integers and print its values. Use the following method header: public static void printArray( intl 1) c- Use the above 2 methods in writing a test program (main) as follows: Declare and initialize 2 integer arrays (Arrayà and ArrayB) of size 10 (fill the 2 arrays with random integers between 1 and 100). Invoke the method printArray to display the values stored in the two Invoke the method logicAnd Invoke the method printArray to display the values of the new array (Arrayc) arrays. Sample Run: ArrayA : 12 20 430 17 ArrayB:3 6 24 15 9 ArrayC :1 0 0 1 0Explanation / Answer
Java code:
import java.util.Random;
public class arraysTest
{
//main method
public static void main(String[]arg)
{
//arrays declaration
int ArrayA[] = new int [10];
int ArrayB[] = new int [10];
//fill the array with random integers
Random r = new Random();
for(int i=0;i<10;i++)
{
ArrayA[i]=r.nextInt((99 -2) + 1) +2;
}
for(int i=0;i<10;i++)
{
ArrayB[i]=r.nextInt((99 -2) + 1) +2;
}
//calling/invoking printArray() method
System.out.print("ArrayA:");
printArray(ArrayA);
//and a new line
System.out.println();
System.out.print("ArrayB:");
//calling/invoking printArray() method
printArray(ArrayB);
System.out.print(" ArrayC:");
//invoking the method logicAnd
int C[]=logicAnd(ArrayA,ArrayB);
//loop to print content of the array C
for(int i=0;i<C.length;i++)
{
System.out.print(C[i]+" ");
}
}
//implementing the method
public static void printArray(int a[])
{
//loop to print content of the array
for(int i=0;i<10;i++)
{
System.out.print(a[i]+" ");
}
}
//implementing the method
public static int[] logicAnd(int[] A, int[] B)
{
//array declaration
int C[]= new int[10];
//loop to traverse array elements
for(int i=0;i<10;i++)
{
//if B[i] divides A[i], then C[i]=1
if(A[i] % B[i] == 0)
{
C[i]=1;
}else
{
//else
C[i]=0;
}
}
return C; //return C
}
}
Output:
ArrayA:23 65 4 77 62 15 26 33 93 97
ArrayB:79 83 91 83 55 5 37 29 88 39
ArrayC:0 0 0 0 0 1 0 0 0 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.