Assignment - Graphical Sorting Assignment

Using Applets, you are going to animate the sorting techniques we have learned in class (Bubble, Selection, Shaker, Insertion. NOT Bin Sort). This diagram is missing many of the required features, but it gives you an idea of what is required.

Here are the instructions.

  1. Starting: Open java. Cut and paste in the starter file at the bottom of this assignment. Run it to make sure that it works.
  2. Making the array: Declare the array as a global variable. Give it at least 20 elements. Make sure all values of the array have values between 1 and 100. Put the values into the array in unsorted order.
  3. Printarray basics: Create a printarray method to draw the array as rectangles on the screen. public void printarray (int a[], int size) For example if the array is {3, 56, 23, 9, 10} then you would have a little bar, followed by a big one, etc. Think of this as a bar chart representing your array. The pseudocode to go in it:
Graphics g = getGraphics();

set the x position to 50

for all of the values in the array

draw a rectangle that starts at x, 50 and goes the length of that array element and down 10 pixels.

increase x by 10 pixels (width of the bar) and 2 pixels (space between).

  1. Call the printarray method inside paint.
  2. Run your program to make sure it works.
  1. Printarray Advanced: Before you draw the array, draw a big white box to cover anything that is there.
g.setColor(Color.white);
g.fillRect(25,25,500,500);
//and reset colour to something useful
  1. Selection Sort: Go to the I:/ or the website and find the selection sort . Find the selection sort method in the program. Cut and paste the method into your program so that you can use it. Since this is no longer an void main program, take out the static.
  2. Run your code. See if it sorts.
  3. Make it sort slower: Go into the selection sort method. Before printarray is called, paste the following line:
for(int i=0; i<5000000; i++); //Waste time

This code does nothing and slows down the program so that we can see it sorthing.

  1. Reset Button: make a method that sets the array back to what it was.
    public void reset()
    {a[0]=1;
    a[1]=56; etc...... for all 20 elements.

    Call the reset method in the action method.

  1. Shaker, Insert and Bubble: Add in the code for the other sorts and adjust them as you adjusted selection sort.
  2. Formatting and Explanations: Add in colour and formatting (Panels, Fonts etc.) There should be explanations to make it clear to the user what is happening.  Example: "Bubble Sort is a n^2 sort. It slowly swaps the next highest element into place." Or maybe have labels for order and description that change depending on the sort chosen. Instead of JLabels use Labels.
  3. Comments: Add in comments, title comments, pre and posts.

Start with this file:

 

import java.awt.*;
import java.applet.*;

public class SortAnimator extends Applet
{
Button select, bubble, insert;
int size = 10;
int a [];
Button reset;

public void init ()
{
select = new Button ("selection sort");
add (select);
bubble = new Button ("bubble sort");
add (bubble);
insert = new Button ("insertion sort");
add (insert);
reset = new Button ("Reset array");
add (reset);
}


public boolean action (Event e, Object o)
{
if (e.target == select)
{
}
else if (e.target == bubble)
{
}
else if (e.target == insert)
{
}
else if (e.target == reset)
{
}
return true;
}

public void paint (Graphics g)
{
g.setColor (Color.red);

}
}

Graphical Sorting Assignment Rubric         Name: ________________

Content (Application)                /22

User-friendly

           /5

Instructions to the user. Outlines characteristics of each sort.

Basic Instructions to user.

Titles. Nicely labeled buttons.

Very basic instructions to user. Needs titles. Spelling and grammar should be checked.

Needs titles and instructions to users. Spelling and grammar should be checked.

Print array& GUI         

          /9

Border, titles, animation, etc. to make the screen look better. Fonts, colour, panels used effectively.

Bars printed to represent the array. Bars coloured. Fonts, colour, panels used.

Basic Bars printed on the screen.

Problems with outputting.

Code

           /8

Initializes array in a function, All of bubble sort, shaker sort, insert sort, selection sort. Something extra.

Doesn’t do one or more of level 4. Reset button works.

One missing of selection, insert, shaker and bubble.

Does not compile.

Style (Communication)                 /8

Comment            

            /4

3 title comments. Excellent spelling and grammar. Comments before sections.

3 title comments. Comments before most sections of code.

3 title comments. Some comments.

None.

Space

            /2

White space before major sections of code. Code is tabbed well.

Some white space/tabbing errors.

Many white space/tabbing errors.

Code is extremely difficult to read.

Pre Post                   

            /2

Very clear pre and post conditions on all functions. Array’s condition and size are always clearly stated.

Pre and post conditions on all functions.

Pre and post conditions on many functions.

Few to none pre and post conditions.