Java data structure multiply-linked data Structures program generation (Service code: java00088)

Source: Internet
Author: User

The program

The purpose of this assignment are to provide some exercise in using multiply-linked data structures.

Your program would be a  number Grid . It would 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 is provided which display the grid, assign values to the cells, does arithmetic on cells, does arithmetic on R oWS and columns, fill cells with values, insert, delete, and move rows and columns. The default value for all cells are an empty string. The program would present a menu of operations to perform. When input was taken from the user to a cell value any input beginning with a double quote mark (") is treated as a string . The quote mark is not a included in the string. Any other input is assumed to being a number and is accepted as a double. data Structures

Each cell in the grid would be represented by A  Node   (a Class) with a  Value  field and pointer (i.e. reference) fields,  right  and  down . The right  pointers is used to link the nodes into rows and the  down  pointers would Lin K the nodes into columns. Each row is being linked as a circle (the  right  field of the final node in each row would point to the fi RST node in the row) as would each column (THE&NBSP, down  pointer of the bottom node pointing to the top node of the column).

A Value is represented-a class with three fields:a double field, Dval, to-hold numeric values, a Str ing field, sval, for character strings, and a tag field which indicates whether the Value is CU rrently a number or string (or is invalid). Arithmetic (+,-, *, and/) can performed on Values.

The grid itself'll be a instance of a grid class. This class would have an integer fields for the number of rows and number of columns. Use the value ten for field width when displaying cells. Grid 'll has 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 would be written.

Internals

After the (number of rows) x (number of columns) nodes has been created and linked together by the constructor for Gr ID All access to nodes would be do through pointer operations beginning at Head.

Values

the  Value  class represents the values that can being stored in the nodes. A value can be either a double or a string. A separate data field is provided for each so it's possible for a " Value " to contain both a double and a string. THE&NBSP tag  field indicates which of these data fields holds the "real value" of the  value . a  Value  can also has an "INVALID" tag. This tag value was only used to (some) intermediate results in arithmetic and a  value  with an INVALID t AG is never stored in a node.  Values  are constructed with  tag  string,  Dval  0, and  sval null.

Arithmetic can is done on  Values  with the operators  plus ,   minus ,   star , and  Slash . Each of these would be implemented as a method of the  value  class taking a  value  as p Arameter and returning  Value . The operators would first check to see if the tags of both operands is DBL. If not arithmetic are 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& nbsp Value . The tag is set to DBL.

You'll write a toString method for Value. Useful methods: String.Format ("%10.4f", X) would return a String representation of the Double X to 4 decimal place s in a field width of ten charactes; String.Format ("%10s", str) would return a string containing the (sub-) string str in a field of charact ERs.

Notice that know whether the input would 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's a string and copy it (after allocating memory) withoutthe quote mark to th E 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 a methods to print the grid, to does arithmetic on the cells, to insert and delete rows and column s, and to assign values to the cells.

The display method would display the grid. Each cell would be displayed in a field of characters and doubles would be the displayed with 4 decimal places. The row and column numbers'll be displayed.

Grid arithmetic

Arithmetic can be do on individual cells with AddNodes, subnodes, mulnodes, and divnodes< /c3> methods. Other methods perform arithmetic to entire rows (addrows, etc) or columns (addcols, etc) at a time. In all cases once you are located the nodes to being 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 F Rom Value.

Inserting and Deleting nodes

Entire rows and columns can is 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

Methods assign values to cells: number and fill. number requires the row and column numbers of both grid cells. These define a rectangular "subgrid" having these of both grid cells as corners.  Number would 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 is used to assign a value to a single cell.

Validation

For all methods The parameter values must is validated. Each row and column number must is in the correct range (between 0 and Grid.rows-1 and between 0 and Grid.col S-1 respectively). If-Row/column pairs is used to define a-subgrid (as with number and fill) The first pair cannot fol The second (but they can is the same). The delrow and Delcol methods must not reduce the grid to a empty grid (there must always is at least O Ne 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 are displayed, the division is not performed and the resulting Value Would have the tag INVALID. Any and validations needed for the methods-make sense must is done.

The driver

To run your grid you'll write a program which would create a grid (using the default size of ten rows and six columns). The program would run in a loop which displays a menu offering all of the choices seen in the sample run shown above, ACCEP TS 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 being apparent under Ove Rly simple testing can cause major, and difficult to trace, problems later on.

Note that much code is used repeatedly. Simplify your program is 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'll hand in a hard copy of your source files and of a sample terminal session. For the terminal session, you'll run your program with sample operations which'll be provided to you. There'll be no electronic submission.

Java data structure multiply-linked data Structures program generation (Service code: java00088)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.