Contents

  1. Chicken.java
  2. Rooster.java

Chicken.java 1/2

[
top][prev][next]
/**
 * 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 {

	// ------------ 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) {
        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;   
    }

	/**
	 * @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.java 2/2

[
top][prev][next]
/**
* Class represents a rooster.
* 
* @author Sara Sprenkle
*/
public class Rooster extends Chicken {
    
    public Rooster( String name, 
		int height, double weight) {
		// all instance fields inherited
		// from super class
		//this.name = name;
		//this.height = height;
		//this.weight = weight;
        super(name, height, weight);
		is_female = false;
	}

	// new functionality
	public void crow() {
        System.out.println("Cocka-Doodle-Doo!");
    }
    
    // overrides superclass; greater gains
	public void feed() {
		weight += .5;
		height += 2;
	}
    
    public static void main(String argv[]) {
        
    }

}

Generated by GNU enscript 1.6.4.