first commit
This commit is contained in:
77
CS1322/p5/AbstractSort.java
Normal file
77
CS1322/p5/AbstractSort.java
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* AbstractSort.java
|
||||
*
|
||||
* Revisions: 1.0 Nov. 04, 2002
|
||||
* Created the AbstractSort class
|
||||
* 1.1 Nov. 04, 2002
|
||||
* Finished, Compiled, Commented
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.1, Nov. 04, 2002
|
||||
*/
|
||||
|
||||
public abstract class AbstractSort {
|
||||
|
||||
/**
|
||||
*Holds the array to be sorted
|
||||
*/
|
||||
protected ArrayWrapper arrayWrap;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
public AbstractSort(ArrayWrapper arrayWrap){
|
||||
this.arrayWrap = arrayWrap;
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Modifier for arrayWrap
|
||||
*@param arrayWrap, the new value of arrayWrap
|
||||
*/
|
||||
public void setArrayWrap(ArrayWrapper arrayWrap){
|
||||
this.arrayWrap = arrayWrap;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for arrayWrap
|
||||
*@return the value of arrayWrap
|
||||
*/
|
||||
public ArrayWrapper getArrayWrapper(){
|
||||
return arrayWrap;
|
||||
}
|
||||
|
||||
/**
|
||||
*Sort arrayWrap
|
||||
*/
|
||||
public abstract void doSort();
|
||||
|
||||
/***************************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class AbstractSort.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class AbstractSort
|
||||
274
CS1322/p5/ArrayWrapper.java
Normal file
274
CS1322/p5/ArrayWrapper.java
Normal file
@@ -0,0 +1,274 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* ArrayWrapper.java
|
||||
*
|
||||
* Revisions: 1.0 Nov. 02, 2002
|
||||
* Created the ArrayWrapper class
|
||||
* 1.1 Nov. 04, 2002
|
||||
* Finished, Commented, Compiled
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.1, Nov. 04, 2002
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
import java.awt.*;
|
||||
import javax.swing.event.*;
|
||||
|
||||
|
||||
public class ArrayWrapper {
|
||||
|
||||
/**
|
||||
*compArray - Stores the Array
|
||||
*/
|
||||
private Comparable[] compArray;
|
||||
|
||||
/**
|
||||
*listeners - Stores the listeners to the array
|
||||
*/
|
||||
private Vector listeners;
|
||||
|
||||
/**
|
||||
*Sets the type of random function to use for randomArray()
|
||||
*/
|
||||
private static int RANDOM_TYPE;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
public ArrayWrapper(Comparable[] compArray){
|
||||
this(compArray,0);
|
||||
}
|
||||
|
||||
public ArrayWrapper(Comparable[] compArray, int i){
|
||||
this.compArray = compArray;
|
||||
listeners = new Vector();
|
||||
RANDOM_TYPE = i;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
//////////////////
|
||||
//Event Handling//
|
||||
//////////////////
|
||||
|
||||
/**
|
||||
*Add a ChangeListener to the listener pool
|
||||
*@param cl, the listener to add
|
||||
*/
|
||||
public void addChangeListener(ChangeListener cl){
|
||||
Enumeration eTemp = listeners.elements();
|
||||
boolean bDoAdd = true;
|
||||
|
||||
while(eTemp.hasMoreElements()){
|
||||
ChangeListener clTemp = (ChangeListener)eTemp.nextElement();
|
||||
if(clTemp.equals(cl)){
|
||||
bDoAdd = false;
|
||||
}
|
||||
}
|
||||
if(bDoAdd){
|
||||
listeners.add(cl);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*Remove a listener from the listener pool
|
||||
*@param cl, the listener to remove
|
||||
*/
|
||||
public void removeChangeListener(ChangeListener cl){
|
||||
int iCount = 0;
|
||||
Enumeration eTemp = listeners.elements();
|
||||
|
||||
while(eTemp.hasMoreElements()){
|
||||
ChangeListener clTemp = (ChangeListener)eTemp.nextElement();
|
||||
if(clTemp.equals(cl)){
|
||||
break;
|
||||
}
|
||||
else{iCount++;}
|
||||
}
|
||||
listeners.removeElementAt(iCount);
|
||||
}
|
||||
|
||||
/**
|
||||
*Fire a ChangeEvent to every listener in the pool
|
||||
*/
|
||||
private void fireChangeEvent(){
|
||||
Enumeration eTemp = listeners.elements();
|
||||
|
||||
while(eTemp.hasMoreElements()){
|
||||
((ChangeListener)eTemp.nextElement()).stateChanged(
|
||||
new ChangeEvent(this));
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////
|
||||
//Array Methods//
|
||||
/////////////////
|
||||
|
||||
/**
|
||||
*@return length of the array
|
||||
*/
|
||||
public int length(){
|
||||
return compArray.length;
|
||||
}
|
||||
|
||||
/**
|
||||
*Return the array
|
||||
*/
|
||||
public Integer[] toIntArray(){
|
||||
return (Integer[])compArray;
|
||||
}
|
||||
|
||||
/**
|
||||
*Swap the values from the two params in the array
|
||||
*@param i, the value to be put in
|
||||
*@param j
|
||||
*/
|
||||
public void swap(int i, int j){
|
||||
Comparable[] cTemp = new Comparable[2];
|
||||
try{
|
||||
cTemp[0] = compArray[i];
|
||||
cTemp[1] = compArray[j];
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println("Swap() Error\n" + e);
|
||||
}
|
||||
|
||||
compArray[j] = cTemp[0];
|
||||
compArray[i] = cTemp[1];
|
||||
fireChangeEvent();
|
||||
}
|
||||
|
||||
/**
|
||||
*@return value
|
||||
*@param i
|
||||
*/
|
||||
public Comparable get(int i){
|
||||
try{
|
||||
compArray[i] = compArray[i];
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println("Get() Error\n" + e);
|
||||
return null;
|
||||
}
|
||||
|
||||
return compArray[i];
|
||||
}
|
||||
|
||||
/**
|
||||
*Set the value
|
||||
*@param i to
|
||||
*@param value
|
||||
*/
|
||||
public void set(int i, Comparable value){
|
||||
try{
|
||||
compArray[i] = value;
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println("Set() Error\n" + e);
|
||||
}
|
||||
fireChangeEvent();
|
||||
}
|
||||
|
||||
/**
|
||||
*compare the values
|
||||
*@param i and
|
||||
*@param j
|
||||
*/
|
||||
public int compare(int i, int j){
|
||||
try{
|
||||
return compArray[i].compareTo(compArray[j]);
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println("compare() Error\n" + e);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*Create an array of Random Integers
|
||||
*@param size, the size of the array
|
||||
*@return an array of length size and unique Integer values
|
||||
*/
|
||||
public static Integer[] getRandomIntegerArray(int size){
|
||||
switch(RANDOM_TYPE){
|
||||
case 0:
|
||||
return randomIntegerArrayUno(size);
|
||||
/*case 1:
|
||||
return randomIntegerArrayDeux(size);
|
||||
break;*/
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*This uses the rather boring Random class from the API
|
||||
*to produce an Integer[] array
|
||||
*@param size, the size of the array
|
||||
*/
|
||||
public static Integer[] randomIntegerArrayUno(int size){
|
||||
Integer[] ioTemp = new Integer[size];
|
||||
Random rando = new Random();
|
||||
boolean bDone;
|
||||
|
||||
for(int i=0; i<size; i++){
|
||||
ioTemp[i] = new Integer(rando.nextInt(size));
|
||||
}
|
||||
|
||||
do{
|
||||
bDone = true;
|
||||
for(int i = 0;i<size;i++){
|
||||
for(int z=0;z<size;z++){
|
||||
if(i != z && ioTemp[i].equals(ioTemp[z])){
|
||||
bDone = false;
|
||||
ioTemp[i] = new Integer(rando.nextInt(size));
|
||||
}
|
||||
}
|
||||
}
|
||||
}while(!bDone);
|
||||
|
||||
return ioTemp;
|
||||
}
|
||||
|
||||
/********************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class ArrayWrapper.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
ArrayWrapper bob = new ArrayWrapper(new Comparable[4]);
|
||||
|
||||
Integer[] temp = getRandomIntegerArray(2000);
|
||||
for(int i=0;i<temp.length;i++){
|
||||
System.out.println(temp[i]);
|
||||
}
|
||||
for(int i = 0;i<temp.length;i++){
|
||||
for(int z=0;z<temp.length;z++){
|
||||
System.out.println("Testing " +i+"."+z);
|
||||
if(i !=z && temp[i].equals(temp[z])){
|
||||
System.out.println("Problem");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class ArrayWrapper
|
||||
78
CS1322/p5/BubbleSort.java
Normal file
78
CS1322/p5/BubbleSort.java
Normal file
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* BubbleSort.java
|
||||
*
|
||||
* Revisions: 1.0 Nov. 04, 2002
|
||||
* Created the BubbleSort class
|
||||
* 1.1 Nov. 07, 2002
|
||||
* Finished, Compiled, Commented
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.1, Nov. 07, 2002
|
||||
*/
|
||||
|
||||
public class BubbleSort extends AbstractSort{
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for BubbleSort
|
||||
*@param arrayWrap, the array to be sorted
|
||||
*/
|
||||
public BubbleSort(ArrayWrapper arrayWrap){
|
||||
super(arrayWrap);
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Sort the Array
|
||||
*/
|
||||
public void doSort(){
|
||||
for(int i=0; i<arrayWrap.length()-1; i++){
|
||||
for(int z=0; z<arrayWrap.length()-1-i; z++){
|
||||
if(arrayWrap.get(z+1).compareTo(arrayWrap.get(z))<0){
|
||||
arrayWrap.swap(z+1,z);
|
||||
}
|
||||
}
|
||||
}
|
||||
}//end doSort()
|
||||
|
||||
/*****************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class BubbleSort.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
ArrayWrapper aw = new ArrayWrapper(ArrayWrapper.getRandomIntegerArray(40));
|
||||
BubbleSort bs = new BubbleSort(aw);
|
||||
|
||||
for(int i=0;i<aw.length();i++){
|
||||
System.out.println(aw.get(i));
|
||||
}
|
||||
bs.doSort();
|
||||
System.out.println("\n/****Testing Now****/\n");
|
||||
for(int i=0;i<aw.length();i++){
|
||||
System.out.println(aw.get(i));
|
||||
}
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class BubbleSort
|
||||
137
CS1322/p5/EventHandler.java
Normal file
137
CS1322/p5/EventHandler.java
Normal file
@@ -0,0 +1,137 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* EventHandler.java
|
||||
*
|
||||
* Revisions: 1.0 Nov. 10, 2002
|
||||
* Created the EventHandler class
|
||||
* 1.1 Nov. 10, 2002
|
||||
* Compiled, Commented, Finished
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.0, Nov. 10, 2002
|
||||
*/
|
||||
|
||||
import java.awt.event.*;
|
||||
import javax.swing.event.*;
|
||||
import java.util.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class EventHandler implements ActionListener{
|
||||
|
||||
/**
|
||||
*Displays user actions to console (y/n)
|
||||
*/
|
||||
public static boolean bVerbose;
|
||||
//similar to bDebug, but doesn't interefere with the program
|
||||
|
||||
/**
|
||||
*Holds the currently selected sort
|
||||
*/
|
||||
private AbstractSort sort;
|
||||
|
||||
/**
|
||||
*ChangeListener, notified of changes
|
||||
*/
|
||||
private ChangeListener chgListener;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Default Constructor
|
||||
*@param cl, the Listener it will Handle
|
||||
*/
|
||||
public EventHandler(ChangeListener cl){
|
||||
chgListener = cl;
|
||||
bVerbose = true;
|
||||
}
|
||||
|
||||
/**
|
||||
*Constructor that is used for the P5 driver
|
||||
*sets bVerbose to false
|
||||
*/
|
||||
public EventHandler(ChangeListener cl, boolean bVerbose){
|
||||
chgListener = cl;
|
||||
this.bVerbose = bVerbose;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Handles the events taken from the ChangeListener
|
||||
*@param ae, the ActionEvent to be analyzed
|
||||
*/
|
||||
public void actionPerformed(ActionEvent ae){
|
||||
if(ae.getSource() instanceof JMenuItem){
|
||||
|
||||
if(bVerbose)System.out.print("Creating Array...");
|
||||
|
||||
ArrayWrapper aw =
|
||||
new ArrayWrapper(ArrayWrapper.getRandomIntegerArray(60));
|
||||
aw.addChangeListener(chgListener);
|
||||
if(bVerbose)System.out.println("Done.");
|
||||
|
||||
String str = ae.getActionCommand();
|
||||
|
||||
if(str.equals("Bubble Sort")){
|
||||
if(bVerbose)System.out.println("User chose Bubble Sort");
|
||||
BubbleSort bs = new BubbleSort(aw);
|
||||
sort = bs;
|
||||
}
|
||||
else if(str.equals("Insertion Sort")){
|
||||
if(bVerbose)System.out.println("User chose Insertion Sort");
|
||||
InsertionSort is = new InsertionSort(aw);
|
||||
sort = is;
|
||||
}
|
||||
else if(str.equals("Merge Sort")){
|
||||
if(bVerbose)System.out.println("User chose Merge Sort");
|
||||
MergeSort M$ = new MergeSort(aw);
|
||||
sort = M$;
|
||||
}
|
||||
else{//if(str.equals("Quick Sort"){
|
||||
if(bVerbose)System.out.println("User chose Quick Sort");
|
||||
QuickSort qs = new QuickSort(aw);
|
||||
sort = qs;
|
||||
}
|
||||
|
||||
}//end if(ae.getSource() instanceof JMenuItem)
|
||||
else if(ae.getSource() instanceof JButton){
|
||||
if(sort == null){
|
||||
if(bVerbose)
|
||||
System.out.println(
|
||||
"User attempted to start without selecting a sort");
|
||||
}
|
||||
else{
|
||||
if(bVerbose)System.out.println("User selected Begin Sort");
|
||||
sort.doSort();
|
||||
}
|
||||
}//end if(ae.getSource() instanceof JButton
|
||||
}//end actionPerformed()
|
||||
|
||||
/***********************************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class EventHandler.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class EventHandler
|
||||
80
CS1322/p5/InsertionSort.java
Normal file
80
CS1322/p5/InsertionSort.java
Normal file
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* InsertionSort.java
|
||||
*
|
||||
* Revisions: 1.0 Nov. 04, 2002
|
||||
* Created the InsertionSort class
|
||||
* 1.1 Nov. 07, 2002
|
||||
* Compiled, Commented, Finished
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.1, Nov. 07, 2002
|
||||
*/
|
||||
|
||||
public class InsertionSort extends AbstractSort{
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for InsertionSort
|
||||
*@param arrayWrap, the array to be sorted
|
||||
*/
|
||||
public InsertionSort(ArrayWrapper arrayWrap){
|
||||
super(arrayWrap);
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Sort the Array
|
||||
*/
|
||||
public void doSort(){
|
||||
for(int i=1; i<arrayWrap.length(); i++){
|
||||
Comparable cTemp = arrayWrap.get(i);
|
||||
int z=i;
|
||||
|
||||
while(z>0 && cTemp.compareTo(arrayWrap.get(z-1))<0){
|
||||
arrayWrap.swap(z,z-1);
|
||||
z--;
|
||||
}
|
||||
arrayWrap.set(z,cTemp);
|
||||
}
|
||||
}//end doSort()
|
||||
|
||||
|
||||
/**
|
||||
* Debugging main for class InsertionSort.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
ArrayWrapper aw = new ArrayWrapper(ArrayWrapper.getRandomIntegerArray(40));
|
||||
InsertionSort is = new InsertionSort(aw);
|
||||
|
||||
for(int i=0;i<aw.length();i++){
|
||||
System.out.println(aw.get(i));
|
||||
}
|
||||
is.doSort();
|
||||
System.out.println("\n/****Testing Now****/\n");
|
||||
for(int i=0;i<aw.length();i++){
|
||||
System.out.println(aw.get(i));
|
||||
}
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class InsertionSort
|
||||
122
CS1322/p5/MergeSort.java
Normal file
122
CS1322/p5/MergeSort.java
Normal file
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* MergeSort.java
|
||||
*
|
||||
* Revisions: 1.0 Nov. 04, 2002
|
||||
* Created the MergeSort class
|
||||
* 1.1 Nov. 05, 2002
|
||||
* Compiled, Finished, Commented
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.1, Nov. 04, 2002
|
||||
*/
|
||||
|
||||
public class MergeSort extends AbstractSort{
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for MergeSort
|
||||
*@param arrayWrap, the array to be sorted
|
||||
*/
|
||||
public MergeSort(ArrayWrapper arrayWrap){
|
||||
super(arrayWrap);
|
||||
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Sort the Array
|
||||
*/
|
||||
public void doSort(){
|
||||
Comparable[] tmpArray = new Comparable[arrayWrap.length()];
|
||||
for(int i=0; i<arrayWrap.length(); i++){
|
||||
tmpArray[i] = arrayWrap.get(i);
|
||||
}
|
||||
mergeSort(tmpArray,0,tmpArray.length-1);
|
||||
}//end doSort()
|
||||
|
||||
/**
|
||||
*Recursive Function to sort 2 arrays
|
||||
*/
|
||||
private void mergeSort(Comparable[] toSort, int iLeft, int iRight){
|
||||
if(iLeft < iRight){
|
||||
int center = (iLeft+iRight)/2;
|
||||
mergeSort(toSort,iLeft,center);
|
||||
mergeSort(toSort,center+1,iRight);
|
||||
merge(toSort,iLeft,center+1,iRight);
|
||||
}
|
||||
}//end mergeSort()
|
||||
|
||||
/**
|
||||
*Merge arrays
|
||||
*/
|
||||
private void merge(Comparable[] toSort, int iLeft, int iCenter, int iRight){
|
||||
int iLeftCounter = iLeft;
|
||||
int iRightCounter = iCenter;
|
||||
int iTempCounter = iLeft;
|
||||
int iLength = (iRight - iLeft) + 1;
|
||||
|
||||
|
||||
while(iLeftCounter<=(iCenter-1) && iRightCounter<= iRight){
|
||||
if(arrayWrap.get(iLeftCounter).compareTo(
|
||||
arrayWrap.get(iRightCounter))<0){
|
||||
toSort[iTempCounter++] = arrayWrap.get(iLeftCounter++);
|
||||
}
|
||||
else{//you're off your rocker if you think im gonna write the if()
|
||||
toSort[iTempCounter++] = arrayWrap.get(iRightCounter++);
|
||||
}
|
||||
}
|
||||
|
||||
while(iLeftCounter <= (iCenter-1)){
|
||||
toSort[iTempCounter++] = arrayWrap.get(iLeftCounter++);
|
||||
}
|
||||
|
||||
while(iRightCounter <= iRight){
|
||||
toSort[iTempCounter++] = arrayWrap.get(iRightCounter++);
|
||||
}
|
||||
|
||||
for(int i=0; i<iLength; i++,iRight--){
|
||||
arrayWrap.set(iRight,toSort[iRight]);
|
||||
}
|
||||
}//end merge()
|
||||
|
||||
|
||||
/**
|
||||
* Debugging main for class MergeSort.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
ArrayWrapper aw = new ArrayWrapper(ArrayWrapper.getRandomIntegerArray(40));
|
||||
MergeSort M$ = new MergeSort(aw); //ok, actually i HATE ppl who say M$
|
||||
//except T.A.'s who say it..
|
||||
|
||||
for(int i=0;i<aw.length();i++){
|
||||
System.out.println(aw.get(i));
|
||||
}
|
||||
M$.doSort();
|
||||
System.out.println("\n/****Testing Now****/\n");
|
||||
for(int i=0;i<aw.length();i++){
|
||||
System.out.println(aw.get(i));
|
||||
}
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class MergeSort
|
||||
29
CS1322/p5/P5.java
Normal file
29
CS1322/p5/P5.java
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* P5.java
|
||||
*
|
||||
* Revisions: 1.0 Nov. 10, 2002
|
||||
* Created the P5 class
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.0, Nov. 10, 2002
|
||||
*/
|
||||
|
||||
public class P5 {
|
||||
|
||||
|
||||
/**
|
||||
* Debugging main for class P5.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
SortFrame sf = new SortFrame(false);
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class P5
|
||||
549
CS1322/p5/P5.nfo.txt
Normal file
549
CS1322/p5/P5.nfo.txt
Normal file
@@ -0,0 +1,549 @@
|
||||
CS1322 -- Fall 2002 -- Program 5
|
||||
--------------------------------
|
||||
|
||||
Title: Graphical Sort Demo
|
||||
Assigned: October 30, 2002
|
||||
Phase 1 Instant Feedback Due: November 6, 2002 11:59:59
|
||||
Project Due: November 11, 2002 11:59:59
|
||||
|
||||
*** NOTE: THIS FILE IS BEST VIEWED WITH AN EDITOR THAT CONTAINS
|
||||
80 CHARACTER COLUMNS PER ROW
|
||||
|
||||
Files Provided
|
||||
========================================================================
|
||||
|
||||
o P5.nfo.txt - this file
|
||||
o SortBarPanel.class - A pluggable JPanel widget that
|
||||
displays an array of Integers
|
||||
graphically
|
||||
|
||||
Introduction
|
||||
========================================================================
|
||||
|
||||
Welcome to the fifth programming assignment. By the end of this assignment
|
||||
you should feel comfortable with several concepts, including:
|
||||
|
||||
o Graphical User Interfaces
|
||||
o Event Driven Programming
|
||||
o Model-View-Controller
|
||||
|
||||
|
||||
Project Overview
|
||||
========================================================================
|
||||
|
||||
Welcome to the 5th Programming Assignment!
|
||||
Your job for this program will be to create a Graphical User Interface
|
||||
that performs a variety of sorting algorithms on an array of Integers and
|
||||
displays the algorithms' progress using a series of bars of assorted length
|
||||
and color. There will be one bar per array index. The length and color of
|
||||
the bar will correspond to the value of the array cell. The means of drawing
|
||||
the bars is provided to you through a pluggable class that extends JPanel.
|
||||
Your GUI will need to demonstrate the following sorting algorithms:
|
||||
Bubble Sort, Insertion Sort, Quick Sort, and Merge Sort.
|
||||
|
||||
A visual guideline of what you'll be making can be found at:
|
||||
|
||||
http://128.61.63.158/cs1322/P5
|
||||
|
||||
|
||||
|
||||
PHASE I: The ArrayWrapper class - Instant Feedback
|
||||
========================================================================
|
||||
|
||||
If you liked wrapper classes like Integer, Double, etc., you'll love this
|
||||
class. Just as Integer is a wrapper for int, providing useful utilities for
|
||||
use with ints (such as converting a String to an int), the ArrayWrapper class
|
||||
will wrap an array of Comparables and provide various operations for that
|
||||
array. One of the key features your ArrayWrapper class will provide is the
|
||||
ability to notify other classes if the array inside the ArrayWrapper has been
|
||||
altered. This is our segway into Events and Event Driven Programming.
|
||||
|
||||
Events
|
||||
-------------------------------
|
||||
|
||||
The Object Oriented features of Java provide a unique way for classes to
|
||||
communicate with each other (besides calling methods on each other). Special
|
||||
classes called "Events" can be sent between other classes. Java provides a
|
||||
series of Event classes as part of its API that can transmit a variety of
|
||||
different signals including "the mouse was clicked" (MouseEvent), "a key was
|
||||
pressed" (KeyEvent), "something has changed" (ChangeEvent), as well as others.
|
||||
You can also code your own Events which can signal whatever you wish depending
|
||||
on what's appropriate to the problem you are working on. Associated with each
|
||||
Event is a Listener interface. So MouseListener corresponds to MouseEvent.
|
||||
Any class that wants to receive an Event must implement the corresponding
|
||||
Listener interface. When a class implements MouseListener, it must contain
|
||||
the following methods: mouseClicked(MouseEvent e), mouseEntered(MouseEvent e),
|
||||
mouseExited(MouseEvent e), mousePressed(MouseEvent e), and
|
||||
mouseReleased(MouseEvent e). This methods are called by the object where the
|
||||
event originated, usually from a button or clickable component in the case
|
||||
of MouseEvents. We say the event was "fired" which basically means the
|
||||
firing component instantiated a new MouseEvent, iterated through all of the
|
||||
listeners registered to the component and called the appropriate listener
|
||||
method on each listener, passing it the instantiated MouseEvent. In the
|
||||
case of MouseEvents being fired by an button, for example, you typically do
|
||||
not code the firing mechanism. This is provided for you in the widget
|
||||
(button) code that already exists. You merely handle the listening end.
|
||||
There are cases (this assignment is one of them) where you will handle
|
||||
the registering of listeners and firing of events yourself.
|
||||
|
||||
So what does it mean to register a listener? In the case of MouseListeners,
|
||||
anything that fires MouseEvents has two methods:
|
||||
addMouseListener(MouseListener ml), and removeMouseListener(MouseListener ml).
|
||||
When you execute a line of code such as:
|
||||
|
||||
<Firing Object>.addMouseListener(<object implementing MouseListener>);
|
||||
|
||||
the objects are communitcating like so:
|
||||
|
||||
Calling Object: "Hey Firing Object, I want you to notify this here
|
||||
MouseListener whenever you fire a MouseEvent."
|
||||
Firing Object: "Okay, no problem. I'll add it to my Vector of objects
|
||||
to notify."
|
||||
|
||||
One of the five appropriate MouseListener methods is then called on all
|
||||
registered listeners whenever needed.
|
||||
|
||||
-------------------------------
|
||||
|
||||
|
||||
Create a class called "ArrayWrapper". This class will contain an
|
||||
array of Comparables and provide some utilities for manipulating this
|
||||
array. Your class should have the following:
|
||||
|
||||
o a private instance variable named "compArray" of type
|
||||
Comparable[]
|
||||
o a private instance variable named "listeners" of type Vector.
|
||||
This variable will hold all the listeners of this ArrayWrapper to
|
||||
be notified if the array changes.
|
||||
o a constructor that takes in an array of Comparables and sets
|
||||
the compArray instance variable to this parameter
|
||||
|
||||
|
||||
Your ArrayWrapper class will be firing a ChangeEvent every time the
|
||||
compArray variable is altered. A ChangeEvent is an extremely simple
|
||||
event. It connotates a very generic meaning: "Something has
|
||||
changed."
|
||||
|
||||
o a public method called addChangeListener(ChangeListener cl) which
|
||||
will register the passed in listener to be notified of
|
||||
ChangeEvents. It should only register the listener if it has not
|
||||
already been registered. (We don't want any listener being
|
||||
notified twice at the same time.)
|
||||
|
||||
o a public method called removeChangeListener(ChangeListener cl)
|
||||
which will unregister this listener from this ArrayWrapper. That
|
||||
listener should no longer be notified when firing ChangeEvents.
|
||||
|
||||
o a private method called fireChangeEvent() which notify all
|
||||
registered listeners by calling the appropriate method. Check
|
||||
out the API to see what the method is.
|
||||
|
||||
|
||||
Your class should provide several utilities for use with arrays.
|
||||
|
||||
o a public method called swap(int i, int j) which will swap
|
||||
the contents of the array at index i and index j. If an
|
||||
exception is thrown, this method should print an appropriate
|
||||
error message and make no modifications to the array.
|
||||
|
||||
o a public method called get(int i) which returns a Comparable
|
||||
that returns the contents of the array at index i. If an
|
||||
exception is thrown this method should print an appropriate error
|
||||
message and return null.
|
||||
|
||||
o a public method called set(int i, Comparable value) which
|
||||
sets the contents of the array. If an exception is thrown, this
|
||||
method should print an appropriate error message and make no
|
||||
modification to the array.
|
||||
|
||||
o a public method called compare(int i, int j) which will return
|
||||
the result of a compareTo operation on the contents at index i
|
||||
and index j. If an exception is thrown, this method should print
|
||||
an appropriate error message and return 0.
|
||||
|
||||
o a public static method called getRandomIntegerArray(int size)
|
||||
and returns Integer[]. This method will create an array of the
|
||||
size specified and assign Integers with random values between 0
|
||||
and (size - 1) to the contents of the array. These values should
|
||||
be exclusive so any one value should not appear twice in the
|
||||
array. (This random array is what you'll be sorting)
|
||||
|
||||
|
||||
Don't forget: Any time compArray is altered, you will need to notify
|
||||
all listeners. Keep this in mind when coding the above methods.
|
||||
|
||||
|
||||
PHASE II: The Sorting Algorithms - Instant Feedback
|
||||
========================================================================
|
||||
|
||||
You will be creating four sorting algorithms that can sort the contents
|
||||
of an ArrayWrapper: BubbleSort, InsertionSort, QuickSort, and MergeSort.
|
||||
For each sort, you must adhere to the algorithm or you receive no credit.
|
||||
|
||||
All the sorting algorithms will be placed in their own class with the
|
||||
appropriate name: BubbleSort, InsertionSort, QuickSort, and MergeSort.
|
||||
Since they are all going to be sorting and they are all going to be sorting
|
||||
an ArrayWrapper, this seems like an idea case for the use of an abstract
|
||||
class.
|
||||
|
||||
Create an abstract class called AbstractSort. This class will be a
|
||||
parent class for all your sorting classes. Include the following in
|
||||
AbstractSort:
|
||||
|
||||
o a protected instance variable called arrayWrap of type
|
||||
ArrayWrapper. This will be the ArrayWrapper instance containing
|
||||
the array you are sorting.
|
||||
|
||||
o a constructor which takes in an ArrayWrapper parameter and sets
|
||||
arrayWrap to this parameter.
|
||||
o accessor and modifier for arrayWrap
|
||||
o a public abstract method called doSort() which returns void.
|
||||
This method will be specified in the subclasses and will actually
|
||||
perform the respective sorting algorithm on the arrayWrap
|
||||
instance variable.
|
||||
|
||||
|
||||
Create a class called BubbleSort which extends AbstractSort. This
|
||||
class will sort (in ascending order) the contents of an ArrayWrapper
|
||||
instance using bubble sort. BubbleSort should include the following:
|
||||
|
||||
o a constructor which takes in an ArrayWrapper parameter and
|
||||
chains to the respective parent constructor.
|
||||
o a public method called doSort() which will sort the array of
|
||||
Comparables inside arrayWrap using the bubble sort algorithm.
|
||||
You may only manipulate the array of Comparables through the
|
||||
methods provided by ArrayWrapper. We do this so that when the
|
||||
array is changed, arrayWrap can fire events to notify its
|
||||
listeners that it has changed. The algorithm should sort in
|
||||
ascending order.
|
||||
|
||||
Create a class called InsertionSort which extends AbstractSort. This
|
||||
class will sort (in ascending order) the contents of an ArrayWrapper
|
||||
instance using insertion sort. InsertionSort should include the following:
|
||||
|
||||
o a constructor which takes in an ArrayWrapper parameter and
|
||||
chains to the respective parent constructor.
|
||||
o a public method called doSort() which will sort the array of
|
||||
Comparables inside arrayWrap using the insertion sort algorithm.
|
||||
You may only manipulate the array of Comparables through the
|
||||
methods provided by ArrayWrapper. We do this so that when the
|
||||
array is changed, arrayWrap can fire events to notify its
|
||||
listeners that it has changed. The algorithm should sort in
|
||||
ascending order.
|
||||
|
||||
Create a class called MergeSort which extends AbstractSort. This
|
||||
class will sort (in ascending order) the contents of an ArrayWrapper
|
||||
instance using merge sort. MergeSort should include the following:
|
||||
|
||||
o a constructor which takes in an ArrayWrapper parameter and
|
||||
chains to the respective parent constructor.
|
||||
o a public method called doSort() which will sort the array of
|
||||
Comparables inside arrayWrap using the merge sort algorithm.
|
||||
You may only manipulate the array of Comparables through the
|
||||
methods provided by ArrayWrapper. We do this so that when the
|
||||
array is changed, arrayWrap can fire events to notify its
|
||||
listeners that it has changed. The algorithm should sort in
|
||||
ascending order.
|
||||
|
||||
Create a class called QuickSort which extends AbstractSort. This
|
||||
class will sort (in ascending order) the contents of an ArrayWrapper
|
||||
instance using quick sort. QuickSort should include the following:
|
||||
|
||||
o a constructor which takes in an ArrayWrapper parameter and
|
||||
chains to the respective parent constructor.
|
||||
o a public method called doSort() which will sort the array of
|
||||
Comparables inside arrayWrap using the quick sort algorithm.
|
||||
You may only manipulate the array of Comparables through the
|
||||
methods provided by ArrayWrapper. We do this so that when the
|
||||
array is changed, arrayWrap can fire events to notify its
|
||||
listeners that it has changed. The algorithm should sort in
|
||||
ascending order.
|
||||
|
||||
|
||||
|
||||
PHASE III - The GUI
|
||||
========================================================================
|
||||
|
||||
You will need to create a Graphical User Interface (GUI) to interact
|
||||
with this program. This is radically different from the typical way
|
||||
we've interfaced with programs - text menus and prompts in a console.
|
||||
A Graphical User Interface utilizes buttons, mouse clicks, drop down
|
||||
menus, window widgets, etc to receive and display information to a
|
||||
user. See the following for all your GUI learning needs:
|
||||
|
||||
http://www.cc.gatech.edu/classes/AY2003/cs1322_fall/
|
||||
test_resources/current/study.html#GUIpieces
|
||||
|
||||
------------------------------------------------
|
||||
|
||||
Provided to you is a class called SortBarPanel which extends JPanel.
|
||||
This widget can display an array of sixty Integers (the wrapper class Integer)
|
||||
graphically as a horizontal bar graph. The length of each bar
|
||||
corresponds to the value inside the array. When sorted ascendingly,
|
||||
this bar graph will look like stair steps. This class has a default
|
||||
constructor and a method:
|
||||
|
||||
public void display(Integer[] array)
|
||||
|
||||
which redraws the bar graph according to the array parameter.
|
||||
|
||||
------------------------------------------------
|
||||
|
||||
You will need a JFrame to display contain and display all the GUI
|
||||
components. Create a class called "SortFrame" that extends JFrame.
|
||||
This class will extend JFrame because it "is-a" JFrame. It will have
|
||||
a "has-a" relationship to all the components to be contained within
|
||||
the JFrame. This means that these components will be instance
|
||||
variables of SortFrame. The components of SortFrame will incude a
|
||||
JMenuBar containing a menu, a JMenu containing the four menu items for
|
||||
the four sorts, four JMenuItems for the selection of each of the four
|
||||
sorting algorithms, a SortBarPanel instance for displaying the status
|
||||
of our sorting array graphically, and a "start" JButton to initiate
|
||||
the sort of a random array. This class will be responsible
|
||||
for redisplaying the sorting array by means of a SortBarPanel
|
||||
instance. In order to do that, the SortFrame instance needs to be
|
||||
notified everytime the array changes. This means SortFrame will have
|
||||
to be a listener of ChangeEvents fired from the ArrayWrapper
|
||||
containing the array being sorted. In order to be notified of these
|
||||
changes, SortFrame must implement ChangeListener.
|
||||
|
||||
NOTE: Regarding JFrames: you cannot simply "add" components to the
|
||||
JFrame container. there is actually another widget that accompanies
|
||||
all JFrames called the content Pane. You must set the layout of this
|
||||
component and add widgets to this component. The content pane of any
|
||||
JFrame can be accessed via the method "getContentPane()". If you try
|
||||
to add stuff to the JFrame directly, your code won't work.
|
||||
|
||||
NOTE: One thing to keep in mind when coding GUIs is that there are
|
||||
lots of little yet simple methods to these GUI components. If you are
|
||||
not used to scouring the API for solutions to your problem, now is the
|
||||
time to start. In most cases, the means of accomplishing any basic
|
||||
GUI task such as setting up your menus or setting up a layout is right
|
||||
under your nose in the API. You just need to spend a little time
|
||||
searching. If the solution to a task doesn't immidiately come to mind
|
||||
as you read below, that's to be expected. But be sure you make an
|
||||
honest effort to search for the solution on your own before you
|
||||
contact the newsgroups or email your TA. It's the best way to learn.
|
||||
|
||||
Your SortFrame class should include the following:
|
||||
|
||||
o a private instance variable called "barPanel" of type
|
||||
SortBarPanel. This will be the JPanel displaying the array.
|
||||
o a private instance variable called "startButton" of type
|
||||
JButton. This button will initiate the sort of a random array.
|
||||
o a private instance variable called menuBar of type JMenuBar.
|
||||
This variable will be the drop-down menu bar for the frame.
|
||||
o a private instance variable called sortMenu of type JMenu.
|
||||
This menu will be the only menu on the menu bar.
|
||||
o four private instance variables named "bubbleItem",
|
||||
"insertionItem", "quickItem", "mergeItem" of type JMenuItem.
|
||||
|
||||
o a public method called stateChanged which takes in a
|
||||
ChangeEvent and returns void. This is the method you are
|
||||
required to specify as a result of implementing ChangeListener.
|
||||
This method should be called when something (an ArrayWrapper
|
||||
perhaps?) fires a ChangeEvent. If this method is called by an
|
||||
ArrayWrapper, that means the array inside the ArrayWrapper must
|
||||
have changed somehow. You should redisplay this somehow altered
|
||||
array using your SortBarPanel instance. The ChangeEvent
|
||||
parameter isn't needed inside this method in this particular case.
|
||||
|
||||
o a default constructor that does the following:
|
||||
- instantiates the super class and places the phrase "Sort
|
||||
Demo" in the title bar of the frame.
|
||||
- sets the layout of the content pane of the frame to be a
|
||||
BorderLayout.
|
||||
- creates all the menu Items and adds them to the sort menu.
|
||||
Each JMenuItem's label should be "Bubble Sort", "Insertion
|
||||
Sort", ... etc, respectively.
|
||||
|
||||
- adds the sort menu to the menu bar. The sort menu's label
|
||||
should be "Sorts".
|
||||
- adds the menu bar to the JFrame.
|
||||
- creates the SortBarPanel instance and adds it to the CENTER
|
||||
of the layout.
|
||||
- creates the start button and adds it to the SOUTH of the
|
||||
layout. The button label should be "Start".
|
||||
- sets the size of the JFrame to be 600x500.
|
||||
- makes it so the user cannot resize the frame.
|
||||
- makes the default close operation for this JFrame to
|
||||
exit on close which will cause the program to exit when we
|
||||
close the frame using the mouse. If we don't do this, when
|
||||
we click the X at the top right, the frame will disappear but
|
||||
the JVM will still be running. If it isn't immidiately
|
||||
apparent how to accomplish the above, read the API. This bullet
|
||||
is teeming with hints.
|
||||
- makes the frame visible.
|
||||
|
||||
NOTE: You are allowed and strongly encouraged to abstract the
|
||||
above functionality to private helper methods.
|
||||
|
||||
Don't let this class wander off too far. We will be coming back to it
|
||||
in the next phase. As it stands now, SortFrame should nicely display
|
||||
a frame with a single menu that says "Sorts". When you click the
|
||||
menu, the four Sorting Menu Items should be displayed. These menu
|
||||
items won't do anything if you click them. In the center should be a
|
||||
big white area that is the blank SortBarPanel. At the bottom should
|
||||
be a button with the word "Start" in the middle. This button should
|
||||
not yet do anything when clicked. The program should exit if you
|
||||
click the X in the top right. Be sure to check out the website if you
|
||||
can't quite visualize it.
|
||||
|
||||
|
||||
|
||||
PHASE IV - Event Handling
|
||||
========================================================================
|
||||
|
||||
Thus far, we have a nice pretty GUI that doesn't do squat. The menus
|
||||
don't work and neither do the buttons. We need an event handler to
|
||||
receive these clicks and then perform an action. See the following
|
||||
for all your event handling needs:
|
||||
|
||||
http://www.cc.gatech.edu/classes/AY2003/cs1322_fall/
|
||||
test_resources/current/study.html#GUIEvents
|
||||
|
||||
We will encapsulate our event handling functionality in another
|
||||
class. Create a class called "EventHandler". This class will be
|
||||
responsible for listening for events fired from the JButton and
|
||||
JMenuItems. (You don't have to worry about firing these events, only
|
||||
receiving them) Since JButtons and JMenuItems fire ActionEvents, you
|
||||
will need to have EventHandler implement ActionListener. EventHandler
|
||||
should have the following:
|
||||
|
||||
o a private instance variable called "sort" that is of type
|
||||
AbstractSort. This variable will be one of the four sorting
|
||||
subclasses. It holds the currently loaded sort.
|
||||
o a private instance variable called "chgListener" of type
|
||||
ChangeListener. This will be the ChangeListener that needs to be
|
||||
notified of any changes to the array being sorted.
|
||||
|
||||
o a public constructor that takes in a ChangeListener and assigns
|
||||
this parameter to the instance variable chgListener.
|
||||
o a public method called "actionPerformed" that takes in an
|
||||
ActionEvent instance. This method is required when implementing
|
||||
ActionListener. It is this method that will be called when the
|
||||
JMenuItems are selected or the start button clicked. When the
|
||||
JMenuItems are selected we want to display a random array of
|
||||
sixty integers in the SortBarPanel. When the start button is
|
||||
clicked, we want to start sorting the array. But actionPerformed
|
||||
is called for both the JMenuItems and the JButton. So how do we
|
||||
know which was the one that actually was clicked by the user?
|
||||
This is where the ActionEvent parameter comes in handy. This
|
||||
parameter contains all kinds of good information. Check out the
|
||||
method "getSource()" in the class EventObject (a parent of
|
||||
ActionEvent). This method returns the firing Object. So in this
|
||||
case, getSource() will always return startButton or one of the
|
||||
four JMenuItems. (How we get startButton and the sort JMenuItems
|
||||
to direct their firings to EventHandler will be discussed in a
|
||||
minute. Just go with it for now.) We can then use instanceof to
|
||||
figure out whether or not the source is one of the four
|
||||
JMenuItems or the startButton.
|
||||
|
||||
------
|
||||
|
||||
If the source was one of the sort JMenuItems, we want to create a
|
||||
new random array, put it inside an ArrayWrapper to be sorted by
|
||||
the sort chosen and display this array in our GUI.
|
||||
|
||||
- First create a new ArrayWrapper from a random array of
|
||||
Integers. You coded a method that gave you a random array of a
|
||||
given size and a constructor for ArrayWrapper that took in any
|
||||
array to be wrapped. Bingo. Just make sure the size of the
|
||||
random array you create is size 60.
|
||||
|
||||
- Now we want to be sure that whenever a change is made to the
|
||||
array in our ArrayWrapper, the appropriate ChangeListener knows
|
||||
about it. Our EventHandler constructor took in a ChangeListener
|
||||
(which will be the SortFrame). We now want to
|
||||
ask the ArrayWrapper to notify chgListener whenever the array is
|
||||
changed. We can do this by telling the ArrayWrapper to add
|
||||
chgListener to it's list of ChangeListeners to fire events to.
|
||||
|
||||
- But we now have another problem. How do we figure out which of
|
||||
the four sorts was chosen. Look at the method
|
||||
"getActionCommand()" in ActionEvent. This method returns the
|
||||
label on the JMenuItem that fired the event. ("Bubble Sort",
|
||||
"Insertion Sort", etc.) We can use this information to determine
|
||||
which of the four sorts was chosen. Once we figure this out, we
|
||||
create a new instance of the appropriate Sort class, passing in
|
||||
the ArrayWrapper to sort, and set this instance to the "sort"
|
||||
instance variable.
|
||||
- We now need to have our SortFrame update the blank SortBarPanel
|
||||
to display this random array contained in ArrayWrapper.
|
||||
|
||||
-----
|
||||
|
||||
If the startButton was the source of the event, first make sure
|
||||
that "sort" is not null. If the user clicks the start button
|
||||
before choosing a sorting algorithm, "sort" will be null. we
|
||||
don't want to cause a NullPointerException. If it's not null,
|
||||
we know the user just clicked a JMenuItem and "sort" has a random
|
||||
array in it ready to go. We now call the Sorting class's
|
||||
"doSort()" method.
|
||||
|
||||
|
||||
------------------------------------------------
|
||||
|
||||
Back to SortFrame:
|
||||
|
||||
Now, in EventHandler, we have something that can process the menu
|
||||
selections and the button clicks from the user. But we need to
|
||||
connect the wires, so to speak, between the button/menu items to the
|
||||
EventHandler. In ArrayWrapper, you coded a method called
|
||||
addChangeListener which took in a ChangeListener to add to the list of
|
||||
Listeners to notify. JButton and JMenuItem have a similar method
|
||||
called "addActionListener". This method takes in an ActionListener.
|
||||
Any time the button is clicked or the JMenuItem is selected, the
|
||||
component fires an ActionEvent to all its registered listeners. Lo
|
||||
and behold, EventHandler implements ActionListener! Add the following
|
||||
to your SortFrame class:
|
||||
|
||||
o a private instance variable called "eh" of type EventHandler.
|
||||
o Just after instantiating your JButton/JMenuItems, add "eh" to
|
||||
these components' list of ActionListeners to notify. Now when a
|
||||
user clicks these components, an ActionEvent will be fired to
|
||||
"eh" where the actionPerformed method will determine what
|
||||
component fired the event and take appropriate action.
|
||||
|
||||
|
||||
|
||||
PHASE V - Testing and Turnin
|
||||
========================================================================
|
||||
|
||||
Create a class called P5 with a main method which instantiates SortFrame.
|
||||
|
||||
Test your code thoroughly. Some things:
|
||||
|
||||
- Swing GUI components can sometimes throw exceptions without the
|
||||
program crashing. i.e. you may see a stack trace on the
|
||||
console but amazingly the program still appears to be
|
||||
functioning. We consider this no different than if the program
|
||||
crashed. You should not have any stack traces from exceptions
|
||||
of any kind. You should catch and handle all exceptions.
|
||||
|
||||
- You may notice that the start button seems to jam while the
|
||||
sorting is going on. Also you may notice you can't seem to
|
||||
close the JFrame or use the Sorts Menu. Don't worry about
|
||||
this. While this would not be ideal for real software, the
|
||||
means around this inconvinience is not within the realm of this
|
||||
course.
|
||||
|
||||
- Alas, we can't allow you to change the speed of the sort. This
|
||||
has been fixed in SortBarPanel.
|
||||
|
||||
Turn in all your java source files. You do not need to turn in the
|
||||
nfo file, SortBarPanel.class or SortBarPanel.html
|
||||
|
||||
Remove all debug prints before submitting. Have fun. Good Luck.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
13
CS1322/p5/P5_RUN_BIG.java
Normal file
13
CS1322/p5/P5_RUN_BIG.java
Normal file
@@ -0,0 +1,13 @@
|
||||
public class P5_RUN_BIG{
|
||||
private static void printArray(Integer[] t){
|
||||
for(int i=0; i<t.length; i++){
|
||||
System.out.print(t[i] + ","+ ((i%10)==0?"\n":""));
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[]){
|
||||
QuickSort qs = new QuickSort(new ArrayWrapper(ArrayWrapper.getRandomIntegerArray(10000)));
|
||||
qs.doSort();
|
||||
P5_RUN_BIG.printArray(qs.getArrayWrapper().toIntArray());
|
||||
}
|
||||
}
|
||||
111
CS1322/p5/QuickSort.java
Normal file
111
CS1322/p5/QuickSort.java
Normal file
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* QuickSort.java
|
||||
*
|
||||
* Revisions: 1.0 Nov. 04, 2002
|
||||
* Created the QuickSort class
|
||||
* 1.1 Nov. 09, 2002
|
||||
* Compiled, Finished, Commented
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.1, Nov. 04, 2002
|
||||
*/
|
||||
|
||||
public class QuickSort extends AbstractSort{
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for QuickSort
|
||||
*@param arrayWrap, the array to be sorted
|
||||
*/
|
||||
public QuickSort(ArrayWrapper arrayWrap){
|
||||
super(arrayWrap);
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Sort the Array
|
||||
*/
|
||||
public void doSort(){
|
||||
quickSort(0,arrayWrap.length()-1);
|
||||
}
|
||||
|
||||
/**
|
||||
*Recursive quicksort
|
||||
*/
|
||||
private void quickSort(int iStart, int iEnd){
|
||||
/*
|
||||
*Algorithm uses some converted C++ code that I learned back
|
||||
*I originally learned this a little over a year ago
|
||||
*it may be similar to some code available over the net or other books
|
||||
*however, I have NO CLUE as to where it came from, as I said I learned
|
||||
*it from my teacher and understand it fully
|
||||
*/
|
||||
|
||||
int iL=iStart, iR=iEnd;
|
||||
Comparable cTemp;
|
||||
Comparable cPivot=arrayWrap.get((iStart+iEnd)/2);
|
||||
|
||||
do
|
||||
{
|
||||
for(;arrayWrap.get(iL).compareTo(cPivot)<0;iL++);
|
||||
for(;arrayWrap.get(iR).compareTo(cPivot)>0;iR--);
|
||||
|
||||
if (iL<=iR)
|
||||
{
|
||||
cTemp = arrayWrap.get(iL);
|
||||
arrayWrap.swap(iL,iR);
|
||||
arrayWrap.set(iR,cTemp);
|
||||
iL++;
|
||||
iR--;
|
||||
}
|
||||
}while(iL<=iR);
|
||||
|
||||
if (iStart<iR){
|
||||
quickSort(iStart, iR);
|
||||
}
|
||||
if (iL<iEnd){
|
||||
quickSort(iL, iEnd);
|
||||
}
|
||||
}//end quickSort()
|
||||
|
||||
/***********************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class QuickSort.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
ArrayWrapper aw = new ArrayWrapper(ArrayWrapper.getRandomIntegerArray(40));
|
||||
QuickSort qs = new QuickSort(aw);
|
||||
|
||||
for(int i=0;i<aw.length();i++){
|
||||
System.out.println(aw.get(i));
|
||||
}
|
||||
qs.doSort();
|
||||
System.out.println("\n/****Testing Now****/\n");
|
||||
for(int i=0;i<aw.length();i++){
|
||||
System.out.println(aw.get(i));
|
||||
}
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class QuickSort
|
||||
342
CS1322/p5/SortBarPanel.html
Normal file
342
CS1322/p5/SortBarPanel.html
Normal file
@@ -0,0 +1,342 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN""http://www.w3.org/TR/REC-html40/frameset.dtd">
|
||||
<!--NewPage-->
|
||||
<HTML>
|
||||
<HEAD>
|
||||
<!-- Generated by javadoc on Wed Oct 30 21:34:30 EST 2002 -->
|
||||
<TITLE>
|
||||
: Class SortBarPanel
|
||||
</TITLE>
|
||||
<LINK REL ="stylesheet" TYPE="text/css" HREF="stylesheet.css" TITLE="Style">
|
||||
</HEAD>
|
||||
<BODY BGCOLOR="white">
|
||||
|
||||
<!-- ========== START OF NAVBAR ========== -->
|
||||
<A NAME="navbar_top"><!-- --></A>
|
||||
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0">
|
||||
<TR>
|
||||
<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
|
||||
<A NAME="navbar_top_firstrow"><!-- --></A>
|
||||
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3">
|
||||
<TR ALIGN="center" VALIGN="top">
|
||||
<TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD>
|
||||
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="overview-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD>
|
||||
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD>
|
||||
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD>
|
||||
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
</TD>
|
||||
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
|
||||
</EM>
|
||||
</TD>
|
||||
</TR>
|
||||
|
||||
<TR>
|
||||
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
|
||||
PREV CLASS
|
||||
NEXT CLASS</FONT></TD>
|
||||
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
|
||||
<A HREF="index.html" TARGET="_top"><B>FRAMES</B></A>
|
||||
<A HREF="SortBarPanel.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD>
|
||||
</TR>
|
||||
<TR>
|
||||
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
|
||||
SUMMARY: <A HREF="#inner_classes_inherited_from_class_javax.swing.JPanel">INNER</A> | <A HREF="#fields_inherited_from_class_javax.swing.JComponent">FIELD</A> | <A HREF="#constructor_summary">CONSTR</A> | <A HREF="#method_summary">METHOD</A></FONT></TD>
|
||||
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
|
||||
DETAIL: FIELD | <A HREF="#constructor_detail">CONSTR</A> | <A HREF="#method_detail">METHOD</A></FONT></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
<!-- =========== END OF NAVBAR =========== -->
|
||||
|
||||
<HR>
|
||||
<!-- ======== START OF CLASS DATA ======== -->
|
||||
<H2>
|
||||
Class SortBarPanel</H2>
|
||||
<PRE>
|
||||
java.lang.Object
|
||||
|
|
||||
+--java.awt.Component
|
||||
|
|
||||
+--java.awt.Container
|
||||
|
|
||||
+--javax.swing.JComponent
|
||||
|
|
||||
+--javax.swing.JPanel
|
||||
|
|
||||
+--<B>SortBarPanel</B>
|
||||
</PRE>
|
||||
<DL>
|
||||
<DT><B>All Implemented Interfaces:</B> <DD>javax.accessibility.Accessible, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable</DD>
|
||||
</DL>
|
||||
<HR>
|
||||
<DL>
|
||||
<DT>public class <B>SortBarPanel</B><DT>extends javax.swing.JPanel</DL>
|
||||
|
||||
<P>
|
||||
This GUI widget displays an array of exactly 60 Integer instances as
|
||||
a horizontal bar graph. The length of the nth bar displayed corresponds
|
||||
to the value of the Integer array at index n-1. (The first bar corresponds
|
||||
to the value at index zero.)
|
||||
<BR><BR>
|
||||
<P>
|
||||
<DL>
|
||||
<DT><B>See Also: </B><DD><A HREF="serialized-form.html#SortBarPanel">Serialized Form</A></DL>
|
||||
<HR>
|
||||
|
||||
<P>
|
||||
<!-- ======== INNER CLASS SUMMARY ======== -->
|
||||
|
||||
<A NAME="inner_classes_inherited_from_class_javax.swing.JPanel"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
|
||||
<TD><B>Inner classes inherited from class javax.swing.JPanel</B></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD><CODE>javax.swing.JPanel.AccessibleJPanel</CODE></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
<A NAME="inner_classes_inherited_from_class_javax.swing.JComponent"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
|
||||
<TD><B>Inner classes inherited from class javax.swing.JComponent</B></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD><CODE>javax.swing.JComponent.AccessibleJComponent</CODE></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
<A NAME="inner_classes_inherited_from_class_java.awt.Container"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
|
||||
<TD><B>Inner classes inherited from class java.awt.Container</B></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD><CODE>java.awt.Container.AccessibleAWTContainer</CODE></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
<A NAME="inner_classes_inherited_from_class_java.awt.Component"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
|
||||
<TD><B>Inner classes inherited from class java.awt.Component</B></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD><CODE>java.awt.Component.AccessibleAWTComponent</CODE></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
|
||||
<!-- =========== FIELD SUMMARY =========== -->
|
||||
|
||||
<A NAME="fields_inherited_from_class_javax.swing.JComponent"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
|
||||
<TD><B>Fields inherited from class javax.swing.JComponent</B></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD><CODE>accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW</CODE></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
<A NAME="fields_inherited_from_class_java.awt.Component"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
|
||||
<TD><B>Fields inherited from class java.awt.Component</B></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD><CODE>BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT</CODE></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
<A NAME="fields_inherited_from_class_java.awt.image.ImageObserver"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
|
||||
<TD><B>Fields inherited from interface java.awt.image.ImageObserver</B></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD><CODE>ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH</CODE></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
|
||||
<!-- ======== CONSTRUCTOR SUMMARY ======== -->
|
||||
|
||||
<A NAME="constructor_summary"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
|
||||
<TD COLSPAN=2><FONT SIZE="+2">
|
||||
<B>Constructor Summary</B></FONT></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD><CODE><B><A HREF="SortBarPanel.html#SortBarPanel()">SortBarPanel</A></B>()</CODE>
|
||||
|
||||
<BR>
|
||||
Defualt Constructor: Instantiates a SortBarPanel</TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
|
||||
<!-- ========== METHOD SUMMARY =========== -->
|
||||
|
||||
<A NAME="method_summary"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
|
||||
<TD COLSPAN=2><FONT SIZE="+2">
|
||||
<B>Method Summary</B></FONT></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
|
||||
<CODE> void</CODE></FONT></TD>
|
||||
<TD><CODE><B><A HREF="SortBarPanel.html#display(java.lang.Integer[])">display</A></B>(java.lang.Integer[] array)</CODE>
|
||||
|
||||
<BR>
|
||||
Draws an array of sixty Integers as a horizontal bar graph.</TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
|
||||
<CODE>protected void</CODE></FONT></TD>
|
||||
<TD><CODE><B><A HREF="SortBarPanel.html#paintComponent(java.awt.Graphics)">paintComponent</A></B>(java.awt.Graphics g)</CODE>
|
||||
|
||||
<BR>
|
||||
</TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
<A NAME="methods_inherited_from_class_javax.swing.JPanel"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
|
||||
<TD><B>Methods inherited from class javax.swing.JPanel</B></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD><CODE>getAccessibleContext, getUIClassID, paramString, updateUI</CODE></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
<A NAME="methods_inherited_from_class_javax.swing.JComponent"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
|
||||
<TD><B>Methods inherited from class javax.swing.JComponent</B></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD><CODE>addAncestorListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getConditionForKeyStroke, getDebugGraphicsOptions, getGraphics, getHeight, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getVerifyInputWhenFocusTarget, getVisibleRect, getWidth, getX, getY, grabFocus, hasFocus, hide, isDoubleBuffered, isFocusCycleRoot, isFocusTraversable, isLightweightComponent, isManagingFocus, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isPreferredSizeSet, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processFocusEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setDebugGraphicsOptions, setDoubleBuffered, setEnabled, setFont, setForeground, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update</CODE></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
<A NAME="methods_inherited_from_class_java.awt.Container"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
|
||||
<TD><B>Methods inherited from class java.awt.Container</B></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD><CODE>add, add, add, add, add, addContainerListener, addImpl, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getLayout, insets, invalidate, isAncestorOf, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setLayout, validate, validateTree</CODE></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
<A NAME="methods_inherited_from_class_java.awt.Component"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
|
||||
<TD><B>Methods inherited from class java.awt.Component</B></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD><CODE>action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, getBackground, getBounds, getColorModel, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocationOnScreen, getName, getParent, getPeer, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, imageUpdate, inside, isDisplayable, isEnabled, isLightweight, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus</CODE></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
<A NAME="methods_inherited_from_class_java.lang.Object"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor">
|
||||
<TD><B>Methods inherited from class java.lang.Object</B></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD><CODE>clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait</CODE></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
|
||||
<P>
|
||||
|
||||
<!-- ============ FIELD DETAIL =========== -->
|
||||
|
||||
|
||||
<!-- ========= CONSTRUCTOR DETAIL ======== -->
|
||||
|
||||
<A NAME="constructor_detail"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
|
||||
<TD COLSPAN=1><FONT SIZE="+2">
|
||||
<B>Constructor Detail</B></FONT></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
|
||||
<A NAME="SortBarPanel()"><!-- --></A><H3>
|
||||
SortBarPanel</H3>
|
||||
<PRE>
|
||||
public <B>SortBarPanel</B>()</PRE>
|
||||
<DL>
|
||||
<DD>Defualt Constructor: Instantiates a SortBarPanel</DL>
|
||||
|
||||
<!-- ============ METHOD DETAIL ========== -->
|
||||
|
||||
<A NAME="method_detail"><!-- --></A>
|
||||
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
|
||||
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
|
||||
<TD COLSPAN=1><FONT SIZE="+2">
|
||||
<B>Method Detail</B></FONT></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
|
||||
<A NAME="display(java.lang.Integer[])"><!-- --></A><H3>
|
||||
display</H3>
|
||||
<PRE>
|
||||
public void <B>display</B>(java.lang.Integer[] array)</PRE>
|
||||
<DL>
|
||||
<DD>Draws an array of sixty Integers as a horizontal bar graph. If the
|
||||
array parameter is null or is not of length 60, an error will be
|
||||
displayed to the console.
|
||||
<BR><BR><DD><DL>
|
||||
<DT><B>Parameters:</B><DD><CODE>array</CODE> - the array of Integers to display as a bar graph.</DL>
|
||||
</DD>
|
||||
</DL>
|
||||
<HR>
|
||||
|
||||
<A NAME="paintComponent(java.awt.Graphics)"><!-- --></A><H3>
|
||||
paintComponent</H3>
|
||||
<PRE>
|
||||
protected void <B>paintComponent</B>(java.awt.Graphics g)</PRE>
|
||||
<DL>
|
||||
<DD><DL>
|
||||
<DT><B>Overrides:</B><DD><CODE>paintComponent</CODE> in class <CODE>javax.swing.JComponent</CODE></DL>
|
||||
</DD>
|
||||
</DL>
|
||||
<!-- ========= END OF CLASS DATA ========= -->
|
||||
<HR>
|
||||
|
||||
<!-- ========== START OF NAVBAR ========== -->
|
||||
<A NAME="navbar_bottom"><!-- --></A>
|
||||
<TABLE BORDER="0" WIDTH="100%" CELLPADDING="1" CELLSPACING="0">
|
||||
<TR>
|
||||
<TD COLSPAN=2 BGCOLOR="#EEEEFF" CLASS="NavBarCell1">
|
||||
<A NAME="navbar_bottom_firstrow"><!-- --></A>
|
||||
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="3">
|
||||
<TR ALIGN="center" VALIGN="top">
|
||||
<TD BGCOLOR="#FFFFFF" CLASS="NavBarCell1Rev"> <FONT CLASS="NavBarFont1Rev"><B>Class</B></FONT> </TD>
|
||||
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="overview-tree.html"><FONT CLASS="NavBarFont1"><B>Tree</B></FONT></A> </TD>
|
||||
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="deprecated-list.html"><FONT CLASS="NavBarFont1"><B>Deprecated</B></FONT></A> </TD>
|
||||
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="index-all.html"><FONT CLASS="NavBarFont1"><B>Index</B></FONT></A> </TD>
|
||||
<TD BGCOLOR="#EEEEFF" CLASS="NavBarCell1"> <A HREF="help-doc.html"><FONT CLASS="NavBarFont1"><B>Help</B></FONT></A> </TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
</TD>
|
||||
<TD ALIGN="right" VALIGN="top" ROWSPAN=3><EM>
|
||||
</EM>
|
||||
</TD>
|
||||
</TR>
|
||||
|
||||
<TR>
|
||||
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
|
||||
PREV CLASS
|
||||
NEXT CLASS</FONT></TD>
|
||||
<TD BGCOLOR="white" CLASS="NavBarCell2"><FONT SIZE="-2">
|
||||
<A HREF="index.html" TARGET="_top"><B>FRAMES</B></A>
|
||||
<A HREF="SortBarPanel.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD>
|
||||
</TR>
|
||||
<TR>
|
||||
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
|
||||
SUMMARY: <A HREF="#inner_classes_inherited_from_class_javax.swing.JPanel">INNER</A> | <A HREF="#fields_inherited_from_class_javax.swing.JComponent">FIELD</A> | <A HREF="#constructor_summary">CONSTR</A> | <A HREF="#method_summary">METHOD</A></FONT></TD>
|
||||
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
|
||||
DETAIL: FIELD | <A HREF="#constructor_detail">CONSTR</A> | <A HREF="#method_detail">METHOD</A></FONT></TD>
|
||||
</TR>
|
||||
</TABLE>
|
||||
<!-- =========== END OF NAVBAR =========== -->
|
||||
|
||||
<HR>
|
||||
|
||||
</BODY>
|
||||
</HTML>
|
||||
169
CS1322/p5/SortFrame.java
Normal file
169
CS1322/p5/SortFrame.java
Normal file
@@ -0,0 +1,169 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* SortFrame.java
|
||||
*
|
||||
* Revisions: 1.0 Nov. 09, 2002
|
||||
* Created the SortFrame class
|
||||
* 1.1 Nov. 10, 2002
|
||||
* Compiled, Finished, Done
|
||||
* 1.2 Nov. 10, 2002
|
||||
* Added Final Phase methods and variables
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* Created with JCreatorLE, some indents are off when viewed through notepad
|
||||
* or EMACS
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.2, Nov. 10, 2002
|
||||
*/
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class SortFrame extends JFrame implements ChangeListener{
|
||||
|
||||
/**
|
||||
*Displays the Array
|
||||
*/
|
||||
private SortBarPanel barPanel;
|
||||
|
||||
/**
|
||||
*Initiate the sort of the random array
|
||||
*/
|
||||
private JButton startButton;
|
||||
|
||||
/**
|
||||
*Drop down menu for the frame
|
||||
*/
|
||||
private JMenuBar menuBar;
|
||||
|
||||
/**
|
||||
*Menu for menubar
|
||||
*/
|
||||
private JMenu sortMenu;
|
||||
|
||||
/**
|
||||
*Sorting type buttons
|
||||
*/
|
||||
private JMenuItem bubbleItem;
|
||||
private JMenuItem quickItem;
|
||||
private JMenuItem insertionItem;
|
||||
private JMenuItem mergeItem;
|
||||
|
||||
/**
|
||||
*A curious variable who wants to know what the heck just happened
|
||||
*/
|
||||
private EventHandler eh;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Default Constructor for SortFrame()
|
||||
*Initializes window
|
||||
*/
|
||||
public SortFrame(){
|
||||
super("Sort Demo");
|
||||
eh = new EventHandler(this);
|
||||
this.initWindow(true);
|
||||
}
|
||||
|
||||
/**
|
||||
*Used in P5 driver
|
||||
*/
|
||||
public SortFrame(boolean bVerbose){
|
||||
super("Sort Demo");
|
||||
eh = new EventHandler(this,bVerbose);
|
||||
this.initWindow(false);
|
||||
}
|
||||
|
||||
/**
|
||||
*Initialize function for the window
|
||||
*@param bPretty, something I didn't have time to fully implement
|
||||
*/
|
||||
private void initWindow(boolean bPretty){
|
||||
|
||||
//init Layout
|
||||
BorderLayout BL = new BorderLayout();
|
||||
this.getContentPane().setLayout(BL);
|
||||
|
||||
//init Sort Items
|
||||
bubbleItem = new JMenuItem("Bubble Sort");
|
||||
quickItem = new JMenuItem("Quick Sort");
|
||||
insertionItem = new JMenuItem("Insertion Sort");
|
||||
mergeItem = new JMenuItem("Merge Sort");
|
||||
|
||||
//add the actionListeners
|
||||
bubbleItem.addActionListener(eh);
|
||||
quickItem.addActionListener(eh);
|
||||
mergeItem.addActionListener(eh);
|
||||
insertionItem.addActionListener(eh);
|
||||
|
||||
//init menues
|
||||
sortMenu = new JMenu("Sorts");
|
||||
sortMenu.add(bubbleItem);
|
||||
sortMenu.add(insertionItem);
|
||||
sortMenu.add(mergeItem);
|
||||
sortMenu.add(quickItem);
|
||||
|
||||
menuBar = new JMenuBar();
|
||||
menuBar.add(sortMenu);
|
||||
|
||||
//BL.addLayoutComponent(menuBar,"NORTH");
|
||||
this.setJMenuBar(menuBar);
|
||||
|
||||
barPanel = new SortBarPanel();
|
||||
this.getContentPane().add(barPanel,BL.CENTER);
|
||||
|
||||
//init button
|
||||
startButton = new JButton("Start");
|
||||
startButton.addActionListener(eh);
|
||||
this.getContentPane().add(startButton,BL.SOUTH);
|
||||
|
||||
//init window properties
|
||||
setSize(600,500);
|
||||
setResizable(false);
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
|
||||
//Display the Window
|
||||
this.show();
|
||||
|
||||
}//end initWindow()
|
||||
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Update the barPanel
|
||||
*/
|
||||
public void stateChanged(ChangeEvent ce){
|
||||
if(ce.getSource() instanceof ArrayWrapper){
|
||||
ArrayWrapper awTemp = (ArrayWrapper)ce.getSource();
|
||||
barPanel.display(awTemp.toIntArray());
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class SortFrame.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
SortFrame f = new SortFrame();
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class SortFrame
|
||||
29
CS1322/p5/class2listener.java
Normal file
29
CS1322/p5/class2listener.java
Normal file
@@ -0,0 +1,29 @@
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import java.lang.*;
|
||||
|
||||
class class2listener implements ActionListener
|
||||
{
|
||||
private class2test win;
|
||||
|
||||
public class2listener(class2test c) { win = c; }
|
||||
|
||||
|
||||
public void actionPerformed(ActionEvent ae)
|
||||
{
|
||||
String s = ae.getActionCommand();
|
||||
if(s.equals("Click here..."))
|
||||
System.out.println("You clicked the 'click here' button");
|
||||
else if(s.equals("Print Stuff"))
|
||||
win.printStuff();
|
||||
else if(s.equals("f 1"))
|
||||
System.out.println("file, menu 1");
|
||||
else if(s.equals("f 2"))
|
||||
System.out.println("file, menu 2");
|
||||
else if(s.equals("e 1"))
|
||||
System.out.println("edit, menu 1");
|
||||
else if(s.equals("e 2"))
|
||||
System.out.println("edit, menu 2");
|
||||
}
|
||||
}
|
||||
|
||||
98
CS1322/p5/class2test.java
Normal file
98
CS1322/p5/class2test.java
Normal file
@@ -0,0 +1,98 @@
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
|
||||
class class2test
|
||||
{
|
||||
private class2listener listener;
|
||||
private JFrame frame;
|
||||
private JTextArea textarea;
|
||||
private JLabel label;
|
||||
private JButton J1;
|
||||
private JButton J2;
|
||||
private JMenuBar menubar;
|
||||
private JMenu menu1;
|
||||
private JMenu menu2;
|
||||
private JMenuItem f1,f2,e1,e2;
|
||||
|
||||
public class2test()
|
||||
{
|
||||
createstuff();
|
||||
dostuff();
|
||||
}
|
||||
|
||||
public void createstuff()
|
||||
{
|
||||
frame = new JFrame("my window");
|
||||
listener = new class2listener(this);
|
||||
textarea = new JTextArea("Type here");
|
||||
|
||||
J1 = new JButton("Click here...");
|
||||
J2 = new JButton("Print Stuff");
|
||||
|
||||
label = new JLabel("My GUI");
|
||||
|
||||
menubar = new JMenuBar();
|
||||
menu1 = new JMenu("file");
|
||||
menu2 = new JMenu("edit");
|
||||
f1 = new JMenuItem("f 1");
|
||||
f2 = new JMenuItem("f 2");
|
||||
e1 = new JMenuItem("e 1");
|
||||
e2 = new JMenuItem("e 2");
|
||||
}
|
||||
|
||||
public void dostuff()
|
||||
{
|
||||
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
Container con = frame.getContentPane();
|
||||
|
||||
JPanel panel = new JPanel();
|
||||
panel.setLayout(new BorderLayout());
|
||||
con.add(panel);
|
||||
|
||||
textarea.setLineWrap(true);
|
||||
|
||||
panel.add(label,BorderLayout.SOUTH);
|
||||
panel.add(textarea,BorderLayout.CENTER);
|
||||
|
||||
menubar.add(menu1);
|
||||
menubar.add(menu2);
|
||||
menu1.add(f1);
|
||||
menu1.add(f2);
|
||||
menu2.add(e1);
|
||||
menu2.add(e2);
|
||||
|
||||
f1.addActionListener(listener);
|
||||
f2.addActionListener(listener);
|
||||
e1.addActionListener(listener);
|
||||
e2.addActionListener(listener);
|
||||
|
||||
panel.add(menubar,BorderLayout.NORTH);
|
||||
|
||||
JPanel panel2 = new JPanel();
|
||||
panel2.setLayout(new GridLayout(2,1));
|
||||
panel2.add(J1);
|
||||
panel2.add(J2);
|
||||
|
||||
J1.addActionListener(listener);
|
||||
J2.addActionListener(listener);
|
||||
|
||||
panel.add(panel2,BorderLayout.EAST);
|
||||
|
||||
frame.setSize(400,300);
|
||||
frame.setVisible(true); // frame.show()
|
||||
}
|
||||
|
||||
public void printStuff()
|
||||
{
|
||||
System.out.println("The text in the JTextArea is: \n" +
|
||||
'\t' + textarea.getText());
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
class2test c = new class2test();
|
||||
}
|
||||
}
|
||||
BIN
CS1322/p5/cs1322-P5.zip
Normal file
BIN
CS1322/p5/cs1322-P5.zip
Normal file
Binary file not shown.
BIN
CS1322/p5/p5.zip
Normal file
BIN
CS1322/p5/p5.zip
Normal file
Binary file not shown.
BIN
CS1322/p5/phase2Feedback.zip
Normal file
BIN
CS1322/p5/phase2Feedback.zip
Normal file
Binary file not shown.
Reference in New Issue
Block a user