Assignment #6 (extended version): Load / Save for Graph Pad



Extend the program created in assignment #5 by load and save options.
Your program should show the additional options:

(L)oad / (S)ave

Load (and save, too, of course) prompts for input of a filename, saving the current graphics (or loading previously saved graphics). The data should be written to a TEXTFILE, containing one graphic object per row, giving the type and all necessary informations.
The elements should therefore be shown in the file as:


Extension to previous version of assignment:

As you will see in the class GraphicsControl, the GraphicObjects are stored in an array ('theList[ ]'). The size of an array is always limited to the size it is initialized to, i.e. an array size is not adjustable (at least not without additional effort). Right now it is set to 10 elements. The easy solution: it could be initialized to contain a higher number of GraphicObjects, e.g. 100000, hoping that no one ever will enter more objects (we can be sure of that with our current user interface). But somebody could write a program that creates a textfile containing e.g. 200000 circles. Ok, we increase our maximum to 500000 then... you guess what could happen then. Whatever fictional situation i'd have to invent, you would never initialize the array to a value that's high enough. An array is simply the wrong data structure. The 'Vector' is the datastructure of choice here (we' talk about Vectors and Lists in the next class).

Many words, a single task:
Exchange the datastructure of 'theList' from 'GraphicObject[]' to 'Vector'. This of course includes changes wherever the array was used.
Since i do NOT want you to change anything in the 'GraphicsEngine' class, i'll provide a version that works with both, Array and Vector. You just have to call the constructor with the Vector, instead of the Array (do you see an advantage of Object Oriented Programming, especially information hiding here ?).

The sources contain the FULL SOLUTION to assignment 5 plus some framework and utility methods needed for assignment 6.
I just decided to provide most of the framework: GraphicsControl is more or less complete, you just have to add some lines in the methods 'save' and 'load', 'load' is nearly complete.
Additionally, the class 'Circle' is complete, you can use it as an example for the things to add in the classes Rectangle and Line.
Complete these tasks first using the old array structure. When everything is complete, move on and change the array to a Vector.

Do NOT use your own version of assignment 5 unless you really know what's going on.
Do NOT use old versions of the classes mixed with the new versions provided.

The sources are HERE.


Where to place the load/save methods ?

(this information is now a little outdated, since the framework coming with the extended assignment shows you directly what to do. Nevertheless: read it, redundancy is often needed in the learning process)

SAVE:
For each class (Rectangle/Circle/Line) override the method 'toString()' (why overriding ? where is it defined ?). 'toString()' must return the object's properties in a single string, containing the data in the way described above. Modify 'GraphicsControl' to save the array 'theList[ ]' by converting each object in the list to a string, save each string as a single line in the text file you must have previously opened.

LOAD:
Open a previously saved text file, containing the graphics data in the way described above. For each line, read the object type (rectangle/circle/line), and create an object by calling a newly designed constructor, that takes a string as input. This string is exactly the line read from the file, it contains all data necessary to create a unique object. Again: for each class (rectabgle/circle/line), you have to create a new constructor, taking the information string as input.
The GraphicsControl module has to decide which object to construct, hence it has to read a single line from the file, check for the desired object type (given by its name as the first parameter in the line), and then call the specific constructor. The class to be utilized here is the StringTokenizer.

Do not forget to catch fileformat errors ! The user could try to open an arbitrary file, containing no meaningful data to your program at all. Catch that case and inform the user !


Note that this approach is not easily extendable any longer: if you want to extend the system by a new graphic module, e.g. a triangle, 'GraphicControl' has to be changed, too, to know about that object.
To force the program to create an object of a class it only knows about at runtime, not explicitely specified in the code, is much more complicated and will not be handled in our course.
If you are interested in that topic:
The keyword is 'class factories', and the methods needed are in the class 'Class'.