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

Design and implement a Java program for programming exercise 8.5, page 306 (name

ID: 3759538 • Letter: D

Question

Design and implement a Java program for programming exercise 8.5, page 306 (name it AddMatricies) as described in the problem statement. Write method addMatrix() as specified. To test this method, the main method of your program prompts the user to enter two matrices that have the same dimensions, and then display their sum as shown in the sample run. Design the main method of your program such that it allows the user to re-run the program with different inputs (i.e., use a loop). Document your code, and organize and space the outputs properly. Use escape characters and formatting objects when applicable.

Explanation / Answer

import java.util.Scanner;

public class Addition_Matrix {
  
   Scanner scan;
   int a[][], b[][], sum[][];
   int rows, columns;

   void create() {
      
       scan = new Scanner(System.in);
      
       System.out.println(" Addition of Matrix");
      
     
       System.out.println(" Enter the rquired number of rows & columns");
       row = Integer.parseInt(scan.nextLine());
       column = Integer.parseInt(scan.nextLine());
      
       a = new int[rows][columns];
       b = new int[rows][columns];
       sum = new int[rows][columns];

       System.out.println("Enter the data for matrix a :");

       for(int i=0; i<rows; i++) {
          
           for(int j=0; j<columns; j++) {
              
               a[i][j] = scan.nextInt();
           }
       }
         
       System.out.println("Enter the data for matrix b :");

       for(int i=0; i<rows; i++) {
          
           for(int j=0; j<columns; j++) {
              
               b[i][j] = scan.nextInt();
           }
       }
   }
  
   void display() {
      
       System.out.println(" The Matrix a is :");
      
       for(int i=0; i<rows; i++) {
          
           for(int j=0; j<columns; j++) {
              
               System.out.print(" " + a[i][j]);
           }
           System.out.println();
       }
      
       System.out.println(" The Matrix b is :");
      
       for(int i=0; i<rows; i++) {
          
           for(int j=0; j<columns; j++) {
              
               System.out.print(" " + b[i][j]);
           }
           System.out.println();
       }
   }
  
   void add() {
      
       for(int i=0; i<rows; i++) {
          
           for(int j=0; j<columns; j++) {
              
               sum[i][j] = a[i][j] + b[i][j];
           }
       }
      
       System.out.println(" The Sum of a abd b is :");
      
       for(int i=0; i<rows; i++) {
          
           for(int j=0; j<columns; j++) {
              
               System.out.print(" " + sum[i][j]);
           }
           System.out.println();
       }
   }
}

class MainClass {
  
   public static void main(String args[]) {
      
       Addition_Matrix obj = new Addition_Matrix();
      
       obj.create();
       obj.display();
       obj.add();
   }
}

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