0
\$\begingroup\$

I am new to writing java code and I would like to improve my skills. I would like someone to review and critic me on where I can improve on. Here is a small program I wrote in java about matrices and a few arithmetic methods applied on it.

import java.util.*;

public class MatrixArithmetic{
    private static Scanner in = new Scanner(System.in);
    public static void main(String[] args){
        menu();
    }
    public static void menu(){
        showOptions();
        int choice = getValue();
        System.out.println();
        while (choice != -1){
            switch (choice){
                case 1:{ // add two matrices
                    int size = getSize();
                    int[][] firstMatrix = getMatrix(size);
                    int[][] secondMatrix = getMatrix(size);
                    System.out.println();
                    System.out.println("First matrix is:");
                    printMatrix(firstMatrix);
                    System.out.println("Second matrix is:");
                    printMatrix(secondMatrix);
                    int[][] result = addMatrix(firstMatrix, secondMatrix, size);
                    System.out.println("The resulting matrix is:");
                    printMatrix(result);
                    System.out.println();
                    System.out.println("Command number 1 completed.");
                    System.out.println();
                    showOptions();
                    choice = getValue();
                    System.out.println();
                    break;
                }
                case 2:{ // subtract two matrices
                    int size = getSize();
                    int[][] firstMatrix = getMatrix(size);
                    int[][] secondMatrix = getMatrix(size);
                    System.out.println();
                    System.out.println("First matrix is:");
                    printMatrix(firstMatrix);
                    System.out.println("Second matrix is:");
                    printMatrix(secondMatrix);
                    int[][] result = subMatrix(firstMatrix, secondMatrix, size);
                    System.out.println("The resulting matrix is:");
                    printMatrix(result);
                    System.out.println();
                    System.out.println("Command number 2 completed.");
                    System.out.println();
                    showOptions();
                    choice = getValue();
                    System.out.println();
                    break;
                }
                case 3:{ // multiply two matrices
                    int size = getSize();
                    int[][] firstMatrix = getMatrix(size);
                    int[][] secondMatrix = getMatrix(size);
                    System.out.println();
                    System.out.println("First matrix is:");
                    printMatrix(firstMatrix);
                    System.out.println("Second matrix is:");
                    printMatrix(secondMatrix);
                    int[][] result = prodTwoMatrix(firstMatrix, secondMatrix, size);
                    System.out.println("The resulting matrix is:");
                    printMatrix(result);
                    System.out.println();
                    System.out.println("Command number 3 completed.");
                    System.out.println();
                    showOptions();
                    choice = getValue();
                    System.out.println();
                    break;
                }
                case 4:{ // multiply matrix to a constant
                    int size = getSize();
                    System.out.print("Enter the multiplication constant: ");
                    int constant = getValue();
                    int[][] matrix = getMatrix(size);
                    System.out.println();
                    System.out.println("The matrix is:");
                    printMatrix(matrix);
                    int[][] result = prodMatrixConstant(matrix, size, constant);
                    System.out.println("The resulting matrix is:");
                    printMatrix(result);
                    System.out.println();
                    System.out.println("Command number 4 completed.");
                    System.out.println();
                    showOptions();
                    choice = getValue();
                    System.out.println();
                    break;
                }
                case 5:{ // transpose matrix
                    int size = getSize();
                    int[][] matrix = getMatrix(size);
                    System.out.println();
                    System.out.println("The matrix is:");
                    printMatrix(matrix);
                    int[][] result = transposeMatrix(matrix, size);
                    System.out.println("The resulting matrix is:");
                    printMatrix(result);
                    System.out.println();
                    System.out.println("Command number 5 completed.");
                    System.out.println();
                    showOptions();
                    choice = getValue();
                    System.out.println();
                    break;
                }
                case 6:{ // trace matrix
                    int size = getSize();
                    int[][] matrix = getMatrix(size);
                    System.out.println();
                    System.out.println("The matrix is:");
                    printMatrix(matrix);
                    int result = traceMatrix(matrix, size);
                    System.out.println();
                    System.out.print("The trace for this matrix is: " + result);
                    System.out.println("\n");
                    System.out.println("Command number 6 completed.");
                    System.out.println();
                    showOptions();
                    choice = getValue();
                    System.out.println();
                    break;
                }
                default:{
                    System.out.println("Testing completed.");
                    choice = -1;
                    break;
                }
            }
        }
    }
    public static void showOptions(){
        System.out.print("Your options are: \n" + 
        "----------------- \n" + 
        "1) Add two 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 \n" +
        "----------------- \n" +
        "Please enter your option: ");
    }
    public static int getValue(){
        return in.nextInt();
    }
    public static int getSize(){
        System.out.print("Enter the size of square matrices: ");
        int size = getValue();
        return size;
    }
    public static int[][] getMatrix(int size){
        int[][] matrix = new int[size][size];
        randMatrix(matrix, size);
        return matrix;
    }
    public static void randMatrix(int[][] matrix, int size){
        Random rand = new Random();
        for(int i = 0; i < size; i++){
            for(int j = 0; j < size; j++){
               matrix[i][j] = rand.nextInt(10);
            }
        }
    }
    public static void printMatrix(int[][] matrix){
        for(int i = 0; i < matrix.length; i++){
            for(int j = 0; j < matrix[i].length; j++){
               System.out.printf("%4s\t", matrix[i][j]);
            }
            System.out.print("\n");
        }
    }
    public static int[][] addMatrix(int[][] firstMatrix, int[][] secondMatrix, int size){
        int[][] add = new int[size][size];
        for(int i = 0; i < size; i++){    
            for(int j = 0; j < size; j++){    
                add[i][j] = firstMatrix[i][j] + secondMatrix[i][j];
            }
        }
        return add;
    }
    public static int[][] subMatrix(int[][] firstMatrix, int[][] secondMatrix, int size){
        int[][] sub = new int[size][size];
        for(int i = 0; i < size; i++){    
            for(int j = 0; j < size; j++){    
                sub[i][j] = firstMatrix[i][j] - secondMatrix[i][j];
            }
        }
        return sub;
    }
    public static int[][] prodTwoMatrix(int[][] firstMatrix, int[][] secondMatrix, int size){
        int[][] product = new int[size][size];
        for(int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                for (int k = 0; k < size; k++) {
                    product[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
                }
            }
        }
        return product;
    }
    public static int[][] prodMatrixConstant(int[][] matrix, int size, int constant){
        int[][] product = new int[size][size];
        for(int i = 0; i < size; i++){    
            for(int j = 0; j < size; j++){    
                product[i][j] = matrix[i][j] * constant;
            }
        }
        return product;
    }
    public static int[][] transposeMatrix(int[][] matrix, int size){
        int[][] newMatrix = new int[size][size];
        for(int i = 0; i < size; i++){
            for(int j = 0; j < size; j++){
               newMatrix[i][j] = 0;
               for(int k = 0; k < size; k++){
                  newMatrix[i][j] = matrix[j][i];
               }
            }
        }
        return newMatrix;
    }
    public static int traceMatrix(int[][] matrix, int size){
        int trace = 0; 
        for (int i = 0; i < size; i++){
            trace += matrix[i][i];
        }
        return trace; 
    }
}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Welcome to Code Review! Can you describe the code more than just "Here is a small program I wrote in java about matrices and a few arithmetic methods applied on it."? The more you tell us about what your code is for, the easier it will be for reviewers to help you. The title applies to too many posts on this site (since many users want their programs to be written better) and needs an edit to simply state the task per site convention. \$\endgroup\$ Commented Sep 11, 2020 at 18:42
  • \$\begingroup\$ The task was to design a Java program to implement matrix arithmetic for square matrices (same number of rows and columns) and to make sure that the program is calling methods to perform (at least) the following operations: 1) Generate: Generate a matrix with values 1 - 10 2) Addition 3) Subtraction 4) Multiplication 5) Multiply two matrices 6) Multiply a matrix by a constant 7) Transposition 8) Matrix Trace \$\endgroup\$
    – Zeke
    Commented Sep 11, 2020 at 19:52
  • \$\begingroup\$ Please edit this information into your question, don't hide it away in comments, especially since you are using a numbered list in your comment and comments don't support numbered list formatting. \$\endgroup\$ Commented Sep 12, 2020 at 6:08

2 Answers 2

2
\$\begingroup\$

In my humble opinion, there are three possible improvements:

  1. Inside the switch clause, there are a lot of repeated statements. You can move all the common parts (read matrix, print matrix) outside the switch clause, and only leave the real matrix computation part inside the switch.
  2. I would suggest make all the computation methods private other than public.
  3. Actually another method can be abstracted, where the input is two matrix and one option, the output is the matrix result.
\$\endgroup\$
0
\$\begingroup\$

The single biggest, most obvious problem in your code is that everything is about doing stuff with matrices, yet there is not a single Matrix object to be found anywhere.

Your central object that everything revolves around is the matrix, and that should be represented by an actual object in your program. So, your central class (or record in Java 14+) should be a Matrix class with methods like plus, minus, mult, etc. Notice that almost every method in your class has Matrix in its name and takes a matrix parameter? That is a sure sign that this parameter should actually be an object that you call the method on.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.