Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Generating GAIGS XML Scripts I Integrating Algorithm Visualization into Computer Science Education Grand Valley State University June 13-16, 2006."— 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 the Output 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 for GAIGS 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, 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.*; public class Example1 { static final String title = "Stack Example"; static final double titleSize = 0.08; public static void 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 <!DOCTYPE show PUBLIC "-//JHAVE//DTD GAIGS SHO//EN" "gaigs_sho.dtd"> Stack Example 0 Example 1 Output: itemsToAdd == 1 ShowFile constructor ShowFile writeSnap ShowFile close

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.*; public class Example2 { static final String title = "Stack and Queue"; public static void 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 … Stack and Queue Example Queue 0 Stack 0 … Example 2 Partial Output: This is the First Snap

12 GAIGSarray I GAIGS provides support for one and two dimensional 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, 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, 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 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: GAIGSarray GAIGSlabel

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

17 public static void 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. private static void 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. private static void 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! 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); 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. Bubblesort Code


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

Similar presentations


Ads by Google