Created
April 13, 2020 01:32
-
-
Save sholloway/2d8dca6f161934ff6ea3aab92ed5c8ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class ArrayExamples { | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
final int N = 2; | |
final int M = 2; | |
final int P = 3; | |
additionExample(N, M, input); | |
printDivider(); | |
subtractionExample(N, M, input); | |
printDivider(); | |
multiplicationExample(N, M, input); | |
printDivider(); | |
divisionExample(N, M, input); | |
} | |
public static void printDivider(){ | |
System.out.println("-------------------------------------------------------------------------------"); | |
} | |
public static void additionExample(int n, int m, Scanner input){ | |
System.out.println("Addition Example"); | |
// Enter matrix1 | |
System.out.println("Enter matrix1: it is 2x2 "); | |
double[][] matrix1 = populateMatrix(n, m, input); | |
// Enter matrix2 | |
System.out.println("Enter matrix2: it is 2x2"); | |
double[][] matrix2 = populateMatrix(n, m, input); | |
// Add two matrices and print the result | |
double[][] sumMatrix = addMatrix(matrix1, matrix2); | |
System.out.println("The addition of the matrices is "); | |
printMatrix(sumMatrix); | |
} | |
public static void subtractionExample(int n, int m, Scanner input){ | |
System.out.println("Subtraction Example"); | |
// Enter matrix1 | |
System.out.println("Enter matrix1: it is 2x2 "); | |
double[][] matrix1 = populateMatrix(n, m, input); | |
// Enter matrix2 | |
System.out.println("Enter matrix2: it is 2x2"); | |
double[][] matrix2 = populateMatrix(n, m, input); | |
// Add two matrices and print the result | |
double[][] subtractionMatrix = subtractMatrix(matrix1, matrix2); | |
System.out.println("The subtraction of the matrices is "); | |
printMatrix(subtractionMatrix); | |
} | |
public static void multiplicationExample(int n, int m, Scanner input){ | |
System.out.println("Multiplication Example"); | |
// Enter matrix1 | |
System.out.println("Enter matrix1: it is 2x2 "); | |
double[][] matrix1 = populateMatrix(n, m, input); | |
// Enter matrix2 | |
System.out.println("Enter matrix2: it is 2x2"); | |
double[][] matrix2 = populateMatrix(n, m, input); | |
// Add two matrices and print the result | |
double[][] subtractionMatrix = multiplyMatrices(matrix1, matrix2); | |
System.out.println("The multiplication of the matrices is "); | |
printMatrix(subtractionMatrix); | |
} | |
public static void divisionExample(int n, int m, Scanner input){ | |
System.out.println("Division Example"); | |
// Enter matrix1 | |
System.out.println("Enter matrix1: it is 2x2 "); | |
double[][] matrix1 = populateMatrix(n, m, input); | |
// Enter matrix2 | |
System.out.println("Enter matrix2: it is 2x2"); | |
double[][] matrix2 = populateMatrix(n, m, input); | |
// Add two matrices and print the result | |
double[][] result = divideMatricies(matrix1, matrix2); | |
System.out.println("The multiplication of the matrices is "); | |
printMatrix(result); | |
} | |
public static double[][] populateMatrix(int n, int m, Scanner input){ | |
double[][] matrix = new double[n][m]; | |
for (int i = 0; i < matrix.length; i++) { | |
for (int j = 0; j < matrix[i].length; j++) { | |
matrix[i][j] = input.nextDouble(); | |
} | |
} | |
return matrix; | |
} | |
public static void printMatrix(double[][] matrix){ | |
String row; | |
for (int i = 0; i < matrix.length; i++) { | |
row = ""; | |
for (int j = 0; j < matrix[i].length; j++) { | |
row += " " + matrix[i][j]; | |
} | |
System.out.println(row); | |
} | |
} | |
/** | |
* The method for adding two matrices | |
*/ | |
public static double[][] addMatrix(double[][] m1, double[][] m2) { | |
double[][] result = new double[m1.length][m1[0].length]; | |
for (int i = 0; i < result.length; i++) { | |
for (int j = 0; j < result[i].length; j++) { | |
result[i][j] = m1[i][j] + m2[i][j]; | |
} | |
} | |
return result; | |
} | |
/** | |
* The method for subtracting one matrix from another | |
*/ | |
public static double[][] subtractMatrix(double[][] m1, double[][] m2) { | |
double[][] result = new double[m1.length][m1[0].length]; | |
for (int i = 0; i < result.length; i++) { | |
for (int j = 0; j < result[i].length; j++) { | |
result[i][j] = m1[i][j] - m2[i][j]; | |
} | |
} | |
return result; | |
} | |
/** | |
* The method for multiplying one matrix from another | |
*/ | |
public static double[][] multiplyMatrices(double[][] m1, double[][] m2) { | |
double[][] result = new double[m1.length][m1[0].length]; | |
for (int i = 0; i < result.length; i++) { | |
for (int j = 0; j < result[i].length; j++) { | |
result[i][j] = m1[i][j] * m2[i][j]; | |
} | |
} | |
return result; | |
} | |
/** | |
* The method for dividing one matrix from another | |
*/ | |
public static double[][] divideMatricies(double[][] m1, double[][] m2) { | |
double[][] result = new double[m1.length][m1[0].length]; | |
for (int i = 0; i < result.length; i++) { | |
for (int j = 0; j < result[i].length; j++) { | |
result[i][j] = m1[i][j] / m2[i][j]; | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment