Input restrictions The function should halt and return a red error message for t
ID: 3837178 • Letter: I
Question
Input restrictions The function should halt and return a red error message for the following conditions The input n is not a positive integer bigger than/equal to 2 The input N is not a positive integer greater than or equal to n+1 4. Create the travel plan The family starts out from Lubbock and visits some randomly picked cities and comes back to Lubbock. However, they do not know in which order they want to travel to the cities to ensure fastest travel. The sections help them with deciding the order of visit. a) Write a function M-file Travel plan. m that uses four inputs s, an array of n distinct numbers (cities to be visited) P, an (Nx2) matrix (containing the city locations, the first row being the start and end of trip) border, an (M x 2) matrix (containing the border points) speed, an (N x N) matrix (containing information about speed of travel between any two cities) The journey always starts from P (1, and ends in P (1, i.e., the city whose location is given by the first row of P. The function will generate all possible different orders of cities in input s (hint: use built-in function perms).Explanation / Answer
import java.awt.*; // Uses AWT's Layout Managers
import java.awt.event.*; // Uses AWT's Event Handlers
import javax.swing.*; // Uses Swing's Container/Components
/**
* The Sudoku game.
* to unravel the quantity puzzle, each row, every column, and every of the
* 9 3×3 sub-grids shall contain all of the digits from one to nine
*/
public category Sudoku extends JFrame the sport properties
public static final int GRID_SIZE = 9; // Size of the board
public static final int SUBGRID_SIZE = 3; // Size of the sub-grid
// Name-constants for UI management (sizes, colours and fonts)
public static final int CELL_SIZE = 60; // Cell width/height in pixels
public static final int CANVAS_WIDTH = CELL_SIZE * GRID_SIZE;
public static final int CANVAS_HEIGHT = CELL_SIZE * GRID_SIZE;
// Board width/height in pixels
public static final Color OPEN_CELL_BGCOLOR = Color.YELLOW;
public static final Color OPEN_CELL_TEXT_YES = new Color(0, 255, 0); // RGB
public static final Color OPEN_CELL_TEXT_NO = Color.RED;
public static final Color CLOSED_CELL_BGCOLOR = new Color(240, 240, 240); // RGB
public static final Color CLOSED_CELL_TEXT = Color.BLACK;
public static final Font FONT_NUMBERS = new Font("Monospaced", Font.BOLD, 20);
// the sport board composes of 9x9 JTextFields,
// every containing String "1" to "9", or empty String
personal JTextField[][] tfCells = new JTextField[GRID_SIZE][GRID_SIZE];
// Puzzle to be resolved and also the mask (which is accustomed management the
// issue level).
// Hardcoded here. further credit for automatic puzzle generation
// with varied issue levels.
personal int[][] puzzle =
,
,
,
,
,
,
,
,
};
// For testing, open solely a pair of cells.
personal boolean[][] masks =
,
,
,
,
,
,
,
,
};
/**
* creator to setup the sport and also the UI parts
*/
public Sudoku() instrumentality cp = getContentPane();
cp.setLayout(new GridLayout(GRID_SIZE, GRID_SIZE)); // 9x9 GridLayout
// allot a typical auditor because the ActionEvent auditor for all the
// JTextFields
// ... [TODO 3] (Later) ....
// Construct 9x9 JTextFields and augment the content-pane
for (int row = 0; row < GRID_SIZE; ++row) mountain pass = 0; mountain pass < GRID_SIZE; ++col) {
tfCells[row][col] = new JTextField(); // allot part of array
cp.add(tfCells[row][col]); // ContentPane adds JTextField
if (masks[row][col]) {
tfCells[row][col].setText(""); // set to empty string
tfCells[row][col].setEditable(true);
tfCells[row][col].setBackground(OPEN_CELL_BGCOLOR);
// Add ActionEvent auditor to method the input
// ... [TODO 4] (Later) ...
} else guide on a way to run the creator
.......
}
// outline the auditor Inner category
// ... [TODO 2] (Later) ...
}
3. Event Handling
Next, we have a tendency to shall program the event handling.
We shall use a typical instance of a Named Inner category (called InputListener) because the ActionEvent auditor for "all" the editable JTextFields. Hence, within the actionPerformed(), we want to spot the actual JTextField (in terms of row and col) that trigger the event. you'll use the ActionEvent.getSource() methodology to retrieve the supply object that has dismissed the event and compare the article with all the 9×9 JTextFields.
Place the inner category at [TODO 2].
// [TODO 2]
// Inner category to be used as ActionEvent auditor for ALL JTextFields
personal category InputListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// All the 9*9 JTextFileds invoke this handler. we want to work out
// that JTextField (which row and column) is that the supply for this invocation.
int rowSelected = -1;
int colSelected = -1;
// Get the supply object that dismissed the event
JTextField supply = (JTextField)e.getSource();
// Scan JTextFileds for all rows and columns, and match with the supply object
Boolean found = false;
for (int row = 0; row < GRID_SIZE && !found; ++row) {
for (int mountain pass = 0; mountain pass < GRID_SIZE && !found; ++col)
}
}