ADD MATRICES JAVA

 Develop a JAVA program to add TWO matrices of suitable order N (The value of N should be read from command line arguments)

PROGRAM:

package jo;

 public class Matrix {

public static void main(String[] args) {

if (args.length != 1) {

System.out.println("Please provide the order N as a command line argument.");

return;

}

int N = Integer.parseInt(args[0]);

// Check if N is a positive integer

if (N <= 0) {

 System.out.println("N should be a positive integer.");

 return;

}

 int[][] matrixA = new int[N][N];

int[][] matrixB = new int[N][N];

int[][] resultMatrix = new int[N][N];

 // Fill matrixA and matrixB with random values for demonstration

for (int i = 0; i< N; i++) {

for (int j = 0; j < N; j++) {

matrixA[i][j] = (int) (Math.random() * 10);

matrixB[i][j] = (int) (Math.random() * 10);

 }

}

// Perform matrix addition

for (int i = 0; i< N; i++) {

for (int j = 0; j < N; j++) {

 resultMatrix[i][j] = matrixA[i][j] + matrixB[i][j];

 }

}

// Display the result matrix

System.out.println("Matrix A:");

printMatrix(matrixA);

System.out.println("Matrix B:");

printMatrix(matrixB);

System.out.println("Resultant Matrix (Sum of the two matrices):");

printMatrix(resultMatrix);

}

public static void printMatrix(int[][] matrix) {

for (int i = 0; i<matrix.length; i++) {

for (int j = 0; j < matrix[i].length; j++) {

 System.out.print(matrix[i][j] + " ");

 }

 System.out.println();

 }

}

}

 

FIX COMMAND LINE ARGUMENT:

STEP1:




 

STEP 2:



(note:

Make sure “Main” Tag => it has your Project name or not?

Then Choose Argument Tag and enter (Program arguments 2- which mean matrix size)

OUTPUT:




Viva questions:

1) What is Integer.parseInt()?
Integer.parseInt() is a method in Java that converts a String into an int (primitive integer).

Example:

String number = "123";

int result = Integer.parseInt(number);  // result = 123 (as an int)

Why it's useful:
When you read input (e.g., from the keyboard or command line), it usually comes as a String. You use
Integer.parseInt() to convert it to an integer.

2) What is an Array?

An array is a data structure in Java used to store multiple values of the same type in a single variable.

int[] numbers = {10, 20, 30, 40};

  • Arrays have a fixed size.
  • Elements are accessed using index, starting from 0.
  • You can store primitives (int, char) or objects (String, etc.).

3) Why do we use the Random function?
We use the
Random function to generate random values, such as random numbers. It's useful in games, simulations, testing, etc.

Example:

import java.util.Random;

 

Random rand = new Random();

int randomNumber = rand.nextInt(100); // Generates a number between 0 and 99

Why it's useful:
It helps in situations where you need unpredictable values, like rolling dice, shuffling cards, or picking random items.

4) What do you mean by command line arguments?
Command line arguments are the values passed to the
main() method of a Java program when it is run from the command line.

Example:

public class Test {

    public static void main(String[] args) {

        System.out.println("Hello, " + args[0]);

    }

}

If you run:

java Test Alice

Output:

Hello, Alice

 











No comments:

Post a Comment