first commit

This commit is contained in:
Jose Caban
2025-06-07 01:59:34 -04:00
commit 388ac241f0
3558 changed files with 9116289 additions and 0 deletions

211
CS1322/t5/Aircraft.java Normal file
View File

@@ -0,0 +1,211 @@
/**
* <PRE>
* Aircraft.java
*
* Revisions: 1.0 Oct. 16, 2002
* Created the Aircraft class
* 1.1 Oct. 27, 2002
* 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, Oct. 27, 2002
*/
public class Aircraft implements Comparable{
//hey what happened to Hungarian Notation?
private String model ;
private String nickname ;
private String role ;
private Integer launchPriority;
private Comparable comparator = nickname;
////////////////
//Constructors//
////////////////
/**
*Constructor for Aircraft
*@param model, the model
*@param nickname, the nickname
*@param role, the role
*@param launchPriority, the launchPriority
*/
public Aircraft(String model, String nickname,
String role, Integer launchPriority){
this.model = model;
this.nickname = nickname;
this.comparator = nickname;
this.role = role;
this.launchPriority = launchPriority;
}
/**
*Constructor for Aircraft
*@param nickname, the nickname
*sets all other params to default
*/
public Aircraft(String nickname){
this("default",nickname,"default",new Integer(0));
}
/**
*Constructor for Aircraft
*@param model, the model
*@param nickname, the nickname
*@param role, the role
*@param launchPriority, the launchPriority
*/
public Aircraft(String model, String nickname,
String role, int launchPriority){
this(model,nickname,role,new Integer(launchPriority));
}
///////////////////////
//Accessors/Modifiers//
///////////////////////
/**
*Accessor for Model
*@return value of model
*/
public String getModel(){
return model;
}
/**
*Modifier for model
*@param the new value for model
*/
public void setModel(String model){
this.model = model;
}
/**
*Accessor for nickname
*@return the value of nickname
*/
public String getNickname(){
return nickname;
}
/**
*Modifier for nickname
*@param the new value of nickname
*/
public void setNickname(String nickname){
this.nickname = nickname;
this.comparator = nickname;
}
/**
*Accessor for role
*@return the role of the aircraft
*/
public String getRole(){
return role;
}
/**
*Modifier for role
*@param the new value for role
*/
public void setRole(String role){
this.role = role;
}
/**
*Accessor for launchPriority
*@return the value of launchPriority
*/
public Integer getLaunchPriority(){
return launchPriority;
}
/**
*Modifier for launchPriority
*@param the new value for launchPriority
*/
public void setLaunchPriority(Integer launchPriority){
this.launchPriority = launchPriority;
}
///////////
//Methods//
///////////
/**
*switches the comparators from the nickname to launchPriority
*/
public void switchComparators(){
if(comparator == nickname){
comparator = launchPriority;
}
else{//otherwise
comparator = nickname;
}
}
/**
*compareTo, returns >,<,or ==
*@param obj, the Object to compare with
*/
public int compareTo(Object obj){
if(!(obj instanceof Aircraft)){
return -5;
}
Aircraft temp = (Aircraft)obj;
if(comparator instanceof String && temp.comparator instanceof String){
return(comparator.compareTo(temp.comparator));
}
else if(comparator instanceof Integer &&
temp.comparator instanceof Integer)
{
return(comparator.compareTo(temp.comparator));
}
else{
System.err.println("Comparing apples and oranges doesn't work");
return -5;
}
}
/**
*@return String representation of Aircraft
*/
public String toString(){
return (model + " " + nickname + " of Type " + role + " with launch " +
"priority " + launchPriority);
}
/**
* Debugging main for class Aircraft.
* This method will rigorously test my code.
*
* <br><br>
* @param args a String array of command line arguments.
*/
public static void main(String[] args) {
Aircraft air = new Aircraft("F-14","Tomcat","Interceptor",8);
System.out.println(air);
System.out.println(air.compareTo(new Integer(8)));
System.out.println(air.compareTo(new Aircraft("Eagle")));
System.out.println(air.compareTo(air));
System.out.println(air.compareTo(new Aircraft("Zebra")));
}// end of main(String[] args)
}// end of class Aircraft

274
CS1322/t5/ArrayWrapper.java Normal file
View 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

414
CS1322/t5/BST.java Normal file
View File

@@ -0,0 +1,414 @@
/**
* <PRE>
* BST.java
*
* Revisions: 1.0 Oct. 16, 2002
* Created the BST class
* 1.1 Oct. 16, 2002
* Compiled, Finished, Tested
* 2.0 Oct. 26, 2002
* Added code to fix problem with remove(), neither autograder
* nor me caught it until after working on
* AircraftCarrier.java
*
*
* </PRE>
*
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
* @version Version 2.0, Oct. 26, 2002
*/
import java.io.*;
public class BST {
public static final boolean bDebug = false;
public BSTNode root;
///////////
//Methods//
///////////
/**
*Add data to tree
*@param data, data to add
*/
public void add(Comparable data){
if(root == null){
root = new BSTNode(data);
}
else{
add(data,root);
}
}//end add(Comparable)
/**
*Add data to tree helper
*@param data, the data to add
*@param current, the current spot on the tree
*/
private void add(Comparable data, BSTNode current){
if(current.getData().compareTo(data) > 0){
if(current.getLeft() == null){
current.setLeft(new BSTNode(data));
}
else{
add(data,current.getLeft());
}
}
else if(current.getData().compareTo(data) < 0){
if(current.getRight() == null){
current.setRight(new BSTNode(data));
}
else{
add(data,current.getRight());
}
}
else{/*do nothing*/}//current.compareTo() == 0
}//end add(Comparable,BSTNode)
/**
*Add data with Iteration
*I did this for my own enjoyment and mental enrichment
*plus those nested ?: look soooooo advanced
*/
public void addItr(Comparable data){
if(root == null){
root = new BSTNode(data);
}
if(data.compareTo(root.getData()) == 0){
return;
}
//assumes data is not already in tree
BSTNode bTraverse =
(data.compareTo(root.getData())>0?root.getRight():root.getLeft());
BSTNode bPrevTraverse = root;
while(bTraverse != null){
bPrevTraverse = bTraverse;
bTraverse =
(data.compareTo(bTraverse.getData())>0?
bTraverse.getRight():bTraverse.getLeft());
}
if(data.compareTo(bPrevTraverse.getData())>0){
bPrevTraverse.setRight(new BSTNode(data));
}
else{
bPrevTraverse.setLeft(new BSTNode(data));
}
}//end addItr(Comparable)
/**
*Check to see if data is in Tree
*@return null if not, the data if true
*/
public Comparable find(Comparable toFind){
BSTNode current = root;
for(;;){
if(current == null){
return null;
}
else if(toFind.compareTo(current.getData()) == 0){
return current.getData();
}
else if(toFind.compareTo(current.getData()) < 0){
current = current.getLeft();
}
else{
current = current.getRight();
}
}
}//end find(Comparable)
/**
*Remove a piece of data from Tree
*@param toDie, the piece of data to be removed
*/
public Comparable remove(Comparable toDie){
BSTNode bLocation = root;
BSTNode bPrevLocation = null;
boolean bDone = false;
while(bDone != true && bLocation != null){
if(toDie.compareTo(bLocation.getData()) == 0){
bDone = true;
}
else if(toDie.compareTo(bLocation.getData()) < 0){
bPrevLocation = bLocation;
bLocation = bLocation.getLeft();
}
else{
bPrevLocation = bLocation;
bLocation = bLocation.getRight();
}
}
if(bLocation == null){
return null;
}
if(bDebug){ //ya know. having a built in Debugger is why I love
//Visual Studio
System.out.println("bDebug Start");
System.out.println(bLocation.getData());
System.out.println("bDebug End");
}
Comparable data = bLocation.getData();
remove(bLocation,bPrevLocation);
return data;
}//end remove(Comparable)
/**
*Recurse Method for removing a node
*@param bLocation, the location of the node
*@param bPrevLocation, the Parent of bLocation
*/
private void remove(BSTNode bLocation,BSTNode bPrevLocation){
//if no kids
if(bLocation.getLeft() == null && bLocation.getRight() == null){
if(bPrevLocation == null){
root = null;
return;
}
if(bPrevLocation.getLeft() == bLocation){
bPrevLocation.setLeft(null);
}
else{//bPrevLocation.getRight() == bLocation
bPrevLocation.setRight(null);
}
}
//one or two kids, gotta kill its parent, how sad
else{
BSTNode bTraverse;
BSTNode bPrevTraverse;
if(bLocation.getRight() != null){
bTraverse = bLocation.getRight();
bPrevTraverse = bLocation;
while(bTraverse.getLeft() != null){
bPrevTraverse = bTraverse;
bTraverse = bTraverse.getLeft();
}
bLocation.setData(bTraverse.getData());
remove(bTraverse,bPrevTraverse);
}
else if(bLocation.getLeft() != null){
bTraverse = bLocation.getLeft();
bPrevTraverse = bLocation;
while(bTraverse.getRight() != null){
bPrevTraverse = bTraverse;
bTraverse = bTraverse.getRight();
}
bLocation.setData(bTraverse.getData());
remove(bTraverse,bPrevTraverse);
}
else{}
}
}//end remove(BSTNode,BSTNode)
/**
*Traverse tree output
*/
public void outputTree(){
outputTree(root);
}
/**
*Traverse current
*/
public void outputTree(BSTNode current){
if(current == null){
return;
}
outputTree(current.getLeft());
System.out.println(current);
outputTree(current.getRight());
}
/**
*Clear Tree
*/
private void clearTree(){
root = null;
}
/**
*Writes BST data to file
*@param oos, the stream to write with
*@param tree, the BST to get data from
*/
public void writeAircraft(BST tree,PrintWriter pw){
writeAircraft(root,pw);
}
/**
*Writes BST data to file
*@param oos, the stream to write with
*@param current, the current node
*/
private void writeAircraft(BSTNode current,PrintWriter pw){
if(current == null){
return;
}
writeAircraft(current.getLeft(),pw);
Aircraft air = (Aircraft)current.getData();
String temp = air.getModel() + " " + air.getNickname() + " " +
air.getRole() + " " + air.getLaunchPriority();
pw.println(temp);
writeAircraft(current.getRight(),pw);
}
/**
*creates the launchQueue for th aircraft carrier
*/
public void createLaunchQueue(BST bst,Heap toAdd){
createLaunchQueue(bst.root,toAdd);
}
/**
*Helper method for createLaunchQueue(BST,Heap)
*/
private void createLaunchQueue(BSTNode current,Heap toAdd){
if(current == null){
return;
}
createLaunchQueue(current.getRight(),toAdd);
((Aircraft)current.getData()).switchComparators();
toAdd.insert(current);
createLaunchQueue(current.getLeft(),toAdd);
}
/**
* Debugging main for class BST.
* This method will rigorously test my code.
*
* <br><br>
* @param args a String array of command line arguments.
*/
public static void main(String[] args) {
//lets test this sucker out
BST bst = new BST();
bst.addItr(new Integer(10));
bst.addItr(new Integer(5));
bst.addItr(new Integer(1));
bst.addItr(new Integer(8));
bst.addItr(new Integer(9));
bst.addItr(new Integer(15));
bst.addItr(new Integer(13));
bst.addItr(new Integer(18));
bst.outputTree();
Integer five = new Integer(5);
System.out.println("\nTesting find()...");
System.out.println(bst.find(five));
System.out.println("Done.\n");
//remove w/0 kids
bst.clearTree();
bst.add(new Integer(10));
bst.add(new Integer(5));
bst.add(new Integer(1));
bst.add(new Integer(8));
bst.add(new Integer(9));
bst.add(new Integer(15));
bst.add(new Integer(13));
bst.add(new Integer(18));
System.out.println("\nTesting remove(0 kids)...");
bst.remove(new Integer(1));
bst.outputTree();
System.out.println("Done\n");
//remove w/1 kid
bst.clearTree();
bst.add(new Integer(10));
bst.add(new Integer(5));
bst.add(new Integer(1));
bst.add(new Integer(8));
bst.add(new Integer(9));
bst.add(new Integer(15));
bst.add(new Integer(13));
bst.add(new Integer(18));
System.out.println("\nTesting remove(1 kid)...");
bst.remove(new Integer(8));
bst.outputTree();
System.out.println("Done\n");
//remove w/2 kids
bst.clearTree();
bst.add(new Integer(10));
bst.add(new Integer(5));
bst.add(new Integer(1));
bst.add(new Integer(8));
bst.add(new Integer(9));
bst.add(new Integer(15));
bst.add(new Integer(13));
bst.add(new Integer(18));
System.out.println("\nTesting remove(2 kids)...");
bst.remove(new Integer(5));
bst.outputTree();
System.out.println("Done\n");
//remove root
bst.clearTree();
bst.add(new Integer(10));
bst.add(new Integer(5));
bst.add(new Integer(1));
bst.add(new Integer(8));
bst.add(new Integer(9));
bst.add(new Integer(15));
bst.add(new Integer(13));
bst.add(new Integer(18));
System.out.println("\nTesting remove(root)...");
bst.remove(new Integer(10));
bst.outputTree();
System.out.println("Done\n");
//remove nothing single thing tree
bst.clearTree();
bst.add(new Integer(10));
bst.remove(new Integer(10));
bst.outputTree();
System.out.println("\nTesting remove(nothing but root tree)...");
bst.remove(new Integer(10));
bst.outputTree();
System.out.println("Done\n");
}// end of main(String[] args)
}// end of class BST

150
CS1322/t5/BSTNode.java Normal file
View File

@@ -0,0 +1,150 @@
/**
* <PRE>
* BSTNode.java
*
* Revisions: 1.0 Oct. 16, 2002
* Created the BSTNode class
* 1.1 Oct. 19, 2002
* Compiled, Finished
* 1.2 Oct. 27, 2002
* 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.2, Oct. 27, 2002
*/
public class BSTNode implements Comparable{
public static final boolean bDebug = false; //nothing to debug
private Comparable data;
private BSTNode left;
private BSTNode right;
////////////////
//Constructors//
////////////////
/**
*Default Constructor
*/
public BSTNode(){
this(null);
}
/**
*@param data, the value of data
*/
public BSTNode(Comparable data){
this.data = data;
left = null;
right = null;
}
///////////////////////
//Accessors/Modifiers//
///////////////////////
/**
*Accessor for data
*@return value of data
*/
public Comparable getData(){
return data;
}
/**
*Modifier for data
*@param the value of data
*/
public void setData(Comparable data){
this.data = data;
}
/**
*Accessor for left
*@return value of left
*/
public BSTNode getLeft(){
return left;
}
/**
*Modifier for left
*@param the value ofleft
*/
public void setLeft(BSTNode left){
this.left = left;
}
/**
*Accessor for right
*@return value of right
*/
public BSTNode getRight(){
return right;
}
/**
*Modifier for right
*@param the value ofright
*/
public void setRight(BSTNode right){
this.right = right;
}
///////////
//Methods//
///////////
/**
*compareTo method
*@return the value of data's compareTo()
*/
public int compareTo(Object o){
if(o instanceof BSTNode){
BSTNode temp = (BSTNode)o;
return data.compareTo(temp.getData());
}
else{
throw new ClassCastException();
}
}
/**
*toString() method
*@return data's toString()
*/
public String toString(){
return data.toString();
}
/**
* Debugging main for class BSTNode.
* This method will rigorously test my code.
*
* <br><br>
* @param args a String array of command line arguments.
*/
public static void main(String[] args) {
BSTNode bob = new BSTNode(new Integer(7));
BSTNode rob = new BSTNode(new Integer(7));
BSTNode tod = new BSTNode(new Integer(6));
System.out.println(bob.compareTo(rob));
System.out.println(bob.compareTo(tod));
System.out.println(bob.toString());
}// end of main(String[] args)
}// end of class BSTNode

78
CS1322/t5/BubbleSort.java Normal file
View 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

175
CS1322/t5/Heap.java Normal file
View File

@@ -0,0 +1,175 @@
/**
* <PRE>
* Heap.java
*
* Revisions: 1.0 Oct. 27, 2002
* Created the Heap class
* 1.1 Oct. 27, 2002
* Compiled, Finished
* 1.2 Oct. 27, 2002
* 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.2, Oct. 27, 2002
*/
import java.util.*;
public class Heap {
private Vector data;
////////////////
//Constructors//
////////////////
/**
*Default Constructor for Heap
*@done sets the first element to null
*/
public Heap(){
data = new Vector();
data.add(null);
}
///////////
//Methods//
///////////
/**
*Swaps the elements at the given locations
*@param a and b, the elements to be swapped
*/
private void swap(int a,int b){
Comparable temp = (Comparable)data.elementAt(a);
data.setElementAt(data.elementAt(b),a);
data.setElementAt(temp,b);
}
/**
*Insert a piece of data into the Heap
*@param toAdd, the data to be added
*/
public void insert(Comparable toAdd){//i changed this to toAdd because
//the bright guy who decided to make us
//use this.data.INSERTHERE() is a sadist
int i = data.size();
data.setElementAt(new Integer(i),0);
data.add(toAdd);
while(i>1 &&
((Comparable)data.elementAt(i)).compareTo(data.elementAt(i/2))>0){
swap(i,(i/2));
i /= 2;
}
}//end insert(Comparable)
/**
*Arranges heap to be a heap
*@param node, the node to be tested for heapitude
*/
private void heapify(int node){
int iLeft = (node*2),iRight = ((node*2)+1),iMin = iLeft;
int iSize = data.size()-1;
if(iLeft <= iSize &&
((Comparable)data.elementAt(iLeft)).compareTo(data.elementAt(node))<0){
iMin = iLeft;
}
else{
iMin = node;
}
if(iRight <= iSize &&
((Comparable)data.elementAt(iRight)).compareTo(data.elementAt(iMin))<0){
iMin = iRight;
}
if(iMin != node){
swap(iMin,node);
heapify(iMin);
}
}//end heapify(int)
/**
*Removes the largest piece of data from the heap
*/
public Comparable remove(){
Comparable removed=null;
if(data.size()-1 > 0) {
removed = (Comparable)data.elementAt(1);
data.setElementAt(data.elementAt(data.size()-1), 1);
//reduce size
Integer temp = (Integer)data.elementAt(0);
temp = new Integer(temp.intValue()-1);
data.setElementAt(temp,0);
data.remove(temp.intValue()+1);
heapify(1);
}
return removed;
}//end remove()
/**
*@return true if the heap is empty, false otherwise
*/
public boolean isEmpty(){
if(data.size()>1){
return false;
}
return true;
}
/**
*String representation of the Heap
*/
public String toString(){
return data.toString();
}
/***************************************************************/
/**
* Debugging main for class Heap.
* This method will rigorously test my code.
*
* <br><br>
* @param args a String array of command line arguments.
*/
public static void main(String[] args) {
Heap heap = new Heap();
System.out.println(heap.isEmpty());
heap.insert(new Integer(5));
System.out.println(heap.isEmpty());
heap.insert(new Integer(21));
heap.insert(new Integer(1));
heap.insert(new Integer(322));
heap.insert(new Integer(12));
heap.insert(new Integer(3252));
heap.insert(new Integer(0));
heap.insert(new Integer(223));
System.out.println(heap.data);
heap.remove();
System.out.println(heap.data);
heap.remove();
System.out.println(heap.data);
}// end of main(String[] args)
}// end of class Heap

View 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/t5/MergeSort.java Normal file
View 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

28
CS1322/t5/MyGUI.java Normal file
View File

@@ -0,0 +1,28 @@
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class MyGUI extends JFrame{
public MyGUI(){
super("My GUI");
this.setLayout(new BorderLayout());
this.getContentPane().add(new JLabel("1"),BorderLayout.EAST);
this.getContentPane().add(new JButton("Button"),BorderLayout.NORTH);
this.getContentPane().add(new JLabel("2"),BorderLayout.WEST);
this.getContentPane().add(new JButton("Button2"),BorderLayout.SOUTH);
this.setJMenuBar(null);
//init window properties
setSize(60,50);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//Display the Window
this.show();
}
public static void main(){
MyGUI g = new MyGUI();
}
}

111
CS1322/t5/QuickSort.java Normal file
View 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

161
CS1322/t5/Test5.java Normal file
View File

@@ -0,0 +1,161 @@
import java.util.*;
public class Test5{
public int getHeight(BST tree){
return getHeight(tree.root);
}
public int getHeight(BSTNode current){
if(current == null){
return 0;
}
int L = getHeight(current.getLeft());
int R = getHeight(current.getRight());
if(L>R){
return ++L;
}
else{
return ++R;
}
}
public boolean isFull(BSTNode current){
if(current == null){
return true;
}
if(current.getRight() == null && current.getLeft() != null ||
current.getRight() != null && current.getLeft() == null){
return false;
}
else{
boolean b = isFull(current.getRight());
boolean c = isFull(current.getLeft());
if(b==false || c==false){
return false;
}
else{
return true;
}
}
}
//HEAP SHIT
public Vector vHeap;
public void add(Comparable c){
int i = vHeap.size();
vHeap.setElementAt(new Integer(214141),0);
vHeap.add(c);
while(i>1 &&
((Comparable)vHeap.elementAt(i)).compareTo(vHeap.elementAt(i/2))<0){
swap(i,(i/2));
i /= 2;
}
}
private void swap(int a,int b){
Comparable temp = (Comparable)vHeap.elementAt(a);
vHeap.setElementAt(vHeap.elementAt(b),a);
vHeap.setElementAt(temp,b);
}
public void heapify(int node){
int iRight = (node*2)+1;
int iLeft = node*2;
int iMin = iLeft;
int iSize = vHeap.size()-1;
Vector data = vHeap;
if(iLeft <= iSize &&
((Comparable)data.elementAt(iLeft)).compareTo(data.elementAt(node))<0){
iMin = iLeft;
}
else{
iMin = node;
}
if(iRight <= iSize &&
((Comparable)data.elementAt(iRight)).compareTo(data.elementAt(iMin))<0){
iMin = iRight;
}
if(iMin != node){
swap(iMin,node);
heapify(iMin);
}
}
public void BubbleSort(Integer[] bob){
for(int i=0; i<bob.length-1; i++){
for(int z=0; z<bob.length-1-i;z++){
if(bob[z+1].compareTo(bob[z])<0){
Integer temp = bob[z];
bob[z] = bob[z+1];
bob[z+1] = temp;
}
}
}
}
public void InsertionSort(Integer[] bob){
for(int i=1; i<bob.length; i++){
Comparable cTemp = bob[i];
int z=i;
while(z>0 && cTemp.compareTo(bob[z-1])<0){
Integer temp = bob[z];
bob[z] = bob[z-1];
bob[z-1] = temp;
z--;
}
}
}
public static void main(String args[]){
BST bob = new BST();
Test5 shit = new Test5();
bob.add(new Integer(10));
System.out.println(shit.getHeight(bob));
System.out.println(shit.isFull(bob.root));
bob.add(new Integer(15));
bob.add(new Integer(20));
bob.add(new Integer(14));
bob.add(new Integer(2));
System.out.println(shit.getHeight(bob));
System.out.println(shit.isFull(bob.root));
bob.root = null;
bob.add(new Integer(10));
bob.add(new Integer(5));
bob.add(new Integer(4));
bob.add(new Integer(3));
System.out.println(shit.getHeight(bob));
System.out.println(shit.isFull(bob.root));
System.out.println("/***************/");
//Enumeration e;
shit.vHeap = new Vector();
shit.vHeap.add(null);
shit.add(new Integer(6));
shit.add(new Integer(89));
shit.add(new Integer(4));
//e = shit.vHeap.elements();
/*while(e.hasMoreElements()){
System.out.println(e.nextElement());
}*/
System.out.println(shit.vHeap);
Integer[] b = ArrayWrapper.getRandomIntegerArray(60);
shit.InsertionSort(b);
for(int i=0;i<b.length;i++){
System.out.println(b[i]);
}
}
}

308
CS1322/t5/t5.txt Normal file
View File

@@ -0,0 +1,308 @@
0) Review any questions from old exams (test1, test2, test3, or test4),
especially anything that you may have missed/not understood....
Binary Search Trees
====================
For all questions, assume that you have a typical BSTNode (with Comparable
data, and BSTNodes for left and right), and a BST class with a root of type
BSTNode.
1) A reverseOrder traversal prints the elements in a BST in reverse
order- namely, largest to smallest (whereas an inorder traversal
prints them smallest to largest). , write the method
public void reverseOrder()
in the class BST, which implements a reverseOrder traversal.
2) Assume that you are given method
private Comparable yank(BSTNode curr)
in the BST class which takes a BSTNode and deletes its left-most right
child, and then returns the data from the node that was just deleted.
(i.e. from curr, if you went right once, then as far left as possible,
the data in the node you found would be returned, and the node would
be delted. Write the method
public Comparable delete(Comparable toDel)
which deletes the first item found in the tree matching toDel,
and returns the data in the node deleted.
3) Write the method
public int countLargerThan(Comparable c)
which returns a count of the number of items larger than c.
Your method must make use of the BST's properties (i.e
you should not compare every item in the tree to c in all cases).
4) Write the method
public int getHeight()
which returns the height of the tree.
5) Write the method
public boolean isFull()
which returns true if the tree is full, false if it is not.
Remember that a full tree is a tree in which each node
has either exactly 0 or exactly 2 children.
The empty tree is considered to be full.
Heaps
=====
For all coding questions, you may assume that the Heap has a Vector, which
holds Comparables called "data". You may also assume that the constructor
initalizes the 0 element of the Vector to contain null, and that the Heap
is a min heap.
6) Given the following min-heap:
23
/ \
45 56
/ \ /
53 48 112
a) show the heap in an array representation.
b) add 11 to the heap, show all steps
c) remove min, show all steps
7) Write the method
public void add(Comparable c)
which adds the item to the heap.
8) Write the method
public Comparable peek()
which returns the smallest element in the Heap, without affecting
the contents of the Heap.
9) Write the method
private void heapify(int index)
which performs the heapify operation in the standard manner
starting at the given index.
10) Write the method
public Comparable removeSmallest()
which removes the smallest item, and then returns it.
Sorting
=======
11) Implement the following sorting methods based on the algorithm
corresponding to the method name:
a) public Comparable[] bubbleSort(Comparable[] data)
b) public Comparable[] insertionSort(Comparable[] data)
c) What is the running time ("Big-Oh") of these two
algorithms?
12) You need to write a program that will sort massive ammounts of
data (say 50 million elements at a time). The sorting must
be accomplished quickly, and due to memory constraints,
you must sort in place [i.e. you may not allocate other arrays].
a) Which sorting algorithm do you choose?
b) What is the "Big-Oh" of this algorithm?
c) Name another algorithm with this same "Big-Oh"
d) Explain why your choice of algorithm is better than
the other one with the same running time.
13) Given the following code:
public class Sort1 {
public static void printArray(int[] data){
System.out.print("[");
for(int i=0;i<data.length;i++)
System.out.print(" "+data[i]);
System.out.println(" ]");
}
public static void swap(int[] data, int a, int b) {
int temp=data[a];
data[a]=data[b];
data[b]=temp;
}
public static void mysterySort(int[] data) {
mysterySort(data,0,data.length);
}//mysterySort
public static void mysterySort(int[] data, int l, int h) {
int a=l+1;
int b=h-1;
int spot;
printArray(data);
if(h<=l)
return;
while(a<b) {
while(data[a]<data[l] && a<b) {
a++;
}//while(data[a]<data[l] && a<b)
while(data[b]>data[l] && a<b) {
b--;
}//while(data[b]>data[l] && a<b)
if(a<b) {
swap(data,a,b);
}
}//while(a<b)
if(a<data.length&&data[a]<data[l])
{
spot=a;
}
else
{
spot=a-1;
}
swap(data,spot,l);
mysterySort(data,l,spot);
mysterySort(data,spot+1,h);
}//mysterySort
public static void main(String[] args){
int[] data={4,3,5,9,1};
mysterySort(data);
printArray(data);
}//main
}//Sort1
**********END CODE*************
a) Name the sorting algorithm implemented
b) State the output when the main method is run
[hint, you should use your knowledge of how the sort
proceeds to help you with this part]
14) Given the following code:
public class Sort2 {
public static void printArray(int[] data) {
System.out.print("[");
for(int i=0;i<data.length;i++)
System.out.print(" "+data[i]);
System.out.println(" ]");
}
public static int[] firstHalf(int[] data) {
int[] ans=new int[data.length/2];
for(int i=0;i<ans.length;i++)
ans[i]=data[i];
return ans;
}
public static int[] secondHalf(int[] data) {
int[] ans=new int[data.length-data.length/2];
for(int i=0;i<ans.length;i++)
ans[i]=data[i+data.length/2];
return ans;
}
public static int[] mysterySort(int[] data) {
if(data.length<=1)
return data;
int[] ans=mysteryHelper(mysterySort(firstHalf(data)),
mysterySort(secondHalf(data)));
printArray(ans);
return ans;
}
public static int[] mysteryHelper(int[] t1,int [] t2) {
int[] ans=new int[t1.length+t2.length];
int i=0; int j=0; int x=0;
while(i<t1.length&&j<t2.length) {
if(t1[i]<t2[j]) {
ans[x]=t1[i];
i++;
} else {
ans[x]=t2[j];
j++;
}
x++;
} while(i<t1.length) {
ans[x]=t1[i];
x++; i++;
} while(j<t2.length) {
ans[x] = t2[j];
x++; j++;
} return ans;
}
public static void main(String[] args) {
int[] data={4,2,6,5,3,9};
mysterySort(data);
}//main
}//Sort2
**********END CODE*************
a) Name the sorting algorithm implemented
b) State the output when the main method is run
[hint, you should use your knowledge of how the sort
proceeds to help you with this part]
GUIS
====
15) Write a class MyGUI which uses composition to create a GUI that
resembles the following:
+-------------------------------------------+
| My GUI Program _ [] X|
+-------------------------------------------+
| | |
| | |
|a label | a button |
| | |
|----------------------+--------------------|
| | |
| | |
|another label | another button |
| | |
+-------------------------------------------+
where the upper and lower left contain JLabels with the appropriate
text, and the upper and lower right contain JButtons with the
appropriate text.
16) Explain what happens if you do not call setSize and setVisible(true)
on the JFrame of the GUI program.
17) Given the following code
import java.util.*;
import javax.swing.*;
public MyClassThatFiresChangeEvents{
private Vector listeners=new Vector();
public void addChangeListener(ChangeListener cl){
//YOUR CODE GOES HERE
}
public void fireChangeEvent(){
//YOUR CODE GOES HERE
}
}
a) Implement the method addChangeListener for the above class
b) Implement the method fireChangeEvent for the above class
Definitions
===========
18) Define the following words:
o Balanced Tree
o Full Tree
o Complete Tree
o Heap
o heapify
o Priority Queue
o GUI
o Component
o Container
o Layout Manager
o JFrame
o JPanel
o JLabel
o JButton
o JTextField
o JMenu
o Event
o Event Listener
o Event Registration
o Semantic Event
o Low-level Event
o MVC
o UI Delegate