Question
Chapter 8 Exercise 36, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY. 8.36 (Latin square) A Latin square is an n-by-n array filled with
Chapter 8 Exercise 36, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.
8.36 (Latin square) 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 program that prompts the user to enter the number n and the array of characters, as shown in the sample output, and checks if the input array is a Latin square. The characters are the first n characters starting from A . Enter number n: 4 Enter 4 rows of letters separated by spaces: A B C D B A D C C D B A D C A B The input array is a Latin square Enter number n: 3 Enter 3 rows of letters separated by spaces: A F D Wrong input: the letters must be from A to C
Outline for the source code
import java.util.Scanner; public class LatinSquares { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter n number: "); int n = input.nextInt(); char[][] m = new char[n][n]; for (int i = 0; i < m.length; i++) { for (int j = 0; j < m[i].length; j++) { m[i][j] = input.next().charAt(0); } } System.out.println(checkLatinSquare(m)); } public static boolean checkLatinSquare(char[][] m) { // first check if grid has valid letters for (int i = 0; i < m.length; i++) { for (int j = 0; j < m[i].length; j++) { //More statements //Do something } } } // check if every row has unique letters for (int i = 0; i < m.length; i++) { if (!isRowValid(m, i)) return false; } // check if every column has unique letters for (int j = 0; j < m[0].length; j++) { if (!isColumnValid(m,j)) return false; } return true; } public static boolean isColumnValid(char[][] m, int column) { //Statements } public static boolean isRowValid(char[][] m, int row) { //Statements } public static void displayMatrix(char[][] m) { //Statements } public static boolean isValidLetter(char ch, int n) { // ch starts off from A, so subtract one from n n--; return (ch >= 'A' && ch <= 'A' + n); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started