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

Using java write a method called logic and that accepts as parameters two arrays

ID: 3781750 • Letter: U

Question

Using java write a method called logic and that accepts as parameters two arrays of integer (A and B) and returns another array of integer (C). C[i] = 1 if B[i] divides A[i], otherwise C[i]=0. Write a test program (main) to declare and initialize two integer arrays of size entered by the user, invoke the method logicAnd, then display the array result. The program put the three arrays (A, B and C) in a 2-D array M as follow. Sample Run: Enter the size of the array: 5 Enter 5 numbers for A: 12 20 4 30 17 Enter 5 numbers for B: 3 6 24 15 9 The results: A 12 20 4 30 17

B 3 6 24 15 9

C 1 0 0 1 0

M 12 20 4 30 17

3 6 24 15 9

1 0 0 1 0

Explanation / Answer

Matrix01.java

import java.util.Scanner;

public class Matrix01 {

       public static void main(String[] args) {
           //Declaring variable
           int size;
          
           //Scanner class object is used to read the inouts entered by the user
       Scanner sc=new Scanner(System.in);
      
       //Getting the size of the array entered by the user
       System.out.print("Enter the size of the array: ");
       size=sc.nextInt();
      
       //Creating an Integer type array Whose name is A
       int A[]=new int[5];
      
       //Creating an Integer type array Whose name is B
       int B[]=new int[5];
      
       //Getting the elements and populating elements into Array A
       System.out.print("Enter 5 numbers for A: ");
       for(int i=0;i<5;i++)
       {
           A[i]=sc.nextInt();
       }
      
       //Getting the elements and populating elements into Array B
       System.out.print("Enter 5 numbers for B: ");
       for(int i=0;i<5;i++)
       {
           B[i]=sc.nextInt();
       }
       //Calling the method by passing the Matrix A and Matrix B as arguments
       logic(A,B);
      
      
   }

       //This method will find the matrix C An display the matrix M
       private static void logic(int[] A, int[] B) {
           int C[]=new int[5];
           for(int i=0;i<5;i++)
           {
               if(A[i]%B[i]==0)
               C[i]=1;
               else
               C[i]=0;
           }
          
           int M[][]=new int[3][5];
          
           int i=0;
               for(int j=0;j<5;j++)
               {
                   M[i][j]=A[j];
               }
               i++;
               for(int j=0;j<5;j++)
               {
                   M[i][j]=B[j];
               }
               i++;
               for(int j=0;j<5;j++)
               {
                   M[i][j]=C[j];
               }
              
               //Displaying the matrix M
               System.out.println("Displaying the Matrix M :");
               for(int l=0;l<3;l++)
               {
                   for(int m=0;m<5;m++)
                   {
                       System.out.print(M[l][m]+" ");
                   }
                   System.out.println();
               }
           }
          
       }

___________________________

Output:

Enter the size of the array: 5
Enter 5 numbers for A: 12 20 4 30 17
Enter 5 numbers for B: 3 6 24 15 9
Displaying the Matrix M :
12 20 4 30 17
3 6 24 15 9
1 0 0 1 0

________Thank You

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