Chicken.java
/**
* A simple Java class that models a Chicken. The state of the chicken is its
* name, height and weight.
*
* Modified to be a better parent class--giving child classes access to
* instance variables.
*
* @author Sara Sprenkle
*/
public class Chicken implements Comparable {
// ------------ INSTANCE VARIABLES -------------------
protected String name;
/** the height of the chicken in centimeters */
protected int height;
/** the weight of the chicken in pounds */
protected double weight;
protected boolean is_female;
public Chicken(String name, int height, double weight) {
this( name, height, weight, true );
}
public Chicken(String name, int height, double weight, boolean is_female) {
this.name = name;
this.height = height;
this.weight = weight;
this.is_female = is_female;
}
// ---- Methods overridden from Object class ----
public String toString() {
String rep = name + " height: " + height + " weight: " + weight;
if( is_female ) {
rep = "Girl " + rep;
} else {
rep = "Boy " + rep;
}
return rep;
}
public boolean equals(Object o) {
if( ! (o instanceof Chicken) ) {
return false;
}
Chicken other = (Chicken) o;
if( ! this.name.equals(other.getName()) ) {
return false;
}
if( this.weight != other.getWeight() ) {
return false;
}
if (this.height != other.getHeight() ) {
return false;
}
return true;
}
//
// ----------- GETTER METHODS ------------
// (also Accessor methods)
/**
* @return the height of the chicken, in centimeters
*/
public int getHeight() {
return height;
}
public double getWeight() {
return weight;
}
public String getName() {
return name;
}
public boolean isFemale() {
return is_female;
}
//
// ------------- MUTATORS -----------
//
public void feed() {
weight += .3;
height += 1;
}
//
// ------------- SETTERS ----------
//
/**
* @param n
* the name of the chicken
*/
public void setName(String n) {
name = n;
}
/**
* @param h
* the height of the chicken, in cm
*/
public void setHeight(int h) {
height = h;
}
/**
* @param w
* the weight of the chicken, in pounds
*/
public void setWeight(double w) {
weight = w;
}
public void setIsFemale( boolean isFemale ) {
is_female = isFemale;
}
/**
* Compares Chicken objects by weight
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(Object o) {
if( ! (o instanceof Chicken) ) {
return -1;
}
Chicken otherObj = (Chicken) o;
if( weight < otherObj.getWeight() ) {
return -1;
}
else if( weight > otherObj.getWeight() ) {
return 1;
}
return 0;
}
/**
* @param args
* the command-line arguments
*/
public static void main(String args[]) {
Chicken firstChicken = new Chicken("Fred", 38, 5.0, false);
// feed Fred
firstChicken.feed();
// print out Fred's name, height, and weight
/*
System.out.println("Name: " + firstChicken.getName() );
System.out.println("Height: " + firstChicken.getHeight() );
System.out.println("Weight: " + firstChicken.getWeight() );
*/
System.out.println(firstChicken);
Chicken second = new Chicken("Fred", 39, 3.3, false);
System.out.println("Does firstChicken equal second? " +
firstChicken.equals(second));
Rooster foghorn = new Rooster("Foghorn", 80, 40);
System.out.println("Does foghorn equal firstChicken?: "
+ foghorn.equals(firstChicken));
}
}
Generated by GNU enscript 1.6.4.