Overview

In this programming assignment, you will apply what you learned about standard I/O, conditionals, and loop statements to create a parameterized Latin square, defined as n x n array with an arrangement of numbers such that each number appears only once in each row and column. Below are some examples of Latin squares with different sizes and configurations.

Figure: see image.

Below is the initial code in LatinSquareDriver.java. You may complete the TO-DO's embedded in the java file.

import java.util.Scanner;

public class LatinSquareDriver {

// Declare constant variables. Note that in general constant identifiers are declared
// in all CAPS to separate them from variable names.
static final int MIN_ORDER= 3;
static final int MAX_ORDER= 99;

public static void main(String[] args) {

// safe checking whether MIN_ORDER < MAX_ORDER and MAX_ORDER < 99
// This checks if the MIN_ORDER constant value set above is not larger // than MAX_ORDER and MAX_ORDER set above is not larger than 99
if (MIN_ORDER >= MAX_ORDER || MAX_ORDER > 99) {
System.out.println("MIN_ORDER must be < MAX_ORDER!");
System.out.println("MIN_ORDER="+MIN_ORDER);
System.out.println("MAX_ORDER=" + MAX_ORDER); System.exit(1); // program exits with an error code

// TODO: ask the user for the order of the square; // the number entered must be in [MIN_ORDER..MAX_ORDER]; // the program should continuously ask the user for the // order of the square, until a valid number is entered. int order = 0;
Scanner sc = new Scanner(System.in);

/* Your code comes here */

// TODO: ask the user for the first number of the square;
// the number entered must be in [1..order];
// the program should continuously ask the user for the
// first number of the square until a valid number is entered.
int current = 0;

/* Your code comes here */

// TODO: generate the latin square using doubly nested loops (a loop inside another loop)
// after you run your code, the output must be saved in a file named "latin_square_A_B.txt",
// where A should be replaced by the order and B by the first number; for example,
// if order is 5 and the first number is 2, the file should be named latin_square_5_2.txt

// Make sure to add an extra space for alignment if current number to display is a single digit number // Please refer to < order 12, first: 7 > example above for spacing.
}
}

Input Parameters & Validation

1. The program should begin by asking the user for the order (i.e., the size) of the Latin square. The examples above show squares with orders 5, 8, and 12.

2. The number entered by the user must be in the closed interval [MIN_ORDER.. MAX_ORDER], with those limits defined by constants (MIN_ORDER, MAX_ORDER) with the same names.

  • The program should continuously ask the user for the order of the square until a valid number is entered.

3. Next, the program should ask the user for the first number of the square (i.e., its top-left number).

  • The number entered must be in the closed interval [1..order].
  • The program should continuously ask the user for the first number of the square until a valid number is entered.

Latin Square

The program should be able to display the correct Latin squares using the exact same output format. For example, when displaying single-digit numbers you should add extra space for the purpose of correctly aligning the numbers. In order to get full credit on this project, you must be able to produce the expected output based on the given input parameters.

Testing

After you run your code, copy and paste the output in a file named latin_square_A_B.txt, where A should be replaced by the order and B by the first number. For example, if the order parameter is 5 and the first number parameter is 2, the file should be named latin_square_5_2.txt.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.