//MAtrix
/**
* Benis Munezero, Kelvin Acevedo, and Kenneth Mortiniera
* COSC 237 Sec 001
* Assignment 1, Task 1,
*/
import java.util.Random;
import java.util.Scanner;
public class Assign1Task1 {
public static final int MIN = 1;
public static final int MAX = 10;
public static final int OPT_MAX = 6;
public static final int OPT_MIN = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
optMenu();
int option;
int commandNum = 0;
do {
option = getInt(sc, "Please enter an option: ", OPT_MAX, OPT_MIN);
if (option == 0) {
break;
}
else {
System.out.println("\nEnter the size of square matrices: ");
int size = getInt(sc, " ");
int [][] firstMatrix = new int[size][size];
initRand (firstMatrix, MIN, MAX);
int [][] secondMatrix = new int[size][size];
initRand (secondMatrix, MIN, MAX);
switch (option) {
case 1:
int [][] addResult = add2DArrays(firstMatrix, secondMatrix);
System.out.println("First matrix is:");
print2DArray(firstMatrix);
System.out.println("Second matrix is:");
print2DArray(secondMatrix);
System.out.println("The resulting matrix is:");
print2DArray(addResult);
commandNum++;
printCommandNum(commandNum);
break;
case 2:
int[][] subResult = sub2DArrays(firstMatrix, secondMatrix);
System.out.println("First matrix is:");
print2DArray(firstMatrix);
System.out.println("Second matrix is:");
print2DArray(secondMatrix);
System.out.println("The resulting matrix is:");
print2DArray(subResult);
commandNum++;
printCommandNum(commandNum);
break;
case 3:
int[][] multResult = mult2DArrays(firstMatrix, secondMatrix);
System.out.println("First matrix is:");
print2DArray(firstMatrix);
System.out.println("Second matrix is:");
print2DArray(secondMatrix);
System.out.println("The resulting matrix is:");
print2DArray(multResult);
commandNum++;
printCommandNum(commandNum);
break;
case 4:
int cons = getInt(sc, "Enter the multiplication constant: ");
int[][] consMultResult = mult2DArrays(firstMatrix, cons);
System.out.println("First matrix is:");
print2DArray(firstMatrix);
System.out.println("The resulting matrix is:");
print2DArray(consMultResult);
commandNum++;
printCommandNum(commandNum);
break;
case 5:
int [][] transResult = transposeMatrix(firstMatrix);
System.out.println("The matrix is: ");
print2DArray(firstMatrix);
System.out.println("The transposed matrix is: ");
print2DArray(transResult);
commandNum++;
printCommandNum(commandNum);
break;
case 6:
int traceResult = traceMatrix(firstMatrix);
System.out.println("The matrix is: ");
print2DArray(firstMatrix);
System.out.println("The trace for this matrix is: " + traceResult);
commandNum++;
printCommandNum(commandNum);
break;
}
}
} while (option != 0);
System.out.println("\nTesting completed.\n" + commandNum + " commands were completed:)");
}
//Method to print out the command number
public static void printCommandNum (int num) {
System.out.println(" Command number " + num + " completed. ");
}
//Method to print options
public static void optMenu() {
System.out.println("Your options are: \n----------------- \n 1) Add 2 matrices\n 2) Subtract 2 matrices\n 3) Multiply 2 matrices\n 4) Multiply matrix by a constant\n 5) Transpose matrix\n 6) Matrix trace\n 0) EXIT");
}
//Method to get an int from the user
public static int getInt(Scanner in, String prompt) {
System.out.print(prompt);
while (!in.hasNextInt()) {
in.next();
System.out.println("Error! Not a valid intenger.\nPlease input a valid intenger!");
}
return in.nextInt();
}
//Method to get a number between 0 and 6 from user for the an option
public static int getInt(Scanner in, String prompt, int max, int min) {
int num;
do {
System.out.print(prompt);
while (!in.hasNextInt()){
in.next();
System.out.println("Error! Not a valid integer.\n Please input a valid integer!");
}
num = in.nextInt();
if (num < min || num > max) {
System.out.println("Error! Not a valid option!");
}
in.nextLine();
}
while (num < min || num > max);
return num;
}
//Method to print out a 2D array
public static void print2DArray(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print("[" + i + "] ");
for (int j = 0; j < arr[i].length; j++) {
System.out.printf("%3d ", arr[i][j]);
}
System.out.println();
}
}
//Method to fill 2D array with random numbers within a certain range
public static void initRand(int[][] nums, int low, int up) {
Random rand = new Random();
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums[i].length; j++) {
nums[i][j] = rand.nextInt(up - low) + low;
}
}
}
//Method to add up two different matrices
public static int[][] add2DArrays(int[][] firstArr, int[][] secondArr) {
int[][] result = new int[firstArr.length][firstArr[0].length];
for (int i = 0; i < firstArr.length; i++) {
for (int j = 0; j < firstArr[i].length; j++) {
result[i][j] = firstArr[i][j] + secondArr[i][j];
}
}
return result;
}
//Method to subtract two different matrices
public static int[][] sub2DArrays(int[][] firstArr, int[][] secondArr) {
int[][] result = new int[firstArr.length][firstArr[0].length];
for (int i = 0; i < firstArr.length; i++) {
for (int j = 0; j < firstArr[i].length; j++) {
result[i][j] = firstArr[i][j] - secondArr[i][j];
}
}
return result;
}
//Overloaded method to mutliply two different matrices
public static int[][] mult2DArrays(int[][] firstArr, int[][] secondArr) {
int[][] result = new int[firstArr.length][firstArr[0].length];
for (int i = 0; i < firstArr.length; i++) {
for (int j = 0; j < secondArr[0].length; j++) {
int sum = 0;
for (int k = 0; k < secondArr[0].length; k++) {
sum += firstArr[i][k] * secondArr[k][j];
//result[i][j] = firstArr[i][j] * secondArr[i][j];
}
result[i][j] = sum;
}
}
return result;
}
//Overloaded method to mutiply a matrix with a constant
public static int[][] mult2DArrays(int[][] arr, int constant) {
int[][] result = new int[arr.length][arr[0].length];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
result[i][j] = arr[i][j] * constant;
}
}
return result;
}
//Method to transpose a matrix
public static int [][] transposeMatrix (int [][] matrix) {
int [][] result = new int[matrix[0].length][matrix.length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
result[j][i] = matrix[i][j];
}
}
return result;
}
//Method to trace a matrix
public static int traceMatrix(int[][] matrix) {
int sum = 0;
for (int i = 0; i < matrix.length; i++) {
sum += matrix[i][i];
}
return sum;
}
}