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

This assignment is based on your knowledge of 2D matrices. Create a program TwoD

ID: 3820490 • Letter: T

Question

This assignment is based on your knowledge of 2D matrices. Create a program TwoDProgram.java. Ask the user to enter number of rows and columns of a double Matrix 1. Ask the user to enter the dimensions of a double Matrix 2 as well. Check if dimensions of Matrix 1 match with dimensions of Matrix 2. If not, show an error message and end the program. Otherwise, Instantiate the two arrays. Ask the user to enter numbers to populate Matrix 1 and Matrix 2. Use Scanner and nextDouble. Repeatedly ask the user to enter a choice (1) Addition (2) Subtraction, (3) Multiplication, and (4) Quit. If the user selects option 1, perform element-wise addition and show the output. For instance: 3 4 1 4 4 8 4 1 + 0 4 = 4 5 3 1 1 1 4 2

Explanation / Answer

TwoDProgram.java

import java.util.Scanner;

public class TwoDProgram {

   public static void main(String[] args) {
       //Derclaring variables
int choice;
char ch;
int mat1rows,mat1cols,mat2rows,mat2cols;
  
       //Scanner object is used to get the inputs entered by the user
               Scanner sc=new Scanner(System.in);
      
               //getting matrix dimensions
               System.out.println("Enter Dimensions of Matrix #1");
               System.out.print("Enter no of Rows :");
               mat1rows=sc.nextInt();
              
               System.out.print("Enter no of Columns :");
               mat1cols=sc.nextInt();
               System.out.println("Enter Dimensions of Matrix #2");
               System.out.print("Enter no of Rows :");
               mat2rows=sc.nextInt();
              
               System.out.print("Enter no of Columns :");
               mat2cols=sc.nextInt();
              
               if((mat1rows==mat2rows) && (mat1cols==mat2cols))
               {
                   double matrix1[][]=new double[mat1rows][mat1cols];
                   double matrix2[][]=new double[mat2rows][mat2cols];
                   double matrix3[][]=null;
                  
                  
                   //Getting matrix 1 values
                   System.out.println(":: Matrix#1 values ::" );
                   for(int i=0;i<mat1rows;i++)
                   {
                       for(int j=0;j<mat1cols;j++)
                       {
                           System.out.print("Enter element ["+i+","+j+"] :");
                           matrix1[i][j]=sc.nextDouble();
                       }
                   }
                   //Getting matrix 2 values                  
                   System.out.println(":: Matrix#2 values ::" );
                   for(int i=0;i<mat2rows;i++)
                   {
                       for(int j=0;j<mat2cols;j++)
                       {
                           System.out.print("Enter element ["+i+","+j+"] :");
                           matrix2[i][j]=sc.nextDouble();
                       }
                   }
                  
                   //This loop continues to execute until the user enters 4
                   while(true)
                   {  
                       //Displaying the Menu
                   System.out.println(":: Menu ::");
                   System.out.println("1.Addition");
                   System.out.println("2.Subtraction");
                   System.out.println("3.Multiplication");
                   System.out.println("4.Quit");
                  
                   //getting the choice entered by the user
                   System.out.print("Enter choice:");
       choice=sc.nextInt();
       switch(choice)
       {
       case 1:{
           //calling the method for addition
           matrix3=matrixAddition(matrix1,matrix2);
           ch='+';
           System.out.println(" :: Matrix Addition ::");
          
           //Displaying the output
           display(matrix1,matrix2,matrix3,ch);
           continue;
       }
       case 2:{
           //calling the method for substraction
           matrix3=matrixSubtraction(matrix1,matrix2);
           ch='-';
           System.out.println(" :: Matrix subtraction ::");
          
           //Displaying the output
           display(matrix1,matrix2,matrix3,ch);
           continue;
       }
      
       case 3:{
           //calling the method for multiplication
           matrix3=matrixMultiplication(matrix1,matrix2);
           ch='*';
           System.out.println(" :: Matrix Multiplication ::");
          
           //Displaying the output
           display(matrix1,matrix2,matrix3,ch);
           continue;
       }
      
       case 4:{
           System.out.println(":: Program Exit ::");
           break;
       }
       default:{
           System.out.println("** Invalid Input ** ");
           continue;
       }
       }
                   break;
                   }
               }
               else
               {
                   System.out.println("** Invalid.No of Rows and no of Columns of two matrices must be equal **");
               }
              
              

   }

   //This method will display the output
   private static void display(double[][] matrix1, double[][] matrix2,
           double[][] matrix3, char ch) {
for(int i=0;i<matrix1.length;i++)
{
for(int j=0;j<matrix1[0].length;j++)
{
System.out.print(matrix1[i][j]+" ");
  
}
if(i==1)
System.out.print(" "+ch+" ");
else
System.out.print(" ");
for(int j=0;j<matrix1[0].length;j++)
{
System.out.print(matrix2[i][j]+" ");
  
}
if(i==1)
System.out.print(" = ");
else
System.out.print(" ");
for(int j=0;j<matrix1[0].length;j++)
{
System.out.print(matrix3[i][j]+" ");
}
System.out.println(" ");
}

      
   }

   //This method will perform matrix multiplication
   private static double[][] matrixMultiplication(double[][] matrix1,
           double[][] matrix2) {
      
       double matrix3[][]=new double[matrix1.length][matrix1[0].length];
       for(int i=0;i<matrix1.length;i++)
       {
           for(int j=0;j<matrix1[0].length;j++)
           {
               matrix3[i][j]=matrix1[i][j]*matrix2[i][j];
           }
       }
      
       return matrix3;
      
      
   }
   //This method will perform matrix subtraction
   private static double[][] matrixSubtraction(double[][] matrix1,
           double[][] matrix2) {
       double matrix3[][]=new double[matrix1.length][matrix1[0].length];
       for(int i=0;i<matrix1.length;i++)
       {
           for(int j=0;j<matrix1[0].length;j++)
           {
               matrix3[i][j]=matrix1[i][j]-matrix2[i][j];
           }
       }
      
       return matrix3;
   }

   //This method will perform matrix addition
   private static double[][] matrixAddition(double[][] matrix1,
           double[][] matrix2) {
       double matrix3[][]=new double[matrix1.length][matrix1[0].length];
       for(int i=0;i<matrix1.length;i++)
       {
           for(int j=0;j<matrix1[0].length;j++)
           {
               matrix3[i][j]=matrix1[i][j]+matrix2[i][j];
           }
       }
      
       return matrix3;
   }

   }

__________________

Output:

Enter Dimensions of Matrix #1
Enter no of Rows :3
Enter no of Columns :2
Enter Dimensions of Matrix #2
Enter no of Rows :3
Enter no of Columns :2
:: Matrix#1 values ::
Enter element [0,0] :3
Enter element [0,1] :4
Enter element [1,0] :4
Enter element [1,1] :1
Enter element [2,0] :3
Enter element [2,1] :1
:: Matrix#2 values ::
Enter element [0,0] :1
Enter element [0,1] :4
Enter element [1,0] :0
Enter element [1,1] :4
Enter element [2,0] :1
Enter element [2,1] :1
:: Menu ::
1.Addition
2.Subtraction
3.Multiplication
4.Quit
Enter choice:1

:: Matrix Addition ::
3.0 4.0 1.0 4.0 4.0 8.0
4.0 1.0 + 0.0 4.0 = 4.0 5.0
3.0 1.0 1.0 1.0 4.0 2.0
:: Menu ::
1.Addition
2.Subtraction
3.Multiplication
4.Quit
Enter choice:2

:: Matrix subtraction ::
3.0 4.0 1.0 4.0 2.0 0.0
4.0 1.0 - 0.0 4.0 = 4.0 -3.0
3.0 1.0 1.0 1.0 2.0 0.0
:: Menu ::
1.Addition
2.Subtraction
3.Multiplication
4.Quit
Enter choice:3

:: Matrix Multiplication ::
3.0 4.0 1.0 4.0 3.0 16.0
4.0 1.0 * 0.0 4.0 = 0.0 4.0
3.0 1.0 1.0 1.0 3.0 1.0
:: Menu ::
1.Addition
2.Subtraction
3.Multiplication
4.Quit
Enter choice:4

_____________

Output#2:

Enter Dimensions of Matrix #1
Enter no of Rows :3
Enter no of Columns :3
Enter Dimensions of Matrix #2
Enter no of Rows :3
Enter no of Columns :2
** Invalid.No of Rows and no of Columns of two matrices must be equal **

______________Thank You