Docsity

Java OOP Lab: Adding Matrices with Multi-dimensional Arrays, Lab Reports of Java Programming

Partial preview of the text

Download Java OOP Lab: Adding Matrices with Multi-dimensional Arrays and more Lab Reports Java Programming in PDF only on Docsity!

Green University of Bangladesh

Department of Computer Science and Engineering

Course Code: CSE 202

Course Name: Object Oriented Programming Lab

Class Test: 01

Date of Performance: 31.10. Date of Submission: 31.10. Submitted By Name : Anjuman Ara Ananna ID : 193002008 Section : DC Batch : 193 Date : 31.10. Submitted To Feroza Naznin Senior Lecturer Dept. of CSE Green University of Bangladesh Marks Obtained Remarks

Class Test: 01 Problem Name: Write a java program to Add Two Matrix Using Multi- dimensional Array of objects. Objectives:

  1. To observe how to declare an array of objects.
  2. To study what methods are in java and how to access them.
  3. To gain knowledge how to declare and initialize a 2D array.
  4. To gain knowledge how to print a 2D array.
  5. To gain knowledge how to add two matrixes using Multi-dimensional array of objects and print the addition. Procedure: Procedure for MatrxAdd Class :
  6. Initialize A[][] = { { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3 }, { 4, 4, 4, 4 } };
  7. Initialize B[][] = { { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3 }, { 4, 4, 4, 4 } };
  8. Initialize C[][] = new int[4][4]; //Here int means int detatype
  9. Initialize rowSize=4;
  10. Initialize colSize=4;
  11. Execute a method which name is printMatrix which will print the matrix.
  12. Execute a static method which name add which will return the value of C.
  13. Execute a method which name is add which will add the two matrix and print them. Procedure for MatrxAddTest(Main Class): 1.We will declare three objects m1,m2,m3.

int B[][] = { { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3 }, { 4, 4, 4, 4 } }; int C[][] = new int[4][4]; int rowSize=4; int colSize=4; void printMatrix(int M[][], int rowSize, int colSize) { for (int i = 0; i < rowSize; i++) { for (int j = 0; j < colSize; j++) System.out.print(M[i][j] + " "); System.out.println(); } } static int[][] add(int A[][], int B[][],

int size) { int i, j; int C[][] = new int[size][size]; for (i = 0; i < size; i++) for (j = 0; j < size; j++) C[i][j] = A[i][j] + B[i][j]; return C; } int[][] add(int M[][]){ int C[][] = add(A, B, 4); System.out.println("\nResultant Matrix:"); printMatrix(C, 4, 4); return null; } }

Output: Discussion: We have done the problem in object oriented programming. We have calculated the sum of two matrixes using Multi-dimensional array of objects and print the addition very carefully. We learnt from our lab report that, how to add two matrixes using array of objects in oriented programming.