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.
}
}
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.
3. Next, the program should ask the user for the first number of the square (i.e., its top-left number).
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.
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.