first commit
This commit is contained in:
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
|
||||
Reference in New Issue
Block a user