first commit
This commit is contained in:
BIN
CS1322/Other/Survey_Test_3.doc
Normal file
BIN
CS1322/Other/Survey_Test_3.doc
Normal file
Binary file not shown.
BIN
CS1322/Other/survey3.doc
Normal file
BIN
CS1322/Other/survey3.doc
Normal file
Binary file not shown.
2
CS1322/final/Animal.java
Normal file
2
CS1322/final/Animal.java
Normal file
@@ -0,0 +1,2 @@
|
||||
public class Animal{
|
||||
}
|
||||
128
CS1322/final/Animals/Animal.java
Normal file
128
CS1322/final/Animals/Animal.java
Normal file
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Animal.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 17, 2002
|
||||
* Created the Animal class
|
||||
* 1.1 Sep. 17, 2002
|
||||
* Finished the Animal Class
|
||||
*
|
||||
* </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, Sep. 17, 2002
|
||||
*/
|
||||
|
||||
public abstract class Animal {
|
||||
|
||||
/**
|
||||
*whether or not the animal is hungry
|
||||
*/
|
||||
private boolean isHungry;
|
||||
|
||||
/**
|
||||
*holds the animal's name
|
||||
*/
|
||||
private String name;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for Animal
|
||||
*@param name the value to assign name
|
||||
*/
|
||||
public Animal(String name){
|
||||
this.name = name;
|
||||
isHungry = false;
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*isHungry Modifier
|
||||
*@param h the value applied to isHungry
|
||||
*/
|
||||
public void setIsHungry(boolean h){
|
||||
isHungry = h;
|
||||
}
|
||||
|
||||
/**
|
||||
*isHungry Accessor
|
||||
*@return the value of isHungry
|
||||
*/
|
||||
public boolean getIsHungry(){
|
||||
return isHungry;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for name
|
||||
*@return the value of name
|
||||
*/
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*@return whether the animal is dangerous or not
|
||||
*/
|
||||
public abstract boolean isDangerous();
|
||||
|
||||
/**
|
||||
*Give the animal something to eat
|
||||
*@param food, the food to feed the animal
|
||||
*/
|
||||
public abstract void eat(String food);
|
||||
|
||||
/**
|
||||
*Equals method
|
||||
*@param the value to test equality with
|
||||
*@return the truthnes of the equality
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
|
||||
if((obj instanceof Animal) == false){
|
||||
return false;
|
||||
}
|
||||
|
||||
Animal temp = (Animal)obj;
|
||||
//added "this." to make it verbose and more readable
|
||||
if(!(temp.getName().equals(this.getName()))){
|
||||
return false;
|
||||
}
|
||||
if(temp.isDangerous() != this.isDangerous()){
|
||||
return false;
|
||||
}
|
||||
if(temp.getIsHungry() != this.getIsHungry()){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Debugging main for class Animal.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
//nuttin ta test
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Animal
|
||||
99
CS1322/final/Animals/Aquarium.java
Normal file
99
CS1322/final/Animals/Aquarium.java
Normal file
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Aquarium.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 26, 2002
|
||||
* Created the Aquarium class
|
||||
* 1.1 Sep. 28, 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, Sep. 28, 2002
|
||||
*/
|
||||
|
||||
public class Aquarium extends ZooBuilding{
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for Aquarium
|
||||
*@param name the name for the Aquarium
|
||||
*/
|
||||
public Aquarium(String name){
|
||||
super(name);
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*@return true if it is safe to enter
|
||||
*Safe if all dangerous animals are 30 feet underwater
|
||||
*/
|
||||
public boolean isSafeToEnter(){
|
||||
Animal aTemp;
|
||||
SwimmingType sTemp;
|
||||
boolean bSafe = true;
|
||||
for(int i=0; i<getTheAnimals().size(); i++){
|
||||
aTemp = (Animal)getTheAnimals().removeFromBack();
|
||||
sTemp = (SwimmingType)aTemp;
|
||||
if(aTemp.isDangerous() && sTemp.getDepth() < 30){
|
||||
bSafe = false;
|
||||
}
|
||||
getTheAnimals().addToFront(aTemp);
|
||||
}
|
||||
return bSafe;
|
||||
}
|
||||
|
||||
/**
|
||||
*override default string method for use with this program
|
||||
*/
|
||||
public String toString(){
|
||||
return ("An Aquarium named " + getName() + ".");
|
||||
}
|
||||
|
||||
/****************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class Aquarium.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Aquarium swimswim = new Aquarium("Smells like Fish");
|
||||
|
||||
swimswim.getTheAnimals().addToFront(new Duck("Bob"));
|
||||
swimswim.getTheAnimals().addToBack(new Gator("Crocodile"));
|
||||
swimswim.getTheAnimals().addToFront(new GoldFish("Can't Remember Jack",5));
|
||||
|
||||
swimswim.getTheAnimals().listOut();
|
||||
System.out.println();
|
||||
|
||||
Gator Tom = (Gator)swimswim.getTheAnimals().getLast();
|
||||
Tom.setDepth(50);
|
||||
|
||||
System.out.println(swimswim.isSafeToEnter());
|
||||
Tom.setDepth(4);
|
||||
System.out.println(swimswim.isSafeToEnter());
|
||||
|
||||
swimswim.getTheAnimals().listOut();
|
||||
|
||||
System.out.println(swimswim);
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Aquarium
|
||||
142
CS1322/final/Animals/Aviary.java
Normal file
142
CS1322/final/Animals/Aviary.java
Normal file
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Aviary.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 26, 2002
|
||||
* Created the Aviary class
|
||||
* 1.1 Sep. 28, 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, Sep. 28, 2002
|
||||
*/
|
||||
|
||||
public class Aviary extends ZooBuilding{
|
||||
|
||||
/**
|
||||
*whether or not the net is closed
|
||||
*/
|
||||
boolean netClosed;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for Aviary
|
||||
*@param name the name of the Aviary
|
||||
*/
|
||||
public Aviary(String name){
|
||||
super(name);
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*@return value of netClosed
|
||||
*/
|
||||
public boolean getNetClosed(){
|
||||
return netClosed;
|
||||
}
|
||||
|
||||
/**
|
||||
*@param netClosed, the new value of netClosed
|
||||
*/
|
||||
public void setNetClosed(boolean netClosed){
|
||||
this.netClosed = netClosed;
|
||||
}
|
||||
|
||||
/////////////
|
||||
//.Methods.//
|
||||
/////////////
|
||||
|
||||
/**
|
||||
*@return the value of netClosed
|
||||
*/
|
||||
public boolean isSafeToEnter(){
|
||||
Animal aTemp;
|
||||
FlyingType fTemp;
|
||||
boolean bSafe = true;
|
||||
for(int i=0; i<getTheAnimals().size(); i++){
|
||||
aTemp = (Animal)getTheAnimals().removeFromBack();
|
||||
fTemp = (FlyingType)aTemp;
|
||||
if(aTemp.isDangerous() && fTemp.getAltitude() < 75){
|
||||
bSafe = false;
|
||||
}
|
||||
getTheAnimals().addToFront(aTemp);
|
||||
}
|
||||
if(!getNetClosed()){
|
||||
bSafe = false;
|
||||
}
|
||||
return bSafe;
|
||||
}
|
||||
|
||||
/**
|
||||
*override default toString for use with this program
|
||||
*/
|
||||
public String toString(){
|
||||
return ("An Aviary named " + getName() + ".");
|
||||
}
|
||||
|
||||
/***********************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class Aviary.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Aviary dancedance = new Aviary("Stinky");
|
||||
dancedance.getTheAnimals().addToBack(new Duck("Bob"));
|
||||
dancedance.getTheAnimals().addToFront(new Bat("Ronald"));
|
||||
dancedance.getTheAnimals().addToFront(new Bat("McDonald"));
|
||||
dancedance.getTheAnimals().listOut();
|
||||
System.out.println();
|
||||
dancedance.setNetClosed(true);
|
||||
System.out.println(dancedance.getHungryAnimals());
|
||||
System.out.println();
|
||||
dancedance.getTheAnimals().listOut();
|
||||
|
||||
Animal temp = (Animal)dancedance.getTheAnimals().getFirst();
|
||||
temp.setIsHungry(true);
|
||||
|
||||
System.out.println();
|
||||
dancedance.getTheAnimals().listOut();
|
||||
System.out.println();
|
||||
dancedance.setNetClosed(true);
|
||||
System.out.println(dancedance.getHungryAnimals());
|
||||
System.out.println();
|
||||
dancedance.getTheAnimals().listOut();
|
||||
|
||||
System.out.println();
|
||||
dancedance.getTheAnimals().listOut();
|
||||
System.out.println();
|
||||
dancedance.setNetClosed(false);
|
||||
dancedance.feedAnimals("insects");
|
||||
|
||||
dancedance.setNetClosed(true);
|
||||
|
||||
dancedance.feedAnimals("insects");
|
||||
System.out.println();
|
||||
dancedance.getTheAnimals().listOut();
|
||||
System.out.println();
|
||||
|
||||
System.out.println(dancedance);
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Aviary
|
||||
145
CS1322/final/Animals/Bat.java
Normal file
145
CS1322/final/Animals/Bat.java
Normal file
@@ -0,0 +1,145 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Bat.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 19, 2002
|
||||
* Created the Bat class
|
||||
*
|
||||
* </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, Sep. 19, 2002
|
||||
*/
|
||||
|
||||
public class Bat extends Animal implements FlyingType{
|
||||
|
||||
/**
|
||||
*altitude of the Bat
|
||||
*/
|
||||
private int altitude;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for Bat
|
||||
*@param name, the name of the bat
|
||||
*/
|
||||
public Bat(String name){
|
||||
super(name);
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Modifier for altitude
|
||||
*@param altitude, the value to be applied to altitude
|
||||
*/
|
||||
public void setAltitude(int altitude){
|
||||
this.altitude = altitude;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for altitude
|
||||
*@return the value of altitude
|
||||
*/
|
||||
public int getAltitude(){
|
||||
return altitude;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Give the dangerous value of the Bat
|
||||
*@return false, the Bat is not dangerous
|
||||
*/
|
||||
public boolean isDangerous(){
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*Eat method for Bat
|
||||
*@param food to eat, if it's "insects" isHungry = false
|
||||
*/
|
||||
public void eat(String food)
|
||||
{
|
||||
if(food.equals("insects")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*Find the equality of Bat
|
||||
*@param obj the Object to be compared to
|
||||
*@return the equality value
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
|
||||
if(!(obj instanceof Bat)){
|
||||
return false;
|
||||
}
|
||||
|
||||
Bat temp = (Bat)obj;
|
||||
//added "this." to make explicit, God why do I type this for every
|
||||
//single animal class?
|
||||
if(temp.getAltitude() != this.getAltitude()){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!(super.equals(obj))){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*Modify String for this particular program
|
||||
*@return the data of the Bat as a string statement
|
||||
*/
|
||||
public String toString(){
|
||||
return ("A Bat named " + getName() + " at altitude " + getAltitude() +
|
||||
" that is " + (getIsHungry() ?"":"not ") + "hungry.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Debugging main for class Bat.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Bat bat = new Bat("Batty");
|
||||
|
||||
//bats can live underground
|
||||
bat.setAltitude(-10);
|
||||
|
||||
System.out.println(bat);
|
||||
|
||||
Bat bat2 = new Bat("Pooty");
|
||||
|
||||
bat2.setAltitude(10);
|
||||
bat2.setIsHungry(true);
|
||||
|
||||
System.out.println(bat2);
|
||||
|
||||
System.out.println(bat.equals(bat2));
|
||||
System.out.println(bat.equals(bat));
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Bat
|
||||
179
CS1322/final/Animals/Bird.java
Normal file
179
CS1322/final/Animals/Bird.java
Normal file
@@ -0,0 +1,179 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Bird.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 18, 2002
|
||||
* Created the Bird class
|
||||
* 1.1 Sep. 18, 2002
|
||||
* Finished the Bird class
|
||||
* 1.1f Sep. 18,2002
|
||||
* Cleaned equals() to make it more readable
|
||||
*
|
||||
* </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.1f, Sep. 18, 2002
|
||||
*/
|
||||
|
||||
|
||||
public class Bird extends Animal implements FlyingType{
|
||||
|
||||
/**
|
||||
*whether or not the bird is dangerous
|
||||
*/
|
||||
private boolean dangerous;
|
||||
|
||||
/**
|
||||
*species of the bird
|
||||
*/
|
||||
private String species;
|
||||
|
||||
/**
|
||||
*altitude of the bird
|
||||
*/
|
||||
private int altitude;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*@param name the value to give to name
|
||||
*@param species the value to give species
|
||||
*@param dangerous the value to give dangerous
|
||||
*@param alt the value to give altitude
|
||||
*/
|
||||
public Bird(String name, String species,
|
||||
boolean dangerous, int alt){
|
||||
super(name);
|
||||
//left "this." for alt just for consistency
|
||||
this.dangerous = dangerous;
|
||||
this.species = species;
|
||||
this.altitude = alt;
|
||||
}
|
||||
|
||||
/**
|
||||
*@param name the value to give to name
|
||||
*@param species the value to give species
|
||||
*@param dangerous the value to give dangerous
|
||||
*Altitude will be set to 0
|
||||
*/
|
||||
public Bird(String name, String species, boolean dangerous){
|
||||
this(name,species,dangerous,0);
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Accessor for species
|
||||
*@return the value of species
|
||||
*/
|
||||
public String getSpecies(){
|
||||
return species;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for altitude
|
||||
*@return the altitude
|
||||
*/
|
||||
public int getAltitude(){
|
||||
return altitude;
|
||||
}
|
||||
|
||||
/**
|
||||
*Modifier for altitude
|
||||
*@param the value to be assigned to altitude
|
||||
*/
|
||||
public void setAltitude(int altitude){
|
||||
this.altitude = altitude;
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the danger of the bird
|
||||
*/
|
||||
public boolean isDangerous(){
|
||||
return dangerous;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Modify String for this particular program
|
||||
*@return the data of the Bird as a string statement
|
||||
*/
|
||||
public String toString(){
|
||||
return ("A " + getSpecies() + " named " + getName() + " at altitude " +
|
||||
getAltitude() + " that is " + (isDangerous()?"":"not ") + "dangerous" +
|
||||
" and is " + (getIsHungry()?"":"not ") + "hungry.");
|
||||
}
|
||||
|
||||
/**
|
||||
*Eat method for Bird
|
||||
*@param food to eat, if it's bird seed isHungry = false
|
||||
*/
|
||||
public void eat(String food)
|
||||
{
|
||||
if(food.equals("bird seed")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*equals method for Bird
|
||||
*@param obj the value to be tested
|
||||
*@return equality true/false
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
|
||||
if(!(obj instanceof Bird)){
|
||||
return false;
|
||||
}
|
||||
if(!super.equals(obj)){
|
||||
return false;
|
||||
}
|
||||
|
||||
Bird bTemp = (Bird)obj;
|
||||
//use of "this." to make it explicit
|
||||
if(bTemp.getSpecies() != this.getSpecies()){
|
||||
return false;
|
||||
}
|
||||
if(bTemp.getAltitude() != this.getAltitude()){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/********************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class Bird.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Bird bird = new Bird("Tweety","Canary",true,26);
|
||||
System.out.println(bird);
|
||||
|
||||
Bird bird2 = new Bird("Bob","Eagle",false);
|
||||
bird2.setIsHungry(true);
|
||||
System.out.println(bird2);
|
||||
System.out.println(bird.equals(bird2));
|
||||
System.out.println(bird.equals(bird));
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Bird
|
||||
149
CS1322/final/Animals/Duck.java
Normal file
149
CS1322/final/Animals/Duck.java
Normal file
@@ -0,0 +1,149 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Duck.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 19, 2002
|
||||
* Created the Duck class
|
||||
* 1.1 Sep. 21, 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, Sep. 21, 2002
|
||||
*/
|
||||
|
||||
public class Duck extends Bird implements SwimmingType{
|
||||
|
||||
/**
|
||||
*depth of Duck
|
||||
*/
|
||||
private int depth;
|
||||
|
||||
/**
|
||||
*whether or not the Duck is in flight
|
||||
*/
|
||||
private boolean flying;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for Duck
|
||||
*@param name the name of the duck
|
||||
*/
|
||||
public Duck(String name){
|
||||
super(name,"Duck",false);
|
||||
setDepth(0);
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Modifier for depth
|
||||
*@param the new value for depth
|
||||
*/
|
||||
public void setDepth(int depth){
|
||||
this.depth = depth;
|
||||
flying = false;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for depth
|
||||
*@return the value of depth
|
||||
*/
|
||||
public int getDepth(){
|
||||
return depth;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for flying
|
||||
*@return value of flying
|
||||
*/
|
||||
public boolean isFlying(){
|
||||
return flying;
|
||||
}
|
||||
|
||||
/**
|
||||
*Set the altitude of the Duck and flying to true
|
||||
*@param altitude the height of the duck
|
||||
*/
|
||||
public void setAltitude(int altitude){
|
||||
super.setAltitude(altitude);
|
||||
flying = true;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Find the equality of Duck
|
||||
*@param obj the Object to be compared to
|
||||
*@return the equality value
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
if(!(obj instanceof Bird)){
|
||||
return false;
|
||||
}
|
||||
Duck dTemp = (Duck)obj;
|
||||
if(dTemp.getDepth() != this.getDepth()){
|
||||
return false;
|
||||
}
|
||||
if(dTemp.isFlying() != this.isFlying()){
|
||||
return false;
|
||||
}
|
||||
if(!(super.equals(obj))){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change standard string to output to be used for this specific program
|
||||
* @return phrase as string
|
||||
*/
|
||||
public String toString(){
|
||||
if(isFlying()){
|
||||
return super.toString();
|
||||
}
|
||||
else{ //!isFlying()
|
||||
return ("A swimming duck named " + getName() + " at depth " +
|
||||
getDepth() + " that is " + (getIsHungry()?"":"not ") +
|
||||
"hungry.");
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class Duck.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Duck duck = new Duck("Daffy");
|
||||
System.out.println(duck);
|
||||
|
||||
duck.setAltitude(8);
|
||||
duck.setIsHungry(true);
|
||||
System.out.println(duck);
|
||||
|
||||
System.out.println(duck.equals(duck));
|
||||
System.out.println(duck.equals("bob"));
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Duck
|
||||
108
CS1322/final/Animals/Fish.java
Normal file
108
CS1322/final/Animals/Fish.java
Normal file
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Fish.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 18, 2002
|
||||
* Created the Fish class
|
||||
* 1.1 Sep. 18, 2002
|
||||
* Finished the Fish class
|
||||
* 1.1f Sep. 18, 2002
|
||||
* Cleaned up equals() to make more readable
|
||||
* </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.1f, Sep. 18, 2002
|
||||
*/
|
||||
|
||||
public abstract class Fish extends Animal implements SwimmingType{
|
||||
|
||||
/**
|
||||
*depth of Fish
|
||||
*/
|
||||
private int depth;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for Fish
|
||||
*@param name, the name of the fish
|
||||
*@param depth, the depth of the fish
|
||||
*/
|
||||
public Fish(String name, int depth){
|
||||
super(name);
|
||||
this.setDepth(depth);
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Set the depth of Fish
|
||||
*@param depth the value to be applied
|
||||
*/
|
||||
public void setDepth(int depth){
|
||||
if(depth < 0){
|
||||
this.depth = 0;
|
||||
}
|
||||
else{
|
||||
this.depth = depth;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*Return the depth of the Fish
|
||||
*@return the value of depth
|
||||
*/
|
||||
public int getDepth(){
|
||||
return depth;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Find the equality of Fish
|
||||
*@param obj the Object to be compared to
|
||||
*@return the equality value
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
|
||||
if(!(obj instanceof Fish)){
|
||||
return false;
|
||||
}
|
||||
if(!super.equals(obj)){
|
||||
return false;
|
||||
}
|
||||
|
||||
Fish fTemp = (Fish)obj;
|
||||
//use of "this." is to make it explicit
|
||||
if(fTemp.getDepth() != this.getDepth()){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Debugging main for class Fish.
|
||||
* 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 Fish
|
||||
36
CS1322/final/Animals/FlyingType.java
Normal file
36
CS1322/final/Animals/FlyingType.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* FlyingType.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 21, 2002
|
||||
* Created the FlyingType interface
|
||||
* 1.1 Sep. 29, 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.1, Sep. 29, 2002
|
||||
*/
|
||||
|
||||
public interface FlyingType {
|
||||
|
||||
/**
|
||||
*set altitude of Animal
|
||||
*@param altitude, the altitude of the animal
|
||||
*/
|
||||
public void setAltitude(int alt);
|
||||
|
||||
/**
|
||||
*@return altitude of the animal
|
||||
*/
|
||||
public int getAltitude();
|
||||
|
||||
}// end of class FlyingType
|
||||
170
CS1322/final/Animals/Gator.java
Normal file
170
CS1322/final/Animals/Gator.java
Normal file
@@ -0,0 +1,170 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Gator.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 18, 2002
|
||||
* Created the Gator class
|
||||
* 1.1 Sep. 18, 2002
|
||||
* Finished the Gator Class
|
||||
* 1.1f Sep. 18, 2002
|
||||
* Cleaned up equals() to make more readable
|
||||
* </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.1f, Sep. 18, 2002
|
||||
*/
|
||||
|
||||
public class Gator extends Animal implements SwimmingType{
|
||||
|
||||
/**
|
||||
*depth of the gator
|
||||
*/
|
||||
private int depth;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Gator Constructor
|
||||
*@param name the name of the gator
|
||||
*sets depth to 0
|
||||
*/
|
||||
public Gator(String name){
|
||||
super(name);
|
||||
this.depth = 0;
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*set the depth of the gator
|
||||
*@param depth the value to be applied
|
||||
*/
|
||||
public void setDepth(int depth){
|
||||
if(depth > 50){
|
||||
this.depth = 50;
|
||||
}
|
||||
else {
|
||||
this.depth = depth;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*get the depth of the gator
|
||||
*@return the depth of the gator
|
||||
*/
|
||||
public int getDepth(){
|
||||
return this.depth;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Give the dangerous value of the Gator
|
||||
*@return true, the gator is dangerous
|
||||
*/
|
||||
public boolean isDangerous(){
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*Have the Gator eat some food
|
||||
*acceptible foods: chicken,beef,pork,human,mystery meat
|
||||
*@param if the food is of the above name, set isHungry to false
|
||||
*/
|
||||
public void eat(String food){
|
||||
if(food.equals("chicken")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
else if(food.equals("beef")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
else if(food.equals("pork")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
else if(food.equals("human")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
else if(food.equals("mystery meat")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
}//end of eat(food)
|
||||
|
||||
/**
|
||||
*Find the equality of Gator
|
||||
*@param obj the Object to be compared to
|
||||
*@return the equality value
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
|
||||
if(!(obj instanceof Gator)){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!super.equals(obj)){
|
||||
return false;
|
||||
}
|
||||
|
||||
Gator temp = (Gator)obj;
|
||||
if(temp.getDepth() != this.getDepth()){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}//end of equals(Object)
|
||||
|
||||
/**
|
||||
* Change standard string to output to be used for this specific program
|
||||
*
|
||||
* <br><br>
|
||||
* @return phrase as string
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return ("A Gator named " + getName() + " at depth " + getDepth() +
|
||||
" which is " + ((this.getIsHungry())?"hungry." : "not hungry."));
|
||||
}
|
||||
/********************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class Gator.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Gator gator = new Gator("bob");
|
||||
gator.setIsHungry(true);
|
||||
System.out.println(gator);
|
||||
|
||||
gator.eat("chicken");
|
||||
System.out.println(gator);
|
||||
|
||||
gator.setDepth(100);
|
||||
System.out.println(gator);
|
||||
|
||||
System.out.println(gator.isDangerous());
|
||||
|
||||
Gator gator2 = new Gator("Steve");
|
||||
|
||||
System.out.println(gator.equals(gator));
|
||||
System.out.println(gator.equals(gator2));
|
||||
String bob = "bob";
|
||||
System.out.println(gator.equals(bob));
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Gator
|
||||
106
CS1322/final/Animals/GoldFish.java
Normal file
106
CS1322/final/Animals/GoldFish.java
Normal file
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* GoldFish.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 19, 2002
|
||||
* Created the GoldFish class
|
||||
* 1.1 Sep. 20, 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, Sep. 20, 2002
|
||||
*/
|
||||
|
||||
public class GoldFish extends Fish{
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for GoldFish
|
||||
*@param name to be given to goldfish
|
||||
*@param value to be given to depth
|
||||
*/
|
||||
public GoldFish(String name, int depth){
|
||||
super(name,depth);
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*isDangerous method for fish
|
||||
*@return false
|
||||
*/
|
||||
public boolean isDangerous(){
|
||||
return false; //you TA's obviously don't know the goldfish like i do...
|
||||
}
|
||||
|
||||
/**
|
||||
*Have the Goldfish eat some food
|
||||
*@param if the food is "fish food", set isHungry to false
|
||||
*/
|
||||
public void eat(String food){
|
||||
if(food.equals("fish food")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Change standard string to output to be used for this specific program
|
||||
* @return phrase as string
|
||||
*/
|
||||
public String toString(){
|
||||
return ("A GoldFish named " + getName() + " at depth " + getDepth() +
|
||||
" that is " + (getIsHungry()?"":"not ") + "hungry.");
|
||||
}
|
||||
|
||||
/**
|
||||
*Equals method for GoldFish
|
||||
*@param obj the value to be compared to
|
||||
*@return false if not a GoldFish
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
if(!(obj instanceof GoldFish)){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!(super.equals(obj))){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Debugging main for class GoldFish.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
GoldFish butch = new GoldFish("Butch", -6); //I've seen em leave their cup
|
||||
System.out.println(butch);
|
||||
|
||||
GoldFish butch2 = new GoldFish("Cassidy",54);
|
||||
butch2.setIsHungry(true);
|
||||
|
||||
System.out.println(butch2);
|
||||
System.out.println(butch.equals(butch2));
|
||||
System.out.println(butch.equals(butch));
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class GoldFish
|
||||
98
CS1322/final/Animals/IOHelper.java
Normal file
98
CS1322/final/Animals/IOHelper.java
Normal file
@@ -0,0 +1,98 @@
|
||||
/*====================================================================*/
|
||||
/* D O N O T E D I T T H I S F I L E */
|
||||
/*====================================================================*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* CS1312: IOHelper
|
||||
* <PRE>
|
||||
* IOHelper - a utility for reading input from the keyboard
|
||||
*
|
||||
* Revisions: 3.0 January 19, 2000
|
||||
* Re-created class IOHelper
|
||||
* Completetly Trashed old versions
|
||||
* Removed backwards compatibility
|
||||
*
|
||||
* </PRE>
|
||||
* @author <A HREF="mailto:cs1312@prism.gatech.edu">CS1312</A>
|
||||
* @version Version 3.0, January 19, 2000
|
||||
*/
|
||||
public class IOHelper
|
||||
{
|
||||
/*=================================================================*/
|
||||
/* C O N S T A N T S */
|
||||
/*=================================================================*/
|
||||
|
||||
/**
|
||||
* A Debug flag.
|
||||
*/
|
||||
public static final boolean bDEBUG = false;
|
||||
|
||||
/*=================================================================*/
|
||||
/* C O N S T R U C T O R */
|
||||
/*=================================================================*/
|
||||
|
||||
/**
|
||||
* Declaring the default constructor private prevents anyone from
|
||||
* making an instance of this class.
|
||||
*/
|
||||
private IOHelper()
|
||||
{
|
||||
System.err.println("IOHelper is not meant to be instantiated");
|
||||
} // end of default constructor, IOHelper()
|
||||
|
||||
/*=================================================================*/
|
||||
/* S T A T I C V A R I A B L E S */
|
||||
/*=================================================================*/
|
||||
|
||||
/**
|
||||
* Used by the BufferedReader to retrieve input.
|
||||
* <BR><BR>
|
||||
* @see #bReader bReader
|
||||
*/
|
||||
protected static InputStreamReader iStreamReader = new
|
||||
InputStreamReader(System.in);
|
||||
|
||||
/**
|
||||
* Retrieves input from the user using a wrapped InputStreamReader.
|
||||
* <BR><BR>
|
||||
* @see #iStreamReader iStreamReader
|
||||
*/
|
||||
protected static BufferedReader bReader = new
|
||||
BufferedReader(iStreamReader);
|
||||
|
||||
/*=================================================================*/
|
||||
/* S T A T I C M E T H O D S */
|
||||
/*=================================================================*/
|
||||
|
||||
/**
|
||||
* Retrieves input from the user using a BufferedReader
|
||||
* <BR><BR>
|
||||
* @return a String entered by the User
|
||||
*/
|
||||
public static final String readLine()
|
||||
{
|
||||
String strReturn = "";
|
||||
try
|
||||
{
|
||||
strReturn = bReader.readLine();
|
||||
} // end of try
|
||||
catch (IOException ioe)
|
||||
{
|
||||
if (bDEBUG)
|
||||
{
|
||||
System.err.println("IOHelper: " + ioe.getMessage());
|
||||
} // end of if
|
||||
} // end of catch(IOException)
|
||||
catch (NullPointerException npe)
|
||||
{
|
||||
/* This should NEVER happen */
|
||||
System.err.println("IOHelper: " + npe.getMessage());
|
||||
System.err.println("Please report this error to cs1312");
|
||||
System.exit(1);
|
||||
} // end of catch(NullPointerException)
|
||||
return strReturn;
|
||||
} // end of static method, readLine()
|
||||
|
||||
} // end of class IOHelper
|
||||
132
CS1322/final/Animals/LLNode.java
Normal file
132
CS1322/final/Animals/LLNode.java
Normal file
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* LLNode.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 21, 2002
|
||||
* Created the LLNode class
|
||||
* 1.1 Sep. 21, 2002
|
||||
* Finished class
|
||||
*
|
||||
* </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, Sep. 21, 2002
|
||||
*/
|
||||
|
||||
public class LLNode {
|
||||
|
||||
/**
|
||||
*holds the node data
|
||||
*/
|
||||
private Object data;
|
||||
|
||||
/**
|
||||
*holds the location of the next node
|
||||
*/
|
||||
private LLNode next;
|
||||
|
||||
//nothing to debug that this will help with,
|
||||
//but its in here for good measure
|
||||
private static final boolean bDebug = false;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Defaul LLNode Constructor
|
||||
*both next and data are initialized to null
|
||||
*/
|
||||
public LLNode(){
|
||||
setNext(null);
|
||||
setData(null);
|
||||
}
|
||||
|
||||
/**
|
||||
*LLNode(Object)
|
||||
*next initialized to null
|
||||
*@param o the value to be given to data
|
||||
*/
|
||||
public LLNode(Object o){
|
||||
setNext(null);
|
||||
setData(o);
|
||||
}
|
||||
|
||||
/**
|
||||
*LLNode(Object,LLNode)
|
||||
*@param o, the value of data
|
||||
*@param next, the value of next
|
||||
*/
|
||||
public LLNode(Object o, LLNode next){
|
||||
setNext(next);
|
||||
setData(o);
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Accessor for data
|
||||
*@return value of data
|
||||
*/
|
||||
public Object getData(){
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
*Modifier for data
|
||||
*@param the value for data
|
||||
*/
|
||||
public void setData(Object data){
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for next
|
||||
*@return the next node
|
||||
*/
|
||||
public LLNode getNext(){
|
||||
return next;
|
||||
}
|
||||
|
||||
/**
|
||||
*Modifier for next
|
||||
*@param the new next node
|
||||
*/
|
||||
public void setNext(LLNode next){
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
public String toString(){
|
||||
return (data.toString());
|
||||
}
|
||||
|
||||
/***********************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class LLNode.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Integer bob = new Integer(5);
|
||||
LLNode node = new LLNode(bob);
|
||||
System.out.println(node);
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class LLNode
|
||||
425
CS1322/final/Animals/LinkedList.java
Normal file
425
CS1322/final/Animals/LinkedList.java
Normal file
@@ -0,0 +1,425 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* LinkedList.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 21, 2002
|
||||
* Created the LinkedList class
|
||||
* 1.1 Sep. 26, 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, Sep. 26, 2002
|
||||
*/
|
||||
|
||||
public class LinkedList {
|
||||
|
||||
/**
|
||||
*Front of the list
|
||||
*/
|
||||
private LLNode head;
|
||||
|
||||
/**
|
||||
*Back of the List
|
||||
*/
|
||||
private LLNode tail;
|
||||
|
||||
//there's actually something to debug this time!
|
||||
private static final boolean bDebug = false;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Default constructor for LinkedList
|
||||
*sets head and tail to null
|
||||
*/
|
||||
public LinkedList(){
|
||||
head = null;
|
||||
tail = null;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Add node to front of list
|
||||
*@param o, the data to be stored in the node
|
||||
*/
|
||||
public void addToFront(Object o){
|
||||
LLNode node = new LLNode(o,head);
|
||||
|
||||
if(bDebug){System.out.print("Adding to Front:");}
|
||||
|
||||
head = node;
|
||||
if(tail == null){
|
||||
this.tail = node;
|
||||
}
|
||||
|
||||
if(bDebug){System.out.println("Done.");}
|
||||
}
|
||||
|
||||
/**
|
||||
*Add node to back of list
|
||||
*@param o, the data to be stored in the node
|
||||
*/
|
||||
public void addToBack(Object o){
|
||||
LLNode node = new LLNode(o);
|
||||
|
||||
if(bDebug){System.out.print("Adding to Back:");}
|
||||
|
||||
if(tail != null){
|
||||
tail.setNext(node);
|
||||
}
|
||||
|
||||
tail = node;
|
||||
if(head == null){
|
||||
head = node;
|
||||
}
|
||||
|
||||
if(bDebug){System.out.println("Done.");}
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the data stored by the first node
|
||||
*/
|
||||
public Object getFirst(){
|
||||
if(head != null){
|
||||
return head.getData();
|
||||
}
|
||||
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the data stored by the last node
|
||||
*/
|
||||
public Object getLast(){
|
||||
if(tail != null){
|
||||
return tail.getData();
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the data stored by the first node, destroy first node
|
||||
*/
|
||||
public Object removeFromFront(){
|
||||
if(head == null){
|
||||
return null;
|
||||
}
|
||||
|
||||
if(bDebug){System.out.print("Removing from Front:");}
|
||||
|
||||
Object temp = head.getData();
|
||||
|
||||
head = head.getNext();
|
||||
|
||||
if(head == null){
|
||||
tail = null;
|
||||
}
|
||||
|
||||
if(bDebug){System.out.println("Done.");}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the data stored by the last node, destroy the last node
|
||||
*/
|
||||
public Object removeFromBack(){
|
||||
if(tail == null){
|
||||
return null;
|
||||
}
|
||||
|
||||
if(bDebug){System.out.print("Removing from Back:");}
|
||||
|
||||
Object temp = tail.getData();
|
||||
LLNode current = head;
|
||||
|
||||
if(current != tail){
|
||||
//find second to last node
|
||||
while(current.getNext() != tail){
|
||||
current = current.getNext();
|
||||
}
|
||||
tail = current;
|
||||
tail.setNext(null);
|
||||
}
|
||||
|
||||
else {
|
||||
tail = null;
|
||||
}
|
||||
|
||||
if(tail == null){
|
||||
head = null;
|
||||
}
|
||||
|
||||
if(bDebug){System.out.println("Done.");}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the number of nodes in the LinkedList
|
||||
*/
|
||||
public int size(){
|
||||
|
||||
if(head == null){
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(head == tail){
|
||||
return 1;
|
||||
}
|
||||
|
||||
int count=0;
|
||||
LLNode current = head;
|
||||
|
||||
while(current!= null){
|
||||
current = current.getNext();
|
||||
count++;
|
||||
}
|
||||
|
||||
return (count);
|
||||
}
|
||||
|
||||
/**
|
||||
*Turn the LinkedList into an Array
|
||||
*@return array containing all the data held by the LinkedList
|
||||
*/
|
||||
public Object[] asArray(){
|
||||
|
||||
Object[] array = new Object[this.size()];
|
||||
|
||||
if(head == null){
|
||||
return array;
|
||||
}
|
||||
|
||||
LLNode current = head;
|
||||
int i=0;
|
||||
while(current != null){
|
||||
array[i] = current.getData();
|
||||
i++;
|
||||
current = current.getNext();
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
*temp method to output list on screen
|
||||
*meant for debugging to circumvent the array method
|
||||
*/
|
||||
public void listOut(){
|
||||
LLNode current = head;
|
||||
|
||||
while(current != null){
|
||||
System.out.println(current.getData());
|
||||
current = current.getNext();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*Find the equality of the LinkedList
|
||||
*@param LL, the list to be compared to
|
||||
*@return the equality value
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
if(!(obj instanceof LinkedList)){
|
||||
return false;
|
||||
}
|
||||
|
||||
LinkedList LL = (LinkedList)obj;
|
||||
|
||||
LLNode thisCurrent = this.head;
|
||||
LLNode temp = LL.head;
|
||||
|
||||
if(this.size() != LL.size()){
|
||||
return false;
|
||||
}
|
||||
while(thisCurrent != null){
|
||||
if(!(thisCurrent.getData().equals(temp.getData()))){
|
||||
return false;
|
||||
}
|
||||
thisCurrent = thisCurrent.getNext();
|
||||
temp = temp.getNext();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change standard string to output to be used for this specific program
|
||||
* @return phrase as string
|
||||
*/
|
||||
public String toString(){
|
||||
String ReallyLong;
|
||||
ReallyLong = "[ ";
|
||||
LLNode current = head;
|
||||
while(current != null){
|
||||
ReallyLong += current.getData().toString();
|
||||
if(current.getNext() != null){
|
||||
ReallyLong += ", ";
|
||||
}
|
||||
current = current.getNext();
|
||||
}
|
||||
ReallyLong += "]";
|
||||
|
||||
return ReallyLong;
|
||||
}
|
||||
|
||||
/**
|
||||
*Check if there is an object in the list matching o
|
||||
*@return true if such an object exists
|
||||
*/
|
||||
public boolean contains(Object o){
|
||||
LLNode current = head;
|
||||
while(current != null){
|
||||
if(current.getData().equals(o)){
|
||||
return true;
|
||||
}
|
||||
current = current.getNext();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*Method meant for debugging
|
||||
*clears the list
|
||||
*/
|
||||
public void clear(){
|
||||
head = null;
|
||||
tail = null;
|
||||
}
|
||||
|
||||
/*********************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class LinkedList.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
LinkedList LL = new LinkedList();
|
||||
Object[] array;
|
||||
LinkedList LL2 = new LinkedList();
|
||||
|
||||
System.out.println("test 1");
|
||||
LL.addToFront("2");
|
||||
LL.addToFront("1");
|
||||
LL.addToBack("3");
|
||||
LL.addToBack("4");
|
||||
LL.listOut();
|
||||
array = LL.asArray();
|
||||
|
||||
for(int i=0; i<array.length; i++){
|
||||
System.out.println(array[i]);
|
||||
}
|
||||
|
||||
System.out.println("test 2");
|
||||
LL.removeFromFront();
|
||||
LL.removeFromBack();
|
||||
LL.removeFromBack();
|
||||
|
||||
array = LL.asArray();
|
||||
|
||||
for(int i=0; i<array.length; i++){
|
||||
System.out.println(array[i]);
|
||||
}
|
||||
|
||||
System.out.println(LL.equals(LL2));
|
||||
|
||||
LL.clear();
|
||||
LL.addToFront("1");
|
||||
LL.addToFront("2");
|
||||
LL.addToBack("3");
|
||||
LL2.addToFront("1");
|
||||
LL2.addToFront("2");
|
||||
LL2.addToBack("3");
|
||||
|
||||
System.out.println(LL2.equals(LL));
|
||||
|
||||
LL.clear();
|
||||
LL2.clear();
|
||||
|
||||
LL.addToBack("2");
|
||||
|
||||
LL.addToFront(new Integer(1));
|
||||
LL2.addToBack("2");
|
||||
|
||||
System.out.println(LL.equals(LL2));
|
||||
|
||||
LL2.addToFront(new Integer(1));
|
||||
|
||||
System.out.println(LL2.equals(LL));
|
||||
|
||||
LL2.removeFromBack();
|
||||
|
||||
System.out.println(LL2.equals(LL));
|
||||
System.out.println(LL.equals(LL));
|
||||
LL.clear();
|
||||
System.out.println(LL.equals(LL2));
|
||||
|
||||
//test what autograder finds as wrong
|
||||
//431, 653, 95, 201, 331, 326, 627, 38, 284, 350, 960, 36
|
||||
LL.clear();
|
||||
LL2.clear();
|
||||
|
||||
LL.addToBack(new Integer(36));
|
||||
LL.addToFront(new Integer(960));
|
||||
LL.addToFront(new Integer(350));
|
||||
LL.addToFront(new Integer(284));
|
||||
LL.addToFront(new Integer(38));
|
||||
LL.addToFront(new Integer(627));
|
||||
LL.addToFront(new Integer(326));
|
||||
LL.addToFront(new Integer(331));
|
||||
LL.addToFront(new Integer(201));
|
||||
LL.addToFront(new Integer(95));
|
||||
LL.addToFront(new Integer(653));
|
||||
LL.addToFront(new Integer(431));
|
||||
|
||||
System.out.println(LL.equals(LL));
|
||||
System.out.println(LL.equals(LL2));
|
||||
LL2 = LL;
|
||||
System.out.println(LL.equals(LL2));
|
||||
|
||||
//Holy crap this is a long debug main!
|
||||
LL2.clear();
|
||||
|
||||
LL2.addToFront(new Integer(960));
|
||||
LL2.addToFront(new Integer(350));
|
||||
LL2.addToFront(new Integer(284));
|
||||
LL2.addToFront(new Integer(38));
|
||||
LL2.addToFront(new Integer(627));
|
||||
LL2.addToFront(new Integer(326));
|
||||
LL2.addToFront(new Integer(331));
|
||||
LL2.addToFront(new Integer(201));
|
||||
LL2.addToFront(new Integer(95));
|
||||
LL2.addToFront(new Integer(653));
|
||||
LL2.addToFront(new Integer(431));
|
||||
|
||||
System.out.println(LL.equals(LL2));
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class LinkedList
|
||||
114
CS1322/final/Animals/Shark.java
Normal file
114
CS1322/final/Animals/Shark.java
Normal file
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Shark.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 19, 2002
|
||||
* Created the Shark class
|
||||
* 1.1 Sep. 20, 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, Sep. 20, 2002
|
||||
*/
|
||||
|
||||
public class Shark extends Fish{
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for Shark
|
||||
*@param name of the Shark
|
||||
*@param depth of the shark
|
||||
*/
|
||||
public Shark(String name, int depth){
|
||||
super(name,depth);
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Eat method for Shark
|
||||
*@param food to eat, if it's human, chicken,fish isHungry = false
|
||||
*/
|
||||
public void eat(String food)
|
||||
{
|
||||
if(food.equals("human")){ //mmmmmm...
|
||||
setIsHungry(false);
|
||||
}
|
||||
else if(food.equals("chicken")){ //tastes like chicken
|
||||
setIsHungry(false);
|
||||
}
|
||||
else if(food.equals("fish")){ //IT ATE ITS OWN SUPERCLASS!!!
|
||||
setIsHungry(false);
|
||||
}
|
||||
}
|
||||
/**
|
||||
*Give the dangerous value of the Shark
|
||||
*@return true, the Shark is dangerous
|
||||
*/
|
||||
public boolean isDangerous(){
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*Find the equality of Shark
|
||||
*@param obj the Object to be compared to
|
||||
*@return the equality value
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
|
||||
if(!(obj instanceof Shark)){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!(super.equals(obj))){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*Modify String for this particular program
|
||||
*@return the data of the Shark as a string statement
|
||||
*/
|
||||
public String toString(){
|
||||
return ("A Shark named " + getName() + " at depth " + getDepth() +
|
||||
" that is " + (getIsHungry()?"":"not ") + "hungry.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Debugging main for class Shark.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Shark shark = new Shark("Sharky",150);
|
||||
shark.setIsHungry(true);
|
||||
shark.setDepth(151);
|
||||
System.out.println(shark);
|
||||
|
||||
Shark shark2 = new Shark("Pissey",-52);
|
||||
System.out.println(shark2);
|
||||
|
||||
System.out.println(shark.equals(shark2));
|
||||
System.out.println(shark.equals(shark));
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Shark
|
||||
35
CS1322/final/Animals/SwimmingType.java
Normal file
35
CS1322/final/Animals/SwimmingType.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* SwimmingType.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 21, 2002
|
||||
* Created the SwimmingType interface
|
||||
* 1.1 Sep. 28, 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.1, Sep. 28, 2002
|
||||
*/
|
||||
|
||||
public interface SwimmingType {
|
||||
|
||||
/**
|
||||
*@param depth, the depth to set animal at
|
||||
*/
|
||||
public void setDepth(int depth);
|
||||
|
||||
/**
|
||||
*@return the depth of the animal
|
||||
*/
|
||||
public int getDepth();
|
||||
|
||||
}// end of class SwimmingType
|
||||
535
CS1322/final/Animals/Zoo.java
Normal file
535
CS1322/final/Animals/Zoo.java
Normal file
@@ -0,0 +1,535 @@
|
||||
/**
|
||||
* CS1322: Programming Assignment #2 - Fall 2002
|
||||
*
|
||||
*
|
||||
* <PRE>
|
||||
* Class Zoo - A class designed to provide a menu for users to interact
|
||||
* with to run the Zoo for P2. This class is provided by CS1322,
|
||||
* for you to modify as needed.
|
||||
* You should add your collaboration statement, to this ident box below,
|
||||
* and place your own @author tag below the one given by the author of
|
||||
* the code given.
|
||||
* Revisions 1.0: September 15th, 2002
|
||||
* Received class from CS1322
|
||||
* 1.1: September 28th, 2002
|
||||
* Finished Coding
|
||||
* </PRE>
|
||||
*
|
||||
* @author <A HREF="mailto:adhilton@cc.gatech.edu">Andrew Hilton</A>
|
||||
* @version Version 1.1, September 28th, 2002
|
||||
*/
|
||||
import java.io.*;
|
||||
public class Zoo
|
||||
{
|
||||
/**
|
||||
* This variable contains all of the buildings (Aquariums and
|
||||
* Aviaries) that are in this Zoo.
|
||||
*/
|
||||
private ZooBuilding[] myBuildings;
|
||||
/**
|
||||
* This constant specifies the number of buildings in the Zoo.
|
||||
*/
|
||||
private static final int NUM_BUILDINGS=6;
|
||||
/**
|
||||
* This constant is the main menu choice for putting a new animal
|
||||
* into the Zoo.
|
||||
*/
|
||||
private static final int CHOICE_PUT_ANIMAL=1;
|
||||
/**
|
||||
* This constant is the main menu choice for listing the animals
|
||||
* in the Zoo.
|
||||
*/
|
||||
private static final int CHOICE_LIST_ANIMALS=2;
|
||||
/**
|
||||
* This constant is the main menu choice for feeding the animals in
|
||||
* the Zoo.
|
||||
*/
|
||||
private static final int CHOICE_FEED_ANIMALS=3;
|
||||
/**
|
||||
* This constant is the main menu choice for moving the net in one
|
||||
* of the Aviaries.
|
||||
*/
|
||||
private static final int CHOICE_MOVE_NET=4;
|
||||
/**
|
||||
* This constant is the main menu choice for quitting the program.
|
||||
*/
|
||||
private static final int CHOICE_QUIT=0;
|
||||
/**
|
||||
* This constant specifies the String that is printed to display the
|
||||
* main menu.
|
||||
*/
|
||||
private static final String MAIN_MENU=
|
||||
"Main Zoo Control\n" +
|
||||
"----------------\n" +
|
||||
CHOICE_PUT_ANIMAL+".) Put an animal in a building\n" +
|
||||
CHOICE_LIST_ANIMALS+".) List the animals in a building\n" +
|
||||
CHOICE_FEED_ANIMALS+".) Feed the Animals in a building\n" +
|
||||
CHOICE_MOVE_NET+".) Move the net in an aviary\n"+
|
||||
CHOICE_QUIT+".) Quit\n"+
|
||||
"\nEnter your choice: ";
|
||||
/**
|
||||
* This constant specifies the animal creation menu choice to make a bat.
|
||||
*/
|
||||
private static final int ANIMAL_BAT=1;
|
||||
/**
|
||||
* This constant specifies the animal creation menu choice to make a bird.
|
||||
*/
|
||||
private static final int ANIMAL_BIRD=2;
|
||||
/**
|
||||
* This constant specifies the animal creation menu choice to make a duck.
|
||||
*/
|
||||
private static final int ANIMAL_DUCK=3;
|
||||
/**
|
||||
* This constant specifies the animal creation menu choice to make a gator.
|
||||
*/
|
||||
private static final int ANIMAL_GATOR=4;
|
||||
/**
|
||||
* This constant specifies the animal creation menu choice to make a
|
||||
* goldfish.
|
||||
*/
|
||||
private static final int ANIMAL_GOLDFISH=5;
|
||||
/**
|
||||
* This constant specifies the animal creation menu choice to make a shark.
|
||||
*/
|
||||
private static final int ANIMAL_SHARK=6;
|
||||
/**
|
||||
* This constant specifies the String that is displayed for the animal
|
||||
* creation menu.
|
||||
*/
|
||||
private static final String ANIMAL_MENU=
|
||||
"Choose an Animal Type:\n"+
|
||||
"----------------------\n"+
|
||||
ANIMAL_BAT+".) Bat\n"+
|
||||
ANIMAL_BIRD+".) Bird\n"+
|
||||
ANIMAL_DUCK+".) Duck\n"+
|
||||
ANIMAL_GATOR+".) Gator\n"+
|
||||
ANIMAL_GOLDFISH+".) Goldfish\n"+
|
||||
ANIMAL_SHARK+".) Shark\n"+
|
||||
"\nEnter your choice: ";
|
||||
/**
|
||||
* The default constructor for Zoo.
|
||||
* This constructor initializes myBuildings to be an array of
|
||||
* size NUM_BUILDINGS, then fills it in, alternating between Aviaries
|
||||
* and Aquariums.
|
||||
*/
|
||||
public Zoo()
|
||||
{
|
||||
myBuildings=new ZooBuilding[NUM_BUILDINGS];
|
||||
for(int i=0;i<myBuildings.length;i++)
|
||||
{
|
||||
if(i%2==0)
|
||||
myBuildings[i]=new Aviary("Aviary number "+(i/2+1));
|
||||
else
|
||||
myBuildings[i]=new Aquarium("Aquarium number "+ (i/2+1) );
|
||||
}//for
|
||||
}//Zoo()
|
||||
/**
|
||||
* Reads user input and creates a specified animal.
|
||||
* This method reads in input from the user to determine what
|
||||
* type of Animal to make, and then reads the appropriate information
|
||||
* for that type of Animal.
|
||||
* @return a newly created Animal that matches the user's specifications,
|
||||
* or null if the user selects an invalid Animal number
|
||||
*/
|
||||
private Animal makeAnimalForUser()
|
||||
{
|
||||
System.out.println(ANIMAL_MENU);
|
||||
int choice= readIntFromUser();
|
||||
Animal toRet=null;
|
||||
String name;
|
||||
String species;
|
||||
String dangerous;
|
||||
boolean isDangerous;
|
||||
int depth;
|
||||
switch(choice)
|
||||
{
|
||||
case ANIMAL_BAT:
|
||||
name=promptAndRead("Name: ");
|
||||
toRet=new Bat(name);
|
||||
break;
|
||||
case ANIMAL_BIRD:
|
||||
name=promptAndRead("Name: ");
|
||||
species=promptAndRead("Species: ");
|
||||
|
||||
do
|
||||
{
|
||||
dangerous=promptAndRead("Dangerous [Y/N]: ");
|
||||
}
|
||||
while(!(dangerous.equalsIgnoreCase("Y")||
|
||||
dangerous.equalsIgnoreCase("N")));
|
||||
isDangerous=dangerous.equalsIgnoreCase("Y");
|
||||
toRet= new Bird(name,species,isDangerous);
|
||||
break;
|
||||
case ANIMAL_DUCK:
|
||||
name=promptAndRead("Name: ");
|
||||
toRet= new Duck(name);
|
||||
break;
|
||||
case ANIMAL_GATOR:
|
||||
name=promptAndRead("Name: ");
|
||||
toRet= new Gator(name);
|
||||
break;
|
||||
case ANIMAL_GOLDFISH:
|
||||
name=promptAndRead("Name: ");
|
||||
System.out.print("Depth: ");
|
||||
depth=readIntFromUser();
|
||||
toRet= new GoldFish(name,depth);
|
||||
break;
|
||||
case ANIMAL_SHARK:
|
||||
name=promptAndRead("Name: ");
|
||||
System.out.print("Depth: ");
|
||||
depth=readIntFromUser();
|
||||
toRet= new Shark(name,depth);
|
||||
break;
|
||||
default:
|
||||
System.out.println("That is not a valid animal type");
|
||||
break;
|
||||
}
|
||||
return toRet;
|
||||
}//makeAnimalForUser
|
||||
/**
|
||||
* This method moves the Animals in all of the buildings around
|
||||
* randomly. In addition to moving the Animals around, this method
|
||||
* makes Animals become hungry, at random- each with a 75% chance
|
||||
* of becoming hungry when this method is called.
|
||||
*/
|
||||
private void moveAnimalsAround()
|
||||
{
|
||||
for(int i=0;i<myBuildings.length;i++)
|
||||
{
|
||||
ZooBuilding zb=myBuildings[i];
|
||||
LinkedList ll=zb.getTheAnimals();
|
||||
Object[] animalsInList=ll.asArray();
|
||||
for(int j=0;j<animalsInList.length;j++)
|
||||
{
|
||||
|
||||
if(animalsInList[j] instanceof FlyingType &&
|
||||
animalsInList[j] instanceof SwimmingType)
|
||||
{
|
||||
//this animal can both swim and fly.
|
||||
//better find out where it is
|
||||
FlyingType ft=(FlyingType) animalsInList[j];
|
||||
SwimmingType st = (SwimmingType) animalsInList[j];
|
||||
int alt=ft.getAltitude();
|
||||
int depth=st.getDepth();
|
||||
boolean swimming;
|
||||
if(alt<=0)
|
||||
swimming=true;
|
||||
else
|
||||
swimming=false;
|
||||
if(swimming)
|
||||
{
|
||||
//if near the water's surface, maybe we take off
|
||||
//(1/4 chance)
|
||||
if(depth<=1 &&Math.random()<0.25)
|
||||
{
|
||||
st.setDepth(0);
|
||||
ft.setAltitude((int)(Math.random()*25)+15);
|
||||
}
|
||||
else
|
||||
{
|
||||
int change=(int)((Math.random()*5)-2);
|
||||
depth += change;
|
||||
st.setDepth(depth);
|
||||
}
|
||||
}//swimming
|
||||
else
|
||||
{
|
||||
//flying low. maybe we land and swim?
|
||||
// (1/4 chance)
|
||||
if(alt <=25 && Math.random()<0.25)
|
||||
{
|
||||
ft.setAltitude(0);
|
||||
st.setDepth(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
int change=(int)(Math.random()*48)-24;
|
||||
alt += change;
|
||||
ft.setAltitude(alt);
|
||||
}
|
||||
}//flying
|
||||
}
|
||||
else if(animalsInList[j] instanceof FlyingType)
|
||||
{
|
||||
FlyingType ft=(FlyingType) animalsInList[j];
|
||||
int alt=ft.getAltitude();
|
||||
int change=(int)((Math.random()*20)+(Math.random()*20)-10);
|
||||
alt += change;
|
||||
if(alt<0)
|
||||
{
|
||||
alt=0;
|
||||
}
|
||||
ft.setAltitude(alt);
|
||||
|
||||
}//flying
|
||||
else if(animalsInList[j] instanceof SwimmingType)
|
||||
{
|
||||
SwimmingType st=(SwimmingType) animalsInList[j];
|
||||
//*** YOUR CODE GOES HERE *** (3)
|
||||
int depth = st.getDepth();
|
||||
/************/
|
||||
|
||||
int change=(int)((Math.random()*30)-12);
|
||||
depth+=change;
|
||||
//*** YOUR CODE GOES HERE *** (4)
|
||||
st.setDepth(depth);
|
||||
/*****************/
|
||||
|
||||
}//swimming
|
||||
if(animalsInList[j] instanceof Animal)
|
||||
{
|
||||
if(Math.random()<0.75) //75% chance of animal getting hungry
|
||||
{
|
||||
((Animal)animalsInList[j]).setIsHungry(true);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
System.err.println("Error: Non-animal in array at index "+
|
||||
j+"\n found: "+animalsInList[j]);
|
||||
System.exit(-1);
|
||||
}
|
||||
}//for j
|
||||
}//for i
|
||||
}//moveAnimalsAround()
|
||||
/**
|
||||
* A conveinece method to prompt the user for input and read it.
|
||||
* Prints the prompt, then reads and returns a line of input.
|
||||
* @param prompt is the prompt to print
|
||||
* @return the line of input read from the user.
|
||||
*/
|
||||
private static String promptAndRead(String prompt)
|
||||
{
|
||||
System.out.print(prompt);
|
||||
return IOHelper.readLine();
|
||||
}
|
||||
/**
|
||||
* Reads in an int from the user and returns it.
|
||||
* This method reads lines of input from the user until the user enters
|
||||
* a valid number. When a valid number is entered, it is converted
|
||||
* from a String to an int and returned.
|
||||
* @return the number that the user entered.
|
||||
*/
|
||||
private static int readIntFromUser()
|
||||
{
|
||||
String line="";
|
||||
int choice=0;
|
||||
boolean done=false;
|
||||
do
|
||||
{
|
||||
//***YOUR CODE GOES HERE*** (1)
|
||||
line = IOHelper.readLine();
|
||||
|
||||
//now do conversion of line to an int
|
||||
try
|
||||
{
|
||||
choice=Integer.parseInt(line);
|
||||
done=true;
|
||||
}
|
||||
catch(NumberFormatException nfe)
|
||||
{
|
||||
System.out.println("That was not a number! Try again");
|
||||
}
|
||||
|
||||
}
|
||||
while(!done);
|
||||
return choice;
|
||||
}//readIntFromUser()
|
||||
/**
|
||||
* Selects a building number.
|
||||
* Prompts the user to select a building, continues to reprompt
|
||||
* until a valid building number is entered.
|
||||
* @return the building number entered by the user
|
||||
*/
|
||||
private int chooseBuilding()
|
||||
{
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
System.out.print("Enter a building number");
|
||||
choice=readIntFromUser();
|
||||
}
|
||||
while(choice<0|| choice>=myBuildings.length);
|
||||
return choice;
|
||||
}//chooseBuilding
|
||||
/**
|
||||
* Handles putting a new Animal into the Zoo.
|
||||
* This method reads input from the user to determine which building the
|
||||
* Animal should go into. Then it calls makeAnimalForUser() to
|
||||
* create a new Animal to put in. If the Animal cannot live in that
|
||||
* type of building, an error is printed and the method returns,
|
||||
* otherwise the Animal is added to the building.
|
||||
*/
|
||||
private void doPutAnimal()
|
||||
{
|
||||
int buildingNumber=chooseBuilding();
|
||||
ZooBuilding zb=myBuildings[buildingNumber];
|
||||
Animal toAdd=makeAnimalForUser();
|
||||
if(toAdd==null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if(((myBuildings[buildingNumber] instanceof Aviary) &&
|
||||
!(toAdd instanceof FlyingType))||
|
||||
((myBuildings[buildingNumber] instanceof Aquarium) &&
|
||||
!(toAdd instanceof SwimmingType)))
|
||||
{
|
||||
System.out.println("That animal cannot live in that building!");
|
||||
return;
|
||||
}
|
||||
//***YOUR CODE GOES HERE *** (5)
|
||||
zb.getTheAnimals().addToBack(toAdd);
|
||||
/**********************************/
|
||||
|
||||
}//doPutAnimal()
|
||||
/**
|
||||
* Lists the animals in a building.
|
||||
* This method prompts the user for a building number, then
|
||||
* lists all of the animals in that building.
|
||||
*/
|
||||
private void doListAnimals()
|
||||
{
|
||||
//*** YOUR CODE GOES HERE *** (6)
|
||||
int choice = chooseBuilding();
|
||||
ZooBuilding zb = myBuildings[choice];
|
||||
Animal temp;
|
||||
|
||||
for(int i=0; i<zb.getTheAnimals().size(); i++){
|
||||
temp = (Animal)zb.getTheAnimals().removeFromFront();
|
||||
System.out.println(temp);
|
||||
zb.getTheAnimals().addToBack(temp);
|
||||
}
|
||||
}//doListAnimals()
|
||||
/**
|
||||
* Feeds the Animals in a building.
|
||||
* This method prompts the user for a building number and a type of food,
|
||||
* then feeds that food to the animals in the specified building.
|
||||
*/
|
||||
private void doFeedAnimals()
|
||||
{
|
||||
int buildingNumber=chooseBuilding();
|
||||
ZooBuilding zb=myBuildings[buildingNumber];
|
||||
String food=promptAndRead("What type of food?");
|
||||
//*** YOUR CODE GOES HERE *** (7)
|
||||
zb.feedAnimals(food);
|
||||
|
||||
}//doFeedAnimals()
|
||||
|
||||
/**
|
||||
*prompt for building, check if its aviary,
|
||||
*and if it is, add/remove the net
|
||||
*/
|
||||
private void doMoveNet()
|
||||
{
|
||||
// *** YOUR CODE GOES HERE *** (9)
|
||||
//Let's do this all in Hungarian Notation :)
|
||||
//So MS isn't completely useless, eh?
|
||||
|
||||
String sEntry;
|
||||
int iChoice = 0;
|
||||
boolean bDone = false;
|
||||
|
||||
do{
|
||||
sEntry = promptAndRead("What building?");
|
||||
try
|
||||
{
|
||||
iChoice=Integer.parseInt(sEntry);
|
||||
bDone=true;
|
||||
}
|
||||
catch(NumberFormatException nfe)
|
||||
{
|
||||
System.out.println("That was not a number! Try again");
|
||||
}
|
||||
}while(!bDone);
|
||||
|
||||
if(!(myBuildings[iChoice] instanceof Aviary)){
|
||||
System.out.println("That building is not an Aviary, it has no net");
|
||||
return;
|
||||
}
|
||||
|
||||
boolean bNet;
|
||||
Aviary avTemp = (Aviary)myBuildings[iChoice];
|
||||
bNet = avTemp.getNetClosed();
|
||||
|
||||
if(bNet){
|
||||
avTemp.setNetClosed(false);
|
||||
}
|
||||
else {//!bNet
|
||||
avTemp.setNetClosed(true);
|
||||
}
|
||||
|
||||
}//doMoveNet
|
||||
private void showZooStatus()
|
||||
{
|
||||
System.out.println("Zoo status");
|
||||
System.out.println("----------");
|
||||
for(int i=0;i<myBuildings.length;i++)
|
||||
{
|
||||
System.out.println(myBuildings[i]);
|
||||
if(myBuildings[i].isSafeToEnter())
|
||||
{
|
||||
System.out.println("\t Is safe to enter.");
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("\t Is NOT safe to enter.");
|
||||
}
|
||||
System.out.println("\t Has "+myBuildings[i].getHungryAnimals()+
|
||||
" hungry animals.");
|
||||
}//for i
|
||||
}//showZooStatus
|
||||
/**
|
||||
* Runs the Zoo simulation.
|
||||
* This method displays the menu and performs the requested action
|
||||
* until the user chooses to quit.
|
||||
*/
|
||||
private void run()
|
||||
{
|
||||
int userChoice;
|
||||
do
|
||||
{
|
||||
moveAnimalsAround();
|
||||
showZooStatus();
|
||||
//***YOUR CODE GOES HERE*** (8)
|
||||
System.out.println(MAIN_MENU);
|
||||
/****************************/
|
||||
|
||||
userChoice=readIntFromUser();
|
||||
switch(userChoice)
|
||||
{
|
||||
case CHOICE_PUT_ANIMAL:
|
||||
doPutAnimal();
|
||||
break;
|
||||
case CHOICE_LIST_ANIMALS:
|
||||
doListAnimals();
|
||||
break;
|
||||
case CHOICE_FEED_ANIMALS:
|
||||
doFeedAnimals();
|
||||
break;
|
||||
//*** YOUR CODE GOES HERE *** (10)
|
||||
case CHOICE_MOVE_NET:
|
||||
doMoveNet();
|
||||
break;
|
||||
case CHOICE_QUIT:
|
||||
/* do nothing */
|
||||
break;
|
||||
default:
|
||||
//***YOUR CODE GOES HERE***(2)
|
||||
System.out.println("That is not a valid choice, try again");
|
||||
break;
|
||||
}//switch(userChoice)
|
||||
}
|
||||
while(userChoice!= CHOICE_QUIT);
|
||||
}//run()
|
||||
/**
|
||||
* The starting point for the program- makes a new Zoo, and calls
|
||||
* run() to do the simulation.
|
||||
* @params args IGNORED.
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Zoo myZoo=new Zoo();
|
||||
myZoo.run();
|
||||
}//main()
|
||||
|
||||
}//end class Zoo
|
||||
125
CS1322/final/Animals/ZooBuilding.java
Normal file
125
CS1322/final/Animals/ZooBuilding.java
Normal file
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* ZooBuilding.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 26, 2002
|
||||
* Created the ZooBuilding class
|
||||
* 1.1 Sep. 28, 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, Sep. 28, 2002
|
||||
*/
|
||||
|
||||
public abstract class ZooBuilding {
|
||||
|
||||
/**
|
||||
*theAnimals list meant for storing
|
||||
*the animals of the building
|
||||
*/
|
||||
private LinkedList theAnimals;
|
||||
|
||||
/**
|
||||
*holds the name of the building
|
||||
*/
|
||||
private String name;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/*
|
||||
*Constructor for ZooBuilding
|
||||
*@param name, the name of the zoobuilding
|
||||
*/
|
||||
public ZooBuilding(String name){
|
||||
this.name = name;
|
||||
theAnimals = new LinkedList();
|
||||
}
|
||||
|
||||
/////////////
|
||||
//Accessors//
|
||||
/////////////
|
||||
|
||||
/**
|
||||
*@return the List of Animals
|
||||
*/
|
||||
public LinkedList getTheAnimals(){
|
||||
return theAnimals;
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the value of name
|
||||
*/
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
*@return number of Animals in the Zoo
|
||||
*/
|
||||
public int getHungryAnimals(){
|
||||
int total=0;
|
||||
Animal aTemp;
|
||||
for(int i=0; i<theAnimals.size(); i++){
|
||||
aTemp = (Animal)theAnimals.removeFromBack();
|
||||
if(aTemp.getIsHungry() == true){
|
||||
total++;
|
||||
}
|
||||
theAnimals.addToFront(aTemp);
|
||||
}
|
||||
return total;
|
||||
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Check if its safe to enter
|
||||
*@return yes/no
|
||||
*/
|
||||
public abstract boolean isSafeToEnter();
|
||||
|
||||
/**
|
||||
*Feed the Animals param food
|
||||
*@param food, the food to feed all the animals
|
||||
*/
|
||||
public void feedAnimals(String food){
|
||||
//why feed them if there is a sign clearly saying not to?
|
||||
if(!(isSafeToEnter())){
|
||||
System.out.println("The building is unsafe to enter!");
|
||||
return;
|
||||
}
|
||||
Animal aTemp;
|
||||
for(int i=0; i<theAnimals.size(); i++){
|
||||
aTemp = (Animal)theAnimals.removeFromBack();
|
||||
aTemp.eat(food);
|
||||
theAnimals.addToFront(aTemp);
|
||||
}
|
||||
}
|
||||
|
||||
/*******************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class ZooBuilding.
|
||||
* 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 ZooBuilding
|
||||
498
CS1322/final/Animals/p2.txt
Normal file
498
CS1322/final/Animals/p2.txt
Normal file
@@ -0,0 +1,498 @@
|
||||
CS1322 - Program 2: Its a zoo out there!
|
||||
===============================================================================
|
||||
|
||||
Assigned: Monday, September 18th, 2002
|
||||
Due: Monday, September 30th, 2002 at 23:59:59
|
||||
Stop Accepting: Tuesday, October 1st, 2002 at 08:00:00
|
||||
Phase I Instant Feedback Deadline: Monday, September 23rd, 2002 at 23:59:59
|
||||
Phase V Instant Feedback Deadline: Monday, September 30th, 2002 at 23:59:59
|
||||
|
||||
|
||||
=======================================================================
|
||||
TOPICS
|
||||
=======================================================================
|
||||
|
||||
Topics you should understand coming into this assignment:
|
||||
o Creation and usage of objects and methods
|
||||
o Iteration and Recursion
|
||||
o Arrays
|
||||
|
||||
Topics that you should understand as a result of doing this assignment:
|
||||
o Inheritance
|
||||
o Interfaces
|
||||
o Polymorphism
|
||||
o Dynamic Binding
|
||||
o Linked Lists
|
||||
o Stacks/Queues
|
||||
o Constructor Chaining
|
||||
===============================================================================
|
||||
Program Overview
|
||||
===============================================================================
|
||||
In this program, you are going to be writing code for a zoo. This zoo has
|
||||
Animals of different types: Sharks, GoldFish, Gators, Birds, Ducks, and Bats.
|
||||
You will keep track of the Animals in various places via LinkedLists.
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Phase 1: Your basic Animals *THIS IS AN INSTANT FEEDBACK PHASE*
|
||||
This phase should take you no more than 2 hours, 10 minutes to complete
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The first step is to make an abstract class Animal.
|
||||
This class should have the following variables and methods:
|
||||
|
||||
public abstract boolean isDangerous();
|
||||
public abstract void eat(String food);
|
||||
a private boolean called "isHungry" and appropriate accessor/modifier
|
||||
(setIsHungry/getIsHungry).
|
||||
a private String called "name" and appropriate accessor (No modifier).
|
||||
a .equals method which behaves as follows
|
||||
- if the parameter passed in is not an Animal, return false
|
||||
- if the parameter passed in has a different name return false
|
||||
- if one animal is dangerous and the other is not, return false
|
||||
- if one animal is hungry and the other is not, return false
|
||||
- otherwise, return true
|
||||
a constructor that takes in a name and sets the name variable from it,
|
||||
and should set isHungry to false.
|
||||
Next, you should make the following subclasses of Animal:
|
||||
|
||||
o Fish, which is abstract
|
||||
- Fish have an private int depth, with appropriate accessor and
|
||||
modifier. Since fish cannot swim at a negative depth, anything
|
||||
less than 0 should be replaced by 0 in the modifier.
|
||||
- Fish have a .equals method which behaves as follows
|
||||
* if the parameter passed in is not a Fish, return false
|
||||
* call the super class's .equals method- if it returns
|
||||
false, then return false.
|
||||
* if the parameter passed in is at a different depth,
|
||||
return false
|
||||
* otherwise, return true
|
||||
- Fish have a constructor that takes a name and an initial depth.
|
||||
They should use super() to call the parent constructor with the name.
|
||||
o Gator, which is NOT abstract
|
||||
- Gators are dangerous, so implement the isDangerous() method
|
||||
appropriately.
|
||||
- The Gator should implement the eat method to compare the food
|
||||
passed in to "chicken", "beef", "pork", "human", and "mystery meat",
|
||||
and if it is one of those, setHungry to false. If the food is
|
||||
not acceptable, the Gator ignores it and does not change its
|
||||
isHungry status.
|
||||
- Gators have a private int depth, with appropriate accessor and
|
||||
modifier, however, Gators do not swim below 50 feet under the
|
||||
water- so if a value of more than 50 is passed to the modifier,
|
||||
it is set to 50. Unlike fish, Gators may come out of the water,
|
||||
so they can have a negative depth. (A gator at -5 feet under
|
||||
the water would be on the ground, 5 feet above the water).
|
||||
- Gators have a .equals method which behaves as follows
|
||||
* if the parameter passed in is not a Gator, return false
|
||||
* call the super class's .equals method- if it returns
|
||||
false, then return false.
|
||||
* if the parameter passed in is at a different depth,
|
||||
return false
|
||||
* otherwise, return true
|
||||
- Gators have a constructor that takes a name. The super constructor
|
||||
should be called with this name, and the depth should be set to 0.
|
||||
- Gators have a toString that returns the following:
|
||||
A Gator named <name> at depth <depth> which is [hungry/not hungry].
|
||||
For example,
|
||||
A Gator named Joe at depth 4 which is hungry.
|
||||
o Bird, which is NOT abstract
|
||||
- Some Birds are dangerous, others are not. Make a private instance
|
||||
variable "dangerous" which is a boolean. Implement isDangerous
|
||||
to return this value. There should be no modifier for this
|
||||
variable.
|
||||
- Birds have a "species" which is a String. This variable should
|
||||
have an accessor, but no modifier.
|
||||
- Birds have an "altitude", which is a private int. Make the variable,
|
||||
accessor, and modifier for altitude.
|
||||
- Birds have a constructor
|
||||
public Bird(String name, String species,
|
||||
boolean dangerous, int alt)
|
||||
which calls the super constructor and set the variables
|
||||
appropriately.
|
||||
- Birds have an eat method which checks if the parameter is
|
||||
"bird seed", and if so the bird eats it and becomes not hungry.
|
||||
- Birds have a toString() method that returns the following:
|
||||
A <species> named <name> at altitude <alt> that is [dangerous/not dangerous]
|
||||
and is [hungry/not hungry].
|
||||
For example:
|
||||
A wren named Chirpy at altitude 50 that is dangerous and hungry.
|
||||
- Birds have a constructor
|
||||
public Bird(String name, String species,boolean dangerous)
|
||||
which chains to the other constructor with an altitude of 0.
|
||||
- Birds have a .equals method which behaves as follows:
|
||||
* if the parameter passed in is not a Bird, return false
|
||||
* call the super class's .equals method- if it returns
|
||||
false, then return false.
|
||||
* if the species or altitudes are not the same, return false.
|
||||
* otherwise, return true.
|
||||
|
||||
At this point, you should have written 4 classes- Animal, Fish, Gator,
|
||||
and Bird. Since Animal and Fish are abstract, you cannot make instances
|
||||
of them directly, however, you should test Gator and Bird in main methods
|
||||
inside those classes prior to submitting for instant feedback.
|
||||
When you think that your code is ready for feedback, submit it as p2-phase1
|
||||
on WebWork and get feedback.
|
||||
|
||||
Reminder: Don't forget about "Add as Zip". Since this program will have
|
||||
several files to submit, you will want to use this feature. Zip
|
||||
your java files up, then select the zip file- choose "Add as Zip" instead
|
||||
of add. WebWork will upload the zip file, then unzip it. When it redisplays
|
||||
the page, it will list all of the java files that were inside the zip.
|
||||
Since you do not have to choose each file separately- this will save
|
||||
you time!
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Phase 2: More animals!
|
||||
This phase should take you no longer than 3 hours, 30 minutes to complete.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
At this point, you should have made your Animal, Fish, Gator, and Bird classes
|
||||
working correctly from getting instant feedback on them- this should also mean
|
||||
that you are comfortable with
|
||||
- inheritance
|
||||
- abstract classes
|
||||
- .equals methods
|
||||
- toString methods
|
||||
|
||||
Now you are going to write some more Animal types on your own.
|
||||
For each type, you will write the eat() method similarly to what
|
||||
was done before. You will simply be given a list of foods that the
|
||||
Animal will eat.
|
||||
|
||||
o Bat is subclass of Animal
|
||||
- Bats eat "insects"
|
||||
- Bats are not dangerous [implement isDangerous() accordingly].
|
||||
- Bats have an altitude, which is an int, with appropriate accessor and
|
||||
modifier methods.
|
||||
- A Bat is equal to another Object iff (if and only if) the other object
|
||||
is a Bat at the same altitude, and the super class's .equals returns
|
||||
true.
|
||||
- Bats have a toString() that returns the following:
|
||||
A Bat named <name> at altitude <alt> that is [hungry/not hungry].
|
||||
- Bats have a constructor
|
||||
public Bat(String name)
|
||||
which chains to the super constructor with their name.
|
||||
o Shark is a subclass of Fish
|
||||
- Sharks eat "human", "chicken", and "fish".
|
||||
- Sharks have a constructor that takes a name and an initial depth.
|
||||
They should use super() to call the parent constructor with both values.
|
||||
- Sharks are dangerous [implement isDangerous() accordingly].
|
||||
- A Shark is equal to another Object iff the other Object is a Shark,
|
||||
and the superclass .equals returns true.
|
||||
- Sharks have a toString() that returns the following:
|
||||
A Shark named <name> at depth <depth> that is [hungry/not hungry].
|
||||
o GoldFish is a subclass of Fish
|
||||
- GoldFish eat "fish food"
|
||||
- GoldFish have a constructor that takes a name and an initial depth.
|
||||
They should use super() to call the parent constructor with both values.
|
||||
- GoldFish are not dangerous [implement isDangerous() accordingly].
|
||||
- A GoldFish is equal to another Object iff the other Object is a GoldFish,
|
||||
and the superclass .equals returns true.
|
||||
- GoldFish have a toString() that returns the following:
|
||||
A GoldFish named <name> at depth <depth> that is [hungry/not hungry].
|
||||
o Duck is a subclass of Bird
|
||||
- Ducks use their inherited behavior to eat.
|
||||
- Ducks can also swim, so they have a depth below the water- the depth
|
||||
variable should be an int, with appropriate accessor and modifier
|
||||
[see next item].
|
||||
- Ducks have a boolean to indicate whether they are swimming or flying.
|
||||
This boolean should be called "flying" and should have an accessor
|
||||
ONLY [called isFlying]. The modifier for setAltitude from
|
||||
the parent class should be overridden to
|
||||
- set flying to true
|
||||
- call the method in the parent class
|
||||
The modifier for depth should
|
||||
- set the depth appropriately
|
||||
- set flying to false
|
||||
- A Duck is equal to another Object iff the other Object is a Duck,
|
||||
the two Ducks are at the same depth, both are flying or both are
|
||||
swimming, and the super class's .equals returns true.
|
||||
- Ducks have a toString that depends on whether the duck is
|
||||
flying or swimming.
|
||||
o If the Duck is flying, the toString should simply call the super
|
||||
class's toString and return the value it returns.
|
||||
o If the Duck is swimming, the toString should return the following:
|
||||
A swimming duck named <name> at depth <depth> that is [hungry/not hungry].
|
||||
- Ducks have a constructor
|
||||
public Duck(String name)
|
||||
which chains to the super constructor and passes in the name, the
|
||||
species "Duck", false for dangerous. After calling the super
|
||||
constructor, setDepth should be used to set the depth to 0.
|
||||
|
||||
At this point, you should have the following concrete [not abstract] classes
|
||||
which should each have test mains that completely test them:
|
||||
- Shark
|
||||
- GoldFish
|
||||
- Gator
|
||||
- Bird
|
||||
- Duck
|
||||
- Bat
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Phase 3: Things that fly and things that swim- Interfaces
|
||||
This phase should take you no longer than 20 minutes to complete
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Did you notice that many of the Animals had method that were similar?
|
||||
Go back and look at which Animals can fly [have an altitude] and which Animals
|
||||
can swim [have a depth].
|
||||
|
||||
Flying Animals: Birds (includes Ducks) and Bats
|
||||
Swimming Animals: Fish (includes Sharks & GoldFish) Gators, and Ducks
|
||||
|
||||
Wouldn't it be nice to have a type for things that can fly and a type
|
||||
for things that can swim? They all have common behaviors, so it seems
|
||||
logical. One idea would be to make some parent class- maybe have
|
||||
FlyingAnimal and SwimmingAnimal as abstract subclasses of Animal....
|
||||
But what about the Ducks? We can only inherit from one class, and
|
||||
Ducks can both swim and fly! So we need interfaces. Interfaces are
|
||||
useful in situations where you want to have a type for "all things that
|
||||
can _________"- in this case, "all things that can fly" and "all things
|
||||
that can swim". Note that a class is an actual type of object. An interface
|
||||
is a type also, but it tends to be more for a category of types that
|
||||
can do similar things. So now we will create two interfaces:
|
||||
|
||||
o The FlyingType interface should have two methods in it:
|
||||
public void setAltitude(int alt);
|
||||
public int getAltitude();
|
||||
o The SwimmingType interface should have two methods in it:
|
||||
public void setDepth(int depth);
|
||||
public int getDepth();
|
||||
|
||||
Now, go back to Bird and Bat- declare them as implementing FlyingType.
|
||||
Then, go back to Fish, Gator, and Duck- declare each of them as implementing
|
||||
SwimmingType.
|
||||
|
||||
Note that their subclasses automatically implement the interfaces since their
|
||||
parent classes do.
|
||||
|
||||
By this time, you should have the following java files made:
|
||||
- Animal.java
|
||||
- Fish.java
|
||||
- Shark.java
|
||||
- GoldFish.java
|
||||
- Gator.java
|
||||
- Bird.java
|
||||
- Duck.java
|
||||
- Bat.java
|
||||
- FlyingType.java
|
||||
- SwimmingType.java
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Phase 4: Making a LinkedList *THIS IS AN INSTANT FEEDBACK PHASE*
|
||||
This phase should not take you longer than 4 hours
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Now, we are going to move to something slightly different. We have
|
||||
spent the past three phases creating an inheritance structure for animals.
|
||||
Now we are going to make a LinkedList that we can use to hold things in
|
||||
a dynamic data structure. LinkedLists in java are slightly different
|
||||
from those in scheme, but they have some striking similarities.
|
||||
|
||||
- First, you will need to create a class LLNode.
|
||||
o This class should have two instance variables:
|
||||
private Object data;
|
||||
private LLNode next;
|
||||
Each of these should have an appropriate accessor and modifier.
|
||||
o There should be three constructors
|
||||
public LLNode() //both next and data are initialized to null
|
||||
public LLNode(Object o) //next is initialized to null, data to o
|
||||
public LLNode(Object o, LLNode next) //both fields initialized from the
|
||||
//params
|
||||
o There should be a toString() method which simply returns the
|
||||
data's toString()
|
||||
For those of you who took scheme, this should sound quite similar
|
||||
to a cons cell. Cons cells had "first" and "rest", LLNodes have
|
||||
"data" and "next".
|
||||
- Now, create a class LinkedList.
|
||||
o This class should have two instance variables
|
||||
private LLNode head;
|
||||
private LLNode tail;
|
||||
head should always reference the front (first item) of the list
|
||||
tail should always reference the back (last item) of the list
|
||||
o There should be a default constructor which initializes both
|
||||
variables to null.
|
||||
o There should be a method
|
||||
public void addToFront(Object o)
|
||||
which puts o into a new LLNode and adds it to the front of the list.
|
||||
When this method returns, head should reference the LLNode containing o.
|
||||
Tail should reference this node iff it is the only node in the list.
|
||||
o There should be a method
|
||||
public void addToBack(Object o)
|
||||
which puts o into a new LLNode and adds it to the end of the list.
|
||||
When the method returns, tail should reference the LLNode containing o.
|
||||
Head should reference this node iff it is the only node in the list.
|
||||
o There should be a method
|
||||
public Object getFirst()
|
||||
which returns [without destroying] the first item in the list.
|
||||
This should return the data, not the node.
|
||||
o There should be a method
|
||||
public Object getLast()
|
||||
which returns [without destroying] the last item in the list.
|
||||
This should return the data, not the node.
|
||||
o There should be a method
|
||||
public Object removeFromFront()
|
||||
which removes and returns the first item in the list.
|
||||
This should return the data, not the node.
|
||||
o There should be a method
|
||||
public Object removeFromBack()
|
||||
which removes and returns the last item in the list.
|
||||
This should return the data, not the node.
|
||||
Note that for both removes methods, if the list becomes empty, both
|
||||
head and tail should be set to null.
|
||||
o The method
|
||||
public int size()
|
||||
which returns a count of the number of items in the list.
|
||||
o The method
|
||||
public Object[] asArray()
|
||||
which returns an array containing all of the items in the list in order.
|
||||
Element 0 of the array should have the data from the head of the list.
|
||||
o The .equals method should return true iff the lists are both the
|
||||
same length and the corresponding elements all are equal to each other
|
||||
according to their .equals methods. NOTE: THIS SHOULD NOT HAVE ANY
|
||||
SIDE EFFECTS ON THE LIST. Be VERY CAREFUL NOT TO DESTROY YOUR LIST.
|
||||
Also: Note that using Strings to compare the lists is NOT a valid
|
||||
approach. The LinkedList containing the String "1" and the LinkedList
|
||||
containing the Integer 1 are completely different, but have the
|
||||
same String representations.
|
||||
o The toString method should return a String that looks like
|
||||
[ first elem, second elem, third elem ]
|
||||
(where "first elem" "second elem" and "third elem" are the
|
||||
toString()s of the first, second, and third items in a three item
|
||||
list respectively..)
|
||||
There should be a "[", followed by the toString() of each data item
|
||||
in order, separated by commas, and ending with a "]".
|
||||
o The method
|
||||
public boolean contains(Object o)
|
||||
which returns true iff there is an Object in the list that .equals(o).
|
||||
Test your LinkedList and LLNode classes, then submit LLNode.java and
|
||||
LinkedList.java for instant feedback as p2-Phase4.
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Phase 5: Aviaries and Aquariums.
|
||||
This phase should not take longer than 1.5 hours
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Now we are going to make Aviaries (places where flying things live) to hold
|
||||
our flying creatures, and Aquariums to hold our swimming creatures.
|
||||
In our zoo, Aviaries have a net which can be closed across the aviary at 75
|
||||
feet up. It is safe to enter an aviary (to feed animals etc) iff the net
|
||||
is closed, and all dangerous animals are above it. It is safe to enter
|
||||
an Aquarium iff all dangerous animals are below 30 feet deep.
|
||||
Note that you may assume that only FlyingType Animals live in Aviaries,
|
||||
and only SwimmingType Animals live in Aquariums.
|
||||
|
||||
- Make an abstract class ZooBuilding which has
|
||||
- public abstract boolean isSafeToEnter()
|
||||
- a private LinkedList called "theAnimals"
|
||||
- an accessor for theAnimals (no modifier)
|
||||
- a private String for name
|
||||
- an accessor for name (no modifier)
|
||||
- a constructor which takes in a name, sets the name variable to
|
||||
that name and initializes theAnimals to a new LinkedList.
|
||||
- public int getHungryAnimals()
|
||||
which returns a count of how many hungry animals are in the Zoo.
|
||||
- public void feedAnimals(String food)
|
||||
this method first checks if it is safe to enter the building.
|
||||
if it is not safe, this method should print
|
||||
"The building is unsafe to enter!" and then return.
|
||||
if it is safe, it should call make all Animals eat() the food
|
||||
passed in (some of them will not like it, but that is their
|
||||
choice)
|
||||
|
||||
- Make a class Aviary which is a subclass of ZooBuilding and has
|
||||
- an instance variable (with accessors and modifiers) called netClosed
|
||||
which is a boolean. The accessor should be called getNetClosed
|
||||
and the modifier should be called setNetClosed.
|
||||
- the method
|
||||
public boolean isSafeToEnter()
|
||||
which returns true if the building is safe to enter, false if
|
||||
not- according to the criteria above.
|
||||
- a constructor which takes the name and chains to the super constructor
|
||||
- a toString() method which returns
|
||||
An Aviary named <name>.
|
||||
- Make a class Aquarium which is a subclass of ZooBuilding and has
|
||||
- the method
|
||||
public boolean isSafeToEnter()
|
||||
which returns true if the building is safe to enter, false if
|
||||
not- according to the criteria above.
|
||||
- a constructor which takes the name and chains to the super constructor
|
||||
- a toString() method which returns
|
||||
An Aquarium named <name>.
|
||||
|
||||
|
||||
Be sure to test your Aviary and Aquarium completely before proceeding to
|
||||
phase 6.
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Phase 6:
|
||||
This phase should not take longer than 1 hour
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
In this phase, you will be making a menu to control your program.
|
||||
Since we have not covered IO yet, most of this code is given to you.
|
||||
|
||||
-> Open and look over Zoo.java <-
|
||||
|
||||
Zoo.java contains a mostly complete implementation of the zoo menu.
|
||||
You only have to do a few things.
|
||||
Look for the places that say
|
||||
|
||||
//***YOUR CODE GOES HERE*** (some number)
|
||||
|
||||
These are where you will be adding code to this file. The directions
|
||||
below are numbered to match the numbers in those comments.
|
||||
|
||||
1. Use IOHelper.readLine() to read a line of input from the user.
|
||||
Store that line of input into the "line variable".
|
||||
2. Print out the message "That is not a valid choice, try again"
|
||||
when the user's selection is not valid.
|
||||
3. Replace the line
|
||||
int depth=0;
|
||||
with the appropriate line of code to get the depth from the current
|
||||
SwimmingType animal.
|
||||
|
||||
4. Add a line of code to set the depth of the current SwimmingType
|
||||
animal to be depth.
|
||||
5. Add the Animal that the user created (toAdd) to the selected building
|
||||
(zb).
|
||||
6. Prompt the user to select a building, then list all of the animals
|
||||
in the building by printing the toString() of its LinkedList of
|
||||
animals. When prompting for a building, find and use the method
|
||||
in Zoo specifically designed for that.
|
||||
7. Use the food (food)that the user picked to feed the animals in the selected
|
||||
building (zb).
|
||||
8. Print the menu (find the appropriate constant).
|
||||
9. Fill in (and javadoc!) the method doMoveNet() so that it
|
||||
- prompts for a building number
|
||||
- checks if the building is an Aviary
|
||||
- if the building is not an Aviary, print
|
||||
"That building is not an Aviary, it has no net"
|
||||
- otherwise toggle (change true to false, false to true) the
|
||||
value of the netClosed on that aviary.
|
||||
10. Add a case to the menu switch case statement to handle the choice
|
||||
for moving the net. Be sure to use the appropriate constant.
|
||||
------------------------------------------------------------------------------
|
||||
DON'T FORGET TO SUBMIT AS P2 SO YOUR ASSIGNMENT WILL BE GRADED.
|
||||
|
||||
ALSO- DON'T FORGET TO PRACTICE "SAFE SUBMIT"
|
||||
Files to turnin for p2 [make a zip file and use "Add as Zip", since there are
|
||||
17 files to submit- it will be much faster for you].
|
||||
|
||||
Animal.java
|
||||
Fish.java
|
||||
Shark.java
|
||||
GoldFish.java
|
||||
Gator.java
|
||||
Bird.java
|
||||
Bat.java
|
||||
Duck.java
|
||||
FlyingType.java
|
||||
SwimmingType.java
|
||||
LLNode.java
|
||||
LinkedList.java
|
||||
ZooBuilding.java
|
||||
Aquarium.java
|
||||
Aviary.java
|
||||
Zoo.java
|
||||
IOHelper.java
|
||||
[any other files needed to make your submission compile and run]
|
||||
|
||||
|
||||
3
CS1322/final/Fish.java
Normal file
3
CS1322/final/Fish.java
Normal file
@@ -0,0 +1,3 @@
|
||||
public class Fish extends Animal implements UnderwaterType{
|
||||
public void submerge(){}
|
||||
}
|
||||
6
CS1322/final/Test.java
Normal file
6
CS1322/final/Test.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public class Test{
|
||||
public void main(String srgs[]){
|
||||
Animal a = new Fish();
|
||||
a.submerge();
|
||||
}
|
||||
}
|
||||
3
CS1322/final/UnderwaterType.java
Normal file
3
CS1322/final/UnderwaterType.java
Normal file
@@ -0,0 +1,3 @@
|
||||
public interface UnderwaterType{
|
||||
public void submerge();
|
||||
}
|
||||
382
CS1322/final/cs1322-f_survey.txt
Normal file
382
CS1322/final/cs1322-f_survey.txt
Normal file
@@ -0,0 +1,382 @@
|
||||
This survey is intended to help us understand your opinions about CS
|
||||
1322. Please answer the questions honestly and openly. We value your
|
||||
views about the course, and we will use feedback from this survey in
|
||||
the future. For multiple choice questions, please circle the
|
||||
letter/number of your reply. If you have additional comments for which
|
||||
you do not have space, please feel free to attach other pages.
|
||||
|
||||
|
||||
Term: Fall 2002 Your major: __________________________
|
||||
|
||||
|
||||
1. OVERALL
|
||||
|
||||
Your lecturer: _________________
|
||||
|
||||
Which lecturer's class did you attend most often? ________________
|
||||
|
||||
Your TA: __________________________
|
||||
|
||||
|
||||
Most previous CS course experience
|
||||
1) Took CS1 Pseudocode
|
||||
2) Took CS1 Scheme
|
||||
3) Took CS1-X Accelerated Scheme
|
||||
4) Transfer student (Language used: _______________________________)
|
||||
5) Already took 1322 before
|
||||
|
||||
Grade received in that course: _______________
|
||||
|
||||
Academic year?
|
||||
|
||||
Freshman Sophomore Junior Senior Graduate
|
||||
|
||||
What were the best things about this course?
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
What were the worst things about this course?
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
----------------------------------------------------------------------
|
||||
|
||||
2. LECTURE
|
||||
|
||||
What percentage of class lectures did you attend? ________________
|
||||
|
||||
If you did not attend class regularly, why?
|
||||
|
||||
|
||||
|
||||
In your opinion, how important is attending lecture to doing well in
|
||||
class?
|
||||
|
||||
1) Does not matter
|
||||
2) It has a small impact
|
||||
3) It is moderately important
|
||||
4) It is quite important
|
||||
5) It is absolutely essential
|
||||
|
||||
Please rate your lecturer's teaching ability
|
||||
1) Very Good
|
||||
2) Good
|
||||
3) OK
|
||||
4) Bad
|
||||
5) Very Bad
|
||||
|
||||
Please critique your lecturer's in-class teaching abilities,
|
||||
presentation skills, etc.:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
----------------------------------------------------------------------
|
||||
|
||||
3. RECITATION
|
||||
|
||||
|
||||
What percentage of recitations, excluding test dates, did you attend?
|
||||
|
||||
________________
|
||||
|
||||
|
||||
In your opinion, how important is attending recitation to doing well in
|
||||
class?
|
||||
|
||||
1) Does not matter
|
||||
2) It has a small impact
|
||||
3) It is moderately important
|
||||
4) It is quite important
|
||||
5) It is absolutely essential
|
||||
|
||||
|
||||
How effective were the TAs in teaching recitation?
|
||||
|
||||
1) I would not know; I did not attend recitations beyond tests.
|
||||
2) They could not explain things very well.
|
||||
3) They were OK. Some things were clear, some weren't.
|
||||
4) They did a good job overall.
|
||||
5) Outstanding. I learned most of the course material from them.
|
||||
|
||||
|
||||
----------------------------------------------------------------------
|
||||
|
||||
4. TEACHING ASSISTANTS
|
||||
|
||||
Please rate your TA's teaching ability
|
||||
1) Very Good
|
||||
2) Good
|
||||
3) OK
|
||||
4) Bad
|
||||
5) Very Bad
|
||||
|
||||
Write any comments about your TA's teaching ability, etc. that you
|
||||
feel you should share with us.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
How much time did you spend with your TA each week?
|
||||
|
||||
1) Very little or no time
|
||||
2) Less than 15 minutes
|
||||
3) About a half hour
|
||||
4) About an hour
|
||||
5) Several hours
|
||||
|
||||
|
||||
Characterize the quality of the assistance provided by your TA, in general.
|
||||
|
||||
1) Not helpful; could not provide assistance at all
|
||||
2) Somewhat helpful, often not able to provide assistance
|
||||
3) Mostly helpful, often provided valuable assistance
|
||||
4) Very helpful, always could help you make progress
|
||||
|
||||
|
||||
Where there any other TAs that you found particularly helpful
|
||||
(ie during lab hours, etc)
|
||||
|
||||
|
||||
|
||||
----------------------------------------------------------------------
|
||||
|
||||
5. EVALUATION MATERIALS
|
||||
|
||||
I felt the programming assignments were
|
||||
|
||||
1) Extremely boring
|
||||
2) Somewhat boring
|
||||
3) So-so
|
||||
4) Fairly interesting
|
||||
5) Very interesting
|
||||
|
||||
|
||||
|
||||
During tests, I was ...
|
||||
|
||||
1) Unable to answer all the questions in the given time
|
||||
2) Always rushed for time, but able to complete the test
|
||||
3) Sometimes rushed for time, but able to complete
|
||||
4) Able to complete the test in time
|
||||
5) Able to leave early
|
||||
|
||||
|
||||
I felt the tests were
|
||||
|
||||
1) Too easy
|
||||
2) Easier than expected
|
||||
3) About what I expected
|
||||
4) More difficult than expected
|
||||
5) Too difficult
|
||||
|
||||
|
||||
Please characterize the practice test materials
|
||||
|
||||
1) What? There were practice tests?
|
||||
2) They were not helpful
|
||||
3) They were somewhat helpful in preparation for tests
|
||||
4) They were very helpful in preparation for tests
|
||||
5) They were all I studied for the tests
|
||||
|
||||
|
||||
|
||||
Please characterize the autograding tools:
|
||||
|
||||
1) Autograding was not useful; there's no need for it in the future
|
||||
2) Autograding was useful in debugging my programs, but not essential
|
||||
3) Autograding was fairly useful; it caught many mistakes
|
||||
4) Autograding was an essential part of my learning
|
||||
|
||||
|
||||
Please provide below any comments that you have concerning any of the
|
||||
evaluation materials mentioned above (tests, programs, practice
|
||||
tests, autograder):
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
----------------------------------------------------------------------
|
||||
|
||||
6. RESOURCES
|
||||
|
||||
Please characterize how much you used each of the course resources
|
||||
listed below on the following scale:
|
||||
|
||||
1 2 3 4 5
|
||||
Never Used it Used it Used it Used it
|
||||
used it not often moderately often extensively
|
||||
|
||||
|
||||
______ Weiss textbook
|
||||
|
||||
______ Some other book(s)
|
||||
|
||||
______ Lecture slides
|
||||
|
||||
______ Lectures
|
||||
|
||||
______ Recitations
|
||||
|
||||
______ Newsgroup
|
||||
|
||||
______ Collaborations with other students
|
||||
|
||||
______ TAs
|
||||
|
||||
______ Other: _________________
|
||||
|
||||
|
||||
Please characterize the usefulness (value for learning) of each of
|
||||
the course resources listed below on the following scale:
|
||||
|
||||
1 2 3 4 5
|
||||
Useless Not very Moderately Very Essential
|
||||
useful useful useful
|
||||
|
||||
______ Weiss textbook
|
||||
|
||||
______ Some other book(s)
|
||||
|
||||
______ Lecture slides
|
||||
|
||||
______ Lectures
|
||||
|
||||
______ Recitations
|
||||
|
||||
______ Newsgroup
|
||||
|
||||
______ Collaborations with other students
|
||||
|
||||
______ TAs
|
||||
|
||||
______ Other: _________________
|
||||
|
||||
|
||||
----------------------------------------------------------------------
|
||||
|
||||
7. COLLABORATION
|
||||
|
||||
How did you work on your programs?
|
||||
|
||||
1) I worked alone
|
||||
2) I did my own work, but assisted others
|
||||
3) I worked with a group, but did my own coding
|
||||
4) I worked with a group, and we all coded together
|
||||
5) I worked with a group, but relied on others for much of the coding
|
||||
6) I got mostly complete programs from others to turn in
|
||||
|
||||
|
||||
|
||||
If you had to code the assigments over again, today, all alone,
|
||||
could you do it without collaboration?
|
||||
|
||||
1) Sure, no problem
|
||||
2) Yes, but it would take a little effort
|
||||
3) With some difficulty
|
||||
4) With great difficulty
|
||||
5) No way
|
||||
|
||||
|
||||
What did you think of the collaboration policy in this course?
|
||||
Please provide any pertinent comments below:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
----------------------------------------------------------------------
|
||||
|
||||
8. OVERALL
|
||||
|
||||
How would you describe your programming abilities, after CS 1322?
|
||||
|
||||
1) I'm still not a very good programmer
|
||||
2) I learned a few things, but I'm don't feel comfortable coding
|
||||
3) I'm OK---I could write programs for other classes
|
||||
4) I can (truthfully) put down Java programming on a resume
|
||||
5) I'm on the verge of becoming a java god
|
||||
|
||||
|
||||
Please characterize the workload of this course
|
||||
|
||||
1) Very light, no problem
|
||||
2) Less than other comparable GT courses
|
||||
3) Reasonable, similar to other comparable GT courses
|
||||
4) Higher than other comparable GT courses
|
||||
5) Extreme, very burdensome
|
||||
|
||||
|
||||
What grade do you expect in the course?
|
||||
|
||||
________
|
||||
|
||||
|
||||
What were the most important things that you learned from this course?
|
||||
|
||||
|
||||
|
||||
|
||||
If you had a friend enrolling in CS 1322, what advice would you give
|
||||
them about the course and how to succeed?
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
1417
CS1322/final/cs1322-finalpract.txt
Normal file
1417
CS1322/final/cs1322-finalpract.txt
Normal file
File diff suppressed because it is too large
Load Diff
BIN
CS1322/final/cs1322-finalref.pdf
Normal file
BIN
CS1322/final/cs1322-finalref.pdf
Normal file
Binary file not shown.
9
CS1322/final/final.java
Normal file
9
CS1322/final/final.java
Normal file
@@ -0,0 +1,9 @@
|
||||
public class finals{
|
||||
public static void main(String srgs[]){
|
||||
char c= (char)( '2' + 5);
|
||||
System.out.println(c);
|
||||
|
||||
int x = '4' / '5';
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
||||
9
CS1322/final/finals.java
Normal file
9
CS1322/final/finals.java
Normal file
@@ -0,0 +1,9 @@
|
||||
public class finals{
|
||||
public static void main(String srgs[]){
|
||||
char c= (char)( '2' + 5);
|
||||
System.out.println(c);
|
||||
|
||||
int x = '4' / '5';
|
||||
System.out.println(x);
|
||||
}
|
||||
}
|
||||
38
CS1322/p0/HelloWorld.java
Normal file
38
CS1322/p0/HelloWorld.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* HelloWorld.java
|
||||
* Class HelloWorld - A Class to welcome me into
|
||||
* the world of Java programming by outputting
|
||||
* HelloWorld.
|
||||
*
|
||||
* Revisions: 1.0 Aug. 26, 2002
|
||||
* Created the HelloWorld class
|
||||
* 1.1 Aug. 26, 2002
|
||||
* Finished, Compiled, Tested
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.1, Aug. 26, 2002
|
||||
*/
|
||||
|
||||
public class HelloWorld {
|
||||
|
||||
|
||||
/**
|
||||
* Prints Hello World! on the screen
|
||||
*
|
||||
* <br><br>
|
||||
* @param args IGNORED
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
//Display " Hello World!"
|
||||
System.out.println("Hello World!");
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class HelloWorld
|
||||
120
CS1322/p0/P0Phase3.java
Normal file
120
CS1322/p0/P0Phase3.java
Normal file
@@ -0,0 +1,120 @@
|
||||
/**
|
||||
* CS1322: Programming Assignment #0 - Fall 2002
|
||||
*
|
||||
* <PRE>
|
||||
* P0Phase3.java
|
||||
* Class P0Phase3 - A class designed to learn methods and
|
||||
* casting, as well as working with expressions.
|
||||
*
|
||||
* Revisions: 1.0 Aug. 26, 2002
|
||||
* Created the P0Phase3 class
|
||||
*
|
||||
* 1.1 Aug. 26, 2002
|
||||
* Finished, Compiled, Tested
|
||||
*
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.1, Aug. 26, 2002
|
||||
*/
|
||||
|
||||
public class P0Phase3 {
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
* Truncate double value into an int
|
||||
*
|
||||
* <br><br>
|
||||
* @param d, value to be truncated
|
||||
* @return truncated value d
|
||||
*/
|
||||
public int roundOff(double d){
|
||||
return ((int) d);
|
||||
}
|
||||
|
||||
/**
|
||||
* Divide dividend by divisor
|
||||
*
|
||||
* <br><br>
|
||||
* @param dividend, value to be divided
|
||||
* @param divisor, parts that the divisor will be split into
|
||||
* @return quotient of dividend and divisor
|
||||
*/
|
||||
public double divide(int dividend, int divisor){
|
||||
return (((double)dividend / divisor));
|
||||
}
|
||||
|
||||
/**
|
||||
* Mulitply x by 2.5
|
||||
*
|
||||
* <br><br>
|
||||
* @param x value to be multiplied by 2.5
|
||||
* @return value of x*2.5
|
||||
*/
|
||||
public double twoANDAHalfTimes(int x){
|
||||
return ((double)(x*2.5));
|
||||
}
|
||||
|
||||
/**
|
||||
* Square the value x
|
||||
*
|
||||
* <BR><BR>
|
||||
* @param x value to be squared
|
||||
* @return value of the square
|
||||
*/
|
||||
public double square(double x){
|
||||
return (x*x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the value of the quadratic
|
||||
* <br><br>
|
||||
* @param x,a,b,c values to be used in quadratic
|
||||
* @return value of the equation with given values
|
||||
*/
|
||||
public double quadratic(double x, int a, int b, int c){
|
||||
//no typecasting needed, the equation is promoted
|
||||
//to double because x is a double
|
||||
return (a*(x*x) + b*x + c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Solve the Quadratic Equation with given values
|
||||
* <br><br>
|
||||
* @param a,b,c values to be used in quadratic equation
|
||||
* @return value of the quadratic
|
||||
*/
|
||||
public double solveQuadr(double a, double b, double c){
|
||||
return (-b + Math.sqrt((b*b) - (4*a*c)))/(2*a);
|
||||
}
|
||||
//////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Debugging main for class P0Phase3.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args IGNORED
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
//Create variable to test out class
|
||||
P0Phase3 tester = new P0Phase3();
|
||||
|
||||
//test methods
|
||||
System.out.println(tester.roundOff(5.454));
|
||||
System.out.println(tester.divide(10,3));
|
||||
System.out.println(tester.twoANDAHalfTimes(5));
|
||||
System.out.println(tester.square(2));
|
||||
System.out.println(tester.quadratic(2,2,3,4));
|
||||
System.out.println(tester.solveQuadr(1,2,1));
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class P0Phase3
|
||||
378
CS1322/p0/Pet.html
Normal file
378
CS1322/p0/Pet.html
Normal file
@@ -0,0 +1,378 @@
|
||||
<!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 Fri Aug 30 00:36:38 EDT 2002 -->
|
||||
<TITLE>
|
||||
: Class Pet
|
||||
</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="Pet.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD>
|
||||
</TR>
|
||||
<TR>
|
||||
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
|
||||
SUMMARY: INNER | FIELD | <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 Pet</H2>
|
||||
<PRE>
|
||||
java.lang.Object
|
||||
|
|
||||
+--<B>Pet</B>
|
||||
</PRE>
|
||||
<HR>
|
||||
<DL>
|
||||
<DT>public class <B>Pet</B><DT>extends java.lang.Object</DL>
|
||||
|
||||
<P>
|
||||
CS1322: Programming Assignment #0 - Fall 2002
|
||||
|
||||
<PRE>
|
||||
Pet.java
|
||||
Class Pet - A Class designed to learn how to work with
|
||||
methods and javadoc.
|
||||
|
||||
Revisions: 1.0 Aug. 26, 2002
|
||||
Created the Pet class
|
||||
|
||||
1.1 Aug. 26, 2002
|
||||
Finished, Compiled, Tested
|
||||
</PRE>
|
||||
|
||||
Collaboration Statement:
|
||||
I worked on the homework assignment alone, using only
|
||||
course materials.
|
||||
<P>
|
||||
<HR>
|
||||
|
||||
<P>
|
||||
<!-- ======== INNER CLASS SUMMARY ======== -->
|
||||
|
||||
|
||||
<!-- =========== FIELD SUMMARY =========== -->
|
||||
|
||||
|
||||
<!-- ======== 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="Pet.html#Pet()">Pet</A></B>()</CODE>
|
||||
|
||||
<BR>
|
||||
</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> int</CODE></FONT></TD>
|
||||
<TD><CODE><B><A HREF="Pet.html#getAge()">getAge</A></B>()</CODE>
|
||||
|
||||
<BR>
|
||||
Get the value of age.</TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
|
||||
<CODE> java.lang.String</CODE></FONT></TD>
|
||||
<TD><CODE><B><A HREF="Pet.html#getAnimalType()">getAnimalType</A></B>()</CODE>
|
||||
|
||||
<BR>
|
||||
Get the value of animalType.</TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
|
||||
<CODE> java.lang.String</CODE></FONT></TD>
|
||||
<TD><CODE><B><A HREF="Pet.html#getName()">getName</A></B>()</CODE>
|
||||
|
||||
<BR>
|
||||
Get the value of name
|
||||
|
||||
<br><br></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
|
||||
<CODE>static void</CODE></FONT></TD>
|
||||
<TD><CODE><B><A HREF="Pet.html#main(java.lang.String[])">main</A></B>(java.lang.String[] args)</CODE>
|
||||
|
||||
<BR>
|
||||
Debugging main for class Pet.</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="Pet.html#setAge(int)">setAge</A></B>(int v)</CODE>
|
||||
|
||||
<BR>
|
||||
Set the value of age.</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="Pet.html#setAnimalType(java.lang.String)">setAnimalType</A></B>(java.lang.String v)</CODE>
|
||||
|
||||
<BR>
|
||||
Set the value of animalType.</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="Pet.html#setName(java.lang.String)">setName</A></B>(java.lang.String n)</CODE>
|
||||
|
||||
<BR>
|
||||
Set the value of name
|
||||
|
||||
<br><br></TD>
|
||||
</TR>
|
||||
<TR BGCOLOR="white" CLASS="TableRowColor">
|
||||
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
|
||||
<CODE> java.lang.String</CODE></FONT></TD>
|
||||
<TD><CODE><B><A HREF="Pet.html#toString()">toString</A></B>()</CODE>
|
||||
|
||||
<BR>
|
||||
Change standard string to output to be used for this specific program
|
||||
|
||||
<br><br></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="Pet()"><!-- --></A><H3>
|
||||
Pet</H3>
|
||||
<PRE>
|
||||
public <B>Pet</B>()</PRE>
|
||||
<DL>
|
||||
</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="getName()"><!-- --></A><H3>
|
||||
getName</H3>
|
||||
<PRE>
|
||||
public java.lang.String <B>getName</B>()</PRE>
|
||||
<DL>
|
||||
<DD>Get the value of name
|
||||
|
||||
<br><br><DD><DL>
|
||||
<DT><B>Returns:</B><DD>value of name</DL>
|
||||
</DD>
|
||||
</DL>
|
||||
<HR>
|
||||
|
||||
<A NAME="setName(java.lang.String)"><!-- --></A><H3>
|
||||
setName</H3>
|
||||
<PRE>
|
||||
public void <B>setName</B>(java.lang.String n)</PRE>
|
||||
<DL>
|
||||
<DD>Set the value of name
|
||||
|
||||
<br><br><DD><DL>
|
||||
<DT><B>Parameters:</B><DD><CODE>n</CODE> - value to assign to name</DL>
|
||||
</DD>
|
||||
</DL>
|
||||
<HR>
|
||||
|
||||
<A NAME="getAge()"><!-- --></A><H3>
|
||||
getAge</H3>
|
||||
<PRE>
|
||||
public int <B>getAge</B>()</PRE>
|
||||
<DL>
|
||||
<DD>Get the value of age.
|
||||
|
||||
<br><br><DD><DL>
|
||||
<DT><B>Returns:</B><DD>value of age.</DL>
|
||||
</DD>
|
||||
</DL>
|
||||
<HR>
|
||||
|
||||
<A NAME="setAge(int)"><!-- --></A><H3>
|
||||
setAge</H3>
|
||||
<PRE>
|
||||
public void <B>setAge</B>(int v)</PRE>
|
||||
<DL>
|
||||
<DD>Set the value of age.
|
||||
|
||||
<br><br><DD><DL>
|
||||
<DT><B>Parameters:</B><DD><CODE>v</CODE> - Value to assign to age.</DL>
|
||||
</DD>
|
||||
</DL>
|
||||
<HR>
|
||||
|
||||
<A NAME="getAnimalType()"><!-- --></A><H3>
|
||||
getAnimalType</H3>
|
||||
<PRE>
|
||||
public java.lang.String <B>getAnimalType</B>()</PRE>
|
||||
<DL>
|
||||
<DD>Get the value of animalType.
|
||||
|
||||
<br><br><DD><DL>
|
||||
<DT><B>Returns:</B><DD>value of animalType.</DL>
|
||||
</DD>
|
||||
</DL>
|
||||
<HR>
|
||||
|
||||
<A NAME="setAnimalType(java.lang.String)"><!-- --></A><H3>
|
||||
setAnimalType</H3>
|
||||
<PRE>
|
||||
public void <B>setAnimalType</B>(java.lang.String v)</PRE>
|
||||
<DL>
|
||||
<DD>Set the value of animalType.
|
||||
|
||||
<br><br><DD><DL>
|
||||
<DT><B>Parameters:</B><DD><CODE>v</CODE> - Value to assign to animalType.</DL>
|
||||
</DD>
|
||||
</DL>
|
||||
<HR>
|
||||
|
||||
<A NAME="toString()"><!-- --></A><H3>
|
||||
toString</H3>
|
||||
<PRE>
|
||||
public java.lang.String <B>toString</B>()</PRE>
|
||||
<DL>
|
||||
<DD>Change standard string to output to be used for this specific program
|
||||
|
||||
<br><br><DD><DL>
|
||||
<DT><B>Overrides:</B><DD><CODE>toString</CODE> in class <CODE>java.lang.Object</CODE></DL>
|
||||
</DD>
|
||||
<DD><DL>
|
||||
<DT><B>Returns:</B><DD>phrase as string</DL>
|
||||
</DD>
|
||||
</DL>
|
||||
<HR>
|
||||
|
||||
<A NAME="main(java.lang.String[])"><!-- --></A><H3>
|
||||
main</H3>
|
||||
<PRE>
|
||||
public static void <B>main</B>(java.lang.String[] args)</PRE>
|
||||
<DL>
|
||||
<DD>Debugging main for class Pet.
|
||||
|
||||
<br><br><DD><DL>
|
||||
<DT><B>Parameters:</B><DD><CODE>args</CODE> - IGNORED</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="Pet.html" TARGET="_top"><B>NO FRAMES</B></A></FONT></TD>
|
||||
</TR>
|
||||
<TR>
|
||||
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
|
||||
SUMMARY: INNER | FIELD | <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>
|
||||
157
CS1322/p0/Pet.java
Normal file
157
CS1322/p0/Pet.java
Normal file
@@ -0,0 +1,157 @@
|
||||
/**
|
||||
* CS1322: Programming Assignment #0 - Fall 2002
|
||||
*
|
||||
* <PRE>
|
||||
* Pet.java
|
||||
* Class Pet - A Class designed to learn how to work with
|
||||
* methods and javadoc.
|
||||
*
|
||||
* Revisions: 1.0 Aug. 26, 2002
|
||||
* Created the Pet class
|
||||
*
|
||||
* 1.1 Aug. 26, 2002
|
||||
* Finished, Compiled, Tested
|
||||
* </PRE>
|
||||
*
|
||||
* Collaboration Statement:
|
||||
* I worked on the homework assignment alone, using only
|
||||
* course materials.
|
||||
*
|
||||
* @author <A HREF="mailto:gtg184g@mail.gatech.edu">Jose Manuel Caban</A>
|
||||
* @version Version 1.1, Aug. 26, 2002
|
||||
*/
|
||||
|
||||
public class Pet {
|
||||
|
||||
////////////////
|
||||
//name methods//
|
||||
////////////////
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* Get the value of name
|
||||
*
|
||||
* <br><br>
|
||||
* @return value of name
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of name
|
||||
*
|
||||
* <br><br>
|
||||
* @param n value to assign to name
|
||||
*/
|
||||
public void setName(String n)
|
||||
{
|
||||
name = n;
|
||||
}
|
||||
|
||||
///////////////
|
||||
//age methods//
|
||||
///////////////
|
||||
private int age;
|
||||
|
||||
/**
|
||||
* Get the value of age.
|
||||
*
|
||||
* <br><br>
|
||||
* @return value of age.
|
||||
*/
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of age.
|
||||
*
|
||||
* <br><br>
|
||||
* @param v Value to assign to age.
|
||||
*/
|
||||
public void setAge(int v) {
|
||||
this.age = v;
|
||||
}
|
||||
|
||||
|
||||
//////////////////////
|
||||
//animalType methods//
|
||||
//////////////////////
|
||||
private String animalType;
|
||||
|
||||
/**
|
||||
* Get the value of animalType.
|
||||
*
|
||||
* <br><br>
|
||||
* @return value of animalType.
|
||||
*/
|
||||
public String getAnimalType() {
|
||||
return animalType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of animalType.
|
||||
*
|
||||
* <br><br>
|
||||
* @param v Value to assign to animalType.
|
||||
*/
|
||||
public void setAnimalType(String v) {
|
||||
this.animalType = v;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
/**
|
||||
* Change standard string to output to be used for this specific program
|
||||
*
|
||||
* <br><br>
|
||||
* @return phrase as string
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return "My name is "+ getName() + "\n" +
|
||||
"I am a "+getAnimalType()+" that is "+getAge()+" year"+((age>1)?"s ":" ")+
|
||||
"old.";
|
||||
}//added the plurality after seeing someone say it was fixed in the autograder
|
||||
//so no points off!!!! yay!
|
||||
|
||||
|
||||
/**
|
||||
* Debugging main for class Pet.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args IGNORED
|
||||
*/
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Pet fluffy = new Pet();
|
||||
Pet fido = new Pet();
|
||||
Pet mrBubbles = new Pet();
|
||||
|
||||
//Fluffy declarations (that sounds rather humorous doesn't it?)
|
||||
fluffy.setName("Fluffy");
|
||||
fluffy.setAge(3);
|
||||
fluffy.setAnimalType("Cat");
|
||||
|
||||
//Fido declarations
|
||||
fido.setName("Fido");
|
||||
fido.setAge(5);
|
||||
fido.setAnimalType("Dog");
|
||||
|
||||
//mrBubbles declaration
|
||||
mrBubbles.setName("Mr. Bubbles");
|
||||
mrBubbles.setAge(1); //give him another week
|
||||
mrBubbles.setAnimalType("Fish");
|
||||
|
||||
//print to screen
|
||||
System.out.println(fluffy);
|
||||
System.out.println(fido);
|
||||
System.out.println(mrBubbles);
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Pet
|
||||
BIN
CS1322/p0/cs1322-P0.zip
Normal file
BIN
CS1322/p0/cs1322-P0.zip
Normal file
Binary file not shown.
BIN
CS1322/p0/p0.zip
Normal file
BIN
CS1322/p0/p0.zip
Normal file
Binary file not shown.
252
CS1322/p1/ArrayTest.java
Normal file
252
CS1322/p1/ArrayTest.java
Normal file
@@ -0,0 +1,252 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* ArrayTest.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 02, 2002
|
||||
* Created the ArrayTest class
|
||||
* 1.1 Sep. 02, 2002
|
||||
* Compiled, Tested, Coding Done
|
||||
* 1.2 Sep. 05, 2002
|
||||
* Thanks to a failed WinXP SP1 Install, had to recomment
|
||||
*
|
||||
* </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, Sep. 05, 2002
|
||||
*/
|
||||
|
||||
public class ArrayTest {
|
||||
public static final boolean bDebug = false;
|
||||
|
||||
//////////////////
|
||||
//from Phase III//
|
||||
//////////////////
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Loop through array and return sum
|
||||
*@return sum of array elements
|
||||
*/
|
||||
public int sum(int intArray[]){
|
||||
int sum = 0;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration sum");
|
||||
}
|
||||
|
||||
//function code
|
||||
for(int i=0;i<intArray.length; i++){
|
||||
sum += intArray[i];
|
||||
}
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration sum");
|
||||
}
|
||||
|
||||
return sum;
|
||||
} //end of sum(int intArray[]);
|
||||
|
||||
/**
|
||||
*create array of size "size" and set set each element .1 higher
|
||||
*than the previous starting with 0.0
|
||||
*@return array with each element 0.1 larger than the previous
|
||||
*/
|
||||
public double[] buildArray(int size){
|
||||
double[] array = new double[size];
|
||||
array[0] = 0.0;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration buildArray");
|
||||
}
|
||||
|
||||
//actual code
|
||||
for(int i = 1; i<array.length;i++){
|
||||
array[i] = array[i-1] + 0.1;
|
||||
}
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration buildArray");
|
||||
}
|
||||
return array;
|
||||
} //end of buildArray(int size)
|
||||
|
||||
/**
|
||||
*Create an array containing the smallest and largest element of intArray
|
||||
*@return the array containing the smallest and largest element of intArray
|
||||
*/
|
||||
public int[] bounds(int intArray[]){
|
||||
int min = intArray[0], max = intArray[0];
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration bounds");
|
||||
}
|
||||
|
||||
//actual code, again
|
||||
for(int i = 0; i<intArray.length;i++){
|
||||
if(intArray[i] > max){
|
||||
max = intArray[i];
|
||||
}
|
||||
if(intArray[i] < min){
|
||||
min = intArray[i];
|
||||
}
|
||||
} //end for loop
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration bounds");
|
||||
}
|
||||
|
||||
int bounds[] = new int[2];
|
||||
bounds[0] = min;
|
||||
bounds[1] = max;
|
||||
return bounds;
|
||||
} //end bounds(int intArray[])
|
||||
|
||||
/********************************************************/
|
||||
|
||||
/////////////////
|
||||
//From Phase IV//
|
||||
/////////////////
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
/**
|
||||
*Traverse through twoDArray
|
||||
*@return the sum of the elements of twoDArray
|
||||
*/
|
||||
public static int sum(int twoDArray[][]){
|
||||
int sum=0;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration sum(twoDArray)");
|
||||
}
|
||||
|
||||
for(int row=0; row<twoDArray.length; row++){
|
||||
for(int col=0; col<twoDArray[row].length; col++){
|
||||
sum += twoDArray[row][col];
|
||||
}
|
||||
}//end for loop
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration sum(twoDArray)");
|
||||
}
|
||||
return sum;
|
||||
}//end of sum(int twoDArray[][])
|
||||
|
||||
/**
|
||||
*Traverse grid
|
||||
*@return the number of "true's"
|
||||
*/
|
||||
public static int[] getRowCounts(boolean grid[][]){
|
||||
int[] total = new int[grid.length];
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration getRowCounts");
|
||||
}
|
||||
|
||||
for(int row=0; row<grid.length; row++){
|
||||
total[row] = 0;
|
||||
for(int col=0; col<grid[row].length; col++){
|
||||
if(grid[row][col] == true){
|
||||
total[row]++;
|
||||
}
|
||||
}
|
||||
}//end for loop
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration getRowCounts");
|
||||
}
|
||||
return total;
|
||||
}//end of getRowCounts
|
||||
|
||||
/**********************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class ArrayTest.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
//////////////////
|
||||
//from Phase III//
|
||||
//////////////////
|
||||
|
||||
//////////////////
|
||||
//test Phase III//
|
||||
//////////////////
|
||||
ArrayTest arraytest = new ArrayTest();
|
||||
|
||||
//test sum()
|
||||
int[] array = new int[10];
|
||||
for(int i = 0; i<array.length;i++){
|
||||
array[i] = i+1;
|
||||
}
|
||||
|
||||
System.out.println(arraytest.sum(array));
|
||||
|
||||
//test buildArray()
|
||||
double[] array1;
|
||||
array1 = arraytest.buildArray(8);
|
||||
for(int i=0;i<array1.length;i++){
|
||||
System.out.println(array1[i]);
|
||||
}
|
||||
|
||||
//test bounds
|
||||
int[] array2;
|
||||
array2 = arraytest.bounds(array);
|
||||
for(int i = 0; i<array2.length;i++){
|
||||
System.out.println(array2[i]);
|
||||
}
|
||||
|
||||
//////////////////
|
||||
//from Phase IV//
|
||||
//////////////////
|
||||
|
||||
System.out.println("\nPhase IV begins here\n");
|
||||
|
||||
/////////////////
|
||||
//test Phase IV//
|
||||
/////////////////
|
||||
int[][] array2D = new int[5][4];
|
||||
|
||||
//test sum(twoDArray())
|
||||
for(int row=0; row<array2D.length; row++){
|
||||
for(int col=0; col<array2D[row].length; col++){
|
||||
array2D[row][col] = row+col;
|
||||
}
|
||||
}
|
||||
System.out.println(arraytest.sum(array2D) + "\n");
|
||||
|
||||
//test getRowCounts()
|
||||
boolean[][] array2D_1 = new boolean[4][5];
|
||||
for(int row=0; row<array2D_1.length; row++){
|
||||
for(int col=0; col<array2D_1[row].length; col++){
|
||||
if(col%2 != 0){
|
||||
array2D_1[row][col] = false;
|
||||
}
|
||||
else { //col%2 == 0
|
||||
array2D_1[row][col] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
int[] truthtellers = arraytest.getRowCounts(array2D_1);
|
||||
for(int i=0; i<truthtellers.length; i++){
|
||||
System.out.println(truthtellers[i]);
|
||||
}
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class ArrayTest
|
||||
140
CS1322/p1/City.java
Normal file
140
CS1322/p1/City.java
Normal file
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* City.java
|
||||
* Class City;
|
||||
* A class designed to build cities
|
||||
*
|
||||
* Revisions: 1.0 Sep. 02, 2002
|
||||
* Created the City class
|
||||
* 1.1 Sep. 02, 2002
|
||||
* Finished, compiled, tested
|
||||
*
|
||||
* </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, Sep. 02, 2002
|
||||
*/
|
||||
|
||||
public class City {
|
||||
//global/field scope variables
|
||||
private String name;
|
||||
private String country;
|
||||
private long population;
|
||||
public static final boolean bDebug = false; //not much to debug here, tho
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for City
|
||||
*@param Sets the name for the City
|
||||
*/
|
||||
public void City(String n){
|
||||
name = n;
|
||||
}
|
||||
|
||||
/**
|
||||
*Default Constructor for City
|
||||
*@param sets the name to "Unknown"
|
||||
*/
|
||||
public void City(){
|
||||
name = "Unknown";
|
||||
}
|
||||
|
||||
/*********************************/
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Returns the City name
|
||||
*@return value of name
|
||||
*/
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
*Set the City name
|
||||
*@param n value to assign to name
|
||||
*/
|
||||
public void setName(String n){
|
||||
name = n;
|
||||
}
|
||||
|
||||
/**
|
||||
*Return the country name
|
||||
*@return value of country
|
||||
*/
|
||||
public String getCountry(){
|
||||
return country;
|
||||
}
|
||||
|
||||
/**
|
||||
*Set the Country name
|
||||
*@param c value to assign to country
|
||||
*/
|
||||
public void setCountry(String c){
|
||||
country = c;
|
||||
}
|
||||
|
||||
/**
|
||||
*Return the population
|
||||
*@return value of population
|
||||
*/
|
||||
public long getPopulation(){
|
||||
return population;
|
||||
}
|
||||
|
||||
/**
|
||||
*Set the population
|
||||
*@param p the value to assign to population
|
||||
*/
|
||||
public void setPopulation(long p){
|
||||
population = p;
|
||||
}
|
||||
|
||||
/***********************************/
|
||||
|
||||
/**
|
||||
* Change standard string to output to be used for this specific program
|
||||
*
|
||||
* <br><br>
|
||||
* @return phrase as string
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return name + ", " + country + ": Population: " + population;
|
||||
}
|
||||
|
||||
/**
|
||||
* Debugging main for class City.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
//run debug with my home town, YAY!!!!
|
||||
City Weston = new City();
|
||||
|
||||
Weston.setName("Weston");
|
||||
Weston.setCountry("United States");
|
||||
Weston.setPopulation(2453); //I think I overshot with that number
|
||||
|
||||
System.out.println(Weston);
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class City
|
||||
104
CS1322/p1/IterationTest.java
Normal file
104
CS1322/p1/IterationTest.java
Normal file
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* IterationTest.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 02, 2002
|
||||
* Created the IterationTest class
|
||||
* 1.1 Sep. 03, 2002
|
||||
* Compiled, Finished, Tested, 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, Sep. 02, 2002
|
||||
*/
|
||||
|
||||
public class IterationTest {
|
||||
|
||||
public static final boolean bDebug = false;
|
||||
|
||||
/**
|
||||
* Prints the specified message the specified number of times.
|
||||
* <BR>
|
||||
* For example: printMessageALot("5 times!",5) would print
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* <BR><BR>
|
||||
* @param message the message to print
|
||||
* @param count the number of times to print the message
|
||||
*/
|
||||
public void printMessageALot(String message, int count) {
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration");
|
||||
}
|
||||
|
||||
//actual code
|
||||
for(int i = 0; i<count; i++){
|
||||
System.out.println(message);
|
||||
}
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration");
|
||||
}
|
||||
} //end printMessageALot(String,int)
|
||||
|
||||
|
||||
/**
|
||||
* Returns the first number raised to the power of the second number.
|
||||
* <BR>
|
||||
* For example: power(4,3) would return 4 * 4 * 4 = 64
|
||||
* <BR><BR>
|
||||
* @param base the number to raise to the power
|
||||
* @param exponent the power to raise the base to (will always be greater
|
||||
* then 0)
|
||||
* @return the base raised to the exponent
|
||||
*/
|
||||
public int power(int base, int exponent) {
|
||||
int current = base;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin iteration");
|
||||
}
|
||||
|
||||
//actual code
|
||||
for(int i=1; i<exponent;i++){
|
||||
current *= base;
|
||||
}
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End iteration");
|
||||
}
|
||||
return current;
|
||||
} //end power(int,int)
|
||||
|
||||
/**
|
||||
* Debugging main for class IterationTest.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
IterationTest iterationtest = new IterationTest();
|
||||
|
||||
//HelloWorld to the XTREME!!! Live the eXPerience!!!
|
||||
iterationtest.printMessageALot("Hello World!",5);
|
||||
|
||||
System.out.println(iterationtest.power(2,2));
|
||||
System.out.println(iterationtest.power(3,3));
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class IterationTest
|
||||
236
CS1322/p1/World.java
Normal file
236
CS1322/p1/World.java
Normal file
@@ -0,0 +1,236 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* World.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 02, 2002
|
||||
* Created the World class
|
||||
* 1.1 Sep. 03, 2002
|
||||
* Compiled, Tested, Run
|
||||
* 1.2 Sep. 05, 2002
|
||||
* Commented (late thanks to failed winXP SP1)
|
||||
* 1.3 Sep. 16, 2002
|
||||
* Added omit nulls, gg Outlook Express for not updating...
|
||||
* </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.3, Sep. 16, 2002
|
||||
*/
|
||||
|
||||
public class World {
|
||||
|
||||
private City[][] map;
|
||||
public static final boolean bDebug = false;
|
||||
|
||||
////////////
|
||||
//Modifier//
|
||||
////////////
|
||||
public void setWorld(City[][] twoDArray){
|
||||
map = twoDArray;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Find city at given latitude and longitude
|
||||
*@param lat, the latitude
|
||||
*@param lon, the longitude
|
||||
*@return the City, or NULL if none exists
|
||||
*/
|
||||
public City getCityAt(int lat, int lon){
|
||||
City temp = new City();
|
||||
|
||||
if(map[lon][lat] != null){
|
||||
temp = map[lon][lat];
|
||||
return temp;
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}//end of getCityAt(int lat, int lon)
|
||||
|
||||
/**
|
||||
*Find cities at given latitude
|
||||
*@param lat, the latitude
|
||||
*@return the Cities at the given latitude
|
||||
*/
|
||||
public City[] getCititesAtLatitude(int lat){
|
||||
|
||||
//omit nulls without destroying map
|
||||
int nulls = 0;
|
||||
|
||||
for(int i=0; i<map.length; i++){
|
||||
if(map[i][lat] == null){
|
||||
nulls++;
|
||||
}
|
||||
}
|
||||
|
||||
//create temp array for latitude
|
||||
City[] array = new City[map.length-nulls];
|
||||
int current = 0;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin Iteration getCititesAtLat");
|
||||
}
|
||||
|
||||
for(int i=0; i<map.length; i++){
|
||||
if(map[i][lat] != null){
|
||||
array[current] = map[i][lat];
|
||||
current++;
|
||||
}
|
||||
}
|
||||
/////////////////
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End Iteration getCititesAtLat");
|
||||
}
|
||||
|
||||
return array;
|
||||
}//end of getCititesAtLatitude, yes its misspelled in the .nfo
|
||||
|
||||
/**
|
||||
*Find cities at given longitude
|
||||
*@param lon, the longitude
|
||||
*@return the Cities at the given longitude
|
||||
*/
|
||||
public City[] getCititesAtLongitude(int lon){
|
||||
//omit nulls without destroying map
|
||||
int nulls = 0;
|
||||
|
||||
for(int i=0; i<map[lon].length; i++){
|
||||
if(map[lon][i] == null){
|
||||
nulls++;
|
||||
}
|
||||
}
|
||||
|
||||
//create temp array for longitude
|
||||
City[] array = new City[map[lon].length-nulls];
|
||||
int current = 0;
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("Begin Iteration getCititesAtLon");
|
||||
}
|
||||
|
||||
for(int i=0; i<map[lon].length; i++){
|
||||
if(map[lon][i] != null){
|
||||
array[current] = map[lon][i];
|
||||
current++;
|
||||
}
|
||||
}
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("End Iteration getCititesAtLon");
|
||||
}
|
||||
|
||||
return array;
|
||||
}//end of getCititesAtLongitude
|
||||
|
||||
/**
|
||||
* Debugging main for class World.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
City[][] twoDArray = new City[16][6];
|
||||
|
||||
//create the 8 cities, 2 per row....mwahahaha...
|
||||
|
||||
//city #1
|
||||
twoDArray[0][0] = new City();
|
||||
twoDArray[0][0].setName("Weston");
|
||||
twoDArray[0][0].setCountry("United States");
|
||||
twoDArray[0][0].setPopulation(4332);
|
||||
|
||||
if(bDebug){
|
||||
System.out.println("works");
|
||||
System.out.flush();
|
||||
}
|
||||
|
||||
//city #2
|
||||
twoDArray[0][3] = new City();
|
||||
twoDArray[0][3].setName("Nerd Land");
|
||||
twoDArray[0][3].setCountry("Everquest on WINEX");
|
||||
twoDArray[0][3].setPopulation(4511); //too many, thats the population
|
||||
|
||||
//city #3
|
||||
twoDArray[1][2] = new City();
|
||||
twoDArray[1][2].setName("Atlanta");
|
||||
twoDArray[1][2].setCountry("Georgia");
|
||||
twoDArray[1][2].setPopulation(3304000); //as of 2000, yes I checked
|
||||
|
||||
//city #4
|
||||
twoDArray[1][3] = new City();
|
||||
twoDArray[1][3].setName("Vulcan");
|
||||
twoDArray[1][3].setCountry("Nerdtopia"); //actually, I like Star Trek :|
|
||||
twoDArray[1][3].setPopulation(5252); //as of 2001
|
||||
|
||||
//city #5
|
||||
twoDArray[2][0] = new City();
|
||||
twoDArray[2][0].setName("Leene Square");
|
||||
twoDArray[2][0].setCountry("Chrono Trigger Land");
|
||||
twoDArray[2][0].setPopulation(21); //probably about right ;)
|
||||
|
||||
//city #6
|
||||
twoDArray[2][2] = new City();
|
||||
twoDArray[2][2].setName("Doom");
|
||||
twoDArray[2][2].setCountry("When Id was good");
|
||||
twoDArray[2][2].setPopulation(3); //number of classics Id made
|
||||
|
||||
//city #7
|
||||
twoDArray[3][1] = new City();
|
||||
twoDArray[3][1].setName("John Romero");
|
||||
twoDArray[3][1].setCountry("Ion Storm"); //hehe, not anymore :(
|
||||
twoDArray[3][1].setPopulation(1);
|
||||
|
||||
//city #8
|
||||
twoDArray[3][3] = new City();
|
||||
twoDArray[3][3].setName("Zelda");
|
||||
twoDArray[3][3].setCountry("Miyamato's Best");
|
||||
twoDArray[3][3].setPopulation(4); //number of classics he made
|
||||
/*************************************************************/
|
||||
|
||||
World world = new World();
|
||||
world.setWorld(twoDArray);
|
||||
|
||||
System.out.println(world.getCityAt(0,0));
|
||||
System.out.println(world.getCityAt(2,1));
|
||||
|
||||
//////////
|
||||
System.out.println("\n END TEST GETCITY START LAT\n");
|
||||
//////////
|
||||
|
||||
//test latitude
|
||||
City[] latitude;
|
||||
latitude = world.getCititesAtLatitude(3);
|
||||
|
||||
for(int i = 0; i<latitude.length; i++){
|
||||
System.out.println(latitude[i]);
|
||||
}
|
||||
|
||||
//////////
|
||||
System.out.println("\n END TEST LAT START LON\n");
|
||||
//////////
|
||||
|
||||
//test longitude
|
||||
City[] longitude;
|
||||
longitude = world.getCititesAtLongitude(2);
|
||||
|
||||
for(int i = 0; i<longitude.length; i++){
|
||||
System.out.println(longitude[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class World
|
||||
390
CS1322/p1/p1.nfo
Normal file
390
CS1322/p1/p1.nfo
Normal file
@@ -0,0 +1,390 @@
|
||||
CS1322- Program 1 - The World as Arrays
|
||||
=======================================
|
||||
|
||||
Assigned: Sunday, September 1st, 2002
|
||||
Due: Monday, September 16th, 2002 at 11:59:59PM
|
||||
Stop accepting: Tuesday, September 17th, 2002 at 08:00:00AM
|
||||
Phase II Feedback deadline: Friday, September 6th, 2002 at 11:59:59PM
|
||||
Phase IV Feedback deadline: Thursday, September 12th, 2002 at 11:59:59PM
|
||||
|
||||
|
||||
TOPICS
|
||||
==============================================================================
|
||||
|
||||
Topics you should understand coming into this assignment:
|
||||
o The basic mechanics of java
|
||||
o Basics of Objects and methods
|
||||
|
||||
Topics that you should understand as a result of doing this assignment:
|
||||
o Object Creation and manipulation
|
||||
o Iteration
|
||||
o Arrays
|
||||
o Multi-dimensional Arrays
|
||||
o Testing and Debugging
|
||||
o Arrays of Objects
|
||||
|
||||
|
||||
PROGRAM WALKTHROUGH
|
||||
==============================================================================
|
||||
|
||||
In this program, you will be managing a set of cities around the world.
|
||||
You will be creating a data structure which will hold a map of several major
|
||||
cities around the world. Their locations will be identified by latitude and
|
||||
longitude(more on this later).
|
||||
|
||||
|
||||
PHASE I - Building Citites
|
||||
==============================================================================
|
||||
|
||||
Each City will have a name, country, population, and of course: a location.
|
||||
We will be creating a class to hold all of this information for a city. Then,
|
||||
we will creating many instance of this class (also known as objects) which
|
||||
will represent real cities.
|
||||
|
||||
First, create a new class named City. This City should be public so that
|
||||
any other class can see it. This City should have several pieces of data:
|
||||
o A name(called "name") which will be represented as a String
|
||||
o A country(called "country") which will also be represented as a String
|
||||
o A population(called "population") will be represented as a number. Now,
|
||||
the population of a country can be very, very large, so for now, we will
|
||||
want to use a primitive integer type which can be larger than an int.
|
||||
Figure out which type this is and declare the population variable of this
|
||||
type.
|
||||
|
||||
Now, you are going to create a constructor for a City. A constructor is a
|
||||
special type of method that is used to create a new City. The format for a
|
||||
constructor is the same as a method, except that the name must be the name of
|
||||
the class it is in, and there can be no return type. Create a constructor for
|
||||
City that takes in a String which represents the name and sets the name of the
|
||||
City to this new name. Remember that a constructor does not return anything.
|
||||
|
||||
Now, make another constructor for City that takes in nothing, and sets the
|
||||
name to "Unknown".
|
||||
|
||||
As in the first assignment, you created accessors and modifiers, do that for
|
||||
all of these variables now. Remember that the variables should be private and
|
||||
the accessors and modifiers public.
|
||||
|
||||
Now, create a toString() method which will display the data for a City in the
|
||||
following format:
|
||||
|
||||
City: <NAME>, <COUNTRY>: Population: <POPULATION>
|
||||
|
||||
examples: Albany, United States: Population: 1267326
|
||||
Moscow, Russia: Population: 5234800
|
||||
|
||||
|
||||
PHASE II - Iteration Practice (instant feedback phase)
|
||||
==============================================================================
|
||||
|
||||
Eventually, we will need to go through all of the cities in the world to
|
||||
perform certain operations. So, in this phase, we're going to practice
|
||||
"looping" through things.
|
||||
|
||||
*******
|
||||
*LOOPS*
|
||||
*******
|
||||
|
||||
Most of the following can be found in lecture slides:
|
||||
|
||||
for loop
|
||||
--------
|
||||
Description: a loop used primarily to perform a block of code a certain fixed
|
||||
number of times.
|
||||
Example:
|
||||
for(int i=0; i<10; i++) {
|
||||
System.out.println("Now i is: "+i);
|
||||
} //end for
|
||||
|
||||
while loop
|
||||
----------
|
||||
Description: a loop used to perform a block of code until some event occurs.
|
||||
Example:
|
||||
boolean userDidSomethingCool = false;
|
||||
while(userDidSomethingCool == false) {
|
||||
System.out.println("Not yet");
|
||||
userDidSomethingCool = didUserDoCoolStuff();
|
||||
} //end while
|
||||
|
||||
do while loop
|
||||
-------------
|
||||
Description: a loop which functions exactly like a while loop, but is
|
||||
guaranteed to execute at least once
|
||||
Example:
|
||||
int choice;
|
||||
int QUIT_CHOICE = 9;
|
||||
do {
|
||||
choice = getUserInputFromMenu();
|
||||
handleChoice(choice);
|
||||
} (while choice != QUIT_CHOICE);
|
||||
|
||||
Create a class named IterationTest (make sure it is public). In this class,
|
||||
you are going to implement some methods that MUST USE ITERATION.
|
||||
|
||||
/**
|
||||
* Prints the specified message the specified number of times.
|
||||
* <BR>
|
||||
* For example: printMessageALot("5 times!",5) would print
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* 5 times!
|
||||
* <BR><BR>
|
||||
* @param message the message to print
|
||||
* @param count the number of times to print the message
|
||||
*/
|
||||
public void printMessageALot(String message, int count) {
|
||||
//YOUR CODE HERE
|
||||
} //end printMessageALot(String,int)
|
||||
|
||||
|
||||
/**
|
||||
* Returns the first number raised to the power of the second number.
|
||||
* <BR>
|
||||
* For example: power(4,3) would return 4 * 4 * 4 = 64
|
||||
* <BR><BR>
|
||||
* @param base the number to raise to the power
|
||||
* @param exponent the power to raise the base to (will always be greater
|
||||
* then 0)
|
||||
* @return the base raised to the exponent
|
||||
*/
|
||||
public int power(int base, int exponent) {
|
||||
//YOUR CODE HERE
|
||||
} //end power(int,int)
|
||||
|
||||
PHASE III - Fun with Arrays
|
||||
==============================================================================
|
||||
|
||||
Now we will work with your first data structure in CS1322, the array. This
|
||||
structure has already been built for you, all you need to do is use it. This
|
||||
phase will get you accustomed to using arrays so that we may use them to
|
||||
manage the recently-created Cities.
|
||||
|
||||
********
|
||||
*ARRAYS*
|
||||
********
|
||||
|
||||
Most of the following can be found in lecture slides:
|
||||
|
||||
Q: What is an array?
|
||||
A: An array is a data structure that can hold a finite amount of data of the
|
||||
same type. You declare how many elements the array can hold, what type of
|
||||
data can go in it, and where. Arrays can be of any type.
|
||||
|
||||
Declaring an array
|
||||
------------------
|
||||
Here is the syntax(format) for declaring an array:
|
||||
<TYPE>[] <NAME> or <TYPE> <NAME>[]
|
||||
For example: int numbers[]; is the same as int[] numbers; Both create an
|
||||
array of ints named numbers.
|
||||
|
||||
Setting the size
|
||||
----------------
|
||||
To set the size of an array, you can create a new array of the specified type
|
||||
and pass the size in as an array parameter. See the following:
|
||||
int numbers[] = new int[8];
|
||||
This statement declares a new int array named numbers and gives it a size of
|
||||
eight. This statement does the same thing:
|
||||
int numbers[];
|
||||
numbers = new int[8];
|
||||
|
||||
Filling up the array with data
|
||||
------------------------------
|
||||
Now we have our numbers array which can only hold eight ints. So, let's put
|
||||
some data into it. The way we get a spot in the array is by indexing it.
|
||||
Here is how we would get the 4th piece of data from the numbers array.
|
||||
int fourth = numbers[3];
|
||||
Q: Why 3?
|
||||
A: Because array indices always start at zero, if we wanted the first piece of
|
||||
data, we would say numbers[0]. Always remember that the index is one less
|
||||
than the element we want. Therefore, the last "cell" in the array would be
|
||||
cell 7, not 8.
|
||||
We can also place data into the array in the same fashion:
|
||||
numbers[2] = 45;
|
||||
This places the number 45 into the third cell in the array.
|
||||
|
||||
Length of an array
|
||||
------------------
|
||||
Arrays have a special way of getting their length. It is a variable named
|
||||
length. To get the length of our numbers array, we would say:
|
||||
numbers.length //returns the int 8
|
||||
|
||||
Iteration and Arrays
|
||||
--------------------
|
||||
Iteration is very useful when dealing with arrays. We could easily create an
|
||||
array that loops through every cell of the array, by having the loop counter
|
||||
of the loop be the index of the array. This way, we could make an operation
|
||||
that performs on every single cell of the array.
|
||||
|
||||
Create a class named ArrayTest (which should be public).
|
||||
|
||||
Code the following methods:
|
||||
|
||||
public int sum(int intArray[])
|
||||
o This method should loop through every cell of the array and return the sum
|
||||
of all of its elements.
|
||||
|
||||
public double[] buildArray(int size)
|
||||
o This method will create an array of doubles of size "size". Each element in
|
||||
the array should be 0.1 larger than the first, with the first being 0.0
|
||||
Example: buildArray(6) would return an array that looks like this:
|
||||
{0.0, 0.1, 0.2, 0.3, 0.4, 0.5}
|
||||
|
||||
public int[] bounds(int intArray[])
|
||||
o This method will iterate through all elements of intArray and create a new
|
||||
int array of size 2. This array will have in its zero position, the
|
||||
smallest value in intArray. This array will have in its one position, the
|
||||
largest value in intArray.
|
||||
Example: int numbers[] = {4,6,8,1,4,0,3};
|
||||
bounds(numbers) would create an array that looks like this:
|
||||
{0,8}
|
||||
|
||||
PHASE IV - Multi-dimensional arrays (instant feedback phase)
|
||||
==============================================================================
|
||||
|
||||
So far, we have worked with one-dimensional arrays. This means that there
|
||||
is only one sequence being worked with. However, we can link arrays to
|
||||
achieve the effect of many dimensions.
|
||||
|
||||
**************************
|
||||
*MULTI-DIMENSIONAL ARRAYS*
|
||||
**************************
|
||||
|
||||
If you picture an array as a row, or a list(one dimension): [][][][][]
|
||||
It can also be pictured as a grid(two dimensions): [][][]
|
||||
[][][]
|
||||
[][][]
|
||||
[][][]
|
||||
Or as a cube(three dimensions): Not going to try and draw a cube...
|
||||
|
||||
This is all in the lecture slides. For this part of the assignment, take a
|
||||
look at Module 18 of the Lecture slides on the course website. The slides
|
||||
will come in very handy for future assignments (including this one)
|
||||
|
||||
Go back into your ArrayTest class.
|
||||
|
||||
Create the following methods:
|
||||
|
||||
public static int sum(int twoDArray[][])
|
||||
o This method will traverse through twoDArray and return the sum of all
|
||||
elements in the 2D array
|
||||
|
||||
public static int[] getRowCounts(int boolean grid[][])
|
||||
o This method will traverse through grid and return the number of "true's" in
|
||||
each row of the array. The returned array will be a one-dimensional array
|
||||
which contains how many occurrences of the boolean value true are in each
|
||||
row of the array. For the remaining part of this assignment, "row" will
|
||||
always refer to the first dimension of the array, and "column" will always
|
||||
refer to the second
|
||||
Example: boolean grid[][] = {{true, false,false,false}, //Row 0
|
||||
{false,true, false,true}, //Row 1
|
||||
{true, true, true, true}}; //Row 2
|
||||
getRowCounts(grid) would return an array that looks like this:
|
||||
{1,2,4}
|
||||
|
||||
PHASE V - bDEBUG and a debug main
|
||||
==============================================================================
|
||||
|
||||
Now you will learn how to debug your programs.
|
||||
|
||||
***********
|
||||
*CONSTANTS*
|
||||
***********
|
||||
|
||||
Constants are variable in a program whose value never changes. For instance,
|
||||
the value of PI will never change, so we make it a constant. To make a
|
||||
constant in Java, use the "final" modifier.
|
||||
|
||||
public static final double PI = 3.141592653;
|
||||
|
||||
We will use a similar technique to debug our code
|
||||
|
||||
debug: To search for and eliminate malfunctioning elements or errors in
|
||||
source: http://www.dictionary.com
|
||||
|
||||
In each one of your classes, create a constant name bDEBUG. It should be
|
||||
public, static, and of course final. It should be a boolean. Also, give this
|
||||
boolean a default value of true.
|
||||
|
||||
Now, go through some of your methods in IterationTest and find a single line
|
||||
of code that does something important. Place this line below it:
|
||||
if(bDEBUG)
|
||||
System.out.println("Just did something important");
|
||||
You can use this constant everywhere that you want to see output from a
|
||||
method. When bDEBUG is set to true, all of this extra information prints out.
|
||||
If you set the constant to false, it does not. This allows your program to
|
||||
run in "debug mode".
|
||||
|
||||
Add the debug constant to all of your classes and use the debugged prints
|
||||
often. You will be graded for good use of this. From now on, all methods
|
||||
which have more functionality than accessors and modifiers will require at
|
||||
least some debug statements in them.
|
||||
|
||||
Debug main
|
||||
----------
|
||||
|
||||
In each one of your classes, make a main method like you did in p0. Your main
|
||||
method should test all of your methods by making variables/arguments for your
|
||||
methods, invoking them, and printing out any results. All classes from now
|
||||
on will require debug mains.
|
||||
|
||||
PHASE VI - A world of Cities
|
||||
==============================================================================
|
||||
|
||||
Now that you know how to make Cities, iterate, and make multi-dimensional
|
||||
arrays, you will put all of it to the test.
|
||||
|
||||
Create a class named World.
|
||||
|
||||
This class will have a private instance variable named map. This variable
|
||||
will be an 2D array of Citites. That's right, we can have arrays of Objects.
|
||||
Each cell in the 2D array will be a City that you created in Phase 1. The
|
||||
idea of arrays of Objects is also covered in Module 18 of the lecture slides.
|
||||
|
||||
Make a constructor for World that take in a 2D array of Cities and assigns the
|
||||
map variable to it. Now, we're going to give the user the ability to search
|
||||
this map by "latitude" and "longitude". For the purposes of this project,
|
||||
this planet will be flat and latitude will be used to access the
|
||||
x-axis(second dimension) while longitude will traverse the
|
||||
y-axis(first dimension). Here is a picture of the world:
|
||||
|
||||
/ \ [ ][ ][*][ ][ ][ ][ ]
|
||||
| [ ][ ][*][ ][ ][ ][ ] * = City
|
||||
longitude [ ][ ][ ][ ][ ][*][ ]
|
||||
| [*][ ][ ][*][ ][ ][ ]
|
||||
\ / [ ][*][ ][ ][ ][ ][ ]
|
||||
|
||||
<- latitude ->
|
||||
|
||||
Note: By default, a 2D array of Objects has all of its values set to null.
|
||||
----
|
||||
|
||||
Create the following method:
|
||||
public City getCityAt(int lat, int lon)
|
||||
o This method will return the City which is located at the specified latitude
|
||||
and longitude. If there is no City there, return null.
|
||||
|
||||
public City[] getCititesAtLatitude(int lat)
|
||||
o Returns an array of all Cities at the specified latitude.
|
||||
|
||||
public City[] getCititesAtLongitude(int lon)
|
||||
o Returns an array of all Cities at the specified longitude
|
||||
|
||||
Make sure you use a debug main here as well as plenty of debug staments. You
|
||||
will want to create the 2D array in the main method as well as all of the
|
||||
Cities. It is your responsibility to test that each of these methods works.
|
||||
Hint: use the toString() method of your City class to help.
|
||||
|
||||
|
||||
DELIVERABLES
|
||||
==============================================================================
|
||||
|
||||
Please turn in the following to webwork:
|
||||
|
||||
City.java
|
||||
IterationTest.java
|
||||
ArrayTest.java
|
||||
World.java
|
||||
|
||||
Make sure that you turn in ALL files by the due time.
|
||||
BIN
CS1322/p1/p1.zip
Normal file
BIN
CS1322/p1/p1.zip
Normal file
Binary file not shown.
128
CS1322/p2/Animal.java
Normal file
128
CS1322/p2/Animal.java
Normal file
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Animal.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 17, 2002
|
||||
* Created the Animal class
|
||||
* 1.1 Sep. 17, 2002
|
||||
* Finished the Animal Class
|
||||
*
|
||||
* </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, Sep. 17, 2002
|
||||
*/
|
||||
|
||||
public abstract class Animal {
|
||||
|
||||
/**
|
||||
*whether or not the animal is hungry
|
||||
*/
|
||||
private boolean isHungry;
|
||||
|
||||
/**
|
||||
*holds the animal's name
|
||||
*/
|
||||
private String name;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for Animal
|
||||
*@param name the value to assign name
|
||||
*/
|
||||
public Animal(String name){
|
||||
this.name = name;
|
||||
isHungry = false;
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*isHungry Modifier
|
||||
*@param h the value applied to isHungry
|
||||
*/
|
||||
public void setIsHungry(boolean h){
|
||||
isHungry = h;
|
||||
}
|
||||
|
||||
/**
|
||||
*isHungry Accessor
|
||||
*@return the value of isHungry
|
||||
*/
|
||||
public boolean getIsHungry(){
|
||||
return isHungry;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for name
|
||||
*@return the value of name
|
||||
*/
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*@return whether the animal is dangerous or not
|
||||
*/
|
||||
public abstract boolean isDangerous();
|
||||
|
||||
/**
|
||||
*Give the animal something to eat
|
||||
*@param food, the food to feed the animal
|
||||
*/
|
||||
public abstract void eat(String food);
|
||||
|
||||
/**
|
||||
*Equals method
|
||||
*@param the value to test equality with
|
||||
*@return the truthnes of the equality
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
|
||||
if((obj instanceof Animal) == false){
|
||||
return false;
|
||||
}
|
||||
|
||||
Animal temp = (Animal)obj;
|
||||
//added "this." to make it verbose and more readable
|
||||
if(!(temp.getName().equals(this.getName()))){
|
||||
return false;
|
||||
}
|
||||
if(temp.isDangerous() != this.isDangerous()){
|
||||
return false;
|
||||
}
|
||||
if(temp.getIsHungry() != this.getIsHungry()){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Debugging main for class Animal.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
//nuttin ta test
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Animal
|
||||
99
CS1322/p2/Aquarium.java
Normal file
99
CS1322/p2/Aquarium.java
Normal file
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Aquarium.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 26, 2002
|
||||
* Created the Aquarium class
|
||||
* 1.1 Sep. 28, 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, Sep. 28, 2002
|
||||
*/
|
||||
|
||||
public class Aquarium extends ZooBuilding{
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for Aquarium
|
||||
*@param name the name for the Aquarium
|
||||
*/
|
||||
public Aquarium(String name){
|
||||
super(name);
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*@return true if it is safe to enter
|
||||
*Safe if all dangerous animals are 30 feet underwater
|
||||
*/
|
||||
public boolean isSafeToEnter(){
|
||||
Animal aTemp;
|
||||
SwimmingType sTemp;
|
||||
boolean bSafe = true;
|
||||
for(int i=0; i<getTheAnimals().size(); i++){
|
||||
aTemp = (Animal)getTheAnimals().removeFromBack();
|
||||
sTemp = (SwimmingType)aTemp;
|
||||
if(aTemp.isDangerous() && sTemp.getDepth() < 30){
|
||||
bSafe = false;
|
||||
}
|
||||
getTheAnimals().addToFront(aTemp);
|
||||
}
|
||||
return bSafe;
|
||||
}
|
||||
|
||||
/**
|
||||
*override default string method for use with this program
|
||||
*/
|
||||
public String toString(){
|
||||
return ("An Aquarium named " + getName() + ".");
|
||||
}
|
||||
|
||||
/****************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class Aquarium.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Aquarium swimswim = new Aquarium("Smells like Fish");
|
||||
|
||||
swimswim.getTheAnimals().addToFront(new Duck("Bob"));
|
||||
swimswim.getTheAnimals().addToBack(new Gator("Crocodile"));
|
||||
swimswim.getTheAnimals().addToFront(new GoldFish("Can't Remember Jack",5));
|
||||
|
||||
swimswim.getTheAnimals().listOut();
|
||||
System.out.println();
|
||||
|
||||
Gator Tom = (Gator)swimswim.getTheAnimals().getLast();
|
||||
Tom.setDepth(50);
|
||||
|
||||
System.out.println(swimswim.isSafeToEnter());
|
||||
Tom.setDepth(4);
|
||||
System.out.println(swimswim.isSafeToEnter());
|
||||
|
||||
swimswim.getTheAnimals().listOut();
|
||||
|
||||
System.out.println(swimswim);
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Aquarium
|
||||
142
CS1322/p2/Aviary.java
Normal file
142
CS1322/p2/Aviary.java
Normal file
@@ -0,0 +1,142 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Aviary.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 26, 2002
|
||||
* Created the Aviary class
|
||||
* 1.1 Sep. 28, 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, Sep. 28, 2002
|
||||
*/
|
||||
|
||||
public class Aviary extends ZooBuilding{
|
||||
|
||||
/**
|
||||
*whether or not the net is closed
|
||||
*/
|
||||
boolean netClosed;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for Aviary
|
||||
*@param name the name of the Aviary
|
||||
*/
|
||||
public Aviary(String name){
|
||||
super(name);
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*@return value of netClosed
|
||||
*/
|
||||
public boolean getNetClosed(){
|
||||
return netClosed;
|
||||
}
|
||||
|
||||
/**
|
||||
*@param netClosed, the new value of netClosed
|
||||
*/
|
||||
public void setNetClosed(boolean netClosed){
|
||||
this.netClosed = netClosed;
|
||||
}
|
||||
|
||||
/////////////
|
||||
//.Methods.//
|
||||
/////////////
|
||||
|
||||
/**
|
||||
*@return the value of netClosed
|
||||
*/
|
||||
public boolean isSafeToEnter(){
|
||||
Animal aTemp;
|
||||
FlyingType fTemp;
|
||||
boolean bSafe = true;
|
||||
for(int i=0; i<getTheAnimals().size(); i++){
|
||||
aTemp = (Animal)getTheAnimals().removeFromBack();
|
||||
fTemp = (FlyingType)aTemp;
|
||||
if(aTemp.isDangerous() && fTemp.getAltitude() < 75){
|
||||
bSafe = false;
|
||||
}
|
||||
getTheAnimals().addToFront(aTemp);
|
||||
}
|
||||
if(!getNetClosed()){
|
||||
bSafe = false;
|
||||
}
|
||||
return bSafe;
|
||||
}
|
||||
|
||||
/**
|
||||
*override default toString for use with this program
|
||||
*/
|
||||
public String toString(){
|
||||
return ("An Aviary named " + getName() + ".");
|
||||
}
|
||||
|
||||
/***********************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class Aviary.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Aviary dancedance = new Aviary("Stinky");
|
||||
dancedance.getTheAnimals().addToBack(new Duck("Bob"));
|
||||
dancedance.getTheAnimals().addToFront(new Bat("Ronald"));
|
||||
dancedance.getTheAnimals().addToFront(new Bat("McDonald"));
|
||||
dancedance.getTheAnimals().listOut();
|
||||
System.out.println();
|
||||
dancedance.setNetClosed(true);
|
||||
System.out.println(dancedance.getHungryAnimals());
|
||||
System.out.println();
|
||||
dancedance.getTheAnimals().listOut();
|
||||
|
||||
Animal temp = (Animal)dancedance.getTheAnimals().getFirst();
|
||||
temp.setIsHungry(true);
|
||||
|
||||
System.out.println();
|
||||
dancedance.getTheAnimals().listOut();
|
||||
System.out.println();
|
||||
dancedance.setNetClosed(true);
|
||||
System.out.println(dancedance.getHungryAnimals());
|
||||
System.out.println();
|
||||
dancedance.getTheAnimals().listOut();
|
||||
|
||||
System.out.println();
|
||||
dancedance.getTheAnimals().listOut();
|
||||
System.out.println();
|
||||
dancedance.setNetClosed(false);
|
||||
dancedance.feedAnimals("insects");
|
||||
|
||||
dancedance.setNetClosed(true);
|
||||
|
||||
dancedance.feedAnimals("insects");
|
||||
System.out.println();
|
||||
dancedance.getTheAnimals().listOut();
|
||||
System.out.println();
|
||||
|
||||
System.out.println(dancedance);
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Aviary
|
||||
145
CS1322/p2/Bat.java
Normal file
145
CS1322/p2/Bat.java
Normal file
@@ -0,0 +1,145 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Bat.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 19, 2002
|
||||
* Created the Bat class
|
||||
*
|
||||
* </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, Sep. 19, 2002
|
||||
*/
|
||||
|
||||
public class Bat extends Animal implements FlyingType{
|
||||
|
||||
/**
|
||||
*altitude of the Bat
|
||||
*/
|
||||
private int altitude;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for Bat
|
||||
*@param name, the name of the bat
|
||||
*/
|
||||
public Bat(String name){
|
||||
super(name);
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Modifier for altitude
|
||||
*@param altitude, the value to be applied to altitude
|
||||
*/
|
||||
public void setAltitude(int altitude){
|
||||
this.altitude = altitude;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for altitude
|
||||
*@return the value of altitude
|
||||
*/
|
||||
public int getAltitude(){
|
||||
return altitude;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Give the dangerous value of the Bat
|
||||
*@return false, the Bat is not dangerous
|
||||
*/
|
||||
public boolean isDangerous(){
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*Eat method for Bat
|
||||
*@param food to eat, if it's "insects" isHungry = false
|
||||
*/
|
||||
public void eat(String food)
|
||||
{
|
||||
if(food.equals("insects")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*Find the equality of Bat
|
||||
*@param obj the Object to be compared to
|
||||
*@return the equality value
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
|
||||
if(!(obj instanceof Bat)){
|
||||
return false;
|
||||
}
|
||||
|
||||
Bat temp = (Bat)obj;
|
||||
//added "this." to make explicit, God why do I type this for every
|
||||
//single animal class?
|
||||
if(temp.getAltitude() != this.getAltitude()){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!(super.equals(obj))){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*Modify String for this particular program
|
||||
*@return the data of the Bat as a string statement
|
||||
*/
|
||||
public String toString(){
|
||||
return ("A Bat named " + getName() + " at altitude " + getAltitude() +
|
||||
" that is " + (getIsHungry() ?"":"not ") + "hungry.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Debugging main for class Bat.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Bat bat = new Bat("Batty");
|
||||
|
||||
//bats can live underground
|
||||
bat.setAltitude(-10);
|
||||
|
||||
System.out.println(bat);
|
||||
|
||||
Bat bat2 = new Bat("Pooty");
|
||||
|
||||
bat2.setAltitude(10);
|
||||
bat2.setIsHungry(true);
|
||||
|
||||
System.out.println(bat2);
|
||||
|
||||
System.out.println(bat.equals(bat2));
|
||||
System.out.println(bat.equals(bat));
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Bat
|
||||
179
CS1322/p2/Bird.java
Normal file
179
CS1322/p2/Bird.java
Normal file
@@ -0,0 +1,179 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Bird.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 18, 2002
|
||||
* Created the Bird class
|
||||
* 1.1 Sep. 18, 2002
|
||||
* Finished the Bird class
|
||||
* 1.1f Sep. 18,2002
|
||||
* Cleaned equals() to make it more readable
|
||||
*
|
||||
* </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.1f, Sep. 18, 2002
|
||||
*/
|
||||
|
||||
|
||||
public class Bird extends Animal implements FlyingType{
|
||||
|
||||
/**
|
||||
*whether or not the bird is dangerous
|
||||
*/
|
||||
private boolean dangerous;
|
||||
|
||||
/**
|
||||
*species of the bird
|
||||
*/
|
||||
private String species;
|
||||
|
||||
/**
|
||||
*altitude of the bird
|
||||
*/
|
||||
private int altitude;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*@param name the value to give to name
|
||||
*@param species the value to give species
|
||||
*@param dangerous the value to give dangerous
|
||||
*@param alt the value to give altitude
|
||||
*/
|
||||
public Bird(String name, String species,
|
||||
boolean dangerous, int alt){
|
||||
super(name);
|
||||
//left "this." for alt just for consistency
|
||||
this.dangerous = dangerous;
|
||||
this.species = species;
|
||||
this.altitude = alt;
|
||||
}
|
||||
|
||||
/**
|
||||
*@param name the value to give to name
|
||||
*@param species the value to give species
|
||||
*@param dangerous the value to give dangerous
|
||||
*Altitude will be set to 0
|
||||
*/
|
||||
public Bird(String name, String species, boolean dangerous){
|
||||
this(name,species,dangerous,0);
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Accessor for species
|
||||
*@return the value of species
|
||||
*/
|
||||
public String getSpecies(){
|
||||
return species;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for altitude
|
||||
*@return the altitude
|
||||
*/
|
||||
public int getAltitude(){
|
||||
return altitude;
|
||||
}
|
||||
|
||||
/**
|
||||
*Modifier for altitude
|
||||
*@param the value to be assigned to altitude
|
||||
*/
|
||||
public void setAltitude(int altitude){
|
||||
this.altitude = altitude;
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the danger of the bird
|
||||
*/
|
||||
public boolean isDangerous(){
|
||||
return dangerous;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Modify String for this particular program
|
||||
*@return the data of the Bird as a string statement
|
||||
*/
|
||||
public String toString(){
|
||||
return ("A " + getSpecies() + " named " + getName() + " at altitude " +
|
||||
getAltitude() + " that is " + (isDangerous()?"":"not ") + "dangerous" +
|
||||
" and is " + (getIsHungry()?"":"not ") + "hungry.");
|
||||
}
|
||||
|
||||
/**
|
||||
*Eat method for Bird
|
||||
*@param food to eat, if it's bird seed isHungry = false
|
||||
*/
|
||||
public void eat(String food)
|
||||
{
|
||||
if(food.equals("bird seed")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*equals method for Bird
|
||||
*@param obj the value to be tested
|
||||
*@return equality true/false
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
|
||||
if(!(obj instanceof Bird)){
|
||||
return false;
|
||||
}
|
||||
if(!super.equals(obj)){
|
||||
return false;
|
||||
}
|
||||
|
||||
Bird bTemp = (Bird)obj;
|
||||
//use of "this." to make it explicit
|
||||
if(bTemp.getSpecies() != this.getSpecies()){
|
||||
return false;
|
||||
}
|
||||
if(bTemp.getAltitude() != this.getAltitude()){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/********************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class Bird.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Bird bird = new Bird("Tweety","Canary",true,26);
|
||||
System.out.println(bird);
|
||||
|
||||
Bird bird2 = new Bird("Bob","Eagle",false);
|
||||
bird2.setIsHungry(true);
|
||||
System.out.println(bird2);
|
||||
System.out.println(bird.equals(bird2));
|
||||
System.out.println(bird.equals(bird));
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Bird
|
||||
149
CS1322/p2/Duck.java
Normal file
149
CS1322/p2/Duck.java
Normal file
@@ -0,0 +1,149 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Duck.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 19, 2002
|
||||
* Created the Duck class
|
||||
* 1.1 Sep. 21, 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, Sep. 21, 2002
|
||||
*/
|
||||
|
||||
public class Duck extends Bird implements SwimmingType{
|
||||
|
||||
/**
|
||||
*depth of Duck
|
||||
*/
|
||||
private int depth;
|
||||
|
||||
/**
|
||||
*whether or not the Duck is in flight
|
||||
*/
|
||||
private boolean flying;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for Duck
|
||||
*@param name the name of the duck
|
||||
*/
|
||||
public Duck(String name){
|
||||
super(name,"Duck",false);
|
||||
setDepth(0);
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Modifier for depth
|
||||
*@param the new value for depth
|
||||
*/
|
||||
public void setDepth(int depth){
|
||||
this.depth = depth;
|
||||
flying = false;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for depth
|
||||
*@return the value of depth
|
||||
*/
|
||||
public int getDepth(){
|
||||
return depth;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for flying
|
||||
*@return value of flying
|
||||
*/
|
||||
public boolean isFlying(){
|
||||
return flying;
|
||||
}
|
||||
|
||||
/**
|
||||
*Set the altitude of the Duck and flying to true
|
||||
*@param altitude the height of the duck
|
||||
*/
|
||||
public void setAltitude(int altitude){
|
||||
super.setAltitude(altitude);
|
||||
flying = true;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Find the equality of Duck
|
||||
*@param obj the Object to be compared to
|
||||
*@return the equality value
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
if(!(obj instanceof Bird)){
|
||||
return false;
|
||||
}
|
||||
Duck dTemp = (Duck)obj;
|
||||
if(dTemp.getDepth() != this.getDepth()){
|
||||
return false;
|
||||
}
|
||||
if(dTemp.isFlying() != this.isFlying()){
|
||||
return false;
|
||||
}
|
||||
if(!(super.equals(obj))){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change standard string to output to be used for this specific program
|
||||
* @return phrase as string
|
||||
*/
|
||||
public String toString(){
|
||||
if(isFlying()){
|
||||
return super.toString();
|
||||
}
|
||||
else{ //!isFlying()
|
||||
return ("A swimming duck named " + getName() + " at depth " +
|
||||
getDepth() + " that is " + (getIsHungry()?"":"not ") +
|
||||
"hungry.");
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class Duck.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Duck duck = new Duck("Daffy");
|
||||
System.out.println(duck);
|
||||
|
||||
duck.setAltitude(8);
|
||||
duck.setIsHungry(true);
|
||||
System.out.println(duck);
|
||||
|
||||
System.out.println(duck.equals(duck));
|
||||
System.out.println(duck.equals("bob"));
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Duck
|
||||
108
CS1322/p2/Fish.java
Normal file
108
CS1322/p2/Fish.java
Normal file
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Fish.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 18, 2002
|
||||
* Created the Fish class
|
||||
* 1.1 Sep. 18, 2002
|
||||
* Finished the Fish class
|
||||
* 1.1f Sep. 18, 2002
|
||||
* Cleaned up equals() to make more readable
|
||||
* </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.1f, Sep. 18, 2002
|
||||
*/
|
||||
|
||||
public abstract class Fish extends Animal implements SwimmingType{
|
||||
|
||||
/**
|
||||
*depth of Fish
|
||||
*/
|
||||
private int depth;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for Fish
|
||||
*@param name, the name of the fish
|
||||
*@param depth, the depth of the fish
|
||||
*/
|
||||
public Fish(String name, int depth){
|
||||
super(name);
|
||||
this.setDepth(depth);
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Set the depth of Fish
|
||||
*@param depth the value to be applied
|
||||
*/
|
||||
public void setDepth(int depth){
|
||||
if(depth < 0){
|
||||
this.depth = 0;
|
||||
}
|
||||
else{
|
||||
this.depth = depth;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*Return the depth of the Fish
|
||||
*@return the value of depth
|
||||
*/
|
||||
public int getDepth(){
|
||||
return depth;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Find the equality of Fish
|
||||
*@param obj the Object to be compared to
|
||||
*@return the equality value
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
|
||||
if(!(obj instanceof Fish)){
|
||||
return false;
|
||||
}
|
||||
if(!super.equals(obj)){
|
||||
return false;
|
||||
}
|
||||
|
||||
Fish fTemp = (Fish)obj;
|
||||
//use of "this." is to make it explicit
|
||||
if(fTemp.getDepth() != this.getDepth()){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Debugging main for class Fish.
|
||||
* 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 Fish
|
||||
36
CS1322/p2/FlyingType.java
Normal file
36
CS1322/p2/FlyingType.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* FlyingType.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 21, 2002
|
||||
* Created the FlyingType interface
|
||||
* 1.1 Sep. 29, 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.1, Sep. 29, 2002
|
||||
*/
|
||||
|
||||
public interface FlyingType {
|
||||
|
||||
/**
|
||||
*set altitude of Animal
|
||||
*@param altitude, the altitude of the animal
|
||||
*/
|
||||
public void setAltitude(int alt);
|
||||
|
||||
/**
|
||||
*@return altitude of the animal
|
||||
*/
|
||||
public int getAltitude();
|
||||
|
||||
}// end of class FlyingType
|
||||
170
CS1322/p2/Gator.java
Normal file
170
CS1322/p2/Gator.java
Normal file
@@ -0,0 +1,170 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Gator.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 18, 2002
|
||||
* Created the Gator class
|
||||
* 1.1 Sep. 18, 2002
|
||||
* Finished the Gator Class
|
||||
* 1.1f Sep. 18, 2002
|
||||
* Cleaned up equals() to make more readable
|
||||
* </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.1f, Sep. 18, 2002
|
||||
*/
|
||||
|
||||
public class Gator extends Animal implements SwimmingType{
|
||||
|
||||
/**
|
||||
*depth of the gator
|
||||
*/
|
||||
private int depth;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Gator Constructor
|
||||
*@param name the name of the gator
|
||||
*sets depth to 0
|
||||
*/
|
||||
public Gator(String name){
|
||||
super(name);
|
||||
this.depth = 0;
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*set the depth of the gator
|
||||
*@param depth the value to be applied
|
||||
*/
|
||||
public void setDepth(int depth){
|
||||
if(depth > 50){
|
||||
this.depth = 50;
|
||||
}
|
||||
else {
|
||||
this.depth = depth;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*get the depth of the gator
|
||||
*@return the depth of the gator
|
||||
*/
|
||||
public int getDepth(){
|
||||
return this.depth;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Give the dangerous value of the Gator
|
||||
*@return true, the gator is dangerous
|
||||
*/
|
||||
public boolean isDangerous(){
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*Have the Gator eat some food
|
||||
*acceptible foods: chicken,beef,pork,human,mystery meat
|
||||
*@param if the food is of the above name, set isHungry to false
|
||||
*/
|
||||
public void eat(String food){
|
||||
if(food.equals("chicken")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
else if(food.equals("beef")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
else if(food.equals("pork")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
else if(food.equals("human")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
else if(food.equals("mystery meat")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
}//end of eat(food)
|
||||
|
||||
/**
|
||||
*Find the equality of Gator
|
||||
*@param obj the Object to be compared to
|
||||
*@return the equality value
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
|
||||
if(!(obj instanceof Gator)){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!super.equals(obj)){
|
||||
return false;
|
||||
}
|
||||
|
||||
Gator temp = (Gator)obj;
|
||||
if(temp.getDepth() != this.getDepth()){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}//end of equals(Object)
|
||||
|
||||
/**
|
||||
* Change standard string to output to be used for this specific program
|
||||
*
|
||||
* <br><br>
|
||||
* @return phrase as string
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
return ("A Gator named " + getName() + " at depth " + getDepth() +
|
||||
" which is " + ((this.getIsHungry())?"hungry." : "not hungry."));
|
||||
}
|
||||
/********************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class Gator.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Gator gator = new Gator("bob");
|
||||
gator.setIsHungry(true);
|
||||
System.out.println(gator);
|
||||
|
||||
gator.eat("chicken");
|
||||
System.out.println(gator);
|
||||
|
||||
gator.setDepth(100);
|
||||
System.out.println(gator);
|
||||
|
||||
System.out.println(gator.isDangerous());
|
||||
|
||||
Gator gator2 = new Gator("Steve");
|
||||
|
||||
System.out.println(gator.equals(gator));
|
||||
System.out.println(gator.equals(gator2));
|
||||
String bob = "bob";
|
||||
System.out.println(gator.equals(bob));
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Gator
|
||||
106
CS1322/p2/GoldFish.java
Normal file
106
CS1322/p2/GoldFish.java
Normal file
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* GoldFish.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 19, 2002
|
||||
* Created the GoldFish class
|
||||
* 1.1 Sep. 20, 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, Sep. 20, 2002
|
||||
*/
|
||||
|
||||
public class GoldFish extends Fish{
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for GoldFish
|
||||
*@param name to be given to goldfish
|
||||
*@param value to be given to depth
|
||||
*/
|
||||
public GoldFish(String name, int depth){
|
||||
super(name,depth);
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*isDangerous method for fish
|
||||
*@return false
|
||||
*/
|
||||
public boolean isDangerous(){
|
||||
return false; //you TA's obviously don't know the goldfish like i do...
|
||||
}
|
||||
|
||||
/**
|
||||
*Have the Goldfish eat some food
|
||||
*@param if the food is "fish food", set isHungry to false
|
||||
*/
|
||||
public void eat(String food){
|
||||
if(food.equals("fish food")){
|
||||
setIsHungry(false);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Change standard string to output to be used for this specific program
|
||||
* @return phrase as string
|
||||
*/
|
||||
public String toString(){
|
||||
return ("A GoldFish named " + getName() + " at depth " + getDepth() +
|
||||
" that is " + (getIsHungry()?"":"not ") + "hungry.");
|
||||
}
|
||||
|
||||
/**
|
||||
*Equals method for GoldFish
|
||||
*@param obj the value to be compared to
|
||||
*@return false if not a GoldFish
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
if(!(obj instanceof GoldFish)){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!(super.equals(obj))){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Debugging main for class GoldFish.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
GoldFish butch = new GoldFish("Butch", -6); //I've seen em leave their cup
|
||||
System.out.println(butch);
|
||||
|
||||
GoldFish butch2 = new GoldFish("Cassidy",54);
|
||||
butch2.setIsHungry(true);
|
||||
|
||||
System.out.println(butch2);
|
||||
System.out.println(butch.equals(butch2));
|
||||
System.out.println(butch.equals(butch));
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class GoldFish
|
||||
98
CS1322/p2/IOHelper.java
Normal file
98
CS1322/p2/IOHelper.java
Normal file
@@ -0,0 +1,98 @@
|
||||
/*====================================================================*/
|
||||
/* D O N O T E D I T T H I S F I L E */
|
||||
/*====================================================================*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* CS1312: IOHelper
|
||||
* <PRE>
|
||||
* IOHelper - a utility for reading input from the keyboard
|
||||
*
|
||||
* Revisions: 3.0 January 19, 2000
|
||||
* Re-created class IOHelper
|
||||
* Completetly Trashed old versions
|
||||
* Removed backwards compatibility
|
||||
*
|
||||
* </PRE>
|
||||
* @author <A HREF="mailto:cs1312@prism.gatech.edu">CS1312</A>
|
||||
* @version Version 3.0, January 19, 2000
|
||||
*/
|
||||
public class IOHelper
|
||||
{
|
||||
/*=================================================================*/
|
||||
/* C O N S T A N T S */
|
||||
/*=================================================================*/
|
||||
|
||||
/**
|
||||
* A Debug flag.
|
||||
*/
|
||||
public static final boolean bDEBUG = false;
|
||||
|
||||
/*=================================================================*/
|
||||
/* C O N S T R U C T O R */
|
||||
/*=================================================================*/
|
||||
|
||||
/**
|
||||
* Declaring the default constructor private prevents anyone from
|
||||
* making an instance of this class.
|
||||
*/
|
||||
private IOHelper()
|
||||
{
|
||||
System.err.println("IOHelper is not meant to be instantiated");
|
||||
} // end of default constructor, IOHelper()
|
||||
|
||||
/*=================================================================*/
|
||||
/* S T A T I C V A R I A B L E S */
|
||||
/*=================================================================*/
|
||||
|
||||
/**
|
||||
* Used by the BufferedReader to retrieve input.
|
||||
* <BR><BR>
|
||||
* @see #bReader bReader
|
||||
*/
|
||||
protected static InputStreamReader iStreamReader = new
|
||||
InputStreamReader(System.in);
|
||||
|
||||
/**
|
||||
* Retrieves input from the user using a wrapped InputStreamReader.
|
||||
* <BR><BR>
|
||||
* @see #iStreamReader iStreamReader
|
||||
*/
|
||||
protected static BufferedReader bReader = new
|
||||
BufferedReader(iStreamReader);
|
||||
|
||||
/*=================================================================*/
|
||||
/* S T A T I C M E T H O D S */
|
||||
/*=================================================================*/
|
||||
|
||||
/**
|
||||
* Retrieves input from the user using a BufferedReader
|
||||
* <BR><BR>
|
||||
* @return a String entered by the User
|
||||
*/
|
||||
public static final String readLine()
|
||||
{
|
||||
String strReturn = "";
|
||||
try
|
||||
{
|
||||
strReturn = bReader.readLine();
|
||||
} // end of try
|
||||
catch (IOException ioe)
|
||||
{
|
||||
if (bDEBUG)
|
||||
{
|
||||
System.err.println("IOHelper: " + ioe.getMessage());
|
||||
} // end of if
|
||||
} // end of catch(IOException)
|
||||
catch (NullPointerException npe)
|
||||
{
|
||||
/* This should NEVER happen */
|
||||
System.err.println("IOHelper: " + npe.getMessage());
|
||||
System.err.println("Please report this error to cs1312");
|
||||
System.exit(1);
|
||||
} // end of catch(NullPointerException)
|
||||
return strReturn;
|
||||
} // end of static method, readLine()
|
||||
|
||||
} // end of class IOHelper
|
||||
132
CS1322/p2/LLNode.java
Normal file
132
CS1322/p2/LLNode.java
Normal file
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* LLNode.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 21, 2002
|
||||
* Created the LLNode class
|
||||
* 1.1 Sep. 21, 2002
|
||||
* Finished class
|
||||
*
|
||||
* </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, Sep. 21, 2002
|
||||
*/
|
||||
|
||||
public class LLNode {
|
||||
|
||||
/**
|
||||
*holds the node data
|
||||
*/
|
||||
private Object data;
|
||||
|
||||
/**
|
||||
*holds the location of the next node
|
||||
*/
|
||||
private LLNode next;
|
||||
|
||||
//nothing to debug that this will help with,
|
||||
//but its in here for good measure
|
||||
private static final boolean bDebug = false;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Defaul LLNode Constructor
|
||||
*both next and data are initialized to null
|
||||
*/
|
||||
public LLNode(){
|
||||
setNext(null);
|
||||
setData(null);
|
||||
}
|
||||
|
||||
/**
|
||||
*LLNode(Object)
|
||||
*next initialized to null
|
||||
*@param o the value to be given to data
|
||||
*/
|
||||
public LLNode(Object o){
|
||||
setNext(null);
|
||||
setData(o);
|
||||
}
|
||||
|
||||
/**
|
||||
*LLNode(Object,LLNode)
|
||||
*@param o, the value of data
|
||||
*@param next, the value of next
|
||||
*/
|
||||
public LLNode(Object o, LLNode next){
|
||||
setNext(next);
|
||||
setData(o);
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Accessor for data
|
||||
*@return value of data
|
||||
*/
|
||||
public Object getData(){
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
*Modifier for data
|
||||
*@param the value for data
|
||||
*/
|
||||
public void setData(Object data){
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for next
|
||||
*@return the next node
|
||||
*/
|
||||
public LLNode getNext(){
|
||||
return next;
|
||||
}
|
||||
|
||||
/**
|
||||
*Modifier for next
|
||||
*@param the new next node
|
||||
*/
|
||||
public void setNext(LLNode next){
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
public String toString(){
|
||||
return (data.toString());
|
||||
}
|
||||
|
||||
/***********************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class LLNode.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Integer bob = new Integer(5);
|
||||
LLNode node = new LLNode(bob);
|
||||
System.out.println(node);
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class LLNode
|
||||
425
CS1322/p2/LinkedList.java
Normal file
425
CS1322/p2/LinkedList.java
Normal file
@@ -0,0 +1,425 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* LinkedList.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 21, 2002
|
||||
* Created the LinkedList class
|
||||
* 1.1 Sep. 26, 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, Sep. 26, 2002
|
||||
*/
|
||||
|
||||
public class LinkedList {
|
||||
|
||||
/**
|
||||
*Front of the list
|
||||
*/
|
||||
private LLNode head;
|
||||
|
||||
/**
|
||||
*Back of the List
|
||||
*/
|
||||
private LLNode tail;
|
||||
|
||||
//there's actually something to debug this time!
|
||||
private static final boolean bDebug = false;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Default constructor for LinkedList
|
||||
*sets head and tail to null
|
||||
*/
|
||||
public LinkedList(){
|
||||
head = null;
|
||||
tail = null;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Add node to front of list
|
||||
*@param o, the data to be stored in the node
|
||||
*/
|
||||
public void addToFront(Object o){
|
||||
LLNode node = new LLNode(o,head);
|
||||
|
||||
if(bDebug){System.out.print("Adding to Front:");}
|
||||
|
||||
head = node;
|
||||
if(tail == null){
|
||||
this.tail = node;
|
||||
}
|
||||
|
||||
if(bDebug){System.out.println("Done.");}
|
||||
}
|
||||
|
||||
/**
|
||||
*Add node to back of list
|
||||
*@param o, the data to be stored in the node
|
||||
*/
|
||||
public void addToBack(Object o){
|
||||
LLNode node = new LLNode(o);
|
||||
|
||||
if(bDebug){System.out.print("Adding to Back:");}
|
||||
|
||||
if(tail != null){
|
||||
tail.setNext(node);
|
||||
}
|
||||
|
||||
tail = node;
|
||||
if(head == null){
|
||||
head = node;
|
||||
}
|
||||
|
||||
if(bDebug){System.out.println("Done.");}
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the data stored by the first node
|
||||
*/
|
||||
public Object getFirst(){
|
||||
if(head != null){
|
||||
return head.getData();
|
||||
}
|
||||
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the data stored by the last node
|
||||
*/
|
||||
public Object getLast(){
|
||||
if(tail != null){
|
||||
return tail.getData();
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the data stored by the first node, destroy first node
|
||||
*/
|
||||
public Object removeFromFront(){
|
||||
if(head == null){
|
||||
return null;
|
||||
}
|
||||
|
||||
if(bDebug){System.out.print("Removing from Front:");}
|
||||
|
||||
Object temp = head.getData();
|
||||
|
||||
head = head.getNext();
|
||||
|
||||
if(head == null){
|
||||
tail = null;
|
||||
}
|
||||
|
||||
if(bDebug){System.out.println("Done.");}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the data stored by the last node, destroy the last node
|
||||
*/
|
||||
public Object removeFromBack(){
|
||||
if(tail == null){
|
||||
return null;
|
||||
}
|
||||
|
||||
if(bDebug){System.out.print("Removing from Back:");}
|
||||
|
||||
Object temp = tail.getData();
|
||||
LLNode current = head;
|
||||
|
||||
if(current != tail){
|
||||
//find second to last node
|
||||
while(current.getNext() != tail){
|
||||
current = current.getNext();
|
||||
}
|
||||
tail = current;
|
||||
tail.setNext(null);
|
||||
}
|
||||
|
||||
else {
|
||||
tail = null;
|
||||
}
|
||||
|
||||
if(tail == null){
|
||||
head = null;
|
||||
}
|
||||
|
||||
if(bDebug){System.out.println("Done.");}
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the number of nodes in the LinkedList
|
||||
*/
|
||||
public int size(){
|
||||
|
||||
if(head == null){
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(head == tail){
|
||||
return 1;
|
||||
}
|
||||
|
||||
int count=0;
|
||||
LLNode current = head;
|
||||
|
||||
while(current!= null){
|
||||
current = current.getNext();
|
||||
count++;
|
||||
}
|
||||
|
||||
return (count);
|
||||
}
|
||||
|
||||
/**
|
||||
*Turn the LinkedList into an Array
|
||||
*@return array containing all the data held by the LinkedList
|
||||
*/
|
||||
public Object[] asArray(){
|
||||
|
||||
Object[] array = new Object[this.size()];
|
||||
|
||||
if(head == null){
|
||||
return array;
|
||||
}
|
||||
|
||||
LLNode current = head;
|
||||
int i=0;
|
||||
while(current != null){
|
||||
array[i] = current.getData();
|
||||
i++;
|
||||
current = current.getNext();
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
*temp method to output list on screen
|
||||
*meant for debugging to circumvent the array method
|
||||
*/
|
||||
public void listOut(){
|
||||
LLNode current = head;
|
||||
|
||||
while(current != null){
|
||||
System.out.println(current.getData());
|
||||
current = current.getNext();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*Find the equality of the LinkedList
|
||||
*@param LL, the list to be compared to
|
||||
*@return the equality value
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
if(!(obj instanceof LinkedList)){
|
||||
return false;
|
||||
}
|
||||
|
||||
LinkedList LL = (LinkedList)obj;
|
||||
|
||||
LLNode thisCurrent = this.head;
|
||||
LLNode temp = LL.head;
|
||||
|
||||
if(this.size() != LL.size()){
|
||||
return false;
|
||||
}
|
||||
while(thisCurrent != null){
|
||||
if(!(thisCurrent.getData().equals(temp.getData()))){
|
||||
return false;
|
||||
}
|
||||
thisCurrent = thisCurrent.getNext();
|
||||
temp = temp.getNext();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change standard string to output to be used for this specific program
|
||||
* @return phrase as string
|
||||
*/
|
||||
public String toString(){
|
||||
String ReallyLong;
|
||||
ReallyLong = "[ ";
|
||||
LLNode current = head;
|
||||
while(current != null){
|
||||
ReallyLong += current.getData().toString();
|
||||
if(current.getNext() != null){
|
||||
ReallyLong += ", ";
|
||||
}
|
||||
current = current.getNext();
|
||||
}
|
||||
ReallyLong += "]";
|
||||
|
||||
return ReallyLong;
|
||||
}
|
||||
|
||||
/**
|
||||
*Check if there is an object in the list matching o
|
||||
*@return true if such an object exists
|
||||
*/
|
||||
public boolean contains(Object o){
|
||||
LLNode current = head;
|
||||
while(current != null){
|
||||
if(current.getData().equals(o)){
|
||||
return true;
|
||||
}
|
||||
current = current.getNext();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*Method meant for debugging
|
||||
*clears the list
|
||||
*/
|
||||
public void clear(){
|
||||
head = null;
|
||||
tail = null;
|
||||
}
|
||||
|
||||
/*********************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class LinkedList.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
LinkedList LL = new LinkedList();
|
||||
Object[] array;
|
||||
LinkedList LL2 = new LinkedList();
|
||||
|
||||
System.out.println("test 1");
|
||||
LL.addToFront("2");
|
||||
LL.addToFront("1");
|
||||
LL.addToBack("3");
|
||||
LL.addToBack("4");
|
||||
LL.listOut();
|
||||
array = LL.asArray();
|
||||
|
||||
for(int i=0; i<array.length; i++){
|
||||
System.out.println(array[i]);
|
||||
}
|
||||
|
||||
System.out.println("test 2");
|
||||
LL.removeFromFront();
|
||||
LL.removeFromBack();
|
||||
LL.removeFromBack();
|
||||
|
||||
array = LL.asArray();
|
||||
|
||||
for(int i=0; i<array.length; i++){
|
||||
System.out.println(array[i]);
|
||||
}
|
||||
|
||||
System.out.println(LL.equals(LL2));
|
||||
|
||||
LL.clear();
|
||||
LL.addToFront("1");
|
||||
LL.addToFront("2");
|
||||
LL.addToBack("3");
|
||||
LL2.addToFront("1");
|
||||
LL2.addToFront("2");
|
||||
LL2.addToBack("3");
|
||||
|
||||
System.out.println(LL2.equals(LL));
|
||||
|
||||
LL.clear();
|
||||
LL2.clear();
|
||||
|
||||
LL.addToBack("2");
|
||||
|
||||
LL.addToFront(new Integer(1));
|
||||
LL2.addToBack("2");
|
||||
|
||||
System.out.println(LL.equals(LL2));
|
||||
|
||||
LL2.addToFront(new Integer(1));
|
||||
|
||||
System.out.println(LL2.equals(LL));
|
||||
|
||||
LL2.removeFromBack();
|
||||
|
||||
System.out.println(LL2.equals(LL));
|
||||
System.out.println(LL.equals(LL));
|
||||
LL.clear();
|
||||
System.out.println(LL.equals(LL2));
|
||||
|
||||
//test what autograder finds as wrong
|
||||
//431, 653, 95, 201, 331, 326, 627, 38, 284, 350, 960, 36
|
||||
LL.clear();
|
||||
LL2.clear();
|
||||
|
||||
LL.addToBack(new Integer(36));
|
||||
LL.addToFront(new Integer(960));
|
||||
LL.addToFront(new Integer(350));
|
||||
LL.addToFront(new Integer(284));
|
||||
LL.addToFront(new Integer(38));
|
||||
LL.addToFront(new Integer(627));
|
||||
LL.addToFront(new Integer(326));
|
||||
LL.addToFront(new Integer(331));
|
||||
LL.addToFront(new Integer(201));
|
||||
LL.addToFront(new Integer(95));
|
||||
LL.addToFront(new Integer(653));
|
||||
LL.addToFront(new Integer(431));
|
||||
|
||||
System.out.println(LL.equals(LL));
|
||||
System.out.println(LL.equals(LL2));
|
||||
LL2 = LL;
|
||||
System.out.println(LL.equals(LL2));
|
||||
|
||||
//Holy crap this is a long debug main!
|
||||
LL2.clear();
|
||||
|
||||
LL2.addToFront(new Integer(960));
|
||||
LL2.addToFront(new Integer(350));
|
||||
LL2.addToFront(new Integer(284));
|
||||
LL2.addToFront(new Integer(38));
|
||||
LL2.addToFront(new Integer(627));
|
||||
LL2.addToFront(new Integer(326));
|
||||
LL2.addToFront(new Integer(331));
|
||||
LL2.addToFront(new Integer(201));
|
||||
LL2.addToFront(new Integer(95));
|
||||
LL2.addToFront(new Integer(653));
|
||||
LL2.addToFront(new Integer(431));
|
||||
|
||||
System.out.println(LL.equals(LL2));
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class LinkedList
|
||||
114
CS1322/p2/Shark.java
Normal file
114
CS1322/p2/Shark.java
Normal file
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Shark.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 19, 2002
|
||||
* Created the Shark class
|
||||
* 1.1 Sep. 20, 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, Sep. 20, 2002
|
||||
*/
|
||||
|
||||
public class Shark extends Fish{
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for Shark
|
||||
*@param name of the Shark
|
||||
*@param depth of the shark
|
||||
*/
|
||||
public Shark(String name, int depth){
|
||||
super(name,depth);
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Eat method for Shark
|
||||
*@param food to eat, if it's human, chicken,fish isHungry = false
|
||||
*/
|
||||
public void eat(String food)
|
||||
{
|
||||
if(food.equals("human")){ //mmmmmm...
|
||||
setIsHungry(false);
|
||||
}
|
||||
else if(food.equals("chicken")){ //tastes like chicken
|
||||
setIsHungry(false);
|
||||
}
|
||||
else if(food.equals("fish")){ //IT ATE ITS OWN SUPERCLASS!!!
|
||||
setIsHungry(false);
|
||||
}
|
||||
}
|
||||
/**
|
||||
*Give the dangerous value of the Shark
|
||||
*@return true, the Shark is dangerous
|
||||
*/
|
||||
public boolean isDangerous(){
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*Find the equality of Shark
|
||||
*@param obj the Object to be compared to
|
||||
*@return the equality value
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
|
||||
if(!(obj instanceof Shark)){
|
||||
return false;
|
||||
}
|
||||
|
||||
if(!(super.equals(obj))){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*Modify String for this particular program
|
||||
*@return the data of the Shark as a string statement
|
||||
*/
|
||||
public String toString(){
|
||||
return ("A Shark named " + getName() + " at depth " + getDepth() +
|
||||
" that is " + (getIsHungry()?"":"not ") + "hungry.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Debugging main for class Shark.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Shark shark = new Shark("Sharky",150);
|
||||
shark.setIsHungry(true);
|
||||
shark.setDepth(151);
|
||||
System.out.println(shark);
|
||||
|
||||
Shark shark2 = new Shark("Pissey",-52);
|
||||
System.out.println(shark2);
|
||||
|
||||
System.out.println(shark.equals(shark2));
|
||||
System.out.println(shark.equals(shark));
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Shark
|
||||
35
CS1322/p2/SwimmingType.java
Normal file
35
CS1322/p2/SwimmingType.java
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* SwimmingType.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 21, 2002
|
||||
* Created the SwimmingType interface
|
||||
* 1.1 Sep. 28, 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.1, Sep. 28, 2002
|
||||
*/
|
||||
|
||||
public interface SwimmingType {
|
||||
|
||||
/**
|
||||
*@param depth, the depth to set animal at
|
||||
*/
|
||||
public void setDepth(int depth);
|
||||
|
||||
/**
|
||||
*@return the depth of the animal
|
||||
*/
|
||||
public int getDepth();
|
||||
|
||||
}// end of class SwimmingType
|
||||
535
CS1322/p2/Zoo.java
Normal file
535
CS1322/p2/Zoo.java
Normal file
@@ -0,0 +1,535 @@
|
||||
/**
|
||||
* CS1322: Programming Assignment #2 - Fall 2002
|
||||
*
|
||||
*
|
||||
* <PRE>
|
||||
* Class Zoo - A class designed to provide a menu for users to interact
|
||||
* with to run the Zoo for P2. This class is provided by CS1322,
|
||||
* for you to modify as needed.
|
||||
* You should add your collaboration statement, to this ident box below,
|
||||
* and place your own @author tag below the one given by the author of
|
||||
* the code given.
|
||||
* Revisions 1.0: September 15th, 2002
|
||||
* Received class from CS1322
|
||||
* 1.1: September 28th, 2002
|
||||
* Finished Coding
|
||||
* </PRE>
|
||||
*
|
||||
* @author <A HREF="mailto:adhilton@cc.gatech.edu">Andrew Hilton</A>
|
||||
* @version Version 1.1, September 28th, 2002
|
||||
*/
|
||||
import java.io.*;
|
||||
public class Zoo
|
||||
{
|
||||
/**
|
||||
* This variable contains all of the buildings (Aquariums and
|
||||
* Aviaries) that are in this Zoo.
|
||||
*/
|
||||
private ZooBuilding[] myBuildings;
|
||||
/**
|
||||
* This constant specifies the number of buildings in the Zoo.
|
||||
*/
|
||||
private static final int NUM_BUILDINGS=6;
|
||||
/**
|
||||
* This constant is the main menu choice for putting a new animal
|
||||
* into the Zoo.
|
||||
*/
|
||||
private static final int CHOICE_PUT_ANIMAL=1;
|
||||
/**
|
||||
* This constant is the main menu choice for listing the animals
|
||||
* in the Zoo.
|
||||
*/
|
||||
private static final int CHOICE_LIST_ANIMALS=2;
|
||||
/**
|
||||
* This constant is the main menu choice for feeding the animals in
|
||||
* the Zoo.
|
||||
*/
|
||||
private static final int CHOICE_FEED_ANIMALS=3;
|
||||
/**
|
||||
* This constant is the main menu choice for moving the net in one
|
||||
* of the Aviaries.
|
||||
*/
|
||||
private static final int CHOICE_MOVE_NET=4;
|
||||
/**
|
||||
* This constant is the main menu choice for quitting the program.
|
||||
*/
|
||||
private static final int CHOICE_QUIT=0;
|
||||
/**
|
||||
* This constant specifies the String that is printed to display the
|
||||
* main menu.
|
||||
*/
|
||||
private static final String MAIN_MENU=
|
||||
"Main Zoo Control\n" +
|
||||
"----------------\n" +
|
||||
CHOICE_PUT_ANIMAL+".) Put an animal in a building\n" +
|
||||
CHOICE_LIST_ANIMALS+".) List the animals in a building\n" +
|
||||
CHOICE_FEED_ANIMALS+".) Feed the Animals in a building\n" +
|
||||
CHOICE_MOVE_NET+".) Move the net in an aviary\n"+
|
||||
CHOICE_QUIT+".) Quit\n"+
|
||||
"\nEnter your choice: ";
|
||||
/**
|
||||
* This constant specifies the animal creation menu choice to make a bat.
|
||||
*/
|
||||
private static final int ANIMAL_BAT=1;
|
||||
/**
|
||||
* This constant specifies the animal creation menu choice to make a bird.
|
||||
*/
|
||||
private static final int ANIMAL_BIRD=2;
|
||||
/**
|
||||
* This constant specifies the animal creation menu choice to make a duck.
|
||||
*/
|
||||
private static final int ANIMAL_DUCK=3;
|
||||
/**
|
||||
* This constant specifies the animal creation menu choice to make a gator.
|
||||
*/
|
||||
private static final int ANIMAL_GATOR=4;
|
||||
/**
|
||||
* This constant specifies the animal creation menu choice to make a
|
||||
* goldfish.
|
||||
*/
|
||||
private static final int ANIMAL_GOLDFISH=5;
|
||||
/**
|
||||
* This constant specifies the animal creation menu choice to make a shark.
|
||||
*/
|
||||
private static final int ANIMAL_SHARK=6;
|
||||
/**
|
||||
* This constant specifies the String that is displayed for the animal
|
||||
* creation menu.
|
||||
*/
|
||||
private static final String ANIMAL_MENU=
|
||||
"Choose an Animal Type:\n"+
|
||||
"----------------------\n"+
|
||||
ANIMAL_BAT+".) Bat\n"+
|
||||
ANIMAL_BIRD+".) Bird\n"+
|
||||
ANIMAL_DUCK+".) Duck\n"+
|
||||
ANIMAL_GATOR+".) Gator\n"+
|
||||
ANIMAL_GOLDFISH+".) Goldfish\n"+
|
||||
ANIMAL_SHARK+".) Shark\n"+
|
||||
"\nEnter your choice: ";
|
||||
/**
|
||||
* The default constructor for Zoo.
|
||||
* This constructor initializes myBuildings to be an array of
|
||||
* size NUM_BUILDINGS, then fills it in, alternating between Aviaries
|
||||
* and Aquariums.
|
||||
*/
|
||||
public Zoo()
|
||||
{
|
||||
myBuildings=new ZooBuilding[NUM_BUILDINGS];
|
||||
for(int i=0;i<myBuildings.length;i++)
|
||||
{
|
||||
if(i%2==0)
|
||||
myBuildings[i]=new Aviary("Aviary number "+(i/2+1));
|
||||
else
|
||||
myBuildings[i]=new Aquarium("Aquarium number "+ (i/2+1) );
|
||||
}//for
|
||||
}//Zoo()
|
||||
/**
|
||||
* Reads user input and creates a specified animal.
|
||||
* This method reads in input from the user to determine what
|
||||
* type of Animal to make, and then reads the appropriate information
|
||||
* for that type of Animal.
|
||||
* @return a newly created Animal that matches the user's specifications,
|
||||
* or null if the user selects an invalid Animal number
|
||||
*/
|
||||
private Animal makeAnimalForUser()
|
||||
{
|
||||
System.out.println(ANIMAL_MENU);
|
||||
int choice= readIntFromUser();
|
||||
Animal toRet=null;
|
||||
String name;
|
||||
String species;
|
||||
String dangerous;
|
||||
boolean isDangerous;
|
||||
int depth;
|
||||
switch(choice)
|
||||
{
|
||||
case ANIMAL_BAT:
|
||||
name=promptAndRead("Name: ");
|
||||
toRet=new Bat(name);
|
||||
break;
|
||||
case ANIMAL_BIRD:
|
||||
name=promptAndRead("Name: ");
|
||||
species=promptAndRead("Species: ");
|
||||
|
||||
do
|
||||
{
|
||||
dangerous=promptAndRead("Dangerous [Y/N]: ");
|
||||
}
|
||||
while(!(dangerous.equalsIgnoreCase("Y")||
|
||||
dangerous.equalsIgnoreCase("N")));
|
||||
isDangerous=dangerous.equalsIgnoreCase("Y");
|
||||
toRet= new Bird(name,species,isDangerous);
|
||||
break;
|
||||
case ANIMAL_DUCK:
|
||||
name=promptAndRead("Name: ");
|
||||
toRet= new Duck(name);
|
||||
break;
|
||||
case ANIMAL_GATOR:
|
||||
name=promptAndRead("Name: ");
|
||||
toRet= new Gator(name);
|
||||
break;
|
||||
case ANIMAL_GOLDFISH:
|
||||
name=promptAndRead("Name: ");
|
||||
System.out.print("Depth: ");
|
||||
depth=readIntFromUser();
|
||||
toRet= new GoldFish(name,depth);
|
||||
break;
|
||||
case ANIMAL_SHARK:
|
||||
name=promptAndRead("Name: ");
|
||||
System.out.print("Depth: ");
|
||||
depth=readIntFromUser();
|
||||
toRet= new Shark(name,depth);
|
||||
break;
|
||||
default:
|
||||
System.out.println("That is not a valid animal type");
|
||||
break;
|
||||
}
|
||||
return toRet;
|
||||
}//makeAnimalForUser
|
||||
/**
|
||||
* This method moves the Animals in all of the buildings around
|
||||
* randomly. In addition to moving the Animals around, this method
|
||||
* makes Animals become hungry, at random- each with a 75% chance
|
||||
* of becoming hungry when this method is called.
|
||||
*/
|
||||
private void moveAnimalsAround()
|
||||
{
|
||||
for(int i=0;i<myBuildings.length;i++)
|
||||
{
|
||||
ZooBuilding zb=myBuildings[i];
|
||||
LinkedList ll=zb.getTheAnimals();
|
||||
Object[] animalsInList=ll.asArray();
|
||||
for(int j=0;j<animalsInList.length;j++)
|
||||
{
|
||||
|
||||
if(animalsInList[j] instanceof FlyingType &&
|
||||
animalsInList[j] instanceof SwimmingType)
|
||||
{
|
||||
//this animal can both swim and fly.
|
||||
//better find out where it is
|
||||
FlyingType ft=(FlyingType) animalsInList[j];
|
||||
SwimmingType st = (SwimmingType) animalsInList[j];
|
||||
int alt=ft.getAltitude();
|
||||
int depth=st.getDepth();
|
||||
boolean swimming;
|
||||
if(alt<=0)
|
||||
swimming=true;
|
||||
else
|
||||
swimming=false;
|
||||
if(swimming)
|
||||
{
|
||||
//if near the water's surface, maybe we take off
|
||||
//(1/4 chance)
|
||||
if(depth<=1 &&Math.random()<0.25)
|
||||
{
|
||||
st.setDepth(0);
|
||||
ft.setAltitude((int)(Math.random()*25)+15);
|
||||
}
|
||||
else
|
||||
{
|
||||
int change=(int)((Math.random()*5)-2);
|
||||
depth += change;
|
||||
st.setDepth(depth);
|
||||
}
|
||||
}//swimming
|
||||
else
|
||||
{
|
||||
//flying low. maybe we land and swim?
|
||||
// (1/4 chance)
|
||||
if(alt <=25 && Math.random()<0.25)
|
||||
{
|
||||
ft.setAltitude(0);
|
||||
st.setDepth(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
int change=(int)(Math.random()*48)-24;
|
||||
alt += change;
|
||||
ft.setAltitude(alt);
|
||||
}
|
||||
}//flying
|
||||
}
|
||||
else if(animalsInList[j] instanceof FlyingType)
|
||||
{
|
||||
FlyingType ft=(FlyingType) animalsInList[j];
|
||||
int alt=ft.getAltitude();
|
||||
int change=(int)((Math.random()*20)+(Math.random()*20)-10);
|
||||
alt += change;
|
||||
if(alt<0)
|
||||
{
|
||||
alt=0;
|
||||
}
|
||||
ft.setAltitude(alt);
|
||||
|
||||
}//flying
|
||||
else if(animalsInList[j] instanceof SwimmingType)
|
||||
{
|
||||
SwimmingType st=(SwimmingType) animalsInList[j];
|
||||
//*** YOUR CODE GOES HERE *** (3)
|
||||
int depth = st.getDepth();
|
||||
/************/
|
||||
|
||||
int change=(int)((Math.random()*30)-12);
|
||||
depth+=change;
|
||||
//*** YOUR CODE GOES HERE *** (4)
|
||||
st.setDepth(depth);
|
||||
/*****************/
|
||||
|
||||
}//swimming
|
||||
if(animalsInList[j] instanceof Animal)
|
||||
{
|
||||
if(Math.random()<0.75) //75% chance of animal getting hungry
|
||||
{
|
||||
((Animal)animalsInList[j]).setIsHungry(true);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
System.err.println("Error: Non-animal in array at index "+
|
||||
j+"\n found: "+animalsInList[j]);
|
||||
System.exit(-1);
|
||||
}
|
||||
}//for j
|
||||
}//for i
|
||||
}//moveAnimalsAround()
|
||||
/**
|
||||
* A conveinece method to prompt the user for input and read it.
|
||||
* Prints the prompt, then reads and returns a line of input.
|
||||
* @param prompt is the prompt to print
|
||||
* @return the line of input read from the user.
|
||||
*/
|
||||
private static String promptAndRead(String prompt)
|
||||
{
|
||||
System.out.print(prompt);
|
||||
return IOHelper.readLine();
|
||||
}
|
||||
/**
|
||||
* Reads in an int from the user and returns it.
|
||||
* This method reads lines of input from the user until the user enters
|
||||
* a valid number. When a valid number is entered, it is converted
|
||||
* from a String to an int and returned.
|
||||
* @return the number that the user entered.
|
||||
*/
|
||||
private static int readIntFromUser()
|
||||
{
|
||||
String line="";
|
||||
int choice=0;
|
||||
boolean done=false;
|
||||
do
|
||||
{
|
||||
//***YOUR CODE GOES HERE*** (1)
|
||||
line = IOHelper.readLine();
|
||||
|
||||
//now do conversion of line to an int
|
||||
try
|
||||
{
|
||||
choice=Integer.parseInt(line);
|
||||
done=true;
|
||||
}
|
||||
catch(NumberFormatException nfe)
|
||||
{
|
||||
System.out.println("That was not a number! Try again");
|
||||
}
|
||||
|
||||
}
|
||||
while(!done);
|
||||
return choice;
|
||||
}//readIntFromUser()
|
||||
/**
|
||||
* Selects a building number.
|
||||
* Prompts the user to select a building, continues to reprompt
|
||||
* until a valid building number is entered.
|
||||
* @return the building number entered by the user
|
||||
*/
|
||||
private int chooseBuilding()
|
||||
{
|
||||
int choice;
|
||||
do
|
||||
{
|
||||
System.out.print("Enter a building number");
|
||||
choice=readIntFromUser();
|
||||
}
|
||||
while(choice<0|| choice>=myBuildings.length);
|
||||
return choice;
|
||||
}//chooseBuilding
|
||||
/**
|
||||
* Handles putting a new Animal into the Zoo.
|
||||
* This method reads input from the user to determine which building the
|
||||
* Animal should go into. Then it calls makeAnimalForUser() to
|
||||
* create a new Animal to put in. If the Animal cannot live in that
|
||||
* type of building, an error is printed and the method returns,
|
||||
* otherwise the Animal is added to the building.
|
||||
*/
|
||||
private void doPutAnimal()
|
||||
{
|
||||
int buildingNumber=chooseBuilding();
|
||||
ZooBuilding zb=myBuildings[buildingNumber];
|
||||
Animal toAdd=makeAnimalForUser();
|
||||
if(toAdd==null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if(((myBuildings[buildingNumber] instanceof Aviary) &&
|
||||
!(toAdd instanceof FlyingType))||
|
||||
((myBuildings[buildingNumber] instanceof Aquarium) &&
|
||||
!(toAdd instanceof SwimmingType)))
|
||||
{
|
||||
System.out.println("That animal cannot live in that building!");
|
||||
return;
|
||||
}
|
||||
//***YOUR CODE GOES HERE *** (5)
|
||||
zb.getTheAnimals().addToBack(toAdd);
|
||||
/**********************************/
|
||||
|
||||
}//doPutAnimal()
|
||||
/**
|
||||
* Lists the animals in a building.
|
||||
* This method prompts the user for a building number, then
|
||||
* lists all of the animals in that building.
|
||||
*/
|
||||
private void doListAnimals()
|
||||
{
|
||||
//*** YOUR CODE GOES HERE *** (6)
|
||||
int choice = chooseBuilding();
|
||||
ZooBuilding zb = myBuildings[choice];
|
||||
Animal temp;
|
||||
|
||||
for(int i=0; i<zb.getTheAnimals().size(); i++){
|
||||
temp = (Animal)zb.getTheAnimals().removeFromFront();
|
||||
System.out.println(temp);
|
||||
zb.getTheAnimals().addToBack(temp);
|
||||
}
|
||||
}//doListAnimals()
|
||||
/**
|
||||
* Feeds the Animals in a building.
|
||||
* This method prompts the user for a building number and a type of food,
|
||||
* then feeds that food to the animals in the specified building.
|
||||
*/
|
||||
private void doFeedAnimals()
|
||||
{
|
||||
int buildingNumber=chooseBuilding();
|
||||
ZooBuilding zb=myBuildings[buildingNumber];
|
||||
String food=promptAndRead("What type of food?");
|
||||
//*** YOUR CODE GOES HERE *** (7)
|
||||
zb.feedAnimals(food);
|
||||
|
||||
}//doFeedAnimals()
|
||||
|
||||
/**
|
||||
*prompt for building, check if its aviary,
|
||||
*and if it is, add/remove the net
|
||||
*/
|
||||
private void doMoveNet()
|
||||
{
|
||||
// *** YOUR CODE GOES HERE *** (9)
|
||||
//Let's do this all in Hungarian Notation :)
|
||||
//So MS isn't completely useless, eh?
|
||||
|
||||
String sEntry;
|
||||
int iChoice = 0;
|
||||
boolean bDone = false;
|
||||
|
||||
do{
|
||||
sEntry = promptAndRead("What building?");
|
||||
try
|
||||
{
|
||||
iChoice=Integer.parseInt(sEntry);
|
||||
bDone=true;
|
||||
}
|
||||
catch(NumberFormatException nfe)
|
||||
{
|
||||
System.out.println("That was not a number! Try again");
|
||||
}
|
||||
}while(!bDone);
|
||||
|
||||
if(!(myBuildings[iChoice] instanceof Aviary)){
|
||||
System.out.println("That building is not an Aviary, it has no net");
|
||||
return;
|
||||
}
|
||||
|
||||
boolean bNet;
|
||||
Aviary avTemp = (Aviary)myBuildings[iChoice];
|
||||
bNet = avTemp.getNetClosed();
|
||||
|
||||
if(bNet){
|
||||
avTemp.setNetClosed(false);
|
||||
}
|
||||
else {//!bNet
|
||||
avTemp.setNetClosed(true);
|
||||
}
|
||||
|
||||
}//doMoveNet
|
||||
private void showZooStatus()
|
||||
{
|
||||
System.out.println("Zoo status");
|
||||
System.out.println("----------");
|
||||
for(int i=0;i<myBuildings.length;i++)
|
||||
{
|
||||
System.out.println(myBuildings[i]);
|
||||
if(myBuildings[i].isSafeToEnter())
|
||||
{
|
||||
System.out.println("\t Is safe to enter.");
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("\t Is NOT safe to enter.");
|
||||
}
|
||||
System.out.println("\t Has "+myBuildings[i].getHungryAnimals()+
|
||||
" hungry animals.");
|
||||
}//for i
|
||||
}//showZooStatus
|
||||
/**
|
||||
* Runs the Zoo simulation.
|
||||
* This method displays the menu and performs the requested action
|
||||
* until the user chooses to quit.
|
||||
*/
|
||||
private void run()
|
||||
{
|
||||
int userChoice;
|
||||
do
|
||||
{
|
||||
moveAnimalsAround();
|
||||
showZooStatus();
|
||||
//***YOUR CODE GOES HERE*** (8)
|
||||
System.out.println(MAIN_MENU);
|
||||
/****************************/
|
||||
|
||||
userChoice=readIntFromUser();
|
||||
switch(userChoice)
|
||||
{
|
||||
case CHOICE_PUT_ANIMAL:
|
||||
doPutAnimal();
|
||||
break;
|
||||
case CHOICE_LIST_ANIMALS:
|
||||
doListAnimals();
|
||||
break;
|
||||
case CHOICE_FEED_ANIMALS:
|
||||
doFeedAnimals();
|
||||
break;
|
||||
//*** YOUR CODE GOES HERE *** (10)
|
||||
case CHOICE_MOVE_NET:
|
||||
doMoveNet();
|
||||
break;
|
||||
case CHOICE_QUIT:
|
||||
/* do nothing */
|
||||
break;
|
||||
default:
|
||||
//***YOUR CODE GOES HERE***(2)
|
||||
System.out.println("That is not a valid choice, try again");
|
||||
break;
|
||||
}//switch(userChoice)
|
||||
}
|
||||
while(userChoice!= CHOICE_QUIT);
|
||||
}//run()
|
||||
/**
|
||||
* The starting point for the program- makes a new Zoo, and calls
|
||||
* run() to do the simulation.
|
||||
* @params args IGNORED.
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Zoo myZoo=new Zoo();
|
||||
myZoo.run();
|
||||
}//main()
|
||||
|
||||
}//end class Zoo
|
||||
125
CS1322/p2/ZooBuilding.java
Normal file
125
CS1322/p2/ZooBuilding.java
Normal file
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* ZooBuilding.java
|
||||
*
|
||||
* Revisions: 1.0 Sep. 26, 2002
|
||||
* Created the ZooBuilding class
|
||||
* 1.1 Sep. 28, 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, Sep. 28, 2002
|
||||
*/
|
||||
|
||||
public abstract class ZooBuilding {
|
||||
|
||||
/**
|
||||
*theAnimals list meant for storing
|
||||
*the animals of the building
|
||||
*/
|
||||
private LinkedList theAnimals;
|
||||
|
||||
/**
|
||||
*holds the name of the building
|
||||
*/
|
||||
private String name;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/*
|
||||
*Constructor for ZooBuilding
|
||||
*@param name, the name of the zoobuilding
|
||||
*/
|
||||
public ZooBuilding(String name){
|
||||
this.name = name;
|
||||
theAnimals = new LinkedList();
|
||||
}
|
||||
|
||||
/////////////
|
||||
//Accessors//
|
||||
/////////////
|
||||
|
||||
/**
|
||||
*@return the List of Animals
|
||||
*/
|
||||
public LinkedList getTheAnimals(){
|
||||
return theAnimals;
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the value of name
|
||||
*/
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
*@return number of Animals in the Zoo
|
||||
*/
|
||||
public int getHungryAnimals(){
|
||||
int total=0;
|
||||
Animal aTemp;
|
||||
for(int i=0; i<theAnimals.size(); i++){
|
||||
aTemp = (Animal)theAnimals.removeFromBack();
|
||||
if(aTemp.getIsHungry() == true){
|
||||
total++;
|
||||
}
|
||||
theAnimals.addToFront(aTemp);
|
||||
}
|
||||
return total;
|
||||
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Check if its safe to enter
|
||||
*@return yes/no
|
||||
*/
|
||||
public abstract boolean isSafeToEnter();
|
||||
|
||||
/**
|
||||
*Feed the Animals param food
|
||||
*@param food, the food to feed all the animals
|
||||
*/
|
||||
public void feedAnimals(String food){
|
||||
//why feed them if there is a sign clearly saying not to?
|
||||
if(!(isSafeToEnter())){
|
||||
System.out.println("The building is unsafe to enter!");
|
||||
return;
|
||||
}
|
||||
Animal aTemp;
|
||||
for(int i=0; i<theAnimals.size(); i++){
|
||||
aTemp = (Animal)theAnimals.removeFromBack();
|
||||
aTemp.eat(food);
|
||||
theAnimals.addToFront(aTemp);
|
||||
}
|
||||
}
|
||||
|
||||
/*******************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class ZooBuilding.
|
||||
* 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 ZooBuilding
|
||||
BIN
CS1322/p2/bill.zip
Normal file
BIN
CS1322/p2/bill.zip
Normal file
Binary file not shown.
BIN
CS1322/p2/cs1322-P2.zip
Normal file
BIN
CS1322/p2/cs1322-P2.zip
Normal file
Binary file not shown.
BIN
CS1322/p2/done3.zip
Normal file
BIN
CS1322/p2/done3.zip
Normal file
Binary file not shown.
498
CS1322/p2/p2.txt
Normal file
498
CS1322/p2/p2.txt
Normal file
@@ -0,0 +1,498 @@
|
||||
CS1322 - Program 2: Its a zoo out there!
|
||||
===============================================================================
|
||||
|
||||
Assigned: Monday, September 18th, 2002
|
||||
Due: Monday, September 30th, 2002 at 23:59:59
|
||||
Stop Accepting: Tuesday, October 1st, 2002 at 08:00:00
|
||||
Phase I Instant Feedback Deadline: Monday, September 23rd, 2002 at 23:59:59
|
||||
Phase V Instant Feedback Deadline: Monday, September 30th, 2002 at 23:59:59
|
||||
|
||||
|
||||
=======================================================================
|
||||
TOPICS
|
||||
=======================================================================
|
||||
|
||||
Topics you should understand coming into this assignment:
|
||||
o Creation and usage of objects and methods
|
||||
o Iteration and Recursion
|
||||
o Arrays
|
||||
|
||||
Topics that you should understand as a result of doing this assignment:
|
||||
o Inheritance
|
||||
o Interfaces
|
||||
o Polymorphism
|
||||
o Dynamic Binding
|
||||
o Linked Lists
|
||||
o Stacks/Queues
|
||||
o Constructor Chaining
|
||||
===============================================================================
|
||||
Program Overview
|
||||
===============================================================================
|
||||
In this program, you are going to be writing code for a zoo. This zoo has
|
||||
Animals of different types: Sharks, GoldFish, Gators, Birds, Ducks, and Bats.
|
||||
You will keep track of the Animals in various places via LinkedLists.
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Phase 1: Your basic Animals *THIS IS AN INSTANT FEEDBACK PHASE*
|
||||
This phase should take you no more than 2 hours, 10 minutes to complete
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The first step is to make an abstract class Animal.
|
||||
This class should have the following variables and methods:
|
||||
|
||||
public abstract boolean isDangerous();
|
||||
public abstract void eat(String food);
|
||||
a private boolean called "isHungry" and appropriate accessor/modifier
|
||||
(setIsHungry/getIsHungry).
|
||||
a private String called "name" and appropriate accessor (No modifier).
|
||||
a .equals method which behaves as follows
|
||||
- if the parameter passed in is not an Animal, return false
|
||||
- if the parameter passed in has a different name return false
|
||||
- if one animal is dangerous and the other is not, return false
|
||||
- if one animal is hungry and the other is not, return false
|
||||
- otherwise, return true
|
||||
a constructor that takes in a name and sets the name variable from it,
|
||||
and should set isHungry to false.
|
||||
Next, you should make the following subclasses of Animal:
|
||||
|
||||
o Fish, which is abstract
|
||||
- Fish have an private int depth, with appropriate accessor and
|
||||
modifier. Since fish cannot swim at a negative depth, anything
|
||||
less than 0 should be replaced by 0 in the modifier.
|
||||
- Fish have a .equals method which behaves as follows
|
||||
* if the parameter passed in is not a Fish, return false
|
||||
* call the super class's .equals method- if it returns
|
||||
false, then return false.
|
||||
* if the parameter passed in is at a different depth,
|
||||
return false
|
||||
* otherwise, return true
|
||||
- Fish have a constructor that takes a name and an initial depth.
|
||||
They should use super() to call the parent constructor with the name.
|
||||
o Gator, which is NOT abstract
|
||||
- Gators are dangerous, so implement the isDangerous() method
|
||||
appropriately.
|
||||
- The Gator should implement the eat method to compare the food
|
||||
passed in to "chicken", "beef", "pork", "human", and "mystery meat",
|
||||
and if it is one of those, setHungry to false. If the food is
|
||||
not acceptable, the Gator ignores it and does not change its
|
||||
isHungry status.
|
||||
- Gators have a private int depth, with appropriate accessor and
|
||||
modifier, however, Gators do not swim below 50 feet under the
|
||||
water- so if a value of more than 50 is passed to the modifier,
|
||||
it is set to 50. Unlike fish, Gators may come out of the water,
|
||||
so they can have a negative depth. (A gator at -5 feet under
|
||||
the water would be on the ground, 5 feet above the water).
|
||||
- Gators have a .equals method which behaves as follows
|
||||
* if the parameter passed in is not a Gator, return false
|
||||
* call the super class's .equals method- if it returns
|
||||
false, then return false.
|
||||
* if the parameter passed in is at a different depth,
|
||||
return false
|
||||
* otherwise, return true
|
||||
- Gators have a constructor that takes a name. The super constructor
|
||||
should be called with this name, and the depth should be set to 0.
|
||||
- Gators have a toString that returns the following:
|
||||
A Gator named <name> at depth <depth> which is [hungry/not hungry].
|
||||
For example,
|
||||
A Gator named Joe at depth 4 which is hungry.
|
||||
o Bird, which is NOT abstract
|
||||
- Some Birds are dangerous, others are not. Make a private instance
|
||||
variable "dangerous" which is a boolean. Implement isDangerous
|
||||
to return this value. There should be no modifier for this
|
||||
variable.
|
||||
- Birds have a "species" which is a String. This variable should
|
||||
have an accessor, but no modifier.
|
||||
- Birds have an "altitude", which is a private int. Make the variable,
|
||||
accessor, and modifier for altitude.
|
||||
- Birds have a constructor
|
||||
public Bird(String name, String species,
|
||||
boolean dangerous, int alt)
|
||||
which calls the super constructor and set the variables
|
||||
appropriately.
|
||||
- Birds have an eat method which checks if the parameter is
|
||||
"bird seed", and if so the bird eats it and becomes not hungry.
|
||||
- Birds have a toString() method that returns the following:
|
||||
A <species> named <name> at altitude <alt> that is [dangerous/not dangerous]
|
||||
and is [hungry/not hungry].
|
||||
For example:
|
||||
A wren named Chirpy at altitude 50 that is dangerous and hungry.
|
||||
- Birds have a constructor
|
||||
public Bird(String name, String species,boolean dangerous)
|
||||
which chains to the other constructor with an altitude of 0.
|
||||
- Birds have a .equals method which behaves as follows:
|
||||
* if the parameter passed in is not a Bird, return false
|
||||
* call the super class's .equals method- if it returns
|
||||
false, then return false.
|
||||
* if the species or altitudes are not the same, return false.
|
||||
* otherwise, return true.
|
||||
|
||||
At this point, you should have written 4 classes- Animal, Fish, Gator,
|
||||
and Bird. Since Animal and Fish are abstract, you cannot make instances
|
||||
of them directly, however, you should test Gator and Bird in main methods
|
||||
inside those classes prior to submitting for instant feedback.
|
||||
When you think that your code is ready for feedback, submit it as p2-phase1
|
||||
on WebWork and get feedback.
|
||||
|
||||
Reminder: Don't forget about "Add as Zip". Since this program will have
|
||||
several files to submit, you will want to use this feature. Zip
|
||||
your java files up, then select the zip file- choose "Add as Zip" instead
|
||||
of add. WebWork will upload the zip file, then unzip it. When it redisplays
|
||||
the page, it will list all of the java files that were inside the zip.
|
||||
Since you do not have to choose each file separately- this will save
|
||||
you time!
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Phase 2: More animals!
|
||||
This phase should take you no longer than 3 hours, 30 minutes to complete.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
At this point, you should have made your Animal, Fish, Gator, and Bird classes
|
||||
working correctly from getting instant feedback on them- this should also mean
|
||||
that you are comfortable with
|
||||
- inheritance
|
||||
- abstract classes
|
||||
- .equals methods
|
||||
- toString methods
|
||||
|
||||
Now you are going to write some more Animal types on your own.
|
||||
For each type, you will write the eat() method similarly to what
|
||||
was done before. You will simply be given a list of foods that the
|
||||
Animal will eat.
|
||||
|
||||
o Bat is subclass of Animal
|
||||
- Bats eat "insects"
|
||||
- Bats are not dangerous [implement isDangerous() accordingly].
|
||||
- Bats have an altitude, which is an int, with appropriate accessor and
|
||||
modifier methods.
|
||||
- A Bat is equal to another Object iff (if and only if) the other object
|
||||
is a Bat at the same altitude, and the super class's .equals returns
|
||||
true.
|
||||
- Bats have a toString() that returns the following:
|
||||
A Bat named <name> at altitude <alt> that is [hungry/not hungry].
|
||||
- Bats have a constructor
|
||||
public Bat(String name)
|
||||
which chains to the super constructor with their name.
|
||||
o Shark is a subclass of Fish
|
||||
- Sharks eat "human", "chicken", and "fish".
|
||||
- Sharks have a constructor that takes a name and an initial depth.
|
||||
They should use super() to call the parent constructor with both values.
|
||||
- Sharks are dangerous [implement isDangerous() accordingly].
|
||||
- A Shark is equal to another Object iff the other Object is a Shark,
|
||||
and the superclass .equals returns true.
|
||||
- Sharks have a toString() that returns the following:
|
||||
A Shark named <name> at depth <depth> that is [hungry/not hungry].
|
||||
o GoldFish is a subclass of Fish
|
||||
- GoldFish eat "fish food"
|
||||
- GoldFish have a constructor that takes a name and an initial depth.
|
||||
They should use super() to call the parent constructor with both values.
|
||||
- GoldFish are not dangerous [implement isDangerous() accordingly].
|
||||
- A GoldFish is equal to another Object iff the other Object is a GoldFish,
|
||||
and the superclass .equals returns true.
|
||||
- GoldFish have a toString() that returns the following:
|
||||
A GoldFish named <name> at depth <depth> that is [hungry/not hungry].
|
||||
o Duck is a subclass of Bird
|
||||
- Ducks use their inherited behavior to eat.
|
||||
- Ducks can also swim, so they have a depth below the water- the depth
|
||||
variable should be an int, with appropriate accessor and modifier
|
||||
[see next item].
|
||||
- Ducks have a boolean to indicate whether they are swimming or flying.
|
||||
This boolean should be called "flying" and should have an accessor
|
||||
ONLY [called isFlying]. The modifier for setAltitude from
|
||||
the parent class should be overridden to
|
||||
- set flying to true
|
||||
- call the method in the parent class
|
||||
The modifier for depth should
|
||||
- set the depth appropriately
|
||||
- set flying to false
|
||||
- A Duck is equal to another Object iff the other Object is a Duck,
|
||||
the two Ducks are at the same depth, both are flying or both are
|
||||
swimming, and the super class's .equals returns true.
|
||||
- Ducks have a toString that depends on whether the duck is
|
||||
flying or swimming.
|
||||
o If the Duck is flying, the toString should simply call the super
|
||||
class's toString and return the value it returns.
|
||||
o If the Duck is swimming, the toString should return the following:
|
||||
A swimming duck named <name> at depth <depth> that is [hungry/not hungry].
|
||||
- Ducks have a constructor
|
||||
public Duck(String name)
|
||||
which chains to the super constructor and passes in the name, the
|
||||
species "Duck", false for dangerous. After calling the super
|
||||
constructor, setDepth should be used to set the depth to 0.
|
||||
|
||||
At this point, you should have the following concrete [not abstract] classes
|
||||
which should each have test mains that completely test them:
|
||||
- Shark
|
||||
- GoldFish
|
||||
- Gator
|
||||
- Bird
|
||||
- Duck
|
||||
- Bat
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Phase 3: Things that fly and things that swim- Interfaces
|
||||
This phase should take you no longer than 20 minutes to complete
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Did you notice that many of the Animals had method that were similar?
|
||||
Go back and look at which Animals can fly [have an altitude] and which Animals
|
||||
can swim [have a depth].
|
||||
|
||||
Flying Animals: Birds (includes Ducks) and Bats
|
||||
Swimming Animals: Fish (includes Sharks & GoldFish) Gators, and Ducks
|
||||
|
||||
Wouldn't it be nice to have a type for things that can fly and a type
|
||||
for things that can swim? They all have common behaviors, so it seems
|
||||
logical. One idea would be to make some parent class- maybe have
|
||||
FlyingAnimal and SwimmingAnimal as abstract subclasses of Animal....
|
||||
But what about the Ducks? We can only inherit from one class, and
|
||||
Ducks can both swim and fly! So we need interfaces. Interfaces are
|
||||
useful in situations where you want to have a type for "all things that
|
||||
can _________"- in this case, "all things that can fly" and "all things
|
||||
that can swim". Note that a class is an actual type of object. An interface
|
||||
is a type also, but it tends to be more for a category of types that
|
||||
can do similar things. So now we will create two interfaces:
|
||||
|
||||
o The FlyingType interface should have two methods in it:
|
||||
public void setAltitude(int alt);
|
||||
public int getAltitude();
|
||||
o The SwimmingType interface should have two methods in it:
|
||||
public void setDepth(int depth);
|
||||
public int getDepth();
|
||||
|
||||
Now, go back to Bird and Bat- declare them as implementing FlyingType.
|
||||
Then, go back to Fish, Gator, and Duck- declare each of them as implementing
|
||||
SwimmingType.
|
||||
|
||||
Note that their subclasses automatically implement the interfaces since their
|
||||
parent classes do.
|
||||
|
||||
By this time, you should have the following java files made:
|
||||
- Animal.java
|
||||
- Fish.java
|
||||
- Shark.java
|
||||
- GoldFish.java
|
||||
- Gator.java
|
||||
- Bird.java
|
||||
- Duck.java
|
||||
- Bat.java
|
||||
- FlyingType.java
|
||||
- SwimmingType.java
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Phase 4: Making a LinkedList *THIS IS AN INSTANT FEEDBACK PHASE*
|
||||
This phase should not take you longer than 4 hours
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Now, we are going to move to something slightly different. We have
|
||||
spent the past three phases creating an inheritance structure for animals.
|
||||
Now we are going to make a LinkedList that we can use to hold things in
|
||||
a dynamic data structure. LinkedLists in java are slightly different
|
||||
from those in scheme, but they have some striking similarities.
|
||||
|
||||
- First, you will need to create a class LLNode.
|
||||
o This class should have two instance variables:
|
||||
private Object data;
|
||||
private LLNode next;
|
||||
Each of these should have an appropriate accessor and modifier.
|
||||
o There should be three constructors
|
||||
public LLNode() //both next and data are initialized to null
|
||||
public LLNode(Object o) //next is initialized to null, data to o
|
||||
public LLNode(Object o, LLNode next) //both fields initialized from the
|
||||
//params
|
||||
o There should be a toString() method which simply returns the
|
||||
data's toString()
|
||||
For those of you who took scheme, this should sound quite similar
|
||||
to a cons cell. Cons cells had "first" and "rest", LLNodes have
|
||||
"data" and "next".
|
||||
- Now, create a class LinkedList.
|
||||
o This class should have two instance variables
|
||||
private LLNode head;
|
||||
private LLNode tail;
|
||||
head should always reference the front (first item) of the list
|
||||
tail should always reference the back (last item) of the list
|
||||
o There should be a default constructor which initializes both
|
||||
variables to null.
|
||||
o There should be a method
|
||||
public void addToFront(Object o)
|
||||
which puts o into a new LLNode and adds it to the front of the list.
|
||||
When this method returns, head should reference the LLNode containing o.
|
||||
Tail should reference this node iff it is the only node in the list.
|
||||
o There should be a method
|
||||
public void addToBack(Object o)
|
||||
which puts o into a new LLNode and adds it to the end of the list.
|
||||
When the method returns, tail should reference the LLNode containing o.
|
||||
Head should reference this node iff it is the only node in the list.
|
||||
o There should be a method
|
||||
public Object getFirst()
|
||||
which returns [without destroying] the first item in the list.
|
||||
This should return the data, not the node.
|
||||
o There should be a method
|
||||
public Object getLast()
|
||||
which returns [without destroying] the last item in the list.
|
||||
This should return the data, not the node.
|
||||
o There should be a method
|
||||
public Object removeFromFront()
|
||||
which removes and returns the first item in the list.
|
||||
This should return the data, not the node.
|
||||
o There should be a method
|
||||
public Object removeFromBack()
|
||||
which removes and returns the last item in the list.
|
||||
This should return the data, not the node.
|
||||
Note that for both removes methods, if the list becomes empty, both
|
||||
head and tail should be set to null.
|
||||
o The method
|
||||
public int size()
|
||||
which returns a count of the number of items in the list.
|
||||
o The method
|
||||
public Object[] asArray()
|
||||
which returns an array containing all of the items in the list in order.
|
||||
Element 0 of the array should have the data from the head of the list.
|
||||
o The .equals method should return true iff the lists are both the
|
||||
same length and the corresponding elements all are equal to each other
|
||||
according to their .equals methods. NOTE: THIS SHOULD NOT HAVE ANY
|
||||
SIDE EFFECTS ON THE LIST. Be VERY CAREFUL NOT TO DESTROY YOUR LIST.
|
||||
Also: Note that using Strings to compare the lists is NOT a valid
|
||||
approach. The LinkedList containing the String "1" and the LinkedList
|
||||
containing the Integer 1 are completely different, but have the
|
||||
same String representations.
|
||||
o The toString method should return a String that looks like
|
||||
[ first elem, second elem, third elem ]
|
||||
(where "first elem" "second elem" and "third elem" are the
|
||||
toString()s of the first, second, and third items in a three item
|
||||
list respectively..)
|
||||
There should be a "[", followed by the toString() of each data item
|
||||
in order, separated by commas, and ending with a "]".
|
||||
o The method
|
||||
public boolean contains(Object o)
|
||||
which returns true iff there is an Object in the list that .equals(o).
|
||||
Test your LinkedList and LLNode classes, then submit LLNode.java and
|
||||
LinkedList.java for instant feedback as p2-Phase4.
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Phase 5: Aviaries and Aquariums.
|
||||
This phase should not take longer than 1.5 hours
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Now we are going to make Aviaries (places where flying things live) to hold
|
||||
our flying creatures, and Aquariums to hold our swimming creatures.
|
||||
In our zoo, Aviaries have a net which can be closed across the aviary at 75
|
||||
feet up. It is safe to enter an aviary (to feed animals etc) iff the net
|
||||
is closed, and all dangerous animals are above it. It is safe to enter
|
||||
an Aquarium iff all dangerous animals are below 30 feet deep.
|
||||
Note that you may assume that only FlyingType Animals live in Aviaries,
|
||||
and only SwimmingType Animals live in Aquariums.
|
||||
|
||||
- Make an abstract class ZooBuilding which has
|
||||
- public abstract boolean isSafeToEnter()
|
||||
- a private LinkedList called "theAnimals"
|
||||
- an accessor for theAnimals (no modifier)
|
||||
- a private String for name
|
||||
- an accessor for name (no modifier)
|
||||
- a constructor which takes in a name, sets the name variable to
|
||||
that name and initializes theAnimals to a new LinkedList.
|
||||
- public int getHungryAnimals()
|
||||
which returns a count of how many hungry animals are in the Zoo.
|
||||
- public void feedAnimals(String food)
|
||||
this method first checks if it is safe to enter the building.
|
||||
if it is not safe, this method should print
|
||||
"The building is unsafe to enter!" and then return.
|
||||
if it is safe, it should call make all Animals eat() the food
|
||||
passed in (some of them will not like it, but that is their
|
||||
choice)
|
||||
|
||||
- Make a class Aviary which is a subclass of ZooBuilding and has
|
||||
- an instance variable (with accessors and modifiers) called netClosed
|
||||
which is a boolean. The accessor should be called getNetClosed
|
||||
and the modifier should be called setNetClosed.
|
||||
- the method
|
||||
public boolean isSafeToEnter()
|
||||
which returns true if the building is safe to enter, false if
|
||||
not- according to the criteria above.
|
||||
- a constructor which takes the name and chains to the super constructor
|
||||
- a toString() method which returns
|
||||
An Aviary named <name>.
|
||||
- Make a class Aquarium which is a subclass of ZooBuilding and has
|
||||
- the method
|
||||
public boolean isSafeToEnter()
|
||||
which returns true if the building is safe to enter, false if
|
||||
not- according to the criteria above.
|
||||
- a constructor which takes the name and chains to the super constructor
|
||||
- a toString() method which returns
|
||||
An Aquarium named <name>.
|
||||
|
||||
|
||||
Be sure to test your Aviary and Aquarium completely before proceeding to
|
||||
phase 6.
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
Phase 6:
|
||||
This phase should not take longer than 1 hour
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
In this phase, you will be making a menu to control your program.
|
||||
Since we have not covered IO yet, most of this code is given to you.
|
||||
|
||||
-> Open and look over Zoo.java <-
|
||||
|
||||
Zoo.java contains a mostly complete implementation of the zoo menu.
|
||||
You only have to do a few things.
|
||||
Look for the places that say
|
||||
|
||||
//***YOUR CODE GOES HERE*** (some number)
|
||||
|
||||
These are where you will be adding code to this file. The directions
|
||||
below are numbered to match the numbers in those comments.
|
||||
|
||||
1. Use IOHelper.readLine() to read a line of input from the user.
|
||||
Store that line of input into the "line variable".
|
||||
2. Print out the message "That is not a valid choice, try again"
|
||||
when the user's selection is not valid.
|
||||
3. Replace the line
|
||||
int depth=0;
|
||||
with the appropriate line of code to get the depth from the current
|
||||
SwimmingType animal.
|
||||
|
||||
4. Add a line of code to set the depth of the current SwimmingType
|
||||
animal to be depth.
|
||||
5. Add the Animal that the user created (toAdd) to the selected building
|
||||
(zb).
|
||||
6. Prompt the user to select a building, then list all of the animals
|
||||
in the building by printing the toString() of its LinkedList of
|
||||
animals. When prompting for a building, find and use the method
|
||||
in Zoo specifically designed for that.
|
||||
7. Use the food (food)that the user picked to feed the animals in the selected
|
||||
building (zb).
|
||||
8. Print the menu (find the appropriate constant).
|
||||
9. Fill in (and javadoc!) the method doMoveNet() so that it
|
||||
- prompts for a building number
|
||||
- checks if the building is an Aviary
|
||||
- if the building is not an Aviary, print
|
||||
"That building is not an Aviary, it has no net"
|
||||
- otherwise toggle (change true to false, false to true) the
|
||||
value of the netClosed on that aviary.
|
||||
10. Add a case to the menu switch case statement to handle the choice
|
||||
for moving the net. Be sure to use the appropriate constant.
|
||||
------------------------------------------------------------------------------
|
||||
DON'T FORGET TO SUBMIT AS P2 SO YOUR ASSIGNMENT WILL BE GRADED.
|
||||
|
||||
ALSO- DON'T FORGET TO PRACTICE "SAFE SUBMIT"
|
||||
Files to turnin for p2 [make a zip file and use "Add as Zip", since there are
|
||||
17 files to submit- it will be much faster for you].
|
||||
|
||||
Animal.java
|
||||
Fish.java
|
||||
Shark.java
|
||||
GoldFish.java
|
||||
Gator.java
|
||||
Bird.java
|
||||
Bat.java
|
||||
Duck.java
|
||||
FlyingType.java
|
||||
SwimmingType.java
|
||||
LLNode.java
|
||||
LinkedList.java
|
||||
ZooBuilding.java
|
||||
Aquarium.java
|
||||
Aviary.java
|
||||
Zoo.java
|
||||
IOHelper.java
|
||||
[any other files needed to make your submission compile and run]
|
||||
|
||||
|
||||
BIN
CS1322/p2/p2.zip
Normal file
BIN
CS1322/p2/p2.zip
Normal file
Binary file not shown.
214
CS1322/p3/FileChanger.java
Normal file
214
CS1322/p3/FileChanger.java
Normal file
@@ -0,0 +1,214 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* FileChanger.java
|
||||
*
|
||||
* Revisions: 1.0 Oct. 05, 2002
|
||||
* Created the FileChanger class
|
||||
* 1.1 Oct. 06, 2002
|
||||
* Compiled, Commented
|
||||
* 2.0 Oct. 06, 2002
|
||||
* Deleted all, start from scratch, clean up code
|
||||
* 2.1 Oct. 06, 2002
|
||||
* Completed classes
|
||||
* 2.2 Oct. 06, 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 2.2, Oct. 06, 2002
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class FileChanger {
|
||||
|
||||
//variables
|
||||
private String strFileName;
|
||||
|
||||
//readers and writers
|
||||
FileInputStream fis;
|
||||
ObjectInputStream ois;
|
||||
|
||||
FileOutputStream fos;
|
||||
ObjectOutputStream oos;
|
||||
|
||||
public static final boolean bDebug = false; //there's nothing to debug
|
||||
//that bDebug can handle
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for FileChanger
|
||||
*@param strFileName the name to be given to the output file
|
||||
*/
|
||||
public FileChanger(String strFileName){
|
||||
this.strFileName = strFileName;
|
||||
}
|
||||
|
||||
////////////////////////////
|
||||
//Initializers/Destructors//
|
||||
////////////////////////////
|
||||
|
||||
/**
|
||||
*Initialize the reader streams
|
||||
*@return true if successful, false if file not found
|
||||
*/
|
||||
public boolean initReader(){
|
||||
|
||||
//init file input stream
|
||||
try{
|
||||
fis = new FileInputStream(strFileName);
|
||||
}
|
||||
catch(FileNotFoundException fnfe){
|
||||
return false;
|
||||
}
|
||||
|
||||
//init obj input stream
|
||||
try{
|
||||
ois = new ObjectInputStream(fis);
|
||||
}
|
||||
catch(IOException ioe){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
*Initialize the write streams
|
||||
*/
|
||||
public void initWriter(){
|
||||
|
||||
//init file output stream
|
||||
try{
|
||||
fos = new FileOutputStream(strFileName);
|
||||
}
|
||||
catch(FileNotFoundException fnfe){
|
||||
System.err.println("File not Found! - initWriter()");
|
||||
}
|
||||
|
||||
//init obj output stream
|
||||
try{
|
||||
oos = new ObjectOutputStream(fos);
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException - initWriter()");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*Close read streams
|
||||
*/
|
||||
public void closeRead(){
|
||||
|
||||
try{
|
||||
fis.close();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException - closeRead()");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*Close write streams
|
||||
*/
|
||||
public void closeWrite(){
|
||||
|
||||
try{
|
||||
oos.flush();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException - flush objstream - closeWrite()");
|
||||
}
|
||||
|
||||
try{
|
||||
fos.close();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException - close filestream - closeWrite()");
|
||||
}
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Retrieve the next object in the file
|
||||
*/
|
||||
public Object nextObject(){
|
||||
try{
|
||||
return ois.readObject();
|
||||
}
|
||||
catch(EOFException eof){
|
||||
return null;
|
||||
}
|
||||
catch(ClassNotFoundException cnfe){
|
||||
System.err.println("Class not found - nextObject()");
|
||||
return null;
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IO Exception - nextObject()");
|
||||
return null;
|
||||
}
|
||||
catch(NullPointerException npe){
|
||||
System.err.println("Null Pointer Exception - nextObject()");
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*Write an Object to the file
|
||||
*@param toWrite, the object to be written
|
||||
*/
|
||||
public void writeObject(Object toWrite){
|
||||
try{
|
||||
oos.writeObject(toWrite);
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IO Exception - writeObject()");
|
||||
}
|
||||
}
|
||||
|
||||
/**********************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class FileChanger.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
FileChanger fg = new FileChanger("This stinks.txt");
|
||||
|
||||
fg.initReader();
|
||||
fg.initWriter();
|
||||
|
||||
fg.writeObject(new Integer(7));
|
||||
fg.writeObject(new Integer(6));
|
||||
fg.writeObject(new String("Bob is a person too"));
|
||||
fg.writeObject(new Boolean(false));
|
||||
|
||||
System.out.println(fg.nextObject());
|
||||
System.out.println(fg.nextObject());
|
||||
System.out.println(fg.nextObject());
|
||||
System.out.println(fg.nextObject());
|
||||
System.out.println(fg.nextObject());
|
||||
|
||||
fg.closeRead();
|
||||
fg.closeWrite();
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class FileChanger
|
||||
150
CS1322/p3/HashNode.java
Normal file
150
CS1322/p3/HashNode.java
Normal file
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* HashNode.java
|
||||
*
|
||||
* Revisions: 1.0 Oct. 06, 2002
|
||||
* Created the HashNode class
|
||||
* 1.1 oCT. 06, 2002
|
||||
* Compiled, Tested, 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. 06, 2002
|
||||
*/
|
||||
|
||||
public class HashNode {
|
||||
|
||||
private HashNode next;
|
||||
private Object key ;
|
||||
private Object data;
|
||||
|
||||
public static final boolean bDebug = false; //there's nothing to debug
|
||||
//that bDebug can handle
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for HashNode
|
||||
*@param key, the object to be stored as the key
|
||||
*@param data, the object to be stored as data
|
||||
*/
|
||||
public HashNode(Object key, Object data){
|
||||
this.key = key ;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Modifier for next
|
||||
*@param next, the next HashNode
|
||||
*/
|
||||
public void setNext(HashNode next){
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for next
|
||||
*@return the next HashNode
|
||||
*/
|
||||
public HashNode getNext(){
|
||||
return next;
|
||||
}
|
||||
|
||||
/**
|
||||
*Modifier for key
|
||||
*@param key, the new value for key
|
||||
*/
|
||||
public void setKey(Object key){
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for key
|
||||
*@return the value of key
|
||||
*/
|
||||
public Object getKey(){
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
*Modifier for data
|
||||
*@param data, the new value for data
|
||||
*/
|
||||
public void setData(Object data){
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for data
|
||||
*@return the value of data
|
||||
*/
|
||||
public Object getData(){
|
||||
return data;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Equals method for HashNode
|
||||
*@return true if both HashNodes have the same key
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
if(!(obj instanceof HashNode)){
|
||||
return false;
|
||||
}
|
||||
|
||||
HashNode hTemp = (HashNode)obj;
|
||||
if(hTemp.getData().equals(this.data)){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return (data.toString());
|
||||
}
|
||||
|
||||
/*************************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class HashNode.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
HashNode dooby = new HashNode("key","data");
|
||||
HashNode stinky = new HashNode("key","data");
|
||||
|
||||
System.out.println(dooby.equals(stinky));
|
||||
|
||||
stinky.setKey(new Integer(6));
|
||||
|
||||
stinky.setData("key");
|
||||
|
||||
stinky.setNext(dooby);
|
||||
|
||||
System.out.println(dooby.equals(stinky));
|
||||
System.out.println(stinky.getKey());
|
||||
System.out.println(stinky.getData());
|
||||
System.out.println(stinky.getNext().getKey());
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class HashNode
|
||||
337
CS1322/p3/HashTable.java
Normal file
337
CS1322/p3/HashTable.java
Normal file
@@ -0,0 +1,337 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* HashTable.java
|
||||
*
|
||||
* Revisions: 1.0 Oct. 06, 2002
|
||||
* Created the HashTable class
|
||||
* 1.1 Oct. 10, 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.0, Oct. 06, 2002
|
||||
*/
|
||||
|
||||
typedef struct HashNode
|
||||
{
|
||||
HashNode* next;
|
||||
int key;
|
||||
int data;
|
||||
};
|
||||
|
||||
HashNode[] hTable;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
public HashTable(int size)
|
||||
{
|
||||
hTable = new HashNode[size];
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*add data to a given location
|
||||
*@param data added to location key
|
||||
*@param key, the location to store data
|
||||
*/
|
||||
public void add(Object key, Object data)
|
||||
{
|
||||
int iKey = Math.abs(key.hashCode()) % hTable.length;
|
||||
|
||||
if(hTable[iKey] == null)
|
||||
{
|
||||
hTable[iKey] = new HashNode(key,data);
|
||||
}
|
||||
else
|
||||
{
|
||||
HashNode temp = new HashNode(key,data);
|
||||
temp.setNext(hTable[iKey]);
|
||||
hTable[iKey] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*@return the data located
|
||||
*@param key
|
||||
*/
|
||||
public Object get(Object key)
|
||||
{
|
||||
int iKey = Math.abs(key.hashCode()) % hTable.length;
|
||||
|
||||
if(hTable[iKey] == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
HashNode current = hTable[iKey];
|
||||
while(current != null)
|
||||
{
|
||||
if(current.getKey().equals(key))
|
||||
{
|
||||
return current.getData();
|
||||
}
|
||||
current = current.getNext();
|
||||
}
|
||||
return null;
|
||||
}//end get(@key)
|
||||
|
||||
/**
|
||||
*Remove hashNode at key and return data
|
||||
*@param key, the node to be removed
|
||||
*/
|
||||
public Object remove(Object key)
|
||||
{
|
||||
int iKey = Math.abs(key.hashCode()) % hTable.length;
|
||||
|
||||
if(hTable[iKey] == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
HashNode current = hTable[iKey];
|
||||
if(current.getKey().equals(key))
|
||||
{
|
||||
hTable[iKey] = current.getNext();
|
||||
return current.getData();
|
||||
}
|
||||
|
||||
else
|
||||
{ //if(the list is long and annoying)
|
||||
//set predefs
|
||||
HashNode hPrev = current; //a little longer and its hPrevInstance
|
||||
current = current.getNext();
|
||||
|
||||
while(current != null)
|
||||
{
|
||||
if(current.getKey().equals(key))
|
||||
{
|
||||
hPrev.setNext(current.getNext());
|
||||
return current.getData();
|
||||
}
|
||||
current = current.getNext();
|
||||
hPrev = hPrev.getNext();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}//end remove(@key)
|
||||
|
||||
/**
|
||||
*@return enumeration of of HashTable
|
||||
*/
|
||||
public Enumeration elements()
|
||||
{
|
||||
Vector vTemp = new Vector();
|
||||
HashNode hCurr;
|
||||
|
||||
for(int i=0; i<hTable.length; i++)
|
||||
{
|
||||
hCurr = hTable[i];
|
||||
while(hCurr != null)
|
||||
{
|
||||
vTemp.add(hCurr.getData());
|
||||
hCurr = hCurr.getNext();
|
||||
}
|
||||
}
|
||||
return vTemp.elements();
|
||||
}
|
||||
|
||||
/**
|
||||
*Rehash Hashtable to iSize
|
||||
*@param iSize, size to be resized to
|
||||
*/
|
||||
public void rehash(int iSize)
|
||||
{
|
||||
//by the way, I like the fact we're using Hungarian notation now;
|
||||
//Microsoft is more useful than the LUG@GATECH would like to think :)
|
||||
//did I already say this in Zoo.java?
|
||||
|
||||
//don't waste CPU cycles
|
||||
if(hTable.length == 0)
|
||||
{
|
||||
hTable = new HashNode[iSize];
|
||||
return;
|
||||
}
|
||||
|
||||
HashNode[] hTemp = new HashNode[hTable.length];
|
||||
|
||||
for(int i=0; i<hTable.length; i++)
|
||||
{
|
||||
hTemp[i] = hTable[i];
|
||||
}
|
||||
|
||||
//define cars needed
|
||||
hTable = new HashNode[iSize];
|
||||
|
||||
HashNode hCurr;
|
||||
|
||||
for(int i=0; i<hTemp.length; i++)
|
||||
{
|
||||
hCurr = hTemp[i];
|
||||
while(hCurr != null)
|
||||
{
|
||||
add(hCurr.getKey(),hCurr.getData());
|
||||
hCurr = hCurr.getNext();
|
||||
}
|
||||
}
|
||||
}//end rehash(int)
|
||||
|
||||
/**
|
||||
*@return size of the hashtable
|
||||
*/
|
||||
public int size()
|
||||
{
|
||||
return hTable.length;
|
||||
}
|
||||
|
||||
/**
|
||||
*@return String version of Hashtable
|
||||
*/
|
||||
public String toString()
|
||||
{
|
||||
String reallyLong = "";
|
||||
HashNode hCurr;
|
||||
|
||||
for(int i=0; i<hTable.length; i++)
|
||||
{
|
||||
hCurr = hTable[i];
|
||||
while(hCurr != null)
|
||||
{
|
||||
reallyLong += hCurr.getData().toString() + "\n";
|
||||
hCurr = hCurr.getNext();
|
||||
}
|
||||
|
||||
}
|
||||
return reallyLong;
|
||||
}
|
||||
|
||||
/**
|
||||
*Adds x elements to the HashTable
|
||||
*!!!Meant for Debugging only!!!
|
||||
*/
|
||||
public void addRandom(int iHowMany)
|
||||
{
|
||||
|
||||
//you know this was fairly creative
|
||||
//I believe this deserves extra credit
|
||||
//or at least send me money
|
||||
for(int i=0; i<iHowMany; i++)
|
||||
{
|
||||
|
||||
//this will force fast systems like mine to have
|
||||
//a different value near every iteration
|
||||
//hell, handling a 3D matrix in Java even caused some delay
|
||||
//on my dual mp2100+, what an inefficient VM sun makes :(
|
||||
//I was going to multithread this, one thread adding 2 starting at
|
||||
//zero, the other adding 2 starting at 1, but then I got lazy...
|
||||
int[][][] hehehe = new int[100][100][100];
|
||||
for(int x=0; x<100; x++)
|
||||
{
|
||||
for(int y=0; y<100; y++)
|
||||
{
|
||||
for(int z=0; z<100; z++)
|
||||
{
|
||||
hehehe[x][y][z] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
hehehe = new int[1][1][1];
|
||||
//end
|
||||
|
||||
switch((int)(System.currentTimeMillis() % 10))
|
||||
{
|
||||
case 0:
|
||||
add(new Integer((int)(System.currentTimeMillis()%10)),
|
||||
new Integer(6));
|
||||
break;
|
||||
case 1:
|
||||
add(new Integer((int)(System.currentTimeMillis()%10)),
|
||||
new Mail("HAHA","HA","Joe"));
|
||||
break;
|
||||
case 2:
|
||||
add(new String("Fido"),new Integer(2));
|
||||
break;
|
||||
case 3:
|
||||
add(new String("Alfonso"),new Integer(45));
|
||||
break;
|
||||
case 4:
|
||||
add(new Double(3.14),new Float(3.14));
|
||||
break;
|
||||
case 5:
|
||||
add(new Integer(43),new Float(6));
|
||||
break;
|
||||
case 6:
|
||||
add(new String("This"),new String("is"));
|
||||
break;
|
||||
case 7:
|
||||
add(new String("getting"),new String("way"));
|
||||
break;
|
||||
case 8:
|
||||
add(new String("too"),new String("long"));
|
||||
break;
|
||||
case 9:
|
||||
add(new String("ha"),new String("laziness"));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}//end addRandom(int)
|
||||
|
||||
/****************************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class HashTable.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args)
|
||||
{
|
||||
HashTable hashey = new HashTable(5);
|
||||
|
||||
//test add
|
||||
hashey.addRandom(15);
|
||||
System.out.println(hashey);
|
||||
|
||||
hashey = new HashTable(5);
|
||||
hashey.addRandom(12);
|
||||
|
||||
System.out.println("Testing elements");
|
||||
Enumeration e = hashey.elements();
|
||||
while(e.hasMoreElements())
|
||||
{
|
||||
System.out.println(e.nextElement());
|
||||
}
|
||||
|
||||
|
||||
|
||||
//hehehehehehhehehe, lets test this sucker for REAL
|
||||
//don't try this at home :)
|
||||
//approx. runtime on a Tiger MPX dual MP2100+ 1.5GB RAM
|
||||
//thats 100% CPU utilaztion at 1.733GHz IPC3
|
||||
//around 60 seconds (im guessing)
|
||||
//:)
|
||||
hashey = new HashTable(200);
|
||||
hashey.addRandom(2000);
|
||||
|
||||
System.out.println(hashey);
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class HashTable
|
||||
134
CS1322/p3/IOHelp.txt
Normal file
134
CS1322/p3/IOHelp.txt
Normal file
@@ -0,0 +1,134 @@
|
||||
This is a walkthrough of how to use File I/O. It will give a walkthrough of
|
||||
how to read from standard in, and how to read from and write to files.
|
||||
|
||||
|
||||
WRITING AND READING TO FILES
|
||||
============================================================================
|
||||
|
||||
/O stands for "input and output." File I/O is reading from or writing to
|
||||
files on the disk. It should be noted that java treats network
|
||||
sockets like files as far as IO goes, like UNIX does. Although networking
|
||||
is not covered in this class, it is quite similar to accessing files on
|
||||
the disk. The standard input, standard error, and standard output are
|
||||
also very similar to files, and in fact the types for these three streams
|
||||
are found in java.io, along with the classes to perform other file operations.
|
||||
|
||||
There are many slight variations in what needs to be done to perform
|
||||
File IO depending on what type of file is being dealt with and what needs
|
||||
to be done with it. We will first focus our attention on reading lines from
|
||||
a text file. The first thing that we should do is open the file in a
|
||||
FileReader (Note that we are creating a FileReader object):
|
||||
|
||||
FileReader fr= new FileReader("myFile.txt");
|
||||
|
||||
The next thing that we should do is to wrap the FileReader in a
|
||||
BufferedReader:
|
||||
|
||||
BufferedReader br= new BufferedReader(fr);
|
||||
|
||||
This code creates a BufferedReader which will ask fr what is in the file
|
||||
and return it to the program line by line.
|
||||
|
||||
The designers of Java decided to use this wrapper class concept to allow a
|
||||
lot of flexibility without having to create hundreds of different classes.
|
||||
Sometimes people will accomplish the above in one statement where perhaps
|
||||
the "wrapper" idea will be more obvious:
|
||||
|
||||
BufferedReader has, among other methods, the method
|
||||
|
||||
String readLine()
|
||||
|
||||
which returns the next line of text from the file without the '\n' on the
|
||||
end- this method also automatically goes to the next line, so the next
|
||||
call to readLine() will give the next line, not the same one. When
|
||||
the program is done reading from the file, it should call the close method
|
||||
on the BufferedReader (which will close the underlying FileReader) so
|
||||
that the associated file resources may be freed (note that while
|
||||
memory is garbage collected, other resources, such as file descriptors
|
||||
are not). The above code, must of course have the exceptions dealt with
|
||||
properly, but that will be discussed in a minute.
|
||||
|
||||
The other major operation that we will concern ourselves with is writing
|
||||
lines of text to a file. This can be accomplished similarly via the
|
||||
use of the FileWriter and PrintWriter classes:
|
||||
|
||||
FileWriter fr=new FileWriter("output.txt");
|
||||
PrintWriter pr= new PrintWriter(fr);
|
||||
|
||||
The PrintWriter class has print() and println() methods which are overloaded
|
||||
to accept just about any paramter type and write it to the underlying
|
||||
FileWriter. It should be noted that print and println do NOT throw
|
||||
exceptions if a problem occurs while writing to the underlying file-
|
||||
instead, they just set an error variable in the PrintWriter instance,
|
||||
which may be checked with the checkError() method. The FileWriter class,
|
||||
may throw an exception when created, as described later. The PrintWriter
|
||||
must also be closed when the program is finished with it. If an output
|
||||
stream in not closed, then not only will the resources not be freed,
|
||||
but also, the data will never actually be written out to the disk,
|
||||
as it is buffered in memory, and flushed only when a certain amount
|
||||
is present, or the close() or flush() method gets called.
|
||||
|
||||
So why do we need to deal with these extra classes? Why can't we
|
||||
just have one class to do it all? And how do these classes interact?
|
||||
|
||||
Each class does a specific job and provides a certain ammount of modularity
|
||||
and abstraction. Lets look at the FileReader and BufferedReader:
|
||||
|
||||
FileReader- has the job of reading characters from a file and
|
||||
assumes that the characters use default encoding.
|
||||
|
||||
BufferedReader- has the job of reading text from a character-input stream,
|
||||
buffering characters so as to provide for the efficient
|
||||
reading of characters, arrays, and lines.
|
||||
|
||||
there are other classes that could be put into a BufferedReader, for example,
|
||||
an InputStreamReader- which basically reads from a byte stream and converts
|
||||
to a character stream, using some encoding scheme, which may be specified.
|
||||
So for example, if the file that you wanted to read used a different Unicode
|
||||
encoding scheme than the default for your system (maybe you are writing
|
||||
some international buisness software), then you would instead need to
|
||||
make a FileInputStream, wrap that in an InputStreamReader, then wrap that
|
||||
in a BufferedReader. In general, "Stream" deals with bytes, whereas
|
||||
"Reader" deals with characters.
|
||||
|
||||
This abstraction gives the input classes more flexibility, in that
|
||||
they can also be used to allow for reading from other input streams
|
||||
than those avaiable from files on the disk- a network connection
|
||||
provides an instance of InputStream to read incoming data from
|
||||
the connection- InputStreams (as just mentioned) deal with bytes-
|
||||
if the program wants to read lines of text, for example, the same
|
||||
wrapping can be performed with the same classes to get a BufferedReader
|
||||
to give lines of text at a time from the network
|
||||
|
||||
|
||||
|
||||
WRITING AND READING OBJECTS TO FILES
|
||||
============================================================================
|
||||
Writing and reading java objects to a file are slightly different than writing
|
||||
and reading Strings to file. First off, in order for a java Object to be able
|
||||
to be written to a file, it must implement Serializable. Only classes which
|
||||
implement the Serializable interface can be written to a file as an Object.
|
||||
|
||||
To write an object to a file, you first need to create the Streams.
|
||||
The reason that you cannot use PrintWriter as you used before is because
|
||||
PrintWriter only can write Strings to a file.
|
||||
The two streams that need to be created are
|
||||
FileOutputStream and ObjectOutputStream.
|
||||
|
||||
|
||||
The constructor to FileOutputStream takes in the name of the file to write to as
|
||||
a String. If the file does not exist, it creates one.
|
||||
The constructor to ObjectOutputStream takes in the newly created
|
||||
FileOutputStream.
|
||||
After creating them, you can write the object to the ObjectOutputStream.
|
||||
If the ObjectOutputStream was called oos, then oos.writeObject(theObject);
|
||||
would write the object to file.
|
||||
After writing all object, make sure to flush the ObjectOutputStream and close
|
||||
the FileOutputStream.
|
||||
|
||||
To read objects from a file, you need to create an ObjectInputStream and a
|
||||
FileInputStream. The constructors for these work similar to the writer.
|
||||
To actually read an object, do ois.readObject(). This will need to be casted
|
||||
to the actuall object that it is. If the file does not exist, it will throw
|
||||
a FileNotFoundException. When the end of the file is reached, it throws
|
||||
an EndOfFileException.
|
||||
138
CS1322/p3/Mail.java
Normal file
138
CS1322/p3/Mail.java
Normal file
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Mail.java
|
||||
*
|
||||
* Revisions: 1.0 Oct. 01, 2002
|
||||
* Created the Mail class
|
||||
* 1.1 Oct. 05, 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, Oct. 01, 2002
|
||||
*/
|
||||
|
||||
public class Mail implements P3Type{
|
||||
|
||||
public static final boolean bDebug = false; //there's nothing to debug
|
||||
//that bDebug can handle
|
||||
|
||||
private String strFrom;
|
||||
private String strTo;
|
||||
private String strMessage;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for mail
|
||||
*@param strMessage, the message value
|
||||
*@param strFrom, who sent the message
|
||||
*@param strTo, who the message will be sent to
|
||||
*/
|
||||
public Mail(String strMessage, String strFrom, String strTo){
|
||||
//I prefer to use the this. references for the constructors
|
||||
//rather than referencing the accessor/modifiers
|
||||
//either way is correct according to lecture, its just
|
||||
//a matter of just how encapsulated it should be
|
||||
this.strMessage = strMessage;
|
||||
this.strFrom = strFrom;
|
||||
this.strTo = strTo;
|
||||
}//end Mail(message,from,to)
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Modifier for strFrom
|
||||
*@param strFrom the value to set this.strFrom to
|
||||
*/
|
||||
public void setStrFrom(String strFrom){
|
||||
this.strFrom = strFrom;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for strFrom
|
||||
*@return value of strFrom
|
||||
*/
|
||||
public String getStrFrom(){
|
||||
return strFrom;
|
||||
}
|
||||
|
||||
/**
|
||||
*Modifier for strTo
|
||||
*@param strTo, the value to set for this.strTo
|
||||
*/
|
||||
public void setStrTo(String strTo){
|
||||
this.strTo = strTo;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for strTo
|
||||
*@return the value of strTo
|
||||
*/
|
||||
public String getStrTo(){
|
||||
return strTo;
|
||||
}
|
||||
|
||||
/**
|
||||
*Modifier for strMessage
|
||||
*@param strMessage the value to set this.strMessage to
|
||||
*/
|
||||
public void setStrMessage(String strMessage){
|
||||
this.strMessage = strMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for strMessage
|
||||
*@return the value of strMessage
|
||||
*/
|
||||
public String getStrMessage(){
|
||||
return strMessage;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
public String toString(){
|
||||
return ("To: " + strTo + "\nFrom: " + strFrom +
|
||||
"\nMessage: " + strMessage);
|
||||
}
|
||||
|
||||
/******************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class Mail.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Mail box = new Mail("Screw You","Bob","Rob");
|
||||
System.out.println(box);
|
||||
|
||||
box.setStrMessage("I love you");
|
||||
box.setStrFrom("Rob");
|
||||
box.setStrTo("Bob");
|
||||
|
||||
System.out.println(box);
|
||||
|
||||
System.out.println(box.getStrTo());
|
||||
System.out.println(box.getStrFrom());
|
||||
System.out.println(box.getStrMessage());
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Mail
|
||||
191
CS1322/p3/MailBox.java
Normal file
191
CS1322/p3/MailBox.java
Normal file
@@ -0,0 +1,191 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* MailBox.java
|
||||
*
|
||||
* Revisions: 1.0 Oct. 01, 2002
|
||||
* Created the MailBox class
|
||||
* 1.1 Oct. 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, Oct. 05, 2002
|
||||
*/
|
||||
|
||||
import java.util.Vector;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class MailBox implements P3Type{
|
||||
|
||||
public static final boolean bDebug = false; //there's nothing to debug
|
||||
//that bDebug can handle
|
||||
|
||||
private String strLName;
|
||||
private String strFName;
|
||||
private Vector vMail;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Constructor for MailBox
|
||||
*@param last the value for strLName
|
||||
*@param first the value for strFName
|
||||
*/
|
||||
public MailBox(String last, String first){
|
||||
strLName = last;
|
||||
strFName = first;
|
||||
vMail = new Vector();
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
//Accessors/Modifiers//
|
||||
///////////////////////
|
||||
|
||||
/**
|
||||
*Modifier for strLName
|
||||
*@param strLName the value to set this.strLName
|
||||
*/
|
||||
public void setStrLName(String strLName){
|
||||
this.strLName = strLName;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for strLName
|
||||
*@return the value of strLName
|
||||
*/
|
||||
public String getStrLName(){
|
||||
return strLName;
|
||||
}
|
||||
|
||||
/**
|
||||
*Modifier for strFName
|
||||
*@param strFName the value to set this.strFName
|
||||
*/
|
||||
public void setStrFName(String strFName){
|
||||
this.strFName = strFName;
|
||||
}
|
||||
|
||||
/**
|
||||
*Accessor for strFName
|
||||
*@return the value of strFName
|
||||
*/
|
||||
public String getStrFName(){
|
||||
return strFName;
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Adds mail to the persons box
|
||||
*@param mail, the mail to be added
|
||||
*/
|
||||
public void addMail(Mail mail){
|
||||
vMail.add(mail);
|
||||
}
|
||||
|
||||
/**
|
||||
*clear vector and return enumeration of all mail
|
||||
*@return an enumeration of the mail a person has
|
||||
*/
|
||||
public Enumeration getMail(){
|
||||
Vector vTemp = (Vector)vMail.clone();
|
||||
Enumeration enum = vTemp.elements();
|
||||
vMail.clear();
|
||||
return enum;
|
||||
}
|
||||
|
||||
/**
|
||||
*toString() override
|
||||
*@return the amount of mail the person has as a string
|
||||
*/
|
||||
public String toString(){
|
||||
return (getStrFName() + " " + getStrLName() + " has " + vMail.size() +
|
||||
" piece" + (vMail.size()==1?"":"s") +
|
||||
/*I used ==1 because when its 0 it should be pieces not piece*/
|
||||
" of mail.");
|
||||
}
|
||||
|
||||
/**
|
||||
*@return true if both Mailboxes are owned by the same person
|
||||
*/
|
||||
public boolean equals(Object obj){
|
||||
if(!(obj instanceof MailBox)){
|
||||
return false;
|
||||
}
|
||||
|
||||
MailBox temp = (MailBox)obj;
|
||||
|
||||
if(!(this.getStrFName().equals(temp.getStrFName()))){
|
||||
return false;
|
||||
}
|
||||
if(!(this.getStrLName().equals(temp.getStrLName()))){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*******************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class MailBox.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
MailBox box1 = new MailBox("Romero" , "John");
|
||||
MailBox box2 = new MailBox("Carmack" , "John");
|
||||
MailBox box3 = new MailBox("Sweeney" , "Tim" );
|
||||
|
||||
//just for reference
|
||||
//Mail(String strMessage, String strFrom, String strTo)
|
||||
//
|
||||
|
||||
Mail mail1 = new Mail("Don't Screw up Doom3","Gamers","John Carmack");
|
||||
Mail mail2 = new Mail("Man you screwed up Daikatana","The World","Romero");
|
||||
Mail spammail;
|
||||
spammail=new Mail("Buy out product!!!!!","Crappy Company","MailBox owner");
|
||||
|
||||
box1.addMail(mail2);
|
||||
box2.addMail(mail1);
|
||||
box3.addMail(spammail);
|
||||
|
||||
System.out.println(box1);
|
||||
|
||||
box1 = new MailBox("Carmack","John");
|
||||
System.out.println(box1.equals(box2));
|
||||
|
||||
box2.addMail(spammail);
|
||||
box2.addMail(new Mail("This sucks","Me","you"));
|
||||
|
||||
System.out.println(box2);
|
||||
|
||||
Enumeration e = box2.getMail();
|
||||
|
||||
System.out.println("\nStart Elements\n");
|
||||
|
||||
while(e.hasMoreElements()){
|
||||
System.out.println(e.nextElement());
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
|
||||
System.out.println(box2.equals(box3));
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class MailBox
|
||||
368
CS1322/p3/Manager.java
Normal file
368
CS1322/p3/Manager.java
Normal file
@@ -0,0 +1,368 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* Manager.java
|
||||
*
|
||||
* Revisions: 1.0 Oct. 11, 2002
|
||||
* Created the Manager class
|
||||
* 1.1 Oct. 16, 2002
|
||||
* Compiled, Commented, Finished
|
||||
* 1.2 Oct. 17, 2002
|
||||
* Corrected Last Name only being shown on to and from for mail
|
||||
* 1.3 Oct. 17, 2002
|
||||
* Corrected infinite loop if letter entered in CcustomerHere()
|
||||
* 2.0 Oct. 17, 2002
|
||||
* Added some comments to make what I did clearer as well
|
||||
* as made certain aspects of the code "better"
|
||||
* 2.1 Oct. 18, 2002
|
||||
* Fixed a Comment in readText()
|
||||
*
|
||||
* </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 2.1, Oct. 18, 2002
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class Manager implements P3Type{
|
||||
|
||||
public static final boolean bDebug = false;
|
||||
|
||||
//declaration of globals
|
||||
private String strCustFName ;
|
||||
private String strCustLName ;
|
||||
private PostOffice objPostOffice = new PostOffice();
|
||||
|
||||
private BufferedReader KB_IN;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
/*
|
||||
This constructor would have been used if readFile() wasn't run on every
|
||||
run of managerActions(). Having the constructor read the file and then
|
||||
waiting to write it after the program is done would mean any mailbox
|
||||
deleted from the PostOffice would not be removed from the file and in
|
||||
case of system failure, when the RAM is flushed the deleted mailboxes would
|
||||
never actually be deleted.
|
||||
|
||||
public Manager(){
|
||||
readFile();
|
||||
}
|
||||
*/
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Initialize the Keyboard input
|
||||
*/
|
||||
private void initInput(){
|
||||
KB_IN = new BufferedReader(new InputStreamReader(System.in));
|
||||
}
|
||||
|
||||
/**
|
||||
*Read contents of MyPO
|
||||
*/
|
||||
private void readFile(){
|
||||
FileChanger fc = new FileChanger(P3Type.POST_OFFICE);
|
||||
boolean truth = fc.initReader();
|
||||
Object temp;
|
||||
|
||||
if(truth != false){
|
||||
do{
|
||||
temp = fc.nextObject();
|
||||
if(temp != null){
|
||||
objPostOffice.issue((MailBox)temp);
|
||||
}
|
||||
}while(temp != null);
|
||||
fc.closeRead();
|
||||
}
|
||||
|
||||
}//end readFile()
|
||||
|
||||
/**
|
||||
*Read Input
|
||||
*/
|
||||
private Object[] readText(String KB_INPUT,int KB_INPUT_PARSE){
|
||||
boolean trust = true;
|
||||
|
||||
//do..while() for purposes of keeping the code running until\
|
||||
//an acceptable value is entered
|
||||
do{
|
||||
try{
|
||||
KB_INPUT = KB_IN.readLine();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException in readText()");
|
||||
}
|
||||
|
||||
try{
|
||||
KB_INPUT_PARSE = Integer.parseInt(KB_INPUT);
|
||||
trust = true;
|
||||
}
|
||||
catch(NumberFormatException nfe){
|
||||
System.err.println("Please enter a number next time");
|
||||
trust = false;
|
||||
}
|
||||
}while(trust != true);
|
||||
|
||||
//set-up return array
|
||||
Object[] toReturn = new Object[2];
|
||||
toReturn[0] = KB_INPUT;
|
||||
toReturn[1] = new Integer(KB_INPUT_PARSE);
|
||||
return toReturn;
|
||||
}//end readText(String,int)
|
||||
|
||||
/**
|
||||
*Perform actions as the manager
|
||||
*@return false if done
|
||||
*/
|
||||
public boolean managerActions(){
|
||||
|
||||
//A Note: if the readFile() function/code is run everytime
|
||||
//managerActions() is run, then the file must be rewritten everytime
|
||||
//a mailbox is deleted. This is good and bad, good for performance
|
||||
//bad in case of a crash
|
||||
//I opted for the lesser performance but better reliability
|
||||
//we don't want the mailboxes people deleted reappearing in case of
|
||||
//system failure do we?
|
||||
readFile();
|
||||
|
||||
System.out.println("\n"+P3Type.INIT_ACTIONS);
|
||||
|
||||
//let's keep this archane Hungarian Style Notation, shall we?
|
||||
String KB_INPUT = "";
|
||||
int KB_INPUT_PARSE = 0;
|
||||
initInput(); //I also moved this for cleanliness
|
||||
|
||||
//In order to lessen the clutter of this code, I created a separate
|
||||
//function to handle the text input
|
||||
Object[] temp = readText(KB_INPUT,KB_INPUT_PARSE);
|
||||
KB_INPUT = (String)temp[0];
|
||||
Integer tucow = (Integer)temp[1];
|
||||
KB_INPUT_PARSE = tucow.intValue();
|
||||
|
||||
|
||||
switch(KB_INPUT_PARSE){
|
||||
case 1:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
boolean trueness = true;
|
||||
do{
|
||||
System.out.println("Please enter the Customer's name");
|
||||
|
||||
try{
|
||||
KB_INPUT = KB_IN.readLine();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException in managerAction()"+
|
||||
" when trying to readLine().");
|
||||
}
|
||||
|
||||
trueness = customerHere(KB_INPUT);
|
||||
}while(trueness);
|
||||
break;
|
||||
//end case 1
|
||||
case 2:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
System.out.print("Adding Rooms: ");
|
||||
objPostOffice.resize();
|
||||
System.out.println("Done.");
|
||||
break;
|
||||
//end case 2
|
||||
case 3:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
System.out.println("The current boxes are: ");
|
||||
Enumeration e = objPostOffice.allBoxes();
|
||||
if(e != null){
|
||||
while(e.hasMoreElements()){
|
||||
System.out.println(e.nextElement());
|
||||
}
|
||||
}
|
||||
break;
|
||||
//end case 3
|
||||
case 0:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
objPostOffice.leavingForTheDay();
|
||||
return false;
|
||||
//end case 0
|
||||
|
||||
default:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
System.out.println("You didn't input 0-3 did you?");
|
||||
return managerActions(); //restart
|
||||
//end default
|
||||
}
|
||||
return true;
|
||||
}//end managerActions()
|
||||
|
||||
/**
|
||||
*Customer Actions
|
||||
*@return false when done
|
||||
*/
|
||||
public boolean customerHere(String strName){
|
||||
for(;;){
|
||||
System.out.println("\n"+P3Type.CUST_HERE);
|
||||
|
||||
initInput();
|
||||
|
||||
String KB_INPUT = "";
|
||||
int KB_INPUT_PARSE = 0;
|
||||
|
||||
//In order to lessen the clutter of this code, I created a separate
|
||||
//function to handle the text input
|
||||
Object[] temp = readText(KB_INPUT,KB_INPUT_PARSE);
|
||||
KB_INPUT = (String)temp[0];
|
||||
Integer tucow = (Integer)temp[1];
|
||||
KB_INPUT_PARSE = tucow.intValue();
|
||||
|
||||
/*get names*/
|
||||
StringTokenizer st = new StringTokenizer(strName);
|
||||
strCustFName = st.nextToken();
|
||||
|
||||
while(st.hasMoreTokens()){
|
||||
strCustLName = st.nextToken();
|
||||
}
|
||||
/////////////
|
||||
|
||||
switch(KB_INPUT_PARSE){
|
||||
case 1:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
MailBox objNewBox = new MailBox(strCustLName,strCustFName);
|
||||
objPostOffice.issue(objNewBox);
|
||||
objPostOffice.leavingForTheDay(); //rewrites mailbox list
|
||||
break;
|
||||
//end case 1
|
||||
case 2:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
int iValue = objPostOffice.remove(strCustLName);
|
||||
if(iValue == -3){
|
||||
System.out.println(P3Type.NO_MAILBOX);
|
||||
}
|
||||
else{ //if(iValue >= 0)
|
||||
System.out.println(P3Type.NUM_TELEGRAMS + iValue);
|
||||
objPostOffice.leavingForTheDay(); //rewrites mailbox list
|
||||
}
|
||||
break;
|
||||
//end case 2
|
||||
case 3:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
String LName = "";
|
||||
String FName = "";
|
||||
String message ="";
|
||||
int iVal;
|
||||
|
||||
System.out.println("Who do you want to send the message to?");
|
||||
try{
|
||||
KB_INPUT = KB_IN.readLine();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException READLine() in case3"+
|
||||
"@ customerHere()");
|
||||
}
|
||||
|
||||
st = new StringTokenizer(KB_INPUT);
|
||||
FName = st.nextToken();
|
||||
while(st.hasMoreTokens()){
|
||||
LName = st.nextToken();
|
||||
}
|
||||
|
||||
System.out.println("What is the message?");
|
||||
|
||||
try{
|
||||
message = KB_IN.readLine();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("Error in case3 readLine() message" +
|
||||
"@ customerHere()");
|
||||
}
|
||||
|
||||
Mail objNewMail = new Mail(message,strCustFName+
|
||||
" "+strCustLName,FName+" "+LName);
|
||||
iVal = objPostOffice.send(objNewMail);
|
||||
|
||||
if(iVal == P3Type.BAD_SENDER){
|
||||
System.out.println("You need to open a mailbox " +
|
||||
"with us first.");
|
||||
}
|
||||
else if(iVal == P3Type.NO_RECIPIENT){
|
||||
System.out.println("The person you are trying " +
|
||||
"to send to doesn't have a " +
|
||||
"mailbox here.");
|
||||
}
|
||||
else {//if(message.equals(P3Type.MAIL_SENT))
|
||||
System.out.println("Mail sent successfully, please allow" +
|
||||
" 6 to 8 weeks for delivery.");
|
||||
}
|
||||
break;
|
||||
//end case 3
|
||||
case 4:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
System.out.println(objPostOffice.retrieve(strCustLName));
|
||||
break;
|
||||
case 0:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
return false;
|
||||
default:
|
||||
if(bDebug == true){
|
||||
System.out.println("Case " + KB_INPUT_PARSE);
|
||||
}
|
||||
|
||||
System.out.println("Didn't enter 0-4, try again");
|
||||
return customerHere(strName); //restart
|
||||
}//end switch
|
||||
}//end for(;;)
|
||||
}//end customerHere(String)
|
||||
|
||||
/**
|
||||
* Debugging main for class Manager.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Manager Bob = new Manager();
|
||||
Bob.managerActions();
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class Manager
|
||||
BIN
CS1322/p3/MyPO
Normal file
BIN
CS1322/p3/MyPO
Normal file
Binary file not shown.
47
CS1322/p3/P3.java
Normal file
47
CS1322/p3/P3.java
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* P3.java
|
||||
*
|
||||
* Revisions: 1.0 Oct. 16, 2002
|
||||
* Created the P3 class
|
||||
* 1.1 Oct. 16, 2002
|
||||
* Compiled, Finished, Done
|
||||
*
|
||||
* </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. 16, 2002
|
||||
*/
|
||||
|
||||
public class P3 {
|
||||
|
||||
|
||||
/**
|
||||
* Debugging main for class P3.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
Manager manager = new Manager();
|
||||
boolean truthitude = true;
|
||||
|
||||
do{
|
||||
truthitude = manager.managerActions();
|
||||
}while(truthitude != false);
|
||||
|
||||
System.out.println(P3Type.GOOD_BYE);
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class P3
|
||||
69
CS1322/p3/P3Type.java
Normal file
69
CS1322/p3/P3Type.java
Normal file
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* P3Type.java
|
||||
* This interface will be used to hold constants for P3.
|
||||
* It extends Serializable so that classes which implement this interface
|
||||
* can be saved to a file
|
||||
*
|
||||
* Created: Fri Aug 02 11:45:48 2002
|
||||
*
|
||||
* @author <a href="mailto:bsbeck@cc.gatech.edu "> Brandon Beck </a>
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public interface P3Type extends Serializable
|
||||
{
|
||||
|
||||
// This variable holds the default size of the hash table.
|
||||
public static final int DEFAULT_SIZE = 10;
|
||||
|
||||
// This variable holds a constant String saying that a person has never
|
||||
// been issued a mailbox before
|
||||
public static final String NO_MAILBOX = "The current customer "
|
||||
+ "has never been issued a mailbox before!";
|
||||
|
||||
// This variable represents that the person trying to send the mail
|
||||
// does not have a mailbox
|
||||
public static final int BAD_SENDER = -1;
|
||||
|
||||
// This variable represents that the person the mail is sent to
|
||||
// does not have a mailbox
|
||||
public static final int NO_RECIPIENT = -2;
|
||||
|
||||
// This variable represents that the mail was sent successfully
|
||||
public static final int MAIL_SENT = 1;
|
||||
|
||||
// This variable holds a constant int representing that a person has not
|
||||
// been issued a mailbox before
|
||||
public static final int NO_MAILBOX_NUM = -3;
|
||||
|
||||
// This variable holds a String telling the customer how many of his
|
||||
// telegrams got thrown away when he closed his mailbox
|
||||
public static final String NUM_TELEGRAMS = "The number of unread " +
|
||||
"telegrams when you closed your mailbox was ";
|
||||
|
||||
// This will be the String constant of the initial menu. These are the
|
||||
// initial actions that a manager of the PostOffice can perform.
|
||||
public static final String INIT_ACTIONS =
|
||||
"1) Help Customer \n" +
|
||||
"2) Add More Rooms \n" +
|
||||
"3) View All MailBoxes \n" +
|
||||
"0) Leave for the Day \n";
|
||||
|
||||
// This will be the String constant of the customer menu. These are the
|
||||
// actions of a customer
|
||||
public static final String CUST_HERE =
|
||||
"1) I want a mailbox at this post office. \n" +
|
||||
"2) I no longer want a post office at this mailbox. \n" +
|
||||
"3) I want to send a telegram. \n" +
|
||||
"4) I want to read all of the telegrams I have received. \n" +
|
||||
"0) Okay, I am done. \n";
|
||||
|
||||
// This is a string representing the file to save the Post Office to
|
||||
public static final String POST_OFFICE = "MyPO";
|
||||
|
||||
// This is a string saying goodbye to the manager
|
||||
public static final String GOOD_BYE = "See you tomorrow";
|
||||
|
||||
} // end P3Type
|
||||
237
CS1322/p3/PostOffice.java
Normal file
237
CS1322/p3/PostOffice.java
Normal file
@@ -0,0 +1,237 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* PostOffice.java
|
||||
*
|
||||
* Revisions: 1.0 Oct. 11, 2002
|
||||
* Created the PostOffice class
|
||||
* 1.1 Oct. 11, 2002
|
||||
* Compiled, Finished, Commented
|
||||
* 2.0 Oct. 16, 2002
|
||||
* Added Phase6 methods
|
||||
*
|
||||
* </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 2.0, Oct. 16, 2002
|
||||
*/
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class PostOffice implements P3Type{
|
||||
|
||||
public static final boolean bDebug = false; //nothing to debug via var tho
|
||||
|
||||
HashTable htExpress = new HashTable(10);
|
||||
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Issue a mailbox with key as last name
|
||||
*@param mbToAdd, the mailbox to be added
|
||||
*/
|
||||
public void issue(MailBox mbToAdd){
|
||||
|
||||
if(htExpress.get(mbToAdd.getStrLName()) == null){ //avoid duplicate
|
||||
htExpress.add(mbToAdd.getStrLName(),mbToAdd);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*Return the mail in a given persons box
|
||||
*@param strLName, the last name of the person
|
||||
*/
|
||||
public String retrieve(String strLName){
|
||||
|
||||
if(htExpress.get(strLName) == null){
|
||||
return P3Type.NO_MAILBOX;
|
||||
}
|
||||
|
||||
MailBox mb = (MailBox)htExpress.get(strLName);
|
||||
Enumeration e = mb.getMail();
|
||||
String strLong = "";
|
||||
int count = 1; //for flair
|
||||
|
||||
while(e.hasMoreElements()){
|
||||
strLong += "\nMessage " + count + ":\n" +
|
||||
e.nextElement().toString() + "\n";
|
||||
count++;
|
||||
}
|
||||
return strLong;
|
||||
}
|
||||
|
||||
/**
|
||||
*Send mail
|
||||
*@param toSend, the message to be sent
|
||||
*/
|
||||
public int send(Mail toSend){
|
||||
String strFromLName = "";
|
||||
String strToLName = "";
|
||||
|
||||
StringTokenizer st;
|
||||
|
||||
//verify sender
|
||||
st = new StringTokenizer(toSend.getStrFrom());
|
||||
|
||||
while(st.hasMoreTokens()){
|
||||
strFromLName = st.nextToken();
|
||||
}
|
||||
|
||||
if(htExpress.get(strFromLName) == null){
|
||||
return P3Type.BAD_SENDER;
|
||||
}
|
||||
|
||||
//verify recipient
|
||||
st = new StringTokenizer(toSend.getStrTo());
|
||||
|
||||
while(st.hasMoreTokens()){
|
||||
strToLName = st.nextToken();
|
||||
}
|
||||
|
||||
if(htExpress.get(strToLName) == null){
|
||||
return P3Type.NO_RECIPIENT;
|
||||
}
|
||||
|
||||
//add the mail
|
||||
MailBox mb = (MailBox)htExpress.get(strToLName);
|
||||
mb.addMail(toSend);
|
||||
|
||||
return P3Type.MAIL_SENT;
|
||||
}//end send(Mail)
|
||||
|
||||
/**
|
||||
*@param strLName, to be removed from PostOffice
|
||||
*@return the number of unread messages in the box
|
||||
*/
|
||||
public int remove(String strLName){
|
||||
|
||||
if(htExpress.get(strLName) == null){
|
||||
return P3Type.NO_MAILBOX_NUM;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
MailBox mTemp = (MailBox)htExpress.remove(strLName);
|
||||
Enumeration e = mTemp.getMail();
|
||||
|
||||
//only because it looks cooler AND takes less typing
|
||||
for(;e.hasMoreElements();count++,e.nextElement());
|
||||
|
||||
return count;
|
||||
}//end remove(String)
|
||||
|
||||
/**
|
||||
*@return enumeration containing the mailboxes
|
||||
*/
|
||||
public Enumeration allBoxes(){
|
||||
//this is a tough one
|
||||
//took 4 of the 5 hours of p3phase5
|
||||
return htExpress.elements();
|
||||
}
|
||||
|
||||
/**
|
||||
*Resize the HashTable
|
||||
*Doubles current size
|
||||
*/
|
||||
public void resize(){
|
||||
htExpress.rehash(htExpress.size() * 2);
|
||||
}
|
||||
|
||||
/* */
|
||||
/*Phase 6*/
|
||||
/* */
|
||||
|
||||
/**
|
||||
*Write mailboxes to file and close program
|
||||
*/
|
||||
public void leavingForTheDay(){
|
||||
FileChanger fc = new FileChanger(P3Type.POST_OFFICE);
|
||||
fc.initWriter();
|
||||
|
||||
Enumeration e = htExpress.elements();
|
||||
while(e.hasMoreElements()){
|
||||
fc.writeObject(e.nextElement());
|
||||
}
|
||||
fc.closeWrite();
|
||||
}
|
||||
|
||||
/**********************************************************/
|
||||
|
||||
/**
|
||||
* Debugging main for class PostOffice.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
//System.testclass.for.me
|
||||
//If Java is so smart, why doesn't it have that!
|
||||
//by the way, how's that commenting style I just thought of /*stuff*/
|
||||
|
||||
|
||||
/*Begin Test Class*/
|
||||
PostOffice po = new PostOffice();
|
||||
|
||||
//create test dummie...er...beta testers
|
||||
|
||||
//game gods
|
||||
po.issue(new MailBox("Romero","John")); //hmm, two John's maybe
|
||||
po.issue(new MailBox("Carmack","John")); //I need a name change?
|
||||
po.issue(new MailBox("Sweeney","Tim"));
|
||||
po.issue(new MailBox("Miyamato","Shigeru"));
|
||||
|
||||
//musical greatness
|
||||
po.issue(new MailBox("Beethoven","Ludwik von"));//hehe test StringToken too
|
||||
po.issue(new MailBox("Stravinsky","Igor"));
|
||||
po.issue(new MailBox("Reznor","Trent"));
|
||||
po.issue(new MailBox("Bowie","David"));
|
||||
|
||||
//good anime characters
|
||||
po.issue(new MailBox("Starwind","Gene"));
|
||||
po.issue(new MailBox("Spiegel","Spike"));
|
||||
|
||||
//assorted
|
||||
po.issue(new MailBox("McGee","Mike")); //that's Mike McGee of the IN
|
||||
po.issue(new MailBox("Dole","Bob")); //viva la Pfiezer!
|
||||
|
||||
/*Test Exception*/
|
||||
po.issue(new MailBox("Romero","John"));
|
||||
|
||||
/*Test Retrieve*/
|
||||
po.send(new Mail("You're career has hit rock bottom","Carmack","Romero"));
|
||||
po.send(new Mail("I mean games for the PalmPC!?","Sweeney","Romero"));
|
||||
|
||||
/*Test Exception*/
|
||||
if(po.send(new Mail("BOOOOOO","Bob","Romero")) == -1){
|
||||
System.out.println("BAD_SENDER");
|
||||
}
|
||||
//despite not receiving the mail, a response!
|
||||
if(po.send(new Mail("FU!","Romero","Bob")) == -2){
|
||||
System.out.println("BAD_RECIPIENT");
|
||||
}
|
||||
|
||||
System.out.println(po.retrieve("Romero"));
|
||||
|
||||
/*Test Remove*/
|
||||
/*Test Exception*/
|
||||
System.out.println(po.remove("Romreo")); //oops :)
|
||||
|
||||
System.out.println(po.remove("Romero"));
|
||||
|
||||
/*Nothing to test for resize() and allBoxes(), tested in HashTable*/
|
||||
|
||||
/*End Test Class*/
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class PostOffice
|
||||
BIN
CS1322/p3/Test.txt
Normal file
BIN
CS1322/p3/Test.txt
Normal file
Binary file not shown.
BIN
CS1322/p3/This stinks.txt
Normal file
BIN
CS1322/p3/This stinks.txt
Normal file
Binary file not shown.
100
CS1322/p3/TricksandTools.txt
Normal file
100
CS1322/p3/TricksandTools.txt
Normal file
@@ -0,0 +1,100 @@
|
||||
This file contains a walkthrough on how to use important tools in Java. These
|
||||
include Vectors, Enumerations and StringTokenizers. All of these classes are
|
||||
located in the package java.util
|
||||
|
||||
VECTORS
|
||||
===============================================================================
|
||||
In order to use the Vector class, you need to import java.util.Vector.
|
||||
Look in the Java API to see a list of all the methods that can be used with
|
||||
Vectors. The great thing about Vectors is that they are dynamic like
|
||||
Linked Lists, but they give instantaneous access to elements like arrays.
|
||||
|
||||
So lets say we had a small java class that utilized Vectors. We want to add
|
||||
words when a person says them into the Vector - as long as the word is not in
|
||||
the Vector already. When they are done speaking, we want to print out all of
|
||||
the words that they said.
|
||||
|
||||
import java.util.Vector;
|
||||
|
||||
public class SaveWords
|
||||
{
|
||||
public SaveWords(){
|
||||
Vector v = new Vector();
|
||||
String word = "";
|
||||
while((word = WordISay()) != null) // as long as I say a word
|
||||
{
|
||||
if(!(v.contains(word))) // check to see if in Vector
|
||||
{
|
||||
// if not add it
|
||||
v.add(word)
|
||||
}
|
||||
} // end while
|
||||
|
||||
// loop through the vector (notice the size() method)
|
||||
for(int i = 0; i<v.size(); i++)
|
||||
{
|
||||
// use v.elementAt to get a specific element without
|
||||
// removing it.
|
||||
System.out.println(v.elementAt(i));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
This is a simple walkthrough. Other useful and important methods deal with
|
||||
removing an object and getting the index of an object. Look in the API for
|
||||
these methods. Check out the elements method as well. This method returns
|
||||
an Enumeration of all the elements in the Vector (see below).
|
||||
|
||||
|
||||
ENUMERATION
|
||||
============================================================================
|
||||
|
||||
The important methods for Enumeration are
|
||||
hasMoreElements and nextElement.
|
||||
|
||||
hasMoreElements returns true if there are more elements while nextElement will
|
||||
return the next Object of the Enumeration. Example of use...
|
||||
|
||||
Enumeration e.....
|
||||
|
||||
while(e.hasMoreElements()){
|
||||
|
||||
// do something with e.nextElement()
|
||||
// remember that this is an Object and may need to be casted into the
|
||||
// appropriate Object
|
||||
|
||||
} // end while
|
||||
|
||||
|
||||
STRING TOKENIZER
|
||||
=============================================================================
|
||||
|
||||
The StringTokenizer class is very similar to the Enumeration class with respect
|
||||
to the methods that are able to be called. The small difference is that the
|
||||
actual methods are called hasMoreTokens() and nextToken(). The nextToken method
|
||||
returns a String as opposed to an Object. The StringTokenizer class does
|
||||
exactly what it says...it tokenizes (breaks up) a string. The default delimiter
|
||||
is white space. Thus if a StringTokenizer was declared as
|
||||
StringTokenizer st = new StringTokenizer("How Are you doing?");
|
||||
|
||||
then the code
|
||||
while(st.hasMoreTokens()){
|
||||
System.out.println(st.nextToken());
|
||||
}
|
||||
|
||||
would produce the following output
|
||||
How
|
||||
Are
|
||||
you
|
||||
doing?
|
||||
|
||||
if instead the StringTokenizer was declared to delimit with respect to o's
|
||||
StringTokenizer st = new StringTokenizer("How Are you doing?", "o");
|
||||
|
||||
then the output would be...
|
||||
H
|
||||
w Are y
|
||||
u d
|
||||
ing?
|
||||
BIN
CS1322/p3/cs1322-P3.zip
Normal file
BIN
CS1322/p3/cs1322-P3.zip
Normal file
Binary file not shown.
524
CS1322/p3/p3.nfo.txt
Normal file
524
CS1322/p3/p3.nfo.txt
Normal file
@@ -0,0 +1,524 @@
|
||||
CS1322: Programming Assignment #3 - Fall 2002.
|
||||
Assigned:
|
||||
Phase 2 CheckOff: Monday, October 7th, 2002 at 23:59:59
|
||||
Phase 4 Checkoff: Wednesday, October 16th, 2002 at 23:59:59
|
||||
DUE Date: Wednesday, October 16th, 2002 at 23:59:59
|
||||
|
||||
|
||||
Program Title: Telegram for Bugs
|
||||
|
||||
|
||||
Files Provided
|
||||
====================================================================
|
||||
|
||||
o p3.nfo.txt - this file
|
||||
o P3Type.java - Interface needed for P3
|
||||
o IOHelp.txt
|
||||
o TricksandTools.txt
|
||||
|
||||
Learning Objectives
|
||||
====================================================================
|
||||
|
||||
o File I/O
|
||||
o Useful Java Tools
|
||||
o Hashtables.
|
||||
|
||||
|
||||
HASHTABLE OVERVIEW
|
||||
====================================================================
|
||||
A HashTable is a data structure which allows for the retrieval of
|
||||
the information associated with a specified key value. They are made from
|
||||
HashNodes. Thus you will add data to a HashNode and then add the HashNode
|
||||
to the HashTable. A HashTable
|
||||
first applies a hashing function to the key value to limit its range
|
||||
to a reasonably small number. That hash value is then used to index
|
||||
into an array to find the appropriate data. Since the hash function
|
||||
is not one-to-one, it is possible that multiple key values could
|
||||
hash to the same value- if this is the case, then a "collision"
|
||||
occurs, and it must be resolved. Hashtables are useful for finding the
|
||||
data quickly- the hashing operation occurs in O(1) time, and then,
|
||||
as long as the number of collisions is small, the data may be found
|
||||
with just a few operations- making for a very fast search. It should
|
||||
be noted and emphasized that HashTables are based on an array.
|
||||
A HashTable holds an array- what exactly that array holds depends
|
||||
on the collision resoulution strategy used in the HashTable.
|
||||
The size of the array is important- if the array is too small,
|
||||
there will be many collisions, and operations will not be efficient.
|
||||
If the array is too large, then there will be wasted space.
|
||||
|
||||
|
||||
What is collision resolution?
|
||||
|
||||
Collision resolution is the process by which entires in a hashtable
|
||||
that have the same hash value are handled. Typical methods include:
|
||||
|
||||
External chaining: The HashTable is an array of linked lists.
|
||||
When a value is added to the table, it is hashed, then added
|
||||
to the appropriate linked list. When a key is searched for,
|
||||
it is hashed, then the correct linked list is searched for that
|
||||
key and the associated data.
|
||||
|
||||
Open Addressing: When a value is added to the HashTable and the
|
||||
desired space is full, an algorithm is applied to continue searching
|
||||
through the array to find an empty space to put the element in.
|
||||
There are two common forms:
|
||||
|
||||
- linear probing: the array index which is being attempted
|
||||
is increased by a fixed ammount (i.e +1 each time) until
|
||||
an open space is found.
|
||||
|
||||
- quadratic probing: the array index which is being attempted
|
||||
is increased by an increasing ammount (i.e +1, then +2, then +3)
|
||||
until an open space is found.
|
||||
When searching for the data, the same probing function is applied
|
||||
until the data is found.
|
||||
|
||||
Coalesced Chaining: An extra area (cellar) is allocated at the end of
|
||||
the array (beyond the range of the hashing function), and when a
|
||||
collision occurs, the data is stored in the first open space in the
|
||||
cellar. When searching for the data, if it is not in the initial space,
|
||||
the cellar is searched.
|
||||
|
||||
Multiple Record Buckets: Have room for a fixed number of collisions
|
||||
to occur at each hash value (i.e. the HashTable is a 2D array).
|
||||
When searching for the data, look in each of the spaces at that
|
||||
hash value.
|
||||
|
||||
|
||||
PROGRAM OVERVIEW
|
||||
====================================================================
|
||||
You will be making a simple telegram/postoffice for this assigment.
|
||||
In order to accomplish this, you will make use of the data structre -
|
||||
HashTable. You will use external chaining as the collision resolution
|
||||
algorithm.
|
||||
|
||||
|
||||
PROGRAM WALKTHROUGH
|
||||
====================================================================
|
||||
|
||||
PHASE I - RECORD CLASSES (Mail.java and MailBox.java)
|
||||
|
||||
This phase should contain no more than 1 hour worth of coding. If you are
|
||||
spending significantly more time than this then please come see a TA. All
|
||||
the TA's are more than happy to help.
|
||||
====================================================================
|
||||
|
||||
Mail.java
|
||||
|
||||
Since a post office handles mail, it will be useful to make a class for
|
||||
mail. Make a Mail class that implements P3Type. This class should have
|
||||
instance variables to store such things as who the mail is from,
|
||||
who the mail is to, and what the message is.
|
||||
Thus, there should be three private variables, all of which are Strings.
|
||||
Call these variables:
|
||||
|
||||
strFrom;
|
||||
strTo;
|
||||
strMessage
|
||||
|
||||
Make accessors and modifiers for of these variables.
|
||||
Next, you need to make a constructor which takes in the message, who
|
||||
the message is from, and who the message is to. The constructor should
|
||||
set each variable appropriately.
|
||||
|
||||
public Mail(String message, String from, String to)
|
||||
|
||||
Lastly, make override the toString method that will return a string
|
||||
which looks like...
|
||||
To: Honey Bee <enter>
|
||||
From: Lovely Flower <enter>
|
||||
Message: I don't like you stealing my sweets!
|
||||
|
||||
|
||||
*********************************************************************
|
||||
MailBox.java
|
||||
|
||||
Make a MailBox class that also implements P3Type and make it hold the first
|
||||
and last name of the person who "owns" this mailbox. The mailbox will also
|
||||
contain all the mail that the person has received.
|
||||
Create three private instance variables:
|
||||
|
||||
String strLName;
|
||||
String strFName;
|
||||
Vector vMail;
|
||||
|
||||
For information on how to use Vectors and to find important methods in
|
||||
the Vector class, consult TricksandTools.txt
|
||||
Make sure you create accessors and modifiers for the person's last name
|
||||
and first name.
|
||||
Now create a constructor which will take in a person's last and first
|
||||
name. You should set these two variables appropriately and initialize
|
||||
the Vector.
|
||||
|
||||
public MailBox(String last, String first)
|
||||
|
||||
After completing that, you need to create a method for "placing" a piece
|
||||
of mail into the persons mailbox. Call this method addMail. It should
|
||||
take in Mail and add that mail to the vector of mail.
|
||||
|
||||
Now you are going to want to write a method that will return all the mail
|
||||
that a person has. After people take mail out of their mailbox, the mail
|
||||
is no longer there. When this method is called, not only should it return
|
||||
all mail that is in the mailbox, it should also contain no more mail
|
||||
(unless a person sends mail to them after they check their mailbox).
|
||||
This means the you should "reset" your mail Vector.
|
||||
This method - called getMail - will return an Enumeration containing all
|
||||
the Mail currently in the mailbox.
|
||||
Look in the TricksandTools.txt file for information about Enumeration as
|
||||
well as other useful tools in Java.
|
||||
|
||||
Create a toString method which will return a String which contains the
|
||||
name of the person who the mailbox is issued to as well as the number of
|
||||
pieces of mail the person has in their mailbox.
|
||||
an example of a String returned is...
|
||||
FName LName has X piece(s) of mail.
|
||||
so,
|
||||
Really Popular has 1 piece of mail.
|
||||
but,
|
||||
Not Me has 4 pieces of mail.
|
||||
|
||||
Lastly, create an equals method that will compare two mailboxes to see if
|
||||
they are equal. Two mailboxes are equal if they are "issued" to the same
|
||||
person (meaning the last name and first name of the two mailboxes are
|
||||
the same)
|
||||
|
||||
|
||||
END OF PHASE 1!!!
|
||||
====================================================================
|
||||
|
||||
PHASE 2. WORKING WITH FILE I/O (FileChanger.java)
|
||||
INSTANT FEEDBACK PHASE
|
||||
|
||||
This phase should contain no more than 2 hours worth of coding. If you are
|
||||
spending significantly more time than this then please come see a TA. All
|
||||
the TA's are more than happy to help.
|
||||
====================================================================
|
||||
FileChanger.java
|
||||
|
||||
This file will allow you to read input from a file and write information
|
||||
to a file.
|
||||
If you read IOhelp.txt, this phase will be alot easier.
|
||||
|
||||
Create the appropriate variables needed for file io.
|
||||
Also, create a private instance variable for the name of the file (strFileName)
|
||||
The constructor for this class should take in a String - the fileName
|
||||
The file name should be set appropriately.
|
||||
|
||||
Next create a method that will initialize all the buffers needed to read
|
||||
the Objects from the file. This method will return true if the file is found.
|
||||
It will return false if the file does not exist
|
||||
|
||||
public boolean initReader(){
|
||||
}
|
||||
|
||||
Create a method that will initialize all the variables needed to write to
|
||||
the file
|
||||
|
||||
public void initWriter(){
|
||||
}
|
||||
|
||||
Now create a method that will return the next Object from the file.
|
||||
If it encounters the end of the file, then null should be returned.
|
||||
|
||||
public Object nextObject(){
|
||||
}
|
||||
|
||||
Next you need to write a method that will write an Object to the file.
|
||||
|
||||
public void writeObject(Object toWrite){
|
||||
}
|
||||
|
||||
Write a method that will close all the buffers open to read the file
|
||||
|
||||
public void closeRead(){
|
||||
}
|
||||
|
||||
Write a method that will close all the buffers open to write to the file
|
||||
|
||||
public void closeWrite(){
|
||||
}
|
||||
|
||||
Write a main to test this class. Use your record classes written above to test
|
||||
this class. One way of doing this would be to create a bunch of Mail and save
|
||||
it to a file. Then read that file and print out all the Mail that is in there.
|
||||
|
||||
END OF PHASE 2!!!
|
||||
Submit this phase to webwork for an autograded checkoff.
|
||||
File(s) to submit: FileChanger.java
|
||||
====================================================================
|
||||
|
||||
PHASE 3. HASHNODE IMPLEMENTATION (HashNode.java)
|
||||
This phase should contain no more than 30 minutes of coding. If you are
|
||||
spending significantly more time than this then please come see a TA. All
|
||||
the TA's are more than happy to help.
|
||||
====================================================================
|
||||
A HashNode should contain Three private instance variables.
|
||||
One is a reference to another HashNode. The other two are Objects -
|
||||
one object is the key, and the other is the data.
|
||||
|
||||
HashNode next;
|
||||
Object key;
|
||||
Object data;
|
||||
|
||||
The constructor for this class should take in the key and the data;
|
||||
public HashNode(Object key, Object data);
|
||||
|
||||
Make an equals method which will check to see if two HashNodes are equal
|
||||
Two HashNodes are equal if they have the same key.
|
||||
|
||||
END OF PHASE 3!!!
|
||||
====================================================================
|
||||
|
||||
PHASE 4. IMPLEMENTING a HASHTABLE (HashTable.java)
|
||||
This phase should contain no more than 3 hours worth of coding. If you are
|
||||
spending significantly more time than this then please come see a TA. All
|
||||
the TA's are more than happy to help.
|
||||
====================================================================
|
||||
For this assignment you will be using external chaining as the
|
||||
hashtable's collision avoidance. Call the hashtable hTable
|
||||
|
||||
The constructor should take in an int that will be the size of the array. The
|
||||
constructor will initialize the array to the given size.
|
||||
|
||||
You will need to code the following methods
|
||||
add(Object key, Object data)
|
||||
get(Object key)
|
||||
remove(Object key)
|
||||
elements()
|
||||
rehash(int iSize)
|
||||
size()
|
||||
toString()
|
||||
|
||||
The add method will add the data at the appropriate location. The location
|
||||
to add the data is found by using the key passed in. Look up hashCode() in the
|
||||
Object class.
|
||||
**NOTE that the value returned from hashCode may be negative
|
||||
|
||||
The get method will return the data that is associated with the given key.
|
||||
It will return null if the data is not found.
|
||||
|
||||
The remove method will remove a HashNode from the Hashtable. The Node that
|
||||
will be removed will be the Node that contains the key. Make sure to return
|
||||
the data that is held in the HashNode deleted (the data not the key).
|
||||
If no HashNode is in the HashTable with the key, null is returned.
|
||||
|
||||
Elements should return an enumeration of all the data in the HashTable...they
|
||||
should be HashNodes
|
||||
|
||||
The rehash method should resize the HashTable to the given size and readd
|
||||
all the data to the appropriate location of the HashTable
|
||||
|
||||
size will return the size of the hashtable
|
||||
|
||||
toString will return a String representign all of the elements in the hashtable
|
||||
|
||||
Make sure to write a main to test this class
|
||||
|
||||
END OF PHASE 4!!!
|
||||
Submit this phase to webwork for an autograded checkoff.
|
||||
File(s) to submit: HashTable.java
|
||||
====================================================================
|
||||
|
||||
PHASE 5. MAKING the POSTOFFICE (Manager.java, PostOffice.java)
|
||||
This phase should contain no more than 5 hours worth of coding. If you are
|
||||
spending significantly more time than this then please come see a TA. All
|
||||
the TA's are more than happy to help.
|
||||
====================================================================
|
||||
Manager.java
|
||||
This class will represent the person running the post office. All this person
|
||||
is in charge of is performing operations given to him. The only functionality
|
||||
of the assistant at this point will be:
|
||||
Help Customer
|
||||
Add More Rooms
|
||||
View All MailBoxes
|
||||
and Leave for the Day
|
||||
|
||||
When a customer comes into the post office, then the manager will be able to
|
||||
Issue New Mailbox
|
||||
Remove a Mailbox
|
||||
Send a Telegram
|
||||
and Retreive All Telegrams
|
||||
|
||||
Now that we know the basic operations of th manager, let's make the PostOffice
|
||||
class so that the Manager can manage HIS PostOffice. We will come back to code
|
||||
this class after we do the post office class
|
||||
|
||||
*********************************************************************
|
||||
|
||||
PostOffice.java
|
||||
|
||||
Make the PostOffice class implement P3Type. This class should contain only
|
||||
one instance variable - A HashTable called htExpress. The default size should
|
||||
be set to the default size in P3Type - 10.
|
||||
|
||||
The first method that you want to write is one which allows the post office to
|
||||
issue another MailBox. Call this method issue. It wil take in the new Mailbox
|
||||
and should add this mailbox to the Hashtable. REMEMBER, the key is the persons
|
||||
last name. A person should not be allowed to have two mailboxes.
|
||||
public void issue(MailBox mbToAdd)
|
||||
|
||||
The second method you will need to write is one that will retrieve all the mail
|
||||
located in a person's mailbox. The person's last name will be passed in to
|
||||
this method. If the person does not own a mailbox at this post office,
|
||||
then return P3Type.NO_MAILBOX. Otherwise the method should
|
||||
return a String representing all the mail that the person has received.
|
||||
public String retrieve(String strLName)
|
||||
|
||||
Now write a method called send(Mail toSend). This method will return an int.
|
||||
If the person trying to send the mail has never been issued a MailBox then
|
||||
return P3Type.BAD_SENDER. This has first priority. If the recipient of the
|
||||
mail has never been issued a MailBox before, then return P3Type.NO_RECIPIENT.
|
||||
Otherwise add the mail to the recipients MailBox and return P3Type.MAIL_SENT.
|
||||
In order to write this method, you will need to get the last name of the
|
||||
sender and recipient. You can assume the last name of the person is all
|
||||
of the characters after the last space. It may be beneficial to use
|
||||
StringTokenizer.
|
||||
public int send(Mail toSend)
|
||||
|
||||
Next write a method that removes a MailBox from the PostOffice. This method
|
||||
should be called remove. It will return the number of unread telegrams in a
|
||||
person's mailbox when it was deleted. If that MailBox has never been
|
||||
issued, then return P3Type.NO_MAILBOX_NUM
|
||||
public int remove(String strLName)
|
||||
|
||||
Another method that will be needed is one to retrieve all of the mailboxes in
|
||||
the Post Office. This can be done by writing a wrapper method for the
|
||||
HashTable elements(). All that needs to be done here is to return the
|
||||
Enumeration of the HashTable.
|
||||
public Enumeration allBoxes()
|
||||
|
||||
|
||||
Lastly write a method that will resize the HashTable (This will make it
|
||||
quicker for the assistant to find the correct mailbox). This method will
|
||||
double the current size of the hashtable.
|
||||
public void resize()
|
||||
|
||||
Remember to test this class
|
||||
|
||||
*********************************************************************
|
||||
|
||||
Now that we have coded the Post Office class, let's go back to the Manager.
|
||||
|
||||
The manager class needs to implement P3Type and hold 3 private instance
|
||||
variables...a String for the customer's last and first name
|
||||
(strCustLName and strCustFName), and a PostOffice
|
||||
|
||||
Make a method called managerActions() that returns a boolean
|
||||
The boolean will be true if the manager has actions to perform and will be
|
||||
false when the manager is ready to go home.
|
||||
Have this method print out P3Type.INIT_ACTIONS. Then wait for the user
|
||||
to make a choice. To figure out how to wait for user input from the keyboard
|
||||
read the file IOhelp.txt.
|
||||
|
||||
The initial actions menu should look like this...
|
||||
|
||||
1) Help Customer
|
||||
2) Add More Rooms
|
||||
3) View All MailBoxes
|
||||
0) Leave for the Day
|
||||
|
||||
If the user enters the selction 1, then you should prompt for the customer's
|
||||
name and go to another method called customerHere() you should continue looping
|
||||
to this method until it returns false (meaning the customer left)
|
||||
If instead the selection choice were 2, then the manager would resize his
|
||||
post office.
|
||||
If 3 were selected, all of the mailboxes in the post office would be printed.
|
||||
If the selection 0 were chosen, then false will be returned and the program
|
||||
will exit.
|
||||
|
||||
The method customerHere() will take in a String strName and return a boolean.
|
||||
The return will be true if the customer still wants to do something at the
|
||||
postoffice. If the customer is done, then false will be returned.
|
||||
First, you should print out the menu P3Type.CUST_HERE which looks like this:
|
||||
|
||||
1) I want a mailbox at this post office.
|
||||
2) I no longer want a post office at this mailbox.
|
||||
3) I want to send a telegram.
|
||||
4) I want to read all of the telegrams I have received.
|
||||
0) Okay, I am done.
|
||||
|
||||
If the customer chooses 1 then you will make a new mailbox and add it
|
||||
to your post office.
|
||||
If the customer chooses 2, you will try to remove the persons mailbox and then
|
||||
do one of two things:
|
||||
- Print P3Type.NO_MAILBOX if the person does not have a mailbox
|
||||
- Or print P3Type.NUM_TELEGRAMS + the number of telgrams the person had
|
||||
If the customer chooses 3, then you should prompt for the person who they
|
||||
want to send the message to, and then prompt for the actual message. Next send
|
||||
the message. And print out an appropriate message depending on the return
|
||||
value
|
||||
Lastly, if the customer chooses the 4 option, then retrieve all the telegrams
|
||||
in his mailbox and print them to the standard out.
|
||||
If 0 is selected, then you should return false and the managerActions menu will
|
||||
be displayed.
|
||||
|
||||
Be SURE to test this class and make sure everything is working up to this point
|
||||
(Since this is a small town, no two people have the same last name)
|
||||
|
||||
END OF PHASE 5!!!
|
||||
====================================================================
|
||||
|
||||
PHASE 6. SAVING THE POSTOFFICE (Updates in Manager.java and PostOffice.java)
|
||||
This phase should contain no more than 1 hour worth of coding. If you are
|
||||
spending significantly more time than this then please come see a TA. All
|
||||
the TA's are more than happy to help.
|
||||
====================================================================
|
||||
|
||||
Inside of PostOffice.java, add one method. This method will be called
|
||||
leavingForTheDay(). This method will be called when the manager chooses the
|
||||
option 0 (Leave for the Day). In this method, you will save all of the
|
||||
MailBoxes in the PostOffice to P3Type.POST_OFFICE.
|
||||
|
||||
*********************************************************************
|
||||
|
||||
Inside of Manager.java, you will have to do two things. First, make it call
|
||||
leavingForTheDay when the manager decides to leave for the day (option 0)
|
||||
|
||||
Next, you will need to read from the file P3Type.POST_OFFICE and add all of the
|
||||
mailboxes into the PostOffice. You can do this by calling issue(Mailbox)
|
||||
|
||||
*********************************************************************
|
||||
|
||||
Lastly, create a file called P3.java which will serve as the driver for this
|
||||
project. This class should make an instance of manager and keep calling
|
||||
managerActions() until the method returns false in which case it prints
|
||||
P3Type.GOOD_BYE
|
||||
|
||||
END OF PHASE 6!!!
|
||||
====================================================================
|
||||
|
||||
|
||||
COMPILERS:
|
||||
========================================================================
|
||||
|
||||
As a reminder, make sure that your program compiles and runs as expected
|
||||
under Sun's JDK 1.3.1. Read the PROGRAM RESTRICTIONS section to see
|
||||
what you can and cannot use to implement this program.
|
||||
|
||||
|
||||
COMMENTS
|
||||
========================================================================
|
||||
|
||||
You must comment your code thoroughly.
|
||||
|
||||
Each method should have a proper javadoc comment with the param and
|
||||
return tags where needed. Each instance and static variable should be
|
||||
javadocced. Any code that is not obvious should be explained briefly in
|
||||
a comment. All closing braces should be commented.
|
||||
|
||||
|
||||
TURNIN
|
||||
========================================================================
|
||||
|
||||
Files to be turned in via Webwork:
|
||||
|
||||
(all .java files you made for this program)
|
||||
This includes but is not limited to
|
||||
- Mail.java
|
||||
- MailBox.java
|
||||
- FileChanger.java
|
||||
- HashNode.java
|
||||
- HashTable.java
|
||||
- Manager.java
|
||||
- PostOffice.java and
|
||||
- P3.java
|
||||
BIN
CS1322/p3/p3.zip
Normal file
BIN
CS1322/p3/p3.zip
Normal file
Binary file not shown.
BIN
CS1322/p3/toons.txt
Normal file
BIN
CS1322/p3/toons.txt
Normal file
Binary file not shown.
211
CS1322/p4/Aircraft.java
Normal file
211
CS1322/p4/Aircraft.java
Normal 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
|
||||
422
CS1322/p4/AircraftCarrier.java
Normal file
422
CS1322/p4/AircraftCarrier.java
Normal file
@@ -0,0 +1,422 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* AircraftCarrier.java
|
||||
*
|
||||
* Revisions: 1.0 Oct. 18, 2002
|
||||
* Created the AircraftCarrier class
|
||||
* 1.1 Oct. 27, 2002
|
||||
* Compiled, Finished, Commented
|
||||
* 1.2 Oct. 28,2002
|
||||
* Whoever writes these stupid .nfo's needs to be more specific
|
||||
* added recode to fix the way launchLog.txt is written
|
||||
*
|
||||
*
|
||||
* </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
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class AircraftCarrier {
|
||||
|
||||
private final String FILE_NAME = "aircraftFile.txt";
|
||||
private BST aircraftBST;
|
||||
private boolean StrikePackageReady;
|
||||
private static final String MENU = (
|
||||
"1) Create New Strike Package\n"+
|
||||
"2) Find Aircraft\n"+
|
||||
"3) Throw Aircraft Overboard\n"+
|
||||
"4) Print Strike Package to file\n"+
|
||||
"5) Launch Aircraft\n"+
|
||||
"6) Quit\n");
|
||||
|
||||
private BufferedReader KB_IN=
|
||||
new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
private Heap launchQueue; //holds the aircraft ordered by LaunchPriority
|
||||
|
||||
//change to false to remove prettiness of console
|
||||
private static final boolean bPretty = true;
|
||||
|
||||
////////////////
|
||||
//Constructors//
|
||||
////////////////
|
||||
|
||||
/**
|
||||
*Default Constructor
|
||||
*starts menu
|
||||
*/
|
||||
public AircraftCarrier(){
|
||||
aircraftBST = new BST();
|
||||
StrikePackageReady = false;
|
||||
doMenu();
|
||||
}
|
||||
|
||||
///////////
|
||||
//Methods//
|
||||
///////////
|
||||
|
||||
/**
|
||||
*Read Input
|
||||
*/
|
||||
private String readLine(){
|
||||
String KB_INPUT ="";
|
||||
|
||||
try{
|
||||
KB_INPUT = KB_IN.readLine();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println("IOException in readLine()");
|
||||
}
|
||||
|
||||
return KB_INPUT;
|
||||
|
||||
}//end readText(String,int)
|
||||
|
||||
/**
|
||||
*Prompt user for Aircraft and then output its specifics
|
||||
*/
|
||||
public void findAircraft(){
|
||||
|
||||
if(!StrikePackageReady){
|
||||
System.out.println("Strike Package not Ready");
|
||||
return;
|
||||
}
|
||||
|
||||
String KB_INPUT;
|
||||
|
||||
System.out.println("Please enter the a plane to search for: ");
|
||||
KB_INPUT = readLine();
|
||||
|
||||
Aircraft craft = new Aircraft(KB_INPUT);
|
||||
|
||||
if(aircraftBST.find(craft) == null){
|
||||
System.out.println("No Aircraft Found");
|
||||
}
|
||||
else{
|
||||
System.out.println(aircraftBST.find(craft));
|
||||
}
|
||||
}//end findAircraft()
|
||||
|
||||
/**
|
||||
*Throws a given aircraft overboard
|
||||
*/
|
||||
private void throwAircraftOverboard(){
|
||||
//On a side note, did you guys consult the Navy about this program?
|
||||
//I really don't think it behooves the Navy to throw their aircraft
|
||||
//overboard
|
||||
//but I could be going out on a limb here too
|
||||
String KB_INPUT;
|
||||
|
||||
if(!StrikePackageReady){
|
||||
System.out.println("Strike Package not Ready");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("Please enter the a plane to throw overboard: ");
|
||||
KB_INPUT = readLine();
|
||||
|
||||
Comparable temp = aircraftBST.remove(new Aircraft(KB_INPUT));
|
||||
|
||||
if(temp == null){
|
||||
System.out.println("No removal made");
|
||||
}
|
||||
else{
|
||||
System.out.println((Aircraft)temp);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*Reads the aircraft from file and adds them to the program
|
||||
*/
|
||||
private void createNewStrikePackage(){
|
||||
BufferedReader br;
|
||||
try{
|
||||
br = new BufferedReader(new FileReader(FILE_NAME));
|
||||
}
|
||||
catch(FileNotFoundException fnfe){
|
||||
System.err.println(FILE_NAME + " not found");
|
||||
return;
|
||||
}
|
||||
catch(Exception e){
|
||||
System.err.println(e);
|
||||
return;
|
||||
}
|
||||
|
||||
String aircraft = "";
|
||||
try{
|
||||
boolean bDone = false;
|
||||
while(!bDone){
|
||||
String temp = br.readLine();
|
||||
|
||||
if(temp == null){
|
||||
bDone = true;
|
||||
}
|
||||
else{
|
||||
aircraft+=" "+temp;
|
||||
}
|
||||
}
|
||||
/*aircraft += br.readLine() while <- br.readLine() != null*/
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println(ioe);
|
||||
return;
|
||||
}
|
||||
|
||||
StringTokenizer st = new StringTokenizer(aircraft);
|
||||
while(st.hasMoreTokens()){
|
||||
aircraftBST.add(new Aircraft(
|
||||
st.nextToken(),
|
||||
st.nextToken(),
|
||||
st.nextToken(),
|
||||
Integer.parseInt(st.nextToken())
|
||||
));
|
||||
}
|
||||
try{
|
||||
br.close();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println(ioe);
|
||||
return;
|
||||
}
|
||||
|
||||
StrikePackageReady = true;
|
||||
}//end createNewStrikePackage()
|
||||
|
||||
/**
|
||||
*Writes the current aircraft to file
|
||||
*/
|
||||
private void printStrikePackage(){
|
||||
FileWriter fw;
|
||||
PrintWriter pw;
|
||||
|
||||
//init fw
|
||||
try{
|
||||
fw = new FileWriter("strikePackage.txt");
|
||||
}
|
||||
catch(FileNotFoundException fnfe){
|
||||
System.err.println(fnfe);
|
||||
return;
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println(ioe);
|
||||
return;
|
||||
}
|
||||
|
||||
//init pw
|
||||
pw = new PrintWriter(fw);
|
||||
|
||||
|
||||
aircraftBST.writeAircraft(aircraftBST,pw);
|
||||
|
||||
try{
|
||||
pw.flush();
|
||||
fw.close();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println(ioe);
|
||||
}
|
||||
}//end printStrikePackage()
|
||||
|
||||
/*************************************************************/
|
||||
|
||||
/***************/
|
||||
//Phase 5 stuff//
|
||||
/***************/
|
||||
|
||||
//private Heap launchQueue //restated for coherency
|
||||
|
||||
/**
|
||||
*writes the aircraft to launchLog.txt and sends them off to battle
|
||||
*/
|
||||
private void launchAircraft(){
|
||||
launchQueue = new Heap();
|
||||
|
||||
aircraftBST.createLaunchQueue(aircraftBST,launchQueue);
|
||||
aircraftBST = null;
|
||||
|
||||
FileWriter fw;
|
||||
PrintWriter pw;
|
||||
|
||||
//init fw
|
||||
try{
|
||||
fw = new FileWriter("launchLog.txt");
|
||||
}
|
||||
catch(FileNotFoundException fnfe){
|
||||
System.err.println(fnfe);
|
||||
return;
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println(ioe);
|
||||
return;
|
||||
}
|
||||
|
||||
//init pw
|
||||
pw = new PrintWriter(fw);
|
||||
|
||||
while(!launchQueue.isEmpty()){
|
||||
Aircraft temp = (Aircraft)(((BSTNode)launchQueue.remove()).getData());
|
||||
|
||||
//old code for the printing of file
|
||||
/*String strTemp = temp.getModel() + " " + temp.getNickname() + " " +
|
||||
temp.getRole() + " " + temp.getLaunchPriority();
|
||||
pw.println(strTemp);*/
|
||||
|
||||
//new code
|
||||
pw.println(temp);
|
||||
|
||||
/*
|
||||
to be honest, I feel the way I did this, the old code, makes more
|
||||
sense than writing "An Aircraft of type blah balh blah" to a file
|
||||
you can reload the file just as easily as aircraftFile.txt.
|
||||
For future TA meetings, please bring up the fact that its annoying
|
||||
to rely on the newgroups (not .announce, the other ones) to find
|
||||
things that should be clear in the instructions to begin with.*/
|
||||
}
|
||||
|
||||
try{
|
||||
pw.flush();
|
||||
fw.close();
|
||||
}
|
||||
catch(IOException ioe){
|
||||
System.err.println(ioe);
|
||||
}
|
||||
StrikePackageReady = false;
|
||||
}//end launchAircraft()
|
||||
|
||||
/**
|
||||
*Writes a menu to screen that runs until the user chooses to quit
|
||||
*/
|
||||
private void doMenu(){
|
||||
boolean quit = false;
|
||||
do{
|
||||
System.out.println(MENU);
|
||||
try{
|
||||
|
||||
int temp = Integer.parseInt(readLine());
|
||||
switch(temp){
|
||||
case 1:
|
||||
if(bPretty){
|
||||
System.out.println("\n\n\n\n/************************");
|
||||
System.out.println(" * Creating new StikePackage...");
|
||||
}
|
||||
createNewStrikePackage();
|
||||
if(bPretty){
|
||||
System.out.println(" * Done.\n\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case 2:
|
||||
if(bPretty){
|
||||
System.out.println("\n\n\n\n/************************");
|
||||
System.out.println(" * Find Aircraft v1.1:");
|
||||
}
|
||||
findAircraft();
|
||||
if(bPretty){
|
||||
System.out.println(" * Done.\n\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case 3:
|
||||
if(bPretty){
|
||||
System.out.println("\n\n\n\n/************************");
|
||||
System.out.println(" * Throw Aircraft Overboard v1.1:");
|
||||
}
|
||||
throwAircraftOverboard();
|
||||
if(bPretty){
|
||||
System.out.println(" * Done.\n\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case 4:
|
||||
if(bPretty){
|
||||
System.out.println("\n\n\n\n/************************");
|
||||
System.out.println(" * Writing StikePackage to File...");
|
||||
}
|
||||
printStrikePackage();
|
||||
if(bPretty){
|
||||
System.out.println(" * Done.\n\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case 5:
|
||||
if(bPretty){
|
||||
System.out.println("\n\n\n\n/************************");
|
||||
System.out.println(" * Launching Aircraft...");
|
||||
}
|
||||
launchAircraft();
|
||||
if(bPretty){
|
||||
System.out.println(" * Done.\n\n");
|
||||
}
|
||||
break;
|
||||
|
||||
case 6:
|
||||
if(bPretty){
|
||||
System.out.println("\n\n\n\n/************************");
|
||||
}
|
||||
System.out.println("Thank you for using pseudo-Aircraft"+
|
||||
"Carrier Version 1.1, send $20 to gtg184g for the"+
|
||||
" full version.\n");
|
||||
if(bPretty){
|
||||
System.out.println(" * Done.");
|
||||
}
|
||||
quit = true;
|
||||
break;
|
||||
default:
|
||||
System.out.println("Try Again, 1-6");
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch(NumberFormatException nfe){
|
||||
System.out.println("Please try again");
|
||||
}
|
||||
}while(!quit);
|
||||
}//end doMenu()
|
||||
|
||||
|
||||
/**
|
||||
* Debugging main for class AircraftCarrier.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
AircraftCarrier bob = new AircraftCarrier();
|
||||
|
||||
/*bob.aircraftBST = new BST();
|
||||
bob.aircraftBST.add(new Aircraft("F-14","Tomcat","Interceptor",8));
|
||||
|
||||
bob.StrikePackageReady = false;
|
||||
bob.findAircraft();
|
||||
|
||||
bob.StrikePackageReady = true;
|
||||
bob.findAircraft();
|
||||
bob.throwAircraftOverboard();
|
||||
bob.aircraftBST.outputTree();
|
||||
|
||||
System.out.println("\nTesting createNewStrikePackage()\n");
|
||||
bob.createNewStrikePackage();
|
||||
|
||||
bob.aircraftBST.outputTree();
|
||||
bob.findAircraft();
|
||||
bob.printStrikePackage();
|
||||
|
||||
bob.launchAircraft();
|
||||
System.out.println(bob.aircraftBST);
|
||||
System.out.println(bob.launchQueue);*/
|
||||
|
||||
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class AircraftCarrier
|
||||
414
CS1322/p4/BST.java
Normal file
414
CS1322/p4/BST.java
Normal 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;
|
||||
|
||||
private 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/p4/BSTNode.java
Normal file
150
CS1322/p4/BSTNode.java
Normal 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
|
||||
175
CS1322/p4/Heap.java
Normal file
175
CS1322/p4/Heap.java
Normal 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
|
||||
39
CS1322/p4/P4.java
Normal file
39
CS1322/p4/P4.java
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* <PRE>
|
||||
* P4.java
|
||||
*
|
||||
* Revisions: 1.0 Oct. 27, 2002
|
||||
* Created the P4 class
|
||||
* 1.1 Oct. 27, 2002
|
||||
* Compiled, tested, 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 P4 {
|
||||
|
||||
/**
|
||||
* Debugging main for class P4.
|
||||
* This method will rigorously test my code.
|
||||
*
|
||||
* <br><br>
|
||||
* @param args a String array of command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
//Approx. Coding time for this file
|
||||
//3.2 Hours
|
||||
|
||||
AircraftCarrier USS_JOSE_RULES = new AircraftCarrier();
|
||||
}// end of main(String[] args)
|
||||
|
||||
}// end of class P4
|
||||
649
CS1322/p4/P4.nfo.txt
Normal file
649
CS1322/p4/P4.nfo.txt
Normal file
@@ -0,0 +1,649 @@
|
||||
CS1322 - Fall 2002 - Program 4
|
||||
|
||||
*** NOTE: THIS FILE IS BEST VIEWED IN AN EDITOR WITH 80 CHARACTER
|
||||
COLUMNS PER ROW ***
|
||||
|
||||
Assigned: Tuesday, October 15th, 2002
|
||||
Due: Monday, October 28th, 2002 at 23:59:59
|
||||
Stop Accepting: Tuesday, October 29th, 2002 at 08:00:00
|
||||
Phase II Instant Feedback Deadline: Monday, October 21st, 2002 at 23:59:59
|
||||
Phase IV Instant Feedback Deadline: Monday, October 28th, 2002 at 23:59:59
|
||||
|
||||
Title: "Java Goes to Sea"
|
||||
|
||||
|
||||
TOPICS
|
||||
======================================================================
|
||||
|
||||
Topics you should already understand that are relevant to this project:
|
||||
|
||||
o .equals method
|
||||
o Polymorphism/Dynamic Binding
|
||||
o Linked Lists
|
||||
o Object I/O
|
||||
o Useful Tools (Vector, StringTokenizer)
|
||||
|
||||
Topics you will learn from this project:
|
||||
|
||||
o Comparable
|
||||
o Binary Search Trees
|
||||
o Heaps
|
||||
o File I/O
|
||||
|
||||
Files provided to you for this project:
|
||||
|
||||
o P4.nfo.txt This file
|
||||
o aircraftFile.txt An input data file for this project
|
||||
|
||||
*NOTE: You are strongly encouraged to read this nfo file completely
|
||||
before starting on the project.
|
||||
|
||||
|
||||
|
||||
PROJECT OVERVIEW
|
||||
======================================================================
|
||||
|
||||
Welcome to the 4th programming assignment! After hearing about your
|
||||
prowess with programming data structures in Java, the United States Navy
|
||||
has asked you to develop a program to manage aircraft carrier flight
|
||||
operations. Aircraft storage in the hangar deck has become extremely
|
||||
disorganized and badly needs a data structure to maintain order
|
||||
efficiently. At the same time, the aircraft launched off a carrier
|
||||
must be deployed in a specific order to properly protect the
|
||||
ship as well as complete mission objectives. The Navy will provide
|
||||
you a text file containing aircraft. It is up to you to maintain a
|
||||
representation of these aircraft in alphabetical order by aircraft
|
||||
nickname while they are waiting in the hangar deck. The skipper of
|
||||
the carrier also wants to be able to locate an aircraft by nickname as
|
||||
well as throw an aircraft overboard (delete it) if need be. We will
|
||||
call a group of aircraft a "Strike Package". When the captain is
|
||||
ready to launch the strike package, he needs the aircraft ordered no
|
||||
longer by nickname but by their launch priority. The Navy also wants
|
||||
you to log (write to a file) the alphabetically ordered aircraft as
|
||||
well as the launch of the strike package. You will need a simple menu
|
||||
system to control the Navy's new software.
|
||||
|
||||
|
||||
|
||||
PHASE I - Aircraft (Approximate Coding Time: 1 hour)
|
||||
======================================================================
|
||||
|
||||
Comparable
|
||||
-----------------------------------
|
||||
|
||||
So...why was the .equals method so great anyway? It's great because
|
||||
it allows the programmer to customize the notion of equality. It was
|
||||
up to him or her to decide whether or not certain attributes of an
|
||||
object were relevant or not when determining the equality of two
|
||||
instances of that class. We will now take that concept one step
|
||||
further by allowing the programmer to customize the notion of not just
|
||||
equality but also "greater than" and "less than". We are no longer
|
||||
limited to saying that two instances are simply unequal. We can now
|
||||
say that this instance is "less than" that instance and that some
|
||||
other instance is "greater than" yet another instance. In order to
|
||||
provide the ability for instances of a class to compare themselves to
|
||||
each other, we need to have that class implement a special interface:
|
||||
Comparable. As in:
|
||||
|
||||
public class BabySeal implements Comparable {
|
||||
|
||||
Comparable has one method:
|
||||
|
||||
public int compareTo(Object o)
|
||||
|
||||
which is used in a similar manner to .equals. The int return value of
|
||||
compareTo represents our "less than", "greater than", or "equals"
|
||||
concepts. Lets say that objects A and B are both instances of the
|
||||
same class.
|
||||
|
||||
If A is LESS than B, the method call
|
||||
A.compareTo(B)
|
||||
will return an int LESS than zero.
|
||||
|
||||
If A is GREATER than B, the method call
|
||||
A.compareTo(B)
|
||||
will return an int GREATER than zero.
|
||||
|
||||
If A is EQUAL TO B, the method call
|
||||
A.compareTo(B)
|
||||
will return an int EQUAL TO zero.
|
||||
|
||||
|
||||
So how do we figure out what constitutes a "less than" or "greater
|
||||
than" relationship. That's up to you and the demands of the problem
|
||||
we're trying to solve just as what constituted equality with .equals
|
||||
was up to the demands of the problem. The guts of a compareTo method
|
||||
will remind you of .equals.
|
||||
|
||||
1. We first must check to see that the object passed in is, in
|
||||
fact, an instance of the same class to which we are comparing it.
|
||||
2. We then must cast the parameter (since it came in as an Object
|
||||
parameter) to the correct class.
|
||||
3. We then perform the necessary operation to decide the
|
||||
relationship and return an int representing that relationship.
|
||||
|
||||
See the lecture slides for more help
|
||||
|
||||
|
||||
Aircraft.java
|
||||
-----------------------------------
|
||||
|
||||
Create a class called Aircraft. The instances of this class will
|
||||
represent the aircraft we're managing for our aircraft carrier. We
|
||||
are going to be sorting these Aircraft instances in a couple different
|
||||
ways: alphabetically by nickname and by launch priority. Since we are sorting
|
||||
them, we will need to determine whether any given Aircraft is greater
|
||||
than or less than another Aircraft. Sounds like we're going to need
|
||||
to have Aircraft implement Comparable.
|
||||
|
||||
Your Aircraft class should have the following:
|
||||
|
||||
o A private instance variable called "model" of type String
|
||||
o A private instance variable called "nickname" of type String
|
||||
o A private instance variable called "role" of type String
|
||||
o A private instance variable called "launchPriority" of type Integer
|
||||
o All accessors and modifiers
|
||||
o A public Constructor taking in all four above instance variables
|
||||
o A public Constructor taking in only the nickname and chaining to the
|
||||
first Constructor
|
||||
|
||||
o A fifth private instance variable called "comparator" of type
|
||||
Comparable. This variable is what we will use to determine the
|
||||
greater than/less than relationship between Aircraft. It will
|
||||
hold a duplicate of either the nickname or the launchPriority.
|
||||
Remember, we want to order the Aircraft by nickname while they are
|
||||
waiting and we want to order them by launchPriority when they are
|
||||
being deployed from the carrier. This variable should initially be
|
||||
set to the nickname.
|
||||
|
||||
o A public method called switchComparators which takes in no parameters
|
||||
and returns void. This method will toggle the comparator
|
||||
variable between the nickname and the launchPriority. If the
|
||||
comparator is currently holding the nickname when this method is
|
||||
called, switch it to the launchPriority and vice versa.
|
||||
|
||||
o the public compareTo method which will compare the comparator
|
||||
variables of the two Aircraft instances and return this
|
||||
comparison. (HINT: String and Integer also implement Comparable)
|
||||
|
||||
How you choose to handle the case where the parameter is not the
|
||||
same class as "this" is up to you. Your means of handling such a
|
||||
case should be for debugging purposes only. You should never
|
||||
allow such a case to actually occur.
|
||||
|
||||
o A public toString method which prints out the following:
|
||||
|
||||
<model> <nickname> of Type <role> with launch priority <launchPriority>
|
||||
(i.e. F-14 Tomcat of Type Interceptor with launch priority 8)
|
||||
|
||||
NOTE: All instance variables should be private
|
||||
|
||||
|
||||
|
||||
PHASE II - Binary Search Trees - INSTANT FEEDBACK
|
||||
(Approximate Coding Time: 2 hours)
|
||||
======================================================================
|
||||
|
||||
So what exactly _is_ a Binary Tree? A Binary Tree is a dynamic data
|
||||
structure that is similar to a linked list, except that each node has two
|
||||
"nexts" instead of just one. We don't call them "next1" and "next2";
|
||||
instead we call them "left" and "right". Any nodes to the left or right
|
||||
of a node are it's children.
|
||||
|
||||
So what exactly _is_ a Binary Search Tree (BST)? A BST is a special Binary
|
||||
Tree that takes on a certain structure based on the ordering (values) of
|
||||
the nodes. This structure dictates that all children on the left of a node
|
||||
are "less than" that node and all children on the right are "greater than"
|
||||
that node. The ordering (the concept of "greater than" and "less than") is
|
||||
whatever we define it to be for certain data.
|
||||
|
||||
For example, the simplest BST is the empty BST. In this case, the "root"
|
||||
reference would be null.
|
||||
|
||||
_|_
|
||||
---
|
||||
|
||||
Suppose we have a Binary Search Tree (BST) with just one node in it. In
|
||||
this case, root would refer to a node, and that node's left and right
|
||||
references would be null. Let's say its value is 5.
|
||||
|
||||
+-----+
|
||||
| 5 |
|
||||
+-----+
|
||||
_/_ _\_
|
||||
--- ---
|
||||
|
||||
If we add 3 to this tree, the BST would determine whether 3 is less than 5.
|
||||
Since it is, the BST would add 3 to the left. This maintains the order of
|
||||
the BST.
|
||||
|
||||
+-----+
|
||||
| 5 |
|
||||
+-----+
|
||||
/
|
||||
+-----+
|
||||
| 3 |
|
||||
+-----+
|
||||
|
||||
If we add 7 to the tree, the BST would add it to the right since 7 is
|
||||
greater than 5.
|
||||
|
||||
+-----+
|
||||
| 5 |
|
||||
+-----+
|
||||
/ \
|
||||
+-----+ +-----+
|
||||
| 3 | | 7 |
|
||||
+-----+ +-----+
|
||||
|
||||
If we add 4 to the tree, the BST would go the left since 4 is less than 5,
|
||||
then it would compare 4 to 3 and add 4 to the right since 4 is greater
|
||||
than 3.
|
||||
|
||||
+-----+
|
||||
| 5 |
|
||||
+-----+
|
||||
/ \
|
||||
/ \
|
||||
+-----+ +-----+
|
||||
| 3 | | 7 |
|
||||
+-----+ +-----+
|
||||
\
|
||||
+-----+
|
||||
| 4 |
|
||||
+-----+
|
||||
|
||||
That was a quick intro to Binary Trees. This program will require you to
|
||||
implement some more sophisticated algorithms.
|
||||
|
||||
Traversals
|
||||
---------------------------------------------
|
||||
|
||||
A traversal is the examination of each node in the BST
|
||||
|
||||
Pre-order traversal- 15
|
||||
perform action->go left->go right / \
|
||||
(15,8,4,12,37,31,97) 8 3
|
||||
/ \ / \
|
||||
In-order traversal- 4 12 31 97
|
||||
go left->perform action->go right
|
||||
(4,8,12,15,31,37,97)
|
||||
|
||||
Post-order traversal-
|
||||
go left->go right->perform action
|
||||
(4,12,8,31,97,37,15)
|
||||
|
||||
|
||||
A few Algorithms for your viewing pleasure...
|
||||
---------------------------------------------
|
||||
Below are a few Algorithms that should prove useful.
|
||||
|
||||
ADDING
|
||||
------
|
||||
1) Check and see if your root is null.
|
||||
a) If your root is null, then go ahead and set the root to a new
|
||||
Node containing the data passed in.
|
||||
b) If the root isn't null, then pass the data and root to the
|
||||
helper.
|
||||
2) Compare the value in current to the value passed in. If the value
|
||||
of the new data is greater than that of the data already in current,
|
||||
then we want to go right. Otherwise we want to recurse left.
|
||||
a) Before going left or right, check and see if that direction is
|
||||
null. If it is, then go ahead and do a current.setLeft() or
|
||||
current.setRight() with a new Node. Otherwise, even when you
|
||||
change the value of current passed in, it's parent is still
|
||||
pointing to null.
|
||||
3) Recurse in the direction you decided, and continue doing the checks
|
||||
until a null value is found.
|
||||
|
||||
FIND
|
||||
----
|
||||
|
||||
1) Check and see if your root is null.
|
||||
a) If your root is null, return null - the value wasn't found
|
||||
b) If the root isn't null, then pass the data and root to the
|
||||
helper.
|
||||
2) Compare the value in current to the value passed in. If the
|
||||
value in current is equal to the value passed in, we found
|
||||
our target. If the value of the new data is greater than
|
||||
that of the data already in current, then we want to go
|
||||
right. Otherwise we want to recurse left.
|
||||
a) Before going left or right, check and see if that direction is
|
||||
null. If it is, then go ahead and return null - the value
|
||||
isn't present.
|
||||
3) Recurse in the direction you decided, and continue doing the checks
|
||||
until a null value is found.
|
||||
|
||||
|
||||
DELETE
|
||||
------
|
||||
|
||||
The exact algorithm of deletion from a list is left up to you.
|
||||
Here are a few guidelines though. There are three cases of node
|
||||
deletion to consider:
|
||||
|
||||
- Deleting a node with no children (i.e. a leaf node): Simply
|
||||
set this node's parent to null.
|
||||
|
||||
- Deleting a node with one child: Have the parent of the node to
|
||||
delete point to the child of the node to delete effectively
|
||||
skipping over the target node
|
||||
|
||||
- Deleting a node with two children: This case is significantly
|
||||
more involved. You must replace the target node with the node
|
||||
from the left subtree with the greatest value or replace the
|
||||
target node with the node from the right subtree with the
|
||||
least value. As an example:
|
||||
|
||||
|
||||
10 <- 10 is the root and the node to delete
|
||||
/ \
|
||||
5 15
|
||||
/ \ / \
|
||||
3 7 12 18
|
||||
/ \ / \ /\ / \
|
||||
1 4 6 9 11 14 17 19
|
||||
|
||||
|
||||
9 is the greatest node of the left subtree
|
||||
11 is the least node of the right subtree
|
||||
|
||||
The resulting tree would be: (we could have used 11 too.
|
||||
It's up to you)
|
||||
|
||||
9
|
||||
/ \
|
||||
5 15
|
||||
/ \ / \
|
||||
3 7 12 18
|
||||
/ \ / /\ / \
|
||||
1 4 6 11 14 17 19
|
||||
|
||||
If we were deleting 15, 14 would be the greatest node of
|
||||
the left subtree and 17 would be the least node of the
|
||||
right subtree.
|
||||
|
||||
Replacing the target node with the appropriate node ensures we
|
||||
maintain the proper BST structure
|
||||
|
||||
|
||||
Now for the project ....
|
||||
|
||||
Create a class called BSTNode. We want to be able to compare our
|
||||
BSTNodes to each other to determine where they would go in a Binary
|
||||
Search Tree. So make sure your BSTNode implements Comparable.
|
||||
|
||||
Your BSTNode class should include the following:
|
||||
|
||||
o A private instance variable called "data" of type Comparable
|
||||
o A private instance variable called "left" of type BSTNode
|
||||
o A private instance variable called "right" of type BSTNode
|
||||
o All accessors and modifiers
|
||||
o The public compareTo method which should return the value of the
|
||||
compareTo method of the "data" instance variable
|
||||
o A public toString method which should return the value of the
|
||||
toString method of the "data" instance variable
|
||||
|
||||
Create a class called BST. This class will arrange BSTNode instances
|
||||
in the proper "left < parent < right" order.
|
||||
|
||||
Your BST class should include the following:
|
||||
|
||||
o A private instance variable called "root" of type BSTNode
|
||||
o The public method "add" which takes in a Comparable and returns void.
|
||||
The Comparable parameter will be the data to add, not a
|
||||
BSTNode. So you will need to create a new BSTNode with this
|
||||
Comparable. Why don't we take in an Object as with
|
||||
LinkedLists? We need to ensure that the data coming can, in
|
||||
fact, be compared to other data in the tree so we can order the
|
||||
tree properly.
|
||||
|
||||
o The public method "find" which takes in a Comparable and returns
|
||||
Comparable. This method should locate the data in the tree
|
||||
that matches the parameter passed in (according to the
|
||||
compareTo method). If no such match exists, return null;
|
||||
|
||||
o The public method "remove" which takes in a Comparable and returns
|
||||
Comparable. This method will locate the data in the tree that
|
||||
matches the parameter passed in (according to the compareTo
|
||||
method) and remove it from the tree while maintaining the BST
|
||||
property. You should then return the data in the tree that
|
||||
matched this parameter. If no data matching the parameter is
|
||||
found, return null.
|
||||
|
||||
|
||||
|
||||
|
||||
PHASE III - Reading in the Aircraft from a file
|
||||
(Approximate Coding Time: 2 hours)
|
||||
========================================================================
|
||||
|
||||
This phase deals with reading in the aircraft from a file and storing
|
||||
them. Provided to you is a file called "aircraftFile.txt". If you
|
||||
open this file, you'll notice that there is one aircraft per line each
|
||||
with four pieces of data separated by spaces. These correspond to the
|
||||
four instance variables of the Aircraft class. You will be reading in
|
||||
each line of this file, creating a new Aircraft instance from the data
|
||||
on this line, and then storing it in a BST. Since the comparator
|
||||
variable in Aircraft is initially set to the nickname, we can be sure
|
||||
that the Aircraft are stored in the BST alphabetically by nickname.
|
||||
|
||||
Create a class called AircraftCarrier. Your class should have the
|
||||
following:
|
||||
|
||||
o A constant (use the keyword "final") called FILE_NAME which
|
||||
matches the name of the file to read in
|
||||
|
||||
o A private instance variable called "aircraftBST" which is of type
|
||||
BST. This variable will store our Aircraft instances in
|
||||
alphabetical order by nickname.
|
||||
|
||||
o A private instance variable called "strikePackageReady" which is of
|
||||
type boolean. This variable should initially be set to
|
||||
false. This variable will ensure we do not try to launch a
|
||||
strike package before the BST is filled with aircraft
|
||||
|
||||
o A method "findAircraft" which takes in no parameters and
|
||||
returns void. This method will be private. This method will
|
||||
prompt the user to enter the nickname of an aircraft to find
|
||||
using the keyboard. (You can use IOHelper if you wish) You
|
||||
should then perform a search of aircraftBST for an Aircraft
|
||||
matching this nickname. If a match is made, print out the
|
||||
Aircraft, otherwise print "No Aircraft Found". If
|
||||
strikePackageFound is set to false then the above actions
|
||||
should not be performed. Instead you should print "Strike
|
||||
Package Not Ready".
|
||||
|
||||
o A method "throwAircraftOverboard" which takes in no
|
||||
parameters and returns void. This method will be private.
|
||||
This method will prompt the user to enter the nickname of an
|
||||
aircraft to remove using the keyboard. (You can use IOHelper
|
||||
if you wish) You should then attempt to remove the Aircraft
|
||||
matching this nickname from the BST. If a removal is made,
|
||||
print out the Aircraft removed, otherwise print "No Aircraft
|
||||
Found". If strikePackageFound is set to false then the above
|
||||
actions should not be performed. Instead you should print
|
||||
"Strike Package Not Ready".
|
||||
|
||||
|
||||
NOTE: You cannot simply pass in the String containing the
|
||||
nickname to the find and remove methods. You cannot call
|
||||
compareTo between two different instances (Aircraft and
|
||||
Strings). Think about the Constructors you made in Aircraft.
|
||||
|
||||
|
||||
o a method "createNewStrikePackage" which takes in no
|
||||
parameters and returns void. This method will be private.
|
||||
This method opens aircraftFile.txt for reading and loads the
|
||||
BST with the newly created Aircraft instances. Use a
|
||||
StringTokenizer to separate the four pieces of data per line:
|
||||
|
||||
private void createNewStrikePackage() {
|
||||
|
||||
assign a new BST to aircraftBST (Be sure you do this! We
|
||||
don't want any old aircraft from our previous strike package
|
||||
in the BST representing this new strike package)
|
||||
|
||||
open file for reading
|
||||
while more lines
|
||||
use a StringTokenizer to gather the model, nickname, role
|
||||
and launchPriority for this Aircraft
|
||||
instantiate the Aircraft
|
||||
add it to the BST
|
||||
end loop
|
||||
|
||||
close the file
|
||||
|
||||
set strikePackageReady to true (Our strike package is now
|
||||
ready for launch)
|
||||
|
||||
*NOTE: The data the autograder will use always contains 4
|
||||
tokens per line: 3 Strings and an int.
|
||||
|
||||
o a private method "printStrikePackage" which takes in no parameters
|
||||
and returns void. This method will be private. This method
|
||||
opens a file called "strikePackage.txt" for writing. The
|
||||
method will perform an in-order traversal of aircraftBST and
|
||||
print one node per line. At the end, your file should have
|
||||
one Aircraft per line sorted by nickname. You should not have
|
||||
any aircraft from any previous printings. Don't forget to
|
||||
close the file.
|
||||
|
||||
|
||||
|
||||
PHASE IV - Heap - INSTANT FEEDBACK
|
||||
(Approximate Coding Time: 3 hours)
|
||||
========================================================================
|
||||
|
||||
Please refer to the following for all your Heap learning needs:
|
||||
|
||||
http://www.cc.gatech.edu/classes/AY2003/cs1322_fall/
|
||||
test_resources/current/study.html#heaps
|
||||
|
||||
|
||||
|
||||
Create a class called Heap that includes the following:
|
||||
|
||||
o a private instance variable called "data" of type Vector. This
|
||||
Vector will hold the data of your heap.
|
||||
|
||||
o the public method "insert" which will take in a Comparable and
|
||||
return void. This method will insert the Comparable into the
|
||||
heap maintaining heap structure.
|
||||
|
||||
o the public method "remove" which will take in no parameters and
|
||||
return a Comparable. This should remove the next element from
|
||||
the heap (which should be the element with the highest priority)
|
||||
while maintaining heap structure. If the heap is empty, return
|
||||
null.
|
||||
|
||||
o the public method "isEmpty" which takes in no parameters and
|
||||
returns a boolean. The method will return true if there are no
|
||||
more elements in the heap and false if there is at least one more
|
||||
element.
|
||||
|
||||
|
||||
|
||||
PHASE V - Filling in AircraftCarrier
|
||||
(Approximate Coding Time: 1.5 hours)
|
||||
========================================================================
|
||||
|
||||
Go back to AircraftCarrier. We will now be adding the ability to
|
||||
launch our Aircraft in order by launchPriority. Our program will be
|
||||
controlled through the use of a text menu displayed in the console.
|
||||
|
||||
Add the following to the class AircraftCarrier:
|
||||
|
||||
o a private instance variable called "launchQueue" of type Heap. This
|
||||
variable will hold the aircraft ordered not by nickname as in
|
||||
aircraftBST but instead by launchPriority.
|
||||
|
||||
o a method "launchAircraft" which takes in no parameters
|
||||
and returns void. This method will be private. In this method,
|
||||
you first must instantiate launchQueue. Then you must traverse
|
||||
aircraftBST in a manner of your choosing. At each node, store
|
||||
the aircraft data in a temporary variable. You should then delete
|
||||
that Aircraft from the tree. Then insert the aircraft into
|
||||
launchQueue. Remember, however, we want to order the aircraft by
|
||||
launchPriority now. But the compareTo method of Aircraft
|
||||
currently orders instances by nickname because that's what the
|
||||
comparator is set to. So before you insert the data into the
|
||||
launchQueue, call switchComparators on the temporary Aircraft variable
|
||||
to switch the compareTo method to order instances by launchPriority.
|
||||
|
||||
After filling your heap, launch the aircraft from the carrier.
|
||||
(Remove each Aircraft from launchQueue until the launchQueue is
|
||||
empty) When you launch an Aircraft, you should display that
|
||||
Aircraft to the screen as well as write that Aircraft to a file
|
||||
called "launchLog.txt". At the end of the launching you should
|
||||
have a file containing each Aircraft launched. You should not
|
||||
have any previous aircraft from any previous launches. The
|
||||
aircraft should appear sequentially by launchPriority. Don't
|
||||
forget to close the file.
|
||||
|
||||
Be sure to set "strikePackageReady" to false since the BST is empty.
|
||||
|
||||
o a private method "doMenu" that should display the following to the
|
||||
screen
|
||||
|
||||
1) Create New Strike Package
|
||||
2) Find Aircraft
|
||||
3) Throw Aircraft Overboard
|
||||
4) Print Strike Package to file
|
||||
5) Launch Aircraft
|
||||
6) Quit
|
||||
|
||||
Your method should read in the user's choice and call the
|
||||
appropriate method. If the user enters something other than a
|
||||
number between 1 and 6, print an appropriate error message and
|
||||
redisplay the menu. If the user enters 6, the program should
|
||||
exit gracefully.
|
||||
|
||||
o a public constructor that takes in no parameters. This constructor
|
||||
will call doMenu.
|
||||
|
||||
|
||||
|
||||
PHASE VI - Wrapping it up
|
||||
(Approximate Coding Time: 1 hour)
|
||||
========================================================================
|
||||
|
||||
Create a driver class called P4 with a main method. This main method
|
||||
will instantiate AircraftCarrier.
|
||||
|
||||
TEST TEST TEST!!
|
||||
|
||||
o Verify the output files generated by your program contain the
|
||||
proper data in the proper order.
|
||||
|
||||
o You may feel free to make up your own aircraft if you feel the
|
||||
ones we provide aren't enough. Keep in mind that the data the
|
||||
autograder will use to test is not the data in the file provided
|
||||
to you. The data the autograder uses will in no way attempt to
|
||||
"trick" your program.
|
||||
|
||||
o Your program should exit gracefully in all circumstances. This
|
||||
means no uncaught exceptions/stack traces.
|
||||
|
||||
o Many of you have been doing debug printing in your project
|
||||
through bDEBUG or otherwise. This is outstanding. Keep it up.
|
||||
Problem is, you're supposed to stop printing debug prints for the
|
||||
final submission. You may lose style points if you leave them in.
|
||||
|
||||
o !!! HEY !!! At no point during your program should you put
|
||||
System.exit(...). When we are grading your project this will
|
||||
cause the autograder to quit as well and we, the TAs, will be
|
||||
most unhappy about this.
|
||||
|
||||
As an aside, you are permitted and in some cases encouraged to add any
|
||||
helper methods or instance variables not explicitly mentioned that you
|
||||
feel will help to accomplish the above tasks.
|
||||
|
||||
Don't forget that when submitting files to webwork, submit ALL your
|
||||
files. Good Luck!
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
16
CS1322/p4/aircraftFile.txt
Normal file
16
CS1322/p4/aircraftFile.txt
Normal file
@@ -0,0 +1,16 @@
|
||||
MH-53 SeaDragon Helicopter 3
|
||||
P-3C Orion Anti-submarine 4
|
||||
F-18 Hornet Multirole 11
|
||||
F-18E SuperHornet Multirole 10
|
||||
F-14 Tomcat Interceptor 8
|
||||
F-4 Phantom Interceptor 9
|
||||
EA-6B Prolwer Jamming 6
|
||||
UH-3 SeaKing Helicopter 16
|
||||
F-8 Crusader Reconaissance 7
|
||||
E-2C Hawkeye AWACS 1
|
||||
S-3B Viking Refueler 5
|
||||
SH-60 SeaHawk Helicopter 2
|
||||
AV-8 Harrier VTOL 12
|
||||
A-6 Intruder Bomber 13
|
||||
V-22 Osprey Transport 14
|
||||
CH-46 SeaKnight Helicopter 15
|
||||
BIN
CS1322/p4/cs1322-P4.zip
Normal file
BIN
CS1322/p4/cs1322-P4.zip
Normal file
Binary file not shown.
15
CS1322/p4/launchLog.txt
Normal file
15
CS1322/p4/launchLog.txt
Normal file
@@ -0,0 +1,15 @@
|
||||
E-2C Hawkeye of Type AWACS with launch priority 1
|
||||
SH-60 SeaHawk of Type Helicopter with launch priority 2
|
||||
MH-53 SeaDragon of Type Helicopter with launch priority 3
|
||||
P-3C Orion of Type Anti-submarine with launch priority 4
|
||||
S-3B Viking of Type Refueler with launch priority 5
|
||||
EA-6B Prolwer of Type Jamming with launch priority 6
|
||||
F-8 Crusader of Type Reconaissance with launch priority 7
|
||||
F-4 Phantom of Type Interceptor with launch priority 9
|
||||
F-18E SuperHornet of Type Multirole with launch priority 10
|
||||
F-18 Hornet of Type Multirole with launch priority 11
|
||||
AV-8 Harrier of Type VTOL with launch priority 12
|
||||
A-6 Intruder of Type Bomber with launch priority 13
|
||||
V-22 Osprey of Type Transport with launch priority 14
|
||||
CH-46 SeaKnight of Type Helicopter with launch priority 15
|
||||
UH-3 SeaKing of Type Helicopter with launch priority 16
|
||||
BIN
CS1322/p4/p4.zip
Normal file
BIN
CS1322/p4/p4.zip
Normal file
Binary file not shown.
BIN
CS1322/p4/p4class.zip
Normal file
BIN
CS1322/p4/p4class.zip
Normal file
Binary file not shown.
0
CS1322/p4/strikePackage.txt
Normal file
0
CS1322/p4/strikePackage.txt
Normal file
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
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user