Question

Chapter 8 Exercise 36, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY. 8.36 (Latin square)...

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);
    }


}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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++) {
              
               //More statements
                //Do something
                m[i][j] = input.next().charAt(0);
            }
        }

        if(checkLatinSquare(m))
           System.out.println("The input array is latin Square");
        else
           System.out.println("The input array is not latin Square");

    }

    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++) {

                if (!isValidLetter(m[i][j], m.length)) {
                    System.out.println("Wrong input:");
                    System.out.println("the letters must be from " + 'A' + "-" +(char)('A' + m.length - 1));
                    return false;
                }
            }
        }
        // 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

        int[] used = new int[m.length];

        for (int i = 0; i < m.length; i++) {

            int index = m[i][column] - 'A';

            if (used[index]==1) {
                return false;
            } else {
               used[index] = 1;
            }
        }

        return true;
    }
    public static boolean isRowValid(char[][] m, int row) {
       //statements

        int[] used = new int[m.length];

        for (int j = 0; j < m[row].length; j++) {

            int index = m[row][j] - 'A';

            if (used[index]==1) {
                return false;
            } else {
               used[index] = 1;
            }
        }

        return true;
    }

    public static void displayMatrix(char[][] m) {
       //statements

        for (int i = 0; i < m.length; i++) {
            for (int j = 0; j < m[i].length; j++) {
                System.out.print(m[i][j] + " ");
            }
            System.out.println("");
        }
    }

    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);
    }


}
=====================================================================

See The Output

1 import java.util.scanner 3 public class LatinSquares 5 public static void main(String[] args) くterminated> LatinSquares [Ja

Let me know if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Chapter 8 Exercise 36, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY. 8.36 (Latin square)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner...

    Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • A two-dimensional square array of integers is a Latin square if the following conditions are true. The first row has no duplicate values. All values in the first row of the square appear in each row...

    A two-dimensional square array of integers is a Latin square if the following conditions are true. The first row has no duplicate values. All values in the first row of the square appear in each row of the square. All values in the first row of the square appear in each column of the square. Examples of Latin Squares 10 30 200 0 20 30 10 30 0 10 20 20 10 0 30 Examples that are NOT Latin Squares...

  • Programming project in Java: You are allowed to use the following methods from the Java API:...

    Programming project in Java: You are allowed to use the following methods from the Java API: class String length charAt class StringBuilder length charAt append toString class Character any method Create a class called HW2 that contains the following methods: 1. isAlphabeticalOrder takes a String as input and returns a boolean: The method returns true if all the letters of the input string are in alphabetical order, regardless of case. The method returns false otherwise. Do not use arrays to...

  • JAVA Only Help on the sections that say Student provide code. The student Provide code has...

    JAVA Only Help on the sections that say Student provide code. The student Provide code has comments that i put to state what i need help with. import java.util.Scanner; public class TicTacToe {     private final int BOARDSIZE = 3; // size of the board     private enum Status { WIN, DRAW, CONTINUE }; // game states     private char[][] board; // board representation     private boolean firstPlayer; // whether it's player 1's move     private boolean gameOver; // whether...

  • cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /**...

    cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i...

  • Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections;...

    Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections; class Grid { private boolean bombGrid[][]; private int countGrid[][]; private int numRows; private int numColumns; private int numBombs; public Grid() { this(10, 10, 25); }    public Grid(int rows, int columns) { this(rows, columns, 25); }    public Grid(int rows, int columns, int numBombs) { this.numRows = rows; this.numColumns = columns; this.numBombs = numBombs; createBombGrid(); createCountGrid(); }    public int getNumRows() { return numRows;...

  • You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement...

    You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; }    There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT