Contents

  1. ./Chicken.java
  2. ./MathTest.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, weight, and gender.
 * 
 * @author Sara Sprenkle
 */
public class Chicken {

	// ------------ INSTANCE VARIABLES -------------------

    /** the chicken's name */
	private String name;
	
    /** the height of the chicken in centimeters */
	private int height;
    
    /** the weight of the chicken in pounds */
	private double weight;
    
    /** true iff the chicken is a female */
    private boolean isFemale;
    
    public static String DEFAULT_NAME = "BUBBA";

    /**
     * @param name the name of the chicken
     * @param height the height of the chicken in centimeters
     * @param weight the weight of the chicken in pounds
     * @param isFemale whether the chicken is female
     */
    public Chicken(String name, int height, double weight, boolean isFemale ) {
        this.name = name;
        this.height = height;
        this.weight = weight;
        this.isFemale = isFemale;
    }
    
   /**
    * Assumes Chickens are females
    * @param name the name of the chicken
    * @param height the height of the chicken in centimeters
    * @param weight the weight of the chicken in pounds
    */
    public Chicken(String name, int height, double weight) {
        this( name, height, weight, true );
    }
    
    /**
     * Default name is Bubba (defined by DEFAULT_NAME)
     * Assumes Chickens are female
     * @param height the height of the chicken in centimeters
     * @param weight the weight of the chicken in pounds
     */
    public Chicken( int height, double weight ) {
        this( DEFAULT_NAME, height, weight, true);
    }
    
  	//
	// ----------- GETTER METHODS ------------
	// (also Accessor methods)

    /**
     * @return the height of the chicken, in centimeters
     */
    public int getHeight() {
        return height;
    }
    
    /**
     * @return the weight of the chicken, in pounds
     */
    public double getWeight() {
        return weight;
    }
    
    /**
     * @return the name of the chicken
     */
    public String getName() {
        return name;
    }
    
    public boolean isFemale() {
        return isFemale;   
    }
    

    //
    // ------------- MUTATORS -----------
    //
    
    /**
     * Feeds the chicken--making the chicken grow
     */
    public void feed() {
        // Better: create constants for these values
        weight += .2;
        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;
    }

    /**
     * @param args
     *            the command-line arguments
     */
    public static void main(String args[]) {
        Chicken one = new Chicken("Fred", 38, 2.0, false);
        Chicken two = new Chicken("Sallie Mae", 45, 3.0);
	Chicken bubbaGirl = new Chicken(50, 5.4);

	System.out.println(one.getName() + " weighs " + one.getWeight() );
        one.feed();
        System.out.println(one.getName() + " ate, so he now weighs " + one.getWeight() );
        System.out.println();

		
	System.out.println("Is " + one.getName() + " a female? " + one.isFemale());

		
	System.out.println("Is " + two.getName() + " a female? " + two.isFemale());

		
	System.out.println("Is " + bubbaGirl.getName() + " a female? " + bubbaGirl.isFemale());
        
    }

}

./MathTest.java 2/2

[
top][prev][next]
/**
 *   Demonstrates that you can't construct a Math object.
 *   
 *   @author Sara Sprenkle
 */
public class MathTest {

    public static void main(String args[]) {
        // Compiler gives an error: 
        // Math() has private access in java.lang.Math
        Math math = new Math();
    }
}

Generated by GNU Enscript 1.6.6.