Question

In: Computer Science

P-3.36 Write a Java program for a matrix class that can add and multiply arbitrary twodimensional...

P-3.36 Write a Java program for a matrix class that can add and multiply arbitrary twodimensional arrays of integers.

Solutions

Expert Solution

Write a Java program for a matrix class that can add and multiply arbitrary two dimensional arrays of integers.

Code :-

import java.util.Scanner;

public class Matrix {

        public static void main(String[] args) {
        
                while(true)
                {
                int choice;
                Scanner input = new Scanner(System.in);
                
                System.out.println("Press 1 to add two matrix:");
                System.out.println("Press 2 to multiply two matrix:");
                System.out.println("Press 3 to exit:");
                System.out.println("Enter your choice:");
                choice = input.nextInt();
                
                
                switch(choice)
                {
                case 1:
                        int m, n, c, d;
                        
                        Scanner sc = new Scanner(System.in);
                    System.out.println("Enter the number of rows and columns of matrix");
                    m = sc.nextInt();
                    n = sc.nextInt();

                    int first[][] = new int[m][n];
                    int second[][] = new int[m][n];
                    int sum[][] = new int[m][n];


                 
                    System.out.println("Enter the elements of first matrix");

                    for (c = 0; c < m; c++)
                      for (d = 0; d < n; d++)
                        first[c][d] = sc.nextInt();

                    System.out.println("Enter the elements of second matrix");

                    for (c = 0 ; c < m; c++)
                      for (d = 0 ; d < n; d++)
                        second[c][d] = sc.nextInt();

                    for (c = 0; c < m; c++)
                      for (d = 0; d < n; d++)
                        sum[c][d] = first[c][d] + second[c][d];  

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

                    for (c = 0; c < m; c++)
                    {
                      for (d = 0; d < n; d++)
                        System.out.print(sum[c][d] + "\t");

                      System.out.println();
                    }
                    break;
                case 2:
                         int m1, n1, p, q, sum1 = 0, c1, d1, k;
                         
                            Scanner in = new Scanner(System.in);
                            System.out.println("Enter the number of rows and columns of first matrix");
                            m1 = in.nextInt();
                            n1 = in.nextInt();
                         
                            int first1[][] = new int[m1][n1];
                         
                            System.out.println("Enter elements of first matrix");
                         
                            for (c1 = 0; c1 < m1; c1++)
                              for (d1 = 0; d1 < n1; d1++)
                                first1[c1][d1] = in.nextInt();
                         
                            System.out.println("Enter the number of rows and columns of second matrix");
                            p = in.nextInt();
                            q = in.nextInt();
                         
                            if (n1 != p)
                              System.out.println("The matrices can't be multiplied with each other.");
                            else
                            {
                              int second1[][] = new int[p][q];
                              int multiply[][] = new int[m1][q];
                         
                              System.out.println("Enter elements of second matrix");
                         
                              for (c1 = 0; c1 < p; c1++)
                                for (d1 = 0; d1 < q; d1++)
                                  second1[c1][d1] = in.nextInt();
                         
                              for (c1 = 0; c1 < m1; c1++) {
                                for (d1 = 0; d1 < q; d1++) {
                                  for (k = 0; k < p; k++)
                                    sum1 = sum1 + first1[c1][k]*second1[k][d1];
                         
                                  multiply[c1][d1] = sum1;
                                  sum1 = 0;
                                }
                              }
                         
                              System.out.println("Product of the matrices:");
                         
                              for (c1 = 0; c1 < m1; c1++) {
                                for (d1 = 0; d1 < q; d1++)
                                  System.out.print(multiply[c1][d1]+"\t");
                         
                                System.out.print("\n");
                              }
                            }
                        
                break;
                case 3:
                        System.out.println("you quit the program");
                         System.exit(0);
            }   

        }

}
}

Output :-


Related Solutions

Using Java Write a simple calculator which can add, subtract, multiply, and divide. Here are the...
Using Java Write a simple calculator which can add, subtract, multiply, and divide. Here are the specifications for the calculator: The calculator has one “accumulator”, which holds the result of all prior calculations. The calculator is set to 0.0 when the calculator is turned on (i.e., instantiated). On the console, the user then enters an operator (+, -, *, or /) and a number. Your console will then apply the operator and operand to the number in the accumulator, and...
Write a Python program to add, multiply and divide any two numbers.
Write a Python program to add, multiply and divide any two numbers.
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide)...
Implement a Java program that is capable of performingthe basic arithmetic operations (add, subtract, multiply, divide) on binary numbers using only logical operations (i.e., not using the actual mathematical operators thatJava already supports).A skeleton for the implementation is provided and can be downloaded from Canvas.In this source file  (BinaryCalculator.java), there is already code to read stringsfrom the keyboard.  The program will exit if the string “QUIT” is received, otherwiseit will attempt to process commands of the form: <binary operand 1> <operator> <binary...
Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide...
Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide Power Root Modulus Upon receiving the user's selection, prompt the user for two numeric values and print the corresponding solution based on the user's menu selection. Ask the user if they would like to use the calculator again. If yes, display the calculator menu. otherwise exit the program. EXAMPLE PROGRAM EXECUTION: Add Subtract Multiply Divide Power Root Modulus Please enter the number of the...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new element to an array.​ Change an element with the new one.​ Search for a particular element in the array.​ ​The code must have four separate methods for each task stated above.​ Do not use any pre-defined Java functions.​ You are free to use int or String data-type for the array.​
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide...
PYTHON: Im writing a program to make a simple calculator that can add, subtract, multiply, divide using functions. It needs to ask for the two numbers from the user and will ask the user for their choice of arithmetic operation 1- subtract 2- add 3- divide 4- multiply
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT