Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Shamel Sameer
- * Period 4th
- * Computer Science 3 honors
- *
- */
- import java.util.Scanner;
- import java.text.DecimalFormat;
- public class LAB01 {
- public static void main (String[] args)
- {
- Scanner input = new Scanner(System.in);
- int[][] matrix1={
- {3,4,2},
- };
- System.out.print("Would you like to add or multiply matricies? ==>");
- String am=input.nextLine();
- if(am.equals("add") || am.equals("Add"))
- AddMatrix(matrix1);
- else if (am.equals("multiply") || am.equals("Multiply"))
- MultMatrix(matrix1);
- }
- public static void MultMatrix(int[][] matrix1)
- {
- Scanner input = new Scanner(System.in);
- DecimalFormat threeDigits = new DecimalFormat("000");
- int rows,coloumns;
- do{
- System.out.print("# of rows? ===>");
- rows= input.nextInt();
- System.out.print("# of coloumns? ===>");
- coloumns=input.nextInt();
- if(rows!=matrix1[0].length )
- System.out.println("Error! # of rows dont match matrix1's # of coloumns.!!!PLEASE REENTER!!!");
- }while(rows!=matrix1[0].length );
- int[][] matrix2= new int[rows][coloumns];
- for(int r=0;r<matrix2.length;r++)
- for(int c=0;c<matrix2[0].length;c++)
- {
- System.out.print("Enter the value at the position [" + r+ "]["+c+"] ==>");
- matrix2[r][c]= input.nextInt();
- }
- int[][] matrix3= new int[matrix1.length][matrix2[0].length];
- for(int r=0;r<matrix1.length;r++)
- {
- for(int c=0;c<matrix2[0].length;c++)
- {
- for(int col=0;col<matrix1[0].length;col++)
- {
- matrix3[r][c]= matrix3[r][c] + matrix1[r][col] * matrix2[col][c];
- }
- }
- }
- System.out.println("-------------ORIGINAL MATRIX--------------");
- for(int r=0;r<matrix1.length;r++)
- {
- for(int c=0;c<matrix1[0].length;c++)
- {
- System.out.print(threeDigits.format(matrix1[r][c])+" ");
- }
- System.out.println();
- }
- System.out.println();
- System.out.println();
- System.out.println();
- System.out.println("-------------THE 2ND MATRIX--------------");
- System.out.println();
- for(int r=0;r<matrix2.length;r++)
- {
- for(int c=0;c<matrix2[0].length;c++)
- {
- System.out.print(threeDigits.format(matrix2[r][c])+" ");
- }
- System.out.println();
- }
- System.out.println();
- System.out.println();
- System.out.println();
- System.out.println();
- System.out.println("-------------THE 3RD MATRIX--------------");
- System.out.println();
- for(int r=0;r<matrix3.length;r++)
- {
- for(int c=0;c<matrix3[0].length;c++)
- {
- System.out.print(threeDigits.format(matrix3[r][c])+" ");
- }
- System.out.println();
- }
- System.out.println();
- System.out.println();
- System.out.println();
- System.out.println();
- }
- public static void AddMatrix(int[][] matrix1){
- DecimalFormat threeDigits = new DecimalFormat("000");
- Scanner input = new Scanner(System.in);
- int rows,coloumns;
- do{
- System.out.print("# of rows? ===>");
- rows= input.nextInt();
- System.out.print("# of coloumns? ===>");
- coloumns=input.nextInt();
- if(rows!=matrix1.length && coloumns!=matrix1[0].length)
- System.out.println("Error! Dimensions dont match matrix1's dimensions.!!!PLEASE REENTER!!!");
- }while(rows!=matrix1.length && coloumns!=matrix1[0].length);
- int[][] matrix2= new int[rows][coloumns];
- for(int r=0;r<matrix1.length;r++)
- for(int c=0;c<matrix1[0].length;c++)
- {
- System.out.print("Enter the value at the position [" + r+ "]["+c+"] ==>");
- matrix2[r][c]= input.nextInt();
- }
- int[][] matrix3= new int[rows][coloumns];
- for(int r=0;r<matrix1.length;r++)
- for(int c=0;c<matrix1[0].length;c++)
- {
- matrix3[r][c]=matrix1[r][c]+matrix2[r][c];
- }
- System.out.println("-------------ORIGINAL MATRIX--------------");
- System.out.println();
- for(int r=0;r<matrix1.length;r++)
- {
- for(int c=0;c<matrix1[0].length;c++)
- {
- System.out.print(threeDigits.format(matrix1[r][c])+" ");
- }
- System.out.println();
- }
- System.out.println();
- System.out.println();
- System.out.println();
- System.out.println("-------------THE 2ND MATRIX--------------");
- System.out.println();
- for(int r=0;r<matrix1.length;r++)
- {
- for(int c=0;c<matrix1[0].length;c++)
- {
- System.out.print(threeDigits.format(matrix2[r][c])+" ");
- }
- System.out.println();
- }
- System.out.println();
- System.out.println();
- System.out.println();
- System.out.println();
- System.out.println("-------------THE PRODUCT MATRIX--------------");
- System.out.println();
- for(int r=0;r<matrix1.length;r++)
- {
- for(int c=0;c<matrix1[0].length;c++)
- {
- System.out.print(threeDigits.format(matrix3[r][c])+" ");
- }
- System.out.println();
- }
- System.out.println();
- System.out.println();
- System.out.println();
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement