Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In JAVA Coding ~ Problem: A Latin square is an n-by-n array filled with n differ

ID: 3906090 • Letter: I

Question

In JAVA Coding ~

Problem: A Latin square is an n-by-n array filled with n different Latin letters, each occurring exactly once in each row and once in each column. Write a java program that prompts the user enter the positive integer n and either the array of characters or a file containing it and checks if the input array is a Latin square.

* Describe how you want to solve this problem: write this description as a comment at the top of your java file

* Put together a Java code that addresses the above problem. Properly comment your code to explain what it does and why the whole piece of code is an actual solution to the above problem.

(Any help will be greatly appreciated, as I seem to be running into multiple errors everytime I fix something. Thanks in advance)

Explanation / Answer

Here get number n from the user(n must be greater than 2, and odd) and arrange into n-by-n array so no column or row contains the same two numbers, which is a latin square.

This is an example of a latin square:
1 2 3
2 3 1
3 1 2

Program



import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class LatinSquare {

    public static void main(String[] args)
    {
       
       int matrix[][] = initMatrix();
       System.out.println("Input Matrix : ");
         printData(matrix);
        if(latinSquare(matrix))
        {
            System.out.println("This is a Latin Square");
        }
        else
        {
            System.out.println("This is not a Latin Square");         
        }

    }
   
   
    public static boolean latinSquare(int[][] array)
    {
        for (int i = 0; i<array.length ;i++)
        {
            // check for duplicates in each row
            if(duplicates(array[i]))
            {
                return false;
            }

            // create a column array
            int[] column = new int[array[i].length];
            for(int j = 0; j<array.length; j++)
            {
                column[j] = array[j][i]; // could throw an exception if the input file isn't square 3x3, 2x2, 4x4, etc
            }

            // check for duplicates in each column
            if(duplicates(column))
            {
                return false;
            }
        }
        return true;
    }

    public static boolean duplicates(int[] array)
    {
        for (int i = 0; i<array.length; i++)
        {
            for(int j = 0; j<array.length; j++)
            {
                if (i != j && array[i] == array[j])
                {
                    return true;
                }
            }   
        }
        return false;
    }

   
    public static int[][] initMatrix()
    {
        int matrix[][];
        Scanner filein = null;
        try
        {
            filein = new Scanner(new File("input.txt"));
            int numRows = Integer.parseInt(filein.nextLine());
            matrix = new int[numRows][];
            parseData(matrix, filein);
            filein.close();
            return matrix;
        }
        catch (FileNotFoundException e)
        {
            System.out.println(e.getMessage());
            if(filein != null)
            {
                filein.close();
            }
            return null;
        }
    }

    public static void parseData(int matrix[][], Scanner in)
    {
        for(int r = 0; r < matrix.length; r++)
        {
            String splitLine[] = in.nextLine().split(" ");
            matrix[r] = new int[splitLine.length];
            for(int c = 0; c < matrix[r].length; c++)
            {
                matrix[r][c] = Integer.parseInt(splitLine[c]);
            }
        }
    }

    public static void printData(int matrix[][])
    {
        for(int r = 0; r < matrix.length; r++)
        {
            for(int c = 0; c < matrix[r].length; c++)
            {
                System.out.print(matrix[r][c] + " ");
            }
            System.out.println();
        }
    }
}

input.txt

3
1 2 3
3 1 2
2 3 1

Output

Input Matrix :
1 2 3
3 1 2
2 3 1
This is a Latin Square

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote