1 / 19

Generating GAIGS XML Scripts I

Generating GAIGS XML Scripts I. Integrating Algorithm Visualization into Computer Science Education Grand Valley State University June 13-16, 2006. Generating Scripts as the Output of Executing Programs. GAIGS XML scripts can be generated by programs written in any programming language.

erolph
Download Presentation

Generating GAIGS XML Scripts I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Generating GAIGS XML Scripts I Integrating Algorithm Visualization into Computer Science Education Grand Valley State University June 13-16, 2006

  2. Generating Scripts as theOutput of Executing Programs • GAIGS XML scripts can be generated by programs written in any programming language. • However, the JHAVÉenvironment is designed to directly support programs written in Java. • Input to programs must be specified on the command line. • The first command line parameter is the file name the script is to be written to. • Support classes are available which can be used to directly generate the required GAIGS XML.

  3. Support Classes forGAIGS XML Generation • ShowFile: Handles the actual writing to the script file. • Structure Classes: Basically one for each of the GAIGS built-in structures. • Linear Structures: GAIGSstack, GAIGSqueue, GAIGSlist • Arrays: GAIGSarray (includes bar graphs) • Trees and Graphs: GAIGStree, GAIGSgraph • Labels: GAIGSlabel • Question Classes: Support various aspects of generating questions in scripts. • XMLfibQuestion, XMLmcQuestion, XMLmsQuestion, XMLtfQuestion

  4. The ShowFile Class • The ShowFile class is responsible for all writing to the script file. • Constructors: • ShowFile(String fileName) • fileName is the name of the file to be written to. • file is opened, and preliminary XML written to it. • Key Methods: • writeSnap(String title, Double titleSize, GAIGSdatastr… ds) • writes to the file the XML for a snap with the title and each of the listed structures. titleSize parameter is optional. • close() • writes any questions and closes the file.

  5. The GAIGSstack Class I • GAIGSstack functions in the usual way as a stack (with push and pop operations. • But also stores color and other information in a way that can remain hidden (if desired) from the client class. • Constructors: • GAIGSstack() • create a stack using defaults for location and color. • GAIGSstack(String n, String c, double x1, y1, x2, y2, size) • create a stack with name n, color c, location <(x1,y1),(y2,y2)>, and fontSize size.

  6. The GAIGSstack Class II • Key Methods: • pop() • removes the most recently added item from the stack. • push(Object o) • adds an item to the stack (with the default color). • push(Object o, String c) • adds an item to the stack with color c. • Key Inherited Methods (from GAIGSlist) • isEmpty() • returns true if the stack has no elements. • peek() • returns (but does not remove) the most recently added item from the stack.

  7. import exe.*; import java.io.*; publicclass Example1 { staticfinalString title = "Stack Example"; staticfinaldouble titleSize = 0.08; publicstaticvoid main (String [] args) throws IOException { GAIGSstack stack = new GAIGSstack(); ShowFile show = new ShowFile(args[0]); int itemsToAdd = Integer.parseInt(args[1]); for (int i = 0; i < itemsToAdd ; i++) { stack.push(i); show.writeSnap(title, titleSize, stack); } show.close(); } } Example 1 Code: Using the ShowFile and GAIGSstack Classes

  8. ShowFile constructor ShowFile writeSnap ShowFile close <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE show PUBLIC "-//JHAVE//DTD GAIGS SHO//EN" "gaigs_sho.dtd"> <show> <snap> <title>Stack Example</title> <stack> <bounds x1="0.0" y1="0.0" x2="1.0" y2="1.0" fontsize="0.05"/> <list_item color="#FFFFFF"> <label>0</label> </list_item> </stack> </snap> <questions> </questions> </show> Example 1 Output: itemsToAdd == 1

  9. GAIGSqueue and GAIGSlist Classes • Much like GAIGSstack, these provide, respectively, basic queue and list functionality. • GAIGSqueue: enqueue and dequeue operations • GAIGSlist: many but not all the operations from the List interface • Like the Java collection classes, GAIGSqueue inherits from GAISlist (as does GAIGSstack). • Please consult the GAIGS API for details on these classes

  10. import exe.*; import java.io.*; publicclass Example2 { staticfinalString title = "Stack and Queue"; publicstaticvoid main (String [] args) throws IOException { GAIGSqueue queue = new GAIGSqueue("Queue", "#666666", 0, 0.2, 0.5, 0.7, 0.1); GAIGSstack stack = new GAIGSstack("Stack", "#DDDDDD", 0.5, 0.2, 1, 0.7, 0.1); ShowFile show = new ShowFile(args[0]); int itemsToAdd = Integer.parseInt(args[1]); for (int i = 0; i < itemsToAdd ; i++) { queue.enqueue(i); stack.push(i); show.writeSnap(title, queue, stack); } show.close(); } } Example 2 Code: Multiple Structures in a Snapshot

  11. <snap> <title>Stack and Queue Example</title> <queue> <name>Queue</name> <bounds x1="0.0" y1="0.2" x2="0.5" y2="0.7" fontsize="0.1"/> <list_item color="#666666"> <label>0</label> </list_item> </queue> <stack> <name>Stack</name> <bounds x1="0.5" y1="0.2" x2="1.0" y2="0.7" fontsize="0.1"/> <list_item color="#DDDDDD"> <label>0</label> </list_item> </stack> </snap> … Example 2 Partial Output: This is the First Snap

  12. GAIGSarray I • GAIGS provides support for one and twodimensional arrays. • Row labels can be specified, and if the array is 2-d column labels as well. • If the array is a 1-d array of int, it can be shown either in the usual format or as a bar graph. • Here we just briefly consider the 1-d case, please consult the GAIGS API for complete details on this class

  13. GAIGSarray II • Constructor: • GAIGSarray (String s, boolean bar, color c, double x1, y1, x2, y2, size) • create a label with name s, color c, location <(x1,y1),(y2,y2)>, and fontSize size.Display as a bargraph if bar == true. • Key Methods: • set(Object o, int loc) and set(Object o, int loc, String c) • set location loc to have value o, optionally with color c • get(int loc) • return the value at location loc • setColor(int loc, String c) • set the color of the item at location loc to c. • setName(String s) • set the name of this structure to s

  14. GAIGSlabel • GAIGSlabel is a work-around for the fact that labels are not (yet) supported in GAIGS. • It is really just an structure that has no elements, hence only the structure's name is displayed. • Constructor: • GAIGSlabel (String s, double x1, y1, x2, y2, size) • create a label with name s, location <(x1,y1),(y2,y2)>, and fontSize size. • the label is placed within these bounds, but is not aligned with any of them. • Key Method: • setLabel(String s) • set the label to s.

  15. GAIGSarray GAIGSlabel Exercise:Create a Bubblesort Visualization • Our exercise will be to create a complete visualization for the (infamous) bubblesort algorithm • Supplied code will create the snapshot shown below:

  16. Supplied Code import java.io.*; import java.util.Random; import exe.*; publicclass Sort { staticfinal String TITLE = null; // no title staticint arraySize; // # of items to sort static GAIGSarray items; // the array of items static GAIGSlabel message; // for status messages Exercise Code: Preamble

  17. publicstaticvoid main(String args[]) throws IOException { // process program parameters and create the show file object ShowFile show = new ShowFile(args[0]); arraySize = Integer.parseInt(args[1]); // define the two structures in the show snapshots items = new GAIGSarray(arraySize, true, "BubbleSort", "#999999", 0.1, 0.1, 0.9, 0.9, 0.07); message = new GAIGSlabel("Initial Array Values", 0.1, -0.45, 0.9, 0.35, 0.07); // initialize the array to be sorted & show it loadArray(); show.writeSnap(TITLE, items, message); // +++ add bubblesort code here +++ // visualization is done show.close(); } Exercise Code: Main method

  18. // Load the array with values from 1 to the array size, then // shuffle these values so that they appear in random order. privatestaticvoid loadArray () { Random rand = new Random(); for (int i = 0; i < arraySize; i++) items.set(i+1,i); for (int i = 0; i < arraySize-1; i++) swap(i, i + (Math.abs(rand.nextInt()) % (arraySize - i)) ); } // Swap two items in the array. privatestaticvoid swap (int loc1, int loc2) { Object temp = items.get(loc1); items.set(items.get(loc2), loc1); items.set(temp, loc2); } Exercise Code: Support Routines

  19. Time to Work! • Below is working code for the bubblesort algorithm. • Your job: • Incorporate it into the supplied code for this exercise. • Decide when snapshots should be taken (when do the interesting events occur?). • Use coloring to show the ongoing actions of the algorithm. • Use the label and the name of the array to produce useful messages about the status of the algorithm. for (int pass = 1; pass < arraySize; pass++) for (int i = 0; i < arraySize-pass; i++) if ((Integer)(items.get(i)) > (Integer)(items.get(i+1))) swap(i, i+1); Bubblesort Code

More Related