Lab 9

advertisement
Lab 9
SortViser – A Visualization Tool for Sorting Algorithms
After completing this lab, you should be able to:
 Retrieve system time for runtime analysis;
 Use runtime to analyze algorithm efficiency;
 Use JFileChooser and JColorChooser classes;
 Understand how to use JMenu and JMenuItem classes to create complex GUI; and
 Understand the use of enum type to define constants.
In the class, we learned several sorting algorithms and demonstrated how a visualization tool can
be helpful in observing the characteristics of different ways of sorting. In this lab, we’ll use such
a tool, SortViser to compare a few sorting algorithms. In addition, we’ll also look under the hood
to see certain interesting features that are essential in the design of this visualization tool. You
may be then asked to add a couple of algorithms readily available in your textbook into this tool.
1. Use the tool to analyze the efficiency of the 4 algorithms and use the Excel to get a trend line
for each approach.
a. Follow the example (for insertion sort) given in the Lab 9 Workbook.xlsx file to
create a table for the other three algorithms (bubble, quick, and Shell) and keep track of
runtime or number of operations as labeled in the status bar or the visualization tool.
Repeat 2 or 3 times to get an average for each dataset level.
b. Compare computation time increased or use a trend line to fit to a quadratic or n*lgn
model.
2. Java methods for retrieving system time
a. Read code for the StopWatch class and extend it to a NanoStopWatch class that can
return elapsed time in nanoseconds. Test your class using the MergeSortTimer class.
b. Write a MergeSortMidified class similar to the MergeSort class. You don’t need
to extend the MergeSorter class this time. [Why?] Just the Java code into your class
and change the base case in the sort method follow the idea represented in pseudo code
below:
if (array length is 10 or smaller) // you may use another constant
{
sort the array using selection sort;
return;
}
Modify the MergeSortTimer class to compare the two algorithms: merge sort and
modified merge sort. Use two arrays, say a and b, but make sure they contain the same
contents by using either System.arraycopy(…) or Arrays.copyOf(…).
3. How to program a menu (using JMenubar, JMenu, and JMenuItem. For more details see
tutorials online at http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html)
a. We will create a menu bar that has two menus: File and Color. The File menu is created
for you with four menu items: New, Open, Save, and Exit. Follow the example to create
the Color menu with three: Foreground, Background, and Default. Add your code in the
createColorMenu method stub to complete this part.
b. You should now see two menus with their menu items displayed, though not functioning
at this time. You can now replace the default hot Java logo with your favorite icon by
uncommenting the line right after the frame is instantiated. [Also, see the recommended
way of creating GUI as shown in SUN’s (now Oracle’s) sample code.]
4. Use the JColorChooser class
a. Now it’s time to activate the menu items. First let’s make setting the Background color to
red when the item is selected.
b. Now let’s use the JColorChooser to give a user all the color options.
c. Activate the other two items, Foreground and Default. The Default item will change the
colors back to black (for font or foreground) and white (background).
5. Use the JFileChooser class
a. Similarly, we can activate the File menu items. The New item simply needs to make the
text area editable.
b. The Open item will show an open dialog box using the JFileChooser. If a file is
selected, the content of the text file should be displayed in the text area.
c. The Save item will show a save dialog box using the JFileChooser. It can cause a file
to be saves to a new file or an existing file.
d. Optionally, handle the Exit item appropriately: prompt the user to save if the file has been
edited.
6. Use the enum type
The enum (short for enumeration) type has been made available since Java 1.5 (released in
2004). According to “The Java Tutorials” on Enum Types (http://docs.oracle.com/javase/
tutorial/java/javaOO/enum.html ):
An enum type is a special data type that enables for a variable to be a set of predefined
constants. The variable must be equal to one of the values that have been predefined for it.
Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST)
and the days of the week.
While generic sample code can be found on the tutorial page mentioned above, we will see
how easy it is to create an Algorithm menu with a predefined AlgorithmEnum.
a. Create a helper method addAlgorithmMenuToFrame(JFrame frame) to add an
Algorithm menu (populated using the AlgorithmEnum as provided). Invoke this
method in the createAndShowGUI() method after the frame is instantiated.
b. Add one more entry for Selection sort into the algorithm enum (note: order matters), and
run the demo again. Now you should see one more item in the Algorithm menu.
c. Optionally, activate a menu item (say, the one for Selection sort), to sort a random integer
array with a fixed size using the SelectionSorter and display the runtime in the text
area. (Of course you may also add a Size menu or pop up a dialog box for some input
from the user about data set size.) You may also append the runtime records in the text
area and eventually save it as your lab report.
Download