Question

What type of data structure to use?

Asked by: purewin

I have this assignment in which I need to create a program that creates a number grid. I'm unsure of what kind of data structure I should use to implement the requirements that follow. Should I use a doubly linked list? Binary search tree? Any thoughts would be greatly appreciated!


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. The right 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, number of columns, and the print width (for displaying a cell). Use the value 10 for print width. It 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 member functions for the grid operations will be written.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-11-08 at 03:50:05ID24881465
Topics

Java Programming Language

,

New to Java Programming

,

Algorithms

Participating Experts
2
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. circular doubly linked list
    can you send general implementation of circular doubly linked list (insert item delete item count items etc.. )
  2. constructors
    hi all, i've got a few points, and this one is relatively easy, so i'm feeling generous. [snip] unit test; interface type Tgrounds=class(TForm) field_1:longint; field_2:longint; constructor make; end; var grounds:Tgrounds; implementation constructor ...
  3. doubly linked list
    I would like to know the code for a DOUBLY LINKED LIST using C language. The following is my problem: The program must be designed to create and maintain any number of doubly linked lists that manipulate a list containing data of any type. For example the structures are as f...
  4. circular doubly linked list
    i need to know how to delete a circular doubly linked list.. ex: while(front !=0){ node *kil; kil = front; ; ;// the code here ; ; ; kil->nrxt = 0; delete kil; } // help pls ...
  5. constructors
    What is the use of having a constructor in a private section.

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: rpnmanPosted on 2009-11-08 at 06:40:57ID: 25770602

I'm puzzled to hear you ask "what kind of data structure" you should use. It sounds to me like what you're describing IS a data structure. I think it is far too specialized to find a particular implementation pre-made.

 For starters, I suggest encapsulating the value-type behavior separately from the position-in-the-data-structure behavior. Make a class that can store the value, and can return information about its type, etc. (I'll call this a "Value" object.) Then make a separate class that holds one of these values and manages the linkages for the grid node behavior, and yet a third class that manages the entire structure. That helps keep responsibilities clear and avoids a lot of bugs.
 
What you have described is a net of nodes in the configuration of a torus. This implies the sizes of columns and rows must be fixed, although they can differ. The chief difficulty in such a structure is how to 'grow' it.

If your "Grid" size is fixed after construction you could pass the constructor a rectangular array of values ( Value [ ] [ ] )  and have it stitch right to left and top to bottom internally. If you can impose this restriction on your design, everything becomes easier.

If you need the torus to be be expandable or contractable, you can only do it entire 'row' or 'column' rings at a time. This implies that either the ring is passed in intact or as a liner array of values ( Value [ ] ) of the appropriate size.

The core of your data structure is specialized object you will create to represent the nodes of the grid, with node pointers to its neighbors. Since you have a no requirement for bidirectional traversal of the rings (???) it should be an easy matter to give these a method insertRight(Node) or insertUp(Node) which allow another Node to be added to the ring. I would suggest making this class a private inner class of the Grid/Torus data structure class - no body else should be manipulating it.


 

by: rpnmanPosted on 2009-11-08 at 06:46:21ID: 25770623

Here's a requirements question:

You say values can be either strings or numbers, and that operations (*, +, - ...) can be performed on them.

Can a value change type? Making things immutable when you can is always a good idea...

Are the types required to be the same for an operation to be performed? If not what is the definition of the operation with mixed types?

Are all the types in the data structure the same? If so you can make a much simpler data structure and use Generics to differentiate type.


 

by: purewinPosted on 2009-11-08 at 07:54:31ID: 25770807

Heh maybe it would help if I posted the entire assignment...here it is:

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. A sample run of the program may look like: 
          [djn@localhost grid]$ java ArithGrid
    
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q           
          -> dis
                    col 0     col 1     col 2     col 3     col 4     col 5     
          row 0                                                                 
          row 1                                                                 
          row 2                                                                 
          row 3                                                                 
          row 4                                                                 
          row 5                                                                 
          row 6                                                                 
          row 7                                                                 
          row 8                                                                 
          row 9                                                                 
          
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q           
          -> n
          from row:  0
          from column:  0
          to row:  9
          to column:  5
          
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q           
          -> dis
                    col 0     col 1     col 2     col 3     col 4     col 5     
          row 0     0         1         2         3         4         5         
          row 1     6         7         8         9         10        11        
          row 2     12        13        14        15        16        17        
          row 3     18        19        20        21        22        23        
          row 4     24        25        26        27        28        29        
          row 5     30        31        32        33        34        35        
          row 6     36        37        38        39        40        41        
          row 7     42        43        44        45        46        47        
          row 8     48        49        50        51        52        53        
          row 9     54        55        56        57        58        59        
          
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q           
          -> f
          from row:  3
          from column:  2
          to row:  6
          to column:  4
          with value:  "whoopie
          
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q           
          -> dis
                    col 0     col 1     col 2     col 3     col 4     col 5     
          row 0     0         1         2         3         4         5         
          row 1     6         7         8         9         10        11        
          row 2     12        13        14        15        16        17        
          row 3     18        19        whoopie   whoopie   whoopie   23        
          row 4     24        25        whoopie   whoopie   whoopie   29        
          row 5     30        31        whoopie   whoopie   whoopie   35        
          row 6     36        37        whoopie   whoopie   whoopie   41        
          row 7     42        43        44        45        46        47        
          row 8     48        49        50        51        52        53        
          row 9     54        55        56        57        58        59        
          
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q           
          -> a
          first node row: 3
          first node column: 1
          second node row: 8
          second node column: 3
          destination node row: 0
          destination node column: 0
          
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q           
          -> dis
                    col 0     col 1     col 2     col 3     col 4     col 5     
          row 0     70        1         2         3         4         5         
          row 1     6         7         8         9         10        11        
          row 2     12        13        14        15        16        17        
          row 3     18        19        whoopie   whoopie   whoopie   23        
          row 4     24        25        whoopie   whoopie   whoopie   29        
          row 5     30        31        whoopie   whoopie   whoopie   35        
          row 6     36        37        whoopie   whoopie   whoopie   41        
          row 7     42        43        44        45        46        47        
          row 8     48        49        50        51        52        53        
          row 9     54        55        56        57        58        59        
          
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q           
          -> dr
          First row:  8
          Second row:  4
          Target row:  1
          
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q           
          -> dis
                    col 0     col 1     col 2     col 3     col 4     col 5     
          row 0     70        1         2         3         4         5         
          row 1     2         1.96      8         9         10        1.82759   
          row 2     12        13        14        15        16        17        
          row 3     18        19        whoopie   whoopie   whoopie   23        
          row 4     24        25        whoopie   whoopie   whoopie   29        
          row 5     30        31        whoopie   whoopie   whoopie   35        
          row 6     36        37        whoopie   whoopie   whoopie   41        
          row 7     42        43        44        45        46        47        
          row 8     48        49        50        51        52        53        
          row 9     54        55        56        57        58        59        
          
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q           
          -> delc
          column number:  3
          
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q           
          -> dis
                    col 0     col 1     col 2     col 3     col 4     
          row 0     70        1         2         4         5         
          row 1     2         1.96      8         10        1.82759   
          row 2     12        13        14        16        17        
          row 3     18        19        whoopie   whoopie   23        
          row 4     24        25        whoopie   whoopie   29        
          row 5     30        31        whoopie   whoopie   35        
          row 6     36        37        whoopie   whoopie   41        
          row 7     42        43        44        46        47        
          row 8     48        49        50        52        53        
          row 9     54        55        56        58        59        
          
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q           
          -> q 
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. The right 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, number of columns, and the print width (for displaying a cell). Use the value 10 for print width. It 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 member functions 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 sval an empty string. 
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. 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. 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) without the 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 member functions 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 member function will display the grid. Each cell will be displayed in a field of print_width characters. The row and column numbers will be displayed. 
Grid arithmetic 
Arithmetic can be done on individual cells with addNodes, subNodes, mulNodes, and divNodes member functions. Other member functions 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 functions work through much fussing with pointers and require a certain amount of care to code. 
Assigning Values to cells 
Two member functions 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 functions must not reduce the grid to an empty grid (there must always be at least one row and one column in the grid). Member functions which do division must check for zero divisors. If a zero divisor is found and error message is displayed and the division is not performed. Any other validations needed for the member functions 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 member function. 
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 a sample terminal session using data which will be supplied to you. You do not need to write external documentation for this program.

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:

Select allOpen in new window

 

by: purewinPosted on 2009-11-08 at 07:55:23ID: 25770808

Bah sorry thought it would be better in there here it is again:


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. A sample run of the program may look like:

          [djn@localhost grid]$ java ArithGrid
   
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q          
          -> dis
                    col 0     col 1     col 2     col 3     col 4     col 5    
          row 0                                                                
          row 1                                                                
          row 2                                                                
          row 3                                                                
          row 4                                                                
          row 5                                                                
          row 6                                                                
          row 7                                                                
          row 8                                                                
          row 9                                                                
         
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q          
          -> n
          from row:  0
          from column:  0
          to row:  9
          to column:  5
         
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q          
          -> dis
                    col 0     col 1     col 2     col 3     col 4     col 5    
          row 0     0         1         2         3         4         5        
          row 1     6         7         8         9         10        11        
          row 2     12        13        14        15        16        17        
          row 3     18        19        20        21        22        23        
          row 4     24        25        26        27        28        29        
          row 5     30        31        32        33        34        35        
          row 6     36        37        38        39        40        41        
          row 7     42        43        44        45        46        47        
          row 8     48        49        50        51        52        53        
          row 9     54        55        56        57        58        59        
         
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q          
          -> f
          from row:  3
          from column:  2
          to row:  6
          to column:  4
          with value:  "whoopie
         
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q          
          -> dis
                    col 0     col 1     col 2     col 3     col 4     col 5    
          row 0     0         1         2         3         4         5        
          row 1     6         7         8         9         10        11        
          row 2     12        13        14        15        16        17        
          row 3     18        19        whoopie   whoopie   whoopie   23        
          row 4     24        25        whoopie   whoopie   whoopie   29        
          row 5     30        31        whoopie   whoopie   whoopie   35        
          row 6     36        37        whoopie   whoopie   whoopie   41        
          row 7     42        43        44        45        46        47        
          row 8     48        49        50        51        52        53        
          row 9     54        55        56        57        58        59        
         
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q          
          -> a
          first node row: 3
          first node column: 1
          second node row: 8
          second node column: 3
          destination node row: 0
          destination node column: 0
         
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q          
          -> dis
                    col 0     col 1     col 2     col 3     col 4     col 5    
          row 0     70        1         2         3         4         5        
          row 1     6         7         8         9         10        11        
          row 2     12        13        14        15        16        17        
          row 3     18        19        whoopie   whoopie   whoopie   23        
          row 4     24        25        whoopie   whoopie   whoopie   29        
          row 5     30        31        whoopie   whoopie   whoopie   35        
          row 6     36        37        whoopie   whoopie   whoopie   41        
          row 7     42        43        44        45        46        47        
          row 8     48        49        50        51        52        53        
          row 9     54        55        56        57        58        59        
         
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q          
          -> dr
          First row:  8
          Second row:  4
          Target row:  1
         
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q          
          -> dis
                    col 0     col 1     col 2     col 3     col 4     col 5    
          row 0     70        1         2         3         4         5        
          row 1     2         1.96      8         9         10        1.82759  
          row 2     12        13        14        15        16        17        
          row 3     18        19        whoopie   whoopie   whoopie   23        
          row 4     24        25        whoopie   whoopie   whoopie   29        
          row 5     30        31        whoopie   whoopie   whoopie   35        
          row 6     36        37        whoopie   whoopie   whoopie   41        
          row 7     42        43        44        45        46        47        
          row 8     48        49        50        51        52        53        
          row 9     54        55        56        57        58        59        
         
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q          
          -> delc
          column number:  3
         
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q          
          -> dis
                    col 0     col 1     col 2     col 3     col 4    
          row 0     70        1         2         4         5        
          row 1     2         1.96      8         10        1.82759  
          row 2     12        13        14        16        17        
          row 3     18        19        whoopie   whoopie   23        
          row 4     24        25        whoopie   whoopie   29        
          row 5     30        31        whoopie   whoopie   35        
          row 6     36        37        whoopie   whoopie   41        
          row 7     42        43        44        46        47        
          row 8     48        49        50        52        53        
          row 9     54        55        56        58        59        
         
          Operations
            display           dis           assign cell       as
            fill              f             number            n
            add cells         a             subtract cells    s
            multiply cells    m             divide cells      d
            add rows          ar            subtract rows     sr
            multiply rows     mr            divide rows       dr
            add columns       ac            subtract columns  sc
            multiply columns  mc            divide columns    dc
            insert row        ir            insert column     ic
            delete row        delr          delete column     delc
            quit              q          
          -> q

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. The right 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, number of columns, and the print width (for displaying a cell). Use the value 10 for print width. It 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 member functions 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 sval an empty string.

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. 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. 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) without the 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 member functions 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 member function will display the grid. Each cell will be displayed in a field of print_width characters. The row and column numbers will be displayed.

Grid arithmetic

Arithmetic can be done on individual cells with addNodes, subNodes, mulNodes, and divNodes member functions. Other member functions 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 functions work through much fussing with pointers and require a certain amount of care to code.

Assigning Values to cells

Two member functions 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 functions must not reduce the grid to an empty grid (there must always be at least one row and one column in the grid). Member functions which do division must check for zero divisors. If a zero divisor is found and error message is displayed and the division is not performed. Any other validations needed for the member functions 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 member function.

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 a sample terminal session using data which will be supplied to you. You do not need to write external documentation for this program.

 

by: rpnmanPosted on 2009-11-08 at 08:08:27ID: 25770846

OK, now it is clear - this is a homework assignment and I can't help you beyond what I already offered unless you post your attempts and ask specific questions. Please start coding. I"d strongly recommend following the encapsulation guidelines I gave in my earlier post. After you have some code to post, I can comment further. I'd start with the Value object and write some way to test it. (JUnit would be best, but you can use the main method on the Value object itself or write a separate test class. THen develope the node object and test it thoroughly. Onlly then procceed to the overall data structure object.

Best regards,

 

by: purewinPosted on 2009-11-08 at 08:16:59ID: 25770870

Right I wasn't expecting anyone to code this for me lol. I was just wondering what type of data structure I would need to do this. I was leaning more towards a binary search tree type of implementation?

 

by: rpnmanPosted on 2009-11-08 at 09:14:59ID: 25771035

No, no, no, no no. That would be irrelvant. This is a case where you need to code your own. A search tree is objects will a well-defined linear ordering - you have a two-dimensional ordering of nodes and a complicated ordering for values.


 

by: jabaswavikaPosted on 2009-11-08 at 18:03:47ID: 25773001

There are two possible objects to use (Double dimentional Array, List of Lists). Let me talk about the advantages and disadvantages of each.

2D Array: Value[ ][ ] values;
A 2-dimentional array is a default choice for this problem. It gives a strong typing and uses minimal memory.
The main disadvantages is when the array needs to be resized. A new array needs to be created and all the data needs to be copied.

List of Lists: ArrayList < ArrayList <Value> >
You really do not need to worry about resizing. Ofcourse an arrayList inturn has an array and when it needs to resize, it creates a new memory location and copies the data. Just that you do not code it explicitly here.
The main disadvantage is that this type of datastructure allows for non-equal row sizes. In other words, different rows can have different row lengths (first row can have 5 values, second one 3 values etc). If a matrix as in the problem needs to be modeled, an explicit condition need to be coded to make each of the rows of equal length.

My choice:
I would go with the 2D array. It is clean/strong typed and gives you enough control to code the different operations.

 

by: purewinPosted on 2009-11-09 at 00:31:29ID: 25774164

For the creation of the Value class I would need to create setters and getters for tag, dval, and sval correct?

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 sval an empty string.

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. 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. 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) without the 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

 

by: purewinPosted on 2009-11-09 at 00:40:59ID: 25774195

And another newbie question how do I add a quotation inside of a screen for example I'm trying to read the first character of a string and if its a " then its a string value. Here is what I have:

if(s.startsWith("""))

There is three quotations there but it makes everything after them a part of the string. Is there a symbol I can use to denote a " as part of the string?

 

by: purewinPosted on 2009-11-09 at 01:23:18ID: 25774437

Okay so I figured I'd open another question for this part since it's getting away from the original topic but I would greatly appreciate it if you guys could take a look at the next question.

http://www.experts-exchange.com/Programming/Languages/Java/Q_24882808.html

 

by: purewinPosted on 2009-11-09 at 01:25:19ID: 25774441

And thanks for the speedy and helpful responses! Definately helped clear some things up!

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...