7027689: /applets/SortDemo demo needs to be improved
authormrkam
Thu, 24 Mar 2011 18:09:50 +0100
changeset 8959 fdeb815ed424
parent 8958 a9671f5efee8
child 8960 dbf27b0abc4c
7027689: /applets/SortDemo demo needs to be improved Reviewed-by: rupashka
jdk/src/share/demo/applets/SortDemo/BidirBubbleSortAlgorithm.java
jdk/src/share/demo/applets/SortDemo/BubbleSortAlgorithm.java
jdk/src/share/demo/applets/SortDemo/QSortAlgorithm.java
jdk/src/share/demo/applets/SortDemo/SortAlgorithm.java
jdk/src/share/demo/applets/SortDemo/SortItem.java
jdk/src/share/demo/applets/SortDemo/example1.html
--- a/jdk/src/share/demo/applets/SortDemo/BidirBubbleSortAlgorithm.java	Thu Mar 24 18:07:40 2011 +0100
+++ b/jdk/src/share/demo/applets/SortDemo/BidirBubbleSortAlgorithm.java	Thu Mar 24 18:09:50 2011 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -29,8 +29,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-/*
- */
 
 /**
  * A bi-directional bubble sort demonstration algorithm
@@ -39,6 +37,8 @@
  * @author James Gosling
  */
 class BidirBubbleSortAlgorithm extends SortAlgorithm {
+
+    @Override
     void sort(int a[]) throws Exception {
         int j;
         int limit = a.length;
@@ -61,9 +61,9 @@
             }
             if (!swapped) {
                 return;
+            } else {
+                swapped = false;
             }
-            else
-                swapped = false;
             for (j = limit; --j >= st;) {
                 if (stopRequested) {
                     return;
--- a/jdk/src/share/demo/applets/SortDemo/BubbleSortAlgorithm.java	Thu Mar 24 18:07:40 2011 +0100
+++ b/jdk/src/share/demo/applets/SortDemo/BubbleSortAlgorithm.java	Thu Mar 24 18:09:50 2011 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -29,8 +29,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-/*
- */
 
 /**
  * A bubble sort demonstration algorithm
@@ -39,23 +37,26 @@
  * @author James Gosling
  */
 class BubbleSortAlgorithm extends SortAlgorithm {
+
+    @Override
     void sort(int a[]) throws Exception {
-        for (int i = a.length; --i>=0; ) {
+        for (int i = a.length; --i >= 0;) {
             boolean swapped = false;
-            for (int j = 0; j<i; j++) {
+            for (int j = 0; j < i; j++) {
                 if (stopRequested) {
                     return;
                 }
-                if (a[j] > a[j+1]) {
+                if (a[j] > a[j + 1]) {
                     int T = a[j];
-                    a[j] = a[j+1];
-                    a[j+1] = T;
+                    a[j] = a[j + 1];
+                    a[j + 1] = T;
                     swapped = true;
                 }
-                pause(i,j);
+                pause(i, j);
             }
-            if (!swapped)
+            if (!swapped) {
                 return;
+            }
         }
     }
 }
--- a/jdk/src/share/demo/applets/SortDemo/QSortAlgorithm.java	Thu Mar 24 18:07:40 2011 +0100
+++ b/jdk/src/share/demo/applets/SortDemo/QSortAlgorithm.java	Thu Mar 24 18:09:50 2011 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -29,8 +29,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-/*
- */
 
 /**
  * A quick sort demonstration algorithm
@@ -39,8 +37,7 @@
  * @author James Gosling
  * @author Kevin A. Smith
  */
-public class QSortAlgorithm extends SortAlgorithm
-{
+public class QSortAlgorithm extends SortAlgorithm {
 
     /**
      * A version of pause() that makes it easier to ensure that we pause
@@ -51,84 +48,83 @@
         return true;
     }
 
-   /** This is a generic version of C.A.R Hoare's Quick Sort
-    * algorithm.  This will handle arrays that are already
-    * sorted, and arrays with duplicate keys.<BR>
-    *
-    * If you think of a one dimensional array as going from
-    * the lowest index on the left to the highest index on the right
-    * then the parameters to this function are lowest index or
-    * left and highest index or right.  The first time you call
-    * this function it will be with the parameters 0, a.length - 1.
-    *
-    * @param a       an integer array
-    * @param lo0     left boundary of array partition
-    * @param hi0     right boundary of array partition
-    */
-   void QuickSort(int a[], int lo0, int hi0) throws Exception
-   {
-      int lo = lo0;
-      int hi = hi0;
-      int mid;
+    /** This is a generic version of C.A.R Hoare's Quick Sort
+     * algorithm.  This will handle arrays that are already
+     * sorted, and arrays with duplicate keys.<BR>
+     *
+     * If you think of a one dimensional array as going from
+     * the lowest index on the left to the highest index on the right
+     * then the parameters to this function are lowest index or
+     * left and highest index or right.  The first time you call
+     * this function it will be with the parameters 0, a.length - 1.
+     *
+     * @param a       an integer array
+     * @param lo0     left boundary of array partition
+     * @param hi0     right boundary of array partition
+     */
+    void QuickSort(int a[], int lo0, int hi0) throws Exception {
+        int lo = lo0;
+        int hi = hi0;
+        int mid;
 
-      if ( hi0 > lo0)
-      {
-
-         /* Arbitrarily establishing partition element as the midpoint of
-          * the array.
-          */
-         mid = a[ ( lo0 + hi0 ) / 2 ];
+        if (hi0 > lo0) {
 
-         // loop through the array until indices cross
-         while( lo <= hi )
-         {
-            /* find the first element that is greater than or equal to
-             * the partition element starting from the left Index.
-             */
-             while( ( lo < hi0 ) && pauseTrue(lo0, hi0) && ( a[lo] < mid ))
-                 ++lo;
-
-            /* find an element that is smaller than or equal to
-             * the partition element starting from the right Index.
+            /* Arbitrarily establishing partition element as the midpoint of
+             * the array.
              */
-             while( ( hi > lo0 ) && pauseTrue(lo0, hi0) && ( a[hi] > mid ))
-                 --hi;
+            mid = a[(lo0 + hi0) / 2];
+
+            // loop through the array until indices cross
+            while (lo <= hi) {
+                /* find the first element that is greater than or equal to
+                 * the partition element starting from the left Index.
+                 */
+                while ((lo < hi0) && pauseTrue(lo0, hi0) && (a[lo] < mid)) {
+                    ++lo;
+                }
 
-            // if the indexes have not crossed, swap
-            if( lo <= hi )
-            {
-               swap(a, lo, hi);
-               ++lo;
-               --hi;
+                /* find an element that is smaller than or equal to
+                 * the partition element starting from the right Index.
+                 */
+                while ((hi > lo0) && pauseTrue(lo0, hi0) && (a[hi] > mid)) {
+                    --hi;
+                }
+
+                // if the indexes have not crossed, swap
+                if (lo <= hi) {
+                    swap(a, lo, hi);
+                    ++lo;
+                    --hi;
+                }
             }
-         }
-
-         /* If the right index has not reached the left side of array
-          * must now sort the left partition.
-          */
-         if( lo0 < hi )
-            QuickSort( a, lo0, hi );
 
-         /* If the left index has not reached the right side of array
-          * must now sort the right partition.
-          */
-         if( lo < hi0 )
-            QuickSort( a, lo, hi0 );
+            /* If the right index has not reached the left side of array
+             * must now sort the left partition.
+             */
+            if (lo0 < hi) {
+                QuickSort(a, lo0, hi);
+            }
 
-      }
-   }
+            /* If the left index has not reached the right side of array
+             * must now sort the right partition.
+             */
+            if (lo < hi0) {
+                QuickSort(a, lo, hi0);
+            }
 
-   private void swap(int a[], int i, int j)
-   {
-      int T;
-      T = a[i];
-      a[i] = a[j];
-      a[j] = T;
+        }
+    }
 
-   }
+    private void swap(int a[], int i, int j) {
+        int T;
+        T = a[i];
+        a[i] = a[j];
+        a[j] = T;
 
-   public void sort(int a[]) throws Exception
-   {
-      QuickSort(a, 0, a.length - 1);
-   }
+    }
+
+    @Override
+    public void sort(int a[]) throws Exception {
+        QuickSort(a, 0, a.length - 1);
+    }
 }
--- a/jdk/src/share/demo/applets/SortDemo/SortAlgorithm.java	Thu Mar 24 18:07:40 2011 +0100
+++ b/jdk/src/share/demo/applets/SortDemo/SortAlgorithm.java	Thu Mar 24 18:09:50 2011 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -29,8 +29,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-/*
- */
 
 /**
  * A generic sort demonstration algorithm
@@ -38,13 +36,12 @@
  *
  * @author James Gosling
  */
+class SortAlgorithm {
 
-class SortAlgorithm {
     /**
      * The sort item.
      */
     private SortItem parent;
-
     /**
      * When true stop sorting.
      */
--- a/jdk/src/share/demo/applets/SortDemo/SortItem.java	Thu Mar 24 18:07:40 2011 +0100
+++ b/jdk/src/share/demo/applets/SortDemo/SortItem.java	Thu Mar 24 18:09:50 2011 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -29,57 +29,50 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-/*
- */
 
-import java.awt.*;
-import java.awt.event.*;
-import java.io.InputStream;
-import java.util.Hashtable;
-import java.net.*;
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Graphics;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
 
 /**
  * A simple applet class to demonstrate a sort algorithm.
  * You can specify a sorting algorithm using the "alg"
- * attribyte. When you click on the applet, a thread is
+ * attribute. When you click on the applet, a thread is
  * forked which animates the sorting algorithm.
  *
  * @author James Gosling
  */
-public class SortItem
-    extends java.applet.Applet
-    implements Runnable, MouseListener {
+@SuppressWarnings("serial")
+public class SortItem extends java.applet.Applet implements Runnable,
+        MouseListener {
 
     /**
      * The thread that is sorting (or null).
      */
     private Thread kicker;
-
     /**
      * The array that is being sorted.
      */
     int arr[];
-
     /**
      * The high water mark.
      */
     int h1 = -1;
-
     /**
      * The low water mark.
      */
     int h2 = -1;
-
     /**
      * The name of the algorithm.
      */
     String algName;
-
     /**
      * The sorting algorithm (or null).
      */
     SortAlgorithm algorithm;
-
     Dimension initialSize = null;
 
     /**
@@ -91,10 +84,10 @@
         double f = initialSize.width / (double) a.length;
 
         for (int i = a.length; --i >= 0;) {
-            a[i] = (int)(i * f);
+            a[i] = (int) (i * f);
         }
         for (int i = a.length; --i >= 0;) {
-            int j = (int)(i * Math.random());
+            int j = (int) (i * Math.random());
             int t = a[i];
             a[i] = a[j];
             a[j] = t;
@@ -128,12 +121,16 @@
         if (kicker != null) {
             repaint();
         }
-        try {Thread.sleep(20);} catch (InterruptedException e){}
+        try {
+            Thread.sleep(20);
+        } catch (InterruptedException e) {
+        }
     }
 
     /**
      * Initialize the applet.
      */
+    @Override
     public void init() {
         String at = getParameter("alg");
         if (at == null) {
@@ -147,6 +144,7 @@
         addMouseListener(this);
     }
 
+    @Override
     public void start() {
         h1 = h2 = -1;
         scramble();
@@ -157,6 +155,7 @@
     /**
      * Deallocate resources of applet.
      */
+    @Override
     public void destroy() {
         removeMouseListener(this);
     }
@@ -165,10 +164,11 @@
      * Paint the array of numbers as a list
      * of horizontal lines of varying lengths.
      */
+    @Override
     public void paint(Graphics g) {
         int a[] = arr;
         int y = 0;
-        int deltaY = 0, deltaX = 0, evenY = 0, evenX = 0;
+        int deltaY = 0, deltaX = 0, evenY = 0;
 
         Dimension currentSize = getSize();
         int currentHeight = currentSize.height;
@@ -184,7 +184,6 @@
         // even size.
         if (!currentSize.equals(initialSize)) {
             evenY = (currentHeight - initialSize.height) % 2;
-            evenX = (currentWidth - initialSize.width) % 2;
             deltaY = (currentHeight - initialSize.height) / 2;
             deltaX = (currentWidth - initialSize.width) / 2;
 
@@ -194,7 +193,6 @@
             }
             if (deltaX < 0) {
                 deltaX = 0;
-                evenX = 0;
             }
         }
 
@@ -227,6 +225,7 @@
     /**
      * Update without erasing the background.
      */
+    @Override
     public void update(Graphics g) {
         paint(g);
     }
@@ -238,15 +237,16 @@
      * @see java.lang.Thread#run
      * @see SortItem#mouseUp
      */
+    @Override
     public void run() {
         try {
             if (algorithm == null) {
-                algorithm = (SortAlgorithm)Class.forName(algName).newInstance();
+                algorithm = (SortAlgorithm) Class.forName(algName).newInstance();
                 algorithm.setParent(this);
             }
             algorithm.init();
             algorithm.sort(arr);
-        } catch(Exception e) {
+        } catch (Exception e) {
         }
     }
 
@@ -254,8 +254,9 @@
      * Stop the applet. Kill any sorting algorithm that
      * is still sorting.
      */
+    @Override
     public synchronized void stop() {
-        if (algorithm != null){
+        if (algorithm != null) {
             try {
                 algorithm.stop();
             } catch (IllegalThreadStateException e) {
@@ -279,35 +280,42 @@
         }
     }
 
-
+    @Override
     public void mouseClicked(MouseEvent e) {
         showStatus(getParameter("alg"));
     }
 
+    @Override
     public void mousePressed(MouseEvent e) {
     }
 
     /**
      * The user clicked in the applet. Start the clock!
      */
+    @Override
     public void mouseReleased(MouseEvent e) {
         startSort();
         e.consume();
     }
 
+    @Override
     public void mouseEntered(MouseEvent e) {
     }
 
+    @Override
     public void mouseExited(MouseEvent e) {
     }
 
+    @Override
     public String getAppletInfo() {
         return "Title: SortDemo \nAuthor: James Gosling 1.17f, 10 Apr 1995 \nA simple applet class to demonstrate a sort algorithm.  \nYou can specify a sorting algorithm using the 'alg' attribute.  \nWhen you click on the applet, a thread is forked which animates \nthe sorting algorithm.";
     }
 
+    @Override
     public String[][] getParameterInfo() {
         String[][] info = {
-          {"alg", "string", "The name of the algorithm to run.  You can choose from the provided algorithms or suppply your own, as long as the classes are runnable as threads and their names end in 'Algorithm.'  BubbleSort is the default.  Example:  Use 'QSort' to run the QSortAlgorithm class."}
+            { "alg", "string",
+                "The name of the algorithm to run.  You can choose from the provided algorithms or suppply your own, as long as the classes are runnable as threads and their names end in 'Algorithm.'  BubbleSort is the default.  Example:  Use 'QSort' to run the QSortAlgorithm class." }
         };
         return info;
     }
--- a/jdk/src/share/demo/applets/SortDemo/example1.html	Thu Mar 24 18:07:40 2011 +0100
+++ b/jdk/src/share/demo/applets/SortDemo/example1.html	Thu Mar 24 18:09:50 2011 +0100
@@ -6,7 +6,7 @@
       <h1>The Sorting Algorithm Demo (1.1)</h1>
       <hr>
 
-      <table cols=3 width="340">
+      <table width="340">
       <tr>
       <th><strong>Bubble Sort</strong></th>
       <th><strong>Bidirectional Bubble Sort</strong></th>