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

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 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
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 Multiplying that matrix to the nth power. Like A^2 = matrix A * matrix A. Elements ONLY can be 0 or 1.
Write a Java program that will add the digits of a person’s birth date to obtain...
Write a Java program that will add the digits of a person’s birth date to obtain a single digit to generate a numerology number. Write one separate method for each of the following tasks (it goes w/o saying that you will have a main() method along with these): date validating date crunching First: Get a Date Numerology has been used since ancient times to shed light on relationships, health, and global events. Each element in a birth date is believed...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT