Please solve in JAVA: Program #1: Write the following public static methods (put
ID: 3662506 • Letter: P
Question
Please solve in JAVA:
Program #1: Write the following public static methods (put in the same class):
(Note: write all the methods as static as specified here)
1. a getDoubMatrix method (with 2 int parameters for the first and second
dimensions), do the following:
allocate memory for a 2-dim. array of doubles using the row size and
column size parameters (assign to a local 2-dim. array)
randomly assign numbers >= 0.0 and <100.0 to each element (use
nested for loops)
return the 2-dim. array
2. an addMatrices method which adds two 2-dim. arrays of doubles:
Check if the size of the first dimension and the size of the 2nd
dimension of the 2-dim. arrays are the same -- if they are NOT the
same, return a 0 X 0 2-dim. array, otherwise do the following;
Allocate memory for a local 2-dim. array with the same sizes as one of
the 2-dim. array parameters
Add each corresponding element in the parameter 2-dim. arrays and
store the result in the corresponding element of the local 2-dim. array
(use nested for loops)
Return the local 2-dim. array
3. a printDoubMatrix method, that prints (to the screen) the String
parameter on a separate line, then the 2-dim. array. Be sure to print each
element of the parameter 2-dim. array formatted (so the numbers line up),
and each element in the 2nd dimension are on the same line (usual way).
4. a transposeMatrix method which transposes a 2-dim. array of doubles:
Allocate memory for a local 2-dim. array with the REVERSED sizes as
the 2-dim. array parameter (for example, if the parameter had 3 rows
and 4 columns, the local array will have 4 rows and 3 columns)
Assign each column of the parameter to the corresponding row of the
local array
Return the local 2-dim. array
5. a multiplyMatrices method (I'm calling the parameters doubMat1 and
doubMat2),
Program #2
Check if the size of 2nd dimension of doubMat1 equals the size of the
1st dimension of doubMat2 -- if NOT, return a 0 X 0 2-dim. array,
otherwise do the following;
Allocate memory for a local 2-dim. array with the size of the 1st
dimension the same as the size of the 1st dimension of doubMat1 and
the size of the 2nd dimension the same as the size of the 2nd
dimension of doubMat2
Multiply the 2-dim. array parameters and store the result in the local 2-
dim. array. You may look up matrix multiplication.
Write a Java application program (could be in the same class as the above
methods) in which you declare the following arrays in main— doubMatrix1,
doubMatrix2 and doubMatrix3 (all 2-dim. array of doubles). In main, assign 2
random numbers (ints between 3 and 10) for the first dimension and the
second dimension. Then main will call ALL of the methods you wrote above,
but the getDoubMatrix method 2 times (once for doubMatrix1, then once for
doubMatrix2). Use doubMatrix3 for the adding and multiplying results. Each
time a 2-dim. array changes, call the printDoubMatrix for it.
DO NOT USE ANY CLASS-SCOPE VARIABLES unless they're static AND final!
ALL METHODS ARE static. ALWAYS check the array bounds correctly (both
dimensions)!
HOW TO USE get pseudo-randomly generated integers between MIN and MAX
(inclusive): (int)(Math.random() * (MAX-MIN+1) ) + MIN
HOW TO USE get pseudo-randomly generated doubles >=0 and Math.random() * (MAX)
SUGGESTION: To test this, hard-code some test 2-dim. arrays, and pass them
to the methods. Then try with the random arrays.
Explanation / Answer
import java.util.Random;
public class MatrixService {
public static void main(String[] args) {
double doubMatrix1[][], doubMatrix2[][], doubMatrix3[][];
int min = 3, max = 10;
int m = (int) (Math.random() * (max - min + 1)) + min;
int n = (int) (Math.random() * (max - min + 1)) + min;
doubMatrix1 = getDoubMatrix(m, n);
doubMatrix2 = getDoubMatrix(m, n);
System.out.println("doubMatrix1:");
printDoubMatrix(doubMatrix1);
System.out.println("doubMatrix2:");
printDoubMatrix(doubMatrix2);
doubMatrix3 = addMatrices(doubMatrix1, doubMatrix2);
System.out.println("Matrix Addition :");
printDoubMatrix(doubMatrix3);
double doubMatrix4[][] = transposeMatrix(doubMatrix1);
System.out.println("Matrix Transpose :");
printDoubMatrix(doubMatrix4);
double doubMatrix5[][] = multiplyMatrices(doubMatrix1, doubMatrix2);
System.out.println("Matrix Multiplication : ");
printDoubMatrix(doubMatrix5);
}
public static double[][] getDoubMatrix(int m, int n) {
double array[][] = new double[m][n];
double start = 0.00;
double end = 100.00;
Random randomNumbers = new Random();
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = start + randomNumbers.nextDouble()
* (end - start);
}
}
return array;
}
public static double[][] addMatrices(double array1[][], double array2[][]) {
double result[][];
if (array1.length != array2.length) {
result = new double[0][0];
return result;
} else {
result = new double[array1.length][array1[0].length];
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
result[i][j] = array1[i][j] + array2[i][j];
}
}
return result;
}
}
// printDoubMatrix
public static void printDoubMatrix(double array1[][]) {
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
System.out.print(array1[i][j] + " ");
}
System.out.println();
}
}
// transposeMatrix
public static double[][] transposeMatrix(double array1[][]) {
double result[][];
result = new double[array1[0].length][array1.length];
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
result[j][i] = array1[i][j];
}
}
return result;
}
// multiplyMatrices
public static double[][] multiplyMatrices(double[][] array1,
double[][] array2) {
int m1rows = array1.length;
int m1cols = array1[0].length;
int m2rows = array2.length;
int m2cols = array2[0].length;
if (m1cols != m2rows) {
throw new IllegalArgumentException("matrices don't match: "
+ m1cols + " != " + m2rows);
}
double[][] result = new double[m1rows][m2cols];
for (int i = 0; i < m1rows; i++) {
for (int j = 0; j < m2cols; j++) {
for (int k = 0; k < m1cols; k++) {
result[i][j] += array1[i][k] * array2[k][j];
return result;
}
}
}
return result;
}
}
OUTPUT:
doubMatrix1:
38.83082655003045 94.40460391575569 79.03671802666177 40.474318133852435 51.46328120751986 51.933590035053456 90.81330988858217 10.1899779535241
39.05764772274808 42.2977592173709 42.58586287291557 14.05488299295058 40.51312448867203 88.71102623518155 16.011607681557383 30.434531419285314
89.40466653427026 39.93829904575756 24.2337157768726 46.731713651106986 1.147640312078635 82.82016287241372 58.90163233061576 87.35046366838148
65.67783666592901 53.2977132485773 40.108236000317035 14.96636884530156 1.6069805581486851 79.55875804563439 69.7839667515143 44.632703458240506
17.755172915785444 84.77428083973425 93.09200177449428 80.66187650900106 71.02820074194001 48.86665305375868 79.49743043450731 84.7570179332951
5.79900178337941 78.84882663612944 45.43735350809265 10.528521925557188 9.268976675408824 65.49630925283873 53.89763104348286 94.31563146126948
19.726288208221753 10.461441938125782 42.706018940582624 75.14324157246797 80.18466539715048 92.41211453307918 52.9802882862483 27.679290242029587
38.577612408090154 3.374760755214412 51.92379688780975 31.218242935861483 57.49938508479436 27.1840503584595 70.0399672388284 65.00134770607848
11.425472463614284 50.78945050612824 1.8409689864655965 15.594475417437792 17.41169367031362 96.14139264057607 51.25481993614933 5.579532519042529
doubMatrix2:
88.50720569115974 83.32606500623535 11.552178622909803 16.66607066092264 20.85039902054223 88.8635856080295 56.457038827461346 4.537832356346916
11.304113365951595 44.26851178370811 94.34259075111069 38.118032786387225 46.75557642394199 20.449376531488372 32.96091520651513 95.40281323728063
9.275777679615294 18.473788802642787 24.12484276502582 38.5858625846762 9.400599973635003 7.52087160019419 0.7052143672359956 55.51070512397593
50.206176648248245 29.48075907112251 28.023759748428812 65.44610870647783 12.550207229280408 10.792563108336806 45.80431582951814 62.832723601984405
33.58919607333043 54.631801812579006 41.39588528575102 0.4810219899492041 23.044878101036936 34.702744835614595 0.1568154763269436 7.157682993208237
24.681399573313236 75.66074533566515 24.69016128830328 93.57959628822685 84.06806279263645 84.58407613919763 1.0460747671172221 25.54167878671868
27.940758716507496 92.93437568382673 17.057860535387405 33.23670623488314 95.33846515450479 79.08930942744377 5.654084866371745 81.78245763637277
86.17950416357661 67.8006255812973 94.289936657026 49.55344655718521 9.055491877552146 44.825235054408296 90.56681060025308 48.121540978329826
16.759220357450754 82.49659028802107 23.659865760484976 70.44185689569586 15.426009623223303 45.14803215716743 91.34908713969156 72.37728045280757
Matrix Addition :
127.33803224119019 177.73066892199103 90.58889664957158 57.14038879477508 72.3136802280621 140.79717564308294 147.27034871604351 14.727810309871016
50.36176108869967 86.566271001079 136.92845362402625 52.172915779337806 87.26870091261401 109.16040276666992 48.97252288807252 125.83734465656593
98.68044421388555 58.41208784840035 48.35855854189842 85.31757623578318 10.54824028571364 90.3410344726079 59.60684669785176 142.8611687923574
115.88401331417725 82.77847231969982 68.13199574874585 80.41247755177939 14.157187787429093 90.3513211539712 115.58828258103244 107.46542706022491
51.34436898911587 139.40608265231324 134.4878870602453 81.14289849895026 94.07307884297694 83.56939788937328 79.65424591083425 91.91470092650334
30.480401356692646 154.5095719717946 70.12751479639593 104.10811821378404 93.33703946804528 150.08038539203636 54.94370581060008 119.85731024798815
47.66704692472925 103.39581762195252 59.76387947597003 108.3799478073511 175.52313055165527 171.50142396052294 58.63437315262005 109.46174787840235
124.75711657166676 71.1753863365117 146.21373354483575 80.7716894930467 66.55487696234651 72.0092854128678 160.60677783908147 113.12288868440831
28.18469282106504 133.2860407941493 25.500834746950574 86.03633231313366 32.837703293536926 141.2894247977435 142.60390707584088 77.9568129718501
Matrix Transpose :
38.83082655003045 39.05764772274808 89.40466653427026 65.67783666592901 17.755172915785444 5.79900178337941 19.726288208221753 38.577612408090154 11.425472463614284
94.40460391575569 42.2977592173709 39.93829904575756 53.2977132485773 84.77428083973425 78.84882663612944 10.461441938125782 3.374760755214412 50.78945050612824
79.03671802666177 42.58586287291557 24.2337157768726 40.108236000317035 93.09200177449428 45.43735350809265 42.706018940582624 51.92379688780975 1.8409689864655965
40.474318133852435 14.05488299295058 46.731713651106986 14.96636884530156 80.66187650900106 10.528521925557188 75.14324157246797 31.218242935861483 15.594475417437792
51.46328120751986 40.51312448867203 1.147640312078635 1.6069805581486851 71.02820074194001 9.268976675408824 80.18466539715048 57.49938508479436 17.41169367031362
51.933590035053456 88.71102623518155 82.82016287241372 79.55875804563439 48.86665305375868 65.49630925283873 92.41211453307918 27.1840503584595 96.14139264057607
90.81330988858217 16.011607681557383 58.90163233061576 69.7839667515143 79.49743043450731 53.89763104348286 52.9802882862483 70.0399672388284 51.25481993614933
10.1899779535241 30.434531419285314 87.35046366838148 44.632703458240506 84.7570179332951 94.31563146126948 27.679290242029587 65.00134770607848 5.579532519042529
Exception in thread "main" java.lang.IllegalArgumentException: matrices don't match: 8 != 9
at MatrixService.multiplyMatrices(MatrixService.java:123)
at MatrixService.main(MatrixService.java:30)
Note : Matrix multiplication is failed beacause of columns of matrix1 is not equals to rows of matrix2