The program
The purpose of this assignment is to provide some exercise in using multiply-linked data structures.
Your program will be a number grid. It will provide a grid with ten rows and six columns. A cell in the grid can hold and display either a number (a double) or a string. Operations will be provided which display the grid, assign values to the cells, do arithmetic on cells, do arithmetic on rows and columns, fill cells with values, insert, delete, and move rows and columns. The default value for all cells is an empty string. The program will present a menu of operations to perform. When input is taken from the user for a cell value any input beginning with a double quote mark (") is treated as a string. The quote mark is not included in the string. Any other input is assumed to be a number and is accepted as a double. Data structures
Each cell in the grid will be represented by a Node (a class) with a Value field and two pointer (i.e. reference) fields, right and down. Theright pointers will be used to link the nodes into rows and the down pointers will link the nodes into columns. Each row will be linked as a circle (the right field of the final node in each row will point to the first node in the row) as will each column (the down pointer of the bottom node pointing to the top node of the column).
A Value is represented as a class with three fields: a double field, dval, to hold numeric values, a String field, sval, for character strings, and a tag field which indicates whether the Value is currently a number or string (or is invalid). Arithmetic (+, -, *, and /) can be performed on Values.
The grid itself will be an instance of a Grid class. This class will have integer fields for the number of rows and number of columns. Use the value 10 for field width when displaying cells. Grid will have a head field which points to the first node of the grid (at row 0, column 0). A constructor and a number of public methods for the grid operations will be written.
Internals
After the (number of rows) × (number of columns) nodes have been created and linked together by the constructor for Grid all access to nodes will be done through pointer operations beginning at head.
Values
The Value class represents the values that can be stored in the nodes. A value can be either a double or a string. A separate data field is provided for each so it is possible for a "Value" to contain both a double and a string. The tag field indicates which of these data fields holds the "real value" of the Value. A Value can also have an "INVALID" tag. This tag value is only used for (some) intermediate results in arithmetic and a Value with an INVALID tag is never stored in a node. Values are constructed with tag STRING, dval 0, and svalnull.
Arithmetic can be done on Values with the operators plus, minus, star, and slash. Each of these will be implemented as a method of the Value class taking a Value as parameter and returning Value. The operators will first check to see if the tags of both operands are DBL. If not no arithmetic is performed and the tag of the resulting Value is set to INVALID. Otherwise the sum, product, etc., is computed and the result stored in the dval field of the resulting Value. The tag is set to DBL.
You will write a toString method for Value. Useful methods: String.format("%10.4f", x) will return a String representation of the double x to 4 decimal places in a field width of 10 charactes; String.format("%10s", str) will return a String containing the (sub-)String str in a field of 10 characters.
Notice that you do not know whether the input will be a double or a string until after it has been entered. To resolve this problem accept the input as a String and check the first character. If it is a double quote mark (") it is a string and copy it (after allocating memory) withoutthe quote mark to the sval field. Otherwise the input is intended as a double. In this case convert it to a double with Double.parseDouble and store the result in the dval field.
The Grid class
The Grid class has methods to print the grid, to do arithmetic on the cells, to insert and delete rows and columns, and to assign values to the cells.
The display method will display the grid. Each cell will be displayed in a field of 10 characters and doubles will be displayed with 4 decimal places. The row and column numbers will be displayed.
Grid arithmetic
Arithmetic can be done on individual cells with addNodes, subNodes, mulNodes, and divNodes methods. Other methods perform arithmetic on entire rows (addRows, etc.) or columns (addCols, etc.) at a time. In all cases once you have located the nodes to be operated on you can just "add," etc., the Values and assign Values without accessing the Value‘s data fields or checking tags by simply using the arithmetic methods from Value.
Inserting and deleting nodes
Entire rows and columns can be inserted into the grid using insertRow and insertCol. Rows and columns can be deleted using deleteRow and deleteCol. These methods work through much fussing with pointers and require a certain amount of care to code.
Assigning Values to cells
Two methods assign values to cells: number and fill. number requires the row and column numbers of two grid cells. These define a rectangular "subgrid" having these two grid cells as corners. number will assign the numbers 0, 1, 2, etc. (as doubles) to these cells. fill is similar but it puts the same Value (passed as a parameter) in the node representing each cell.
Notice that fill can be used to assign a value to a single cell.
Validation
For all methods the parameter values must be validated. Each row and column number must be in the correct range (between 0 and Grid.rows-1 and between 0 and Grid.cols-1 respectively). If two row/column pairs are used to define a subgrid (as with number and fill) the first pair cannot follow the second (but they can be the same). The delRow and delCol methods must not reduce the grid to an empty grid (there must always be at least one row and one column in the grid). Methods which do division must check for zero divisors. If a zero divisor is found and error message is displayed, the division is not performed and the resulting Value will have tag INVALID. Any other validations needed for the methods to make sense must be done.
The driver
To run your grid you will write a program which will create a grid (using the default size of ten rows and six columns). The program will run in a loop which displays a menu offering all of the choices seen in the sample run shown above, accepts user input, and then performs the requested operation by calling the appropriate method.
A final note
Test your work thoroughly and in small pieces as you proceed. A small problem in Value or the constructor for Grid, for example, which might not be apparent under overly simple testing can cause major, and difficult to trace, problems later on.
Note that much code is used repeatedly in this program. Simplify your program by factoring out this common code where appropriate. For example, adding a few private methods to class Grid to handle common operations in moving around the grid can clean up your code considerably.
To hand in
You will hand in a hard copy of your source files and of a sample terminal session. For the terminal session you will run your program with sample operations which will be provided to you. There will be no electronic submission.