Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. /**
  2. * Shamel Sameer
  3. * Period 4th
  4. * Computer Science 3 honors
  5. *
  6. */
  7.  
  8. import java.util.Scanner;
  9. import java.text.DecimalFormat;
  10. public class LAB01 {
  11.  
  12. public static void main (String[] args)
  13. {
  14. Scanner input = new Scanner(System.in);
  15. int[][] matrix1={
  16. {3,4,2},
  17.  
  18. };
  19.  
  20. System.out.print("Would you like to add or multiply matricies? ==>");
  21. String am=input.nextLine();
  22. if(am.equals("add") || am.equals("Add"))
  23. AddMatrix(matrix1);
  24. else if (am.equals("multiply") || am.equals("Multiply"))
  25. MultMatrix(matrix1);
  26.  
  27.  
  28.  
  29.  
  30. }
  31.  
  32. public static void MultMatrix(int[][] matrix1)
  33. {
  34.  
  35. Scanner input = new Scanner(System.in);
  36. DecimalFormat threeDigits = new DecimalFormat("000");
  37.  
  38. int rows,coloumns;
  39.  
  40.  
  41. do{
  42. System.out.print("# of rows? ===>");
  43. rows= input.nextInt();
  44.  
  45. System.out.print("# of coloumns? ===>");
  46. coloumns=input.nextInt();
  47.  
  48. if(rows!=matrix1[0].length )
  49. System.out.println("Error! # of rows dont match matrix1's # of coloumns.!!!PLEASE REENTER!!!");
  50.  
  51. }while(rows!=matrix1[0].length );
  52.  
  53. int[][] matrix2= new int[rows][coloumns];
  54.  
  55. for(int r=0;r<matrix2.length;r++)
  56. for(int c=0;c<matrix2[0].length;c++)
  57. {
  58. System.out.print("Enter the value at the position [" + r+ "]["+c+"] ==>");
  59. matrix2[r][c]= input.nextInt();
  60.  
  61. }
  62.  
  63. int[][] matrix3= new int[matrix1.length][matrix2[0].length];
  64.  
  65. for(int r=0;r<matrix1.length;r++)
  66. {
  67.  
  68. for(int c=0;c<matrix2[0].length;c++)
  69. {
  70. for(int col=0;col<matrix1[0].length;col++)
  71. {
  72. matrix3[r][c]= matrix3[r][c] + matrix1[r][col] * matrix2[col][c];
  73. }
  74.  
  75. }
  76. }
  77.  
  78.  
  79.  
  80.  
  81. System.out.println("-------------ORIGINAL MATRIX--------------");
  82.  
  83.  
  84.  
  85. for(int r=0;r<matrix1.length;r++)
  86. {
  87.  
  88.  
  89. for(int c=0;c<matrix1[0].length;c++)
  90. {
  91. System.out.print(threeDigits.format(matrix1[r][c])+" ");
  92.  
  93. }
  94. System.out.println();
  95. }
  96.  
  97.  
  98. System.out.println();
  99. System.out.println();
  100. System.out.println();
  101.  
  102. System.out.println("-------------THE 2ND MATRIX--------------");
  103. System.out.println();
  104.  
  105. for(int r=0;r<matrix2.length;r++)
  106. {
  107.  
  108.  
  109. for(int c=0;c<matrix2[0].length;c++)
  110. {
  111. System.out.print(threeDigits.format(matrix2[r][c])+" ");
  112.  
  113. }
  114.  
  115. System.out.println();
  116.  
  117. }
  118. System.out.println();
  119. System.out.println();
  120. System.out.println();
  121. System.out.println();
  122.  
  123. System.out.println("-------------THE 3RD MATRIX--------------");
  124. System.out.println();
  125.  
  126. for(int r=0;r<matrix3.length;r++)
  127. {
  128. for(int c=0;c<matrix3[0].length;c++)
  129. {
  130. System.out.print(threeDigits.format(matrix3[r][c])+" ");
  131.  
  132. }
  133. System.out.println();
  134. }
  135.  
  136. System.out.println();
  137. System.out.println();
  138. System.out.println();
  139. System.out.println();
  140.  
  141.  
  142. }
  143.  
  144. public static void AddMatrix(int[][] matrix1){
  145.  
  146.  
  147. DecimalFormat threeDigits = new DecimalFormat("000");
  148. Scanner input = new Scanner(System.in);
  149.  
  150. int rows,coloumns;
  151.  
  152.  
  153. do{
  154. System.out.print("# of rows? ===>");
  155. rows= input.nextInt();
  156.  
  157. System.out.print("# of coloumns? ===>");
  158. coloumns=input.nextInt();
  159.  
  160. if(rows!=matrix1.length && coloumns!=matrix1[0].length)
  161. System.out.println("Error! Dimensions dont match matrix1's dimensions.!!!PLEASE REENTER!!!");
  162.  
  163. }while(rows!=matrix1.length && coloumns!=matrix1[0].length);
  164.  
  165. int[][] matrix2= new int[rows][coloumns];
  166. for(int r=0;r<matrix1.length;r++)
  167. for(int c=0;c<matrix1[0].length;c++)
  168. {
  169. System.out.print("Enter the value at the position [" + r+ "]["+c+"] ==>");
  170. matrix2[r][c]= input.nextInt();
  171.  
  172. }
  173.  
  174. int[][] matrix3= new int[rows][coloumns];
  175. for(int r=0;r<matrix1.length;r++)
  176. for(int c=0;c<matrix1[0].length;c++)
  177. {
  178. matrix3[r][c]=matrix1[r][c]+matrix2[r][c];
  179.  
  180. }
  181. System.out.println("-------------ORIGINAL MATRIX--------------");
  182. System.out.println();
  183.  
  184.  
  185.  
  186. for(int r=0;r<matrix1.length;r++)
  187. {
  188.  
  189.  
  190. for(int c=0;c<matrix1[0].length;c++)
  191. {
  192. System.out.print(threeDigits.format(matrix1[r][c])+" ");
  193.  
  194. }
  195. System.out.println();
  196. }
  197.  
  198.  
  199. System.out.println();
  200. System.out.println();
  201. System.out.println();
  202.  
  203. System.out.println("-------------THE 2ND MATRIX--------------");
  204. System.out.println();
  205.  
  206. for(int r=0;r<matrix1.length;r++)
  207. {
  208.  
  209.  
  210. for(int c=0;c<matrix1[0].length;c++)
  211. {
  212. System.out.print(threeDigits.format(matrix2[r][c])+" ");
  213.  
  214. }
  215.  
  216. System.out.println();
  217.  
  218. }
  219. System.out.println();
  220. System.out.println();
  221. System.out.println();
  222. System.out.println();
  223.  
  224. System.out.println("-------------THE PRODUCT MATRIX--------------");
  225. System.out.println();
  226.  
  227. for(int r=0;r<matrix1.length;r++)
  228. {
  229. for(int c=0;c<matrix1[0].length;c++)
  230. {
  231. System.out.print(threeDigits.format(matrix3[r][c])+" ");
  232.  
  233. }
  234. System.out.println();
  235. }
  236.  
  237. System.out.println();
  238. System.out.println();
  239. System.out.println();
  240. System.out.println();
  241.  
  242.  
  243.  
  244. }
  245.  
  246.  
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement