Class Chicken


public class Chicken extends Object
A Java class that represents a chicken. The state of the chicken is its name, height and weight. Difference from previous version: added overloaded constructor and overriden toString and equals methods.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Chicken(int height, double weight)
    Constructs a new Chicken object with the default name "Bubba".
    Chicken(String name, int height, double weight)
    Constructs a new Chicken object with the charactistics as specified by the parameters.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Determines if the Object o is equivalent to this Chicken, based on their name, height, and weight.
    void
    Feeds the chicken, increasing the chicken's weight and height
    int
    Returns the chicken's height, in centimeters
    Returns the chicken's name
    double
    Returns the chicken's weight, in pounds
    static void
    main(String[] args)
    Tests the Chicken class
    void
    setHeight(int h)
    Sets the height of the chicken, in cm
    void
    Sets the name of the chicken
    void
    setWeight(double w)
    Sets the weight of the chicken, in pounds
    Returns a string representation of the chicken.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Chicken

      public Chicken(String name, int height, double weight)
      Constructs a new Chicken object with the charactistics as specified by the parameters.
      Parameters:
      name - the name of the chicken
      height - the height of the chicken in centimeters
      weight - the weight of the chicken in pounds
    • Chicken

      public Chicken(int height, double weight)
      Constructs a new Chicken object with the default name "Bubba". height and weight specified by parameters
      Parameters:
      height - the height of the chicken in centimeters
      weight - the weight of the chicken in pounds
  • Method Details

    • getHeight

      public int getHeight()
      Returns the chicken's height, in centimeters
      Returns:
      the height of the chicken, in centimeters
    • getWeight

      public double getWeight()
      Returns the chicken's weight, in pounds
      Returns:
      the weight of the chicken, in pounds
    • getName

      public String getName()
      Returns the chicken's name
      Returns:
      the name of the chicken
    • feed

      public void feed()
      Feeds the chicken, increasing the chicken's weight and height
    • setName

      public void setName(String n)
      Sets the name of the chicken
      Parameters:
      n - the name of the chicken
    • setHeight

      public void setHeight(int h)
      Sets the height of the chicken, in cm
      Parameters:
      h - the height of the chicken, in cm
    • setWeight

      public void setWeight(double w)
      Sets the weight of the chicken, in pounds
      Parameters:
      w - the weight of the chicken, in pounds
    • toString

      public String toString()
      Returns a string representation of the chicken. Format:
      Chicken name: <name>
      \tweight: <weight> pounds
      \theight: <height> cm

      Weight is displayed to one decimal place

      Overrides:
      toString in class Object
      Returns:
      a string representation of this Chicken
    • equals

      public boolean equals(Object o)
      Determines if the Object o is equivalent to this Chicken, based on their name, height, and weight.
      Overrides:
      equals in class Object
      Parameters:
      o - the object to compare
      Returns:
      true if this object is the same as the o argument (by name, height, and wight). Otherwise, returns false.
    • main

      public static void main(String[] args)
      Tests the Chicken class
      Parameters:
      args - the command-line arguments