Creating classes

Creating classes

public class Rodent
{
  float weight; 
  int age;
  boolean alive;
  boolean declawed;

  /** Create a newborn rodent */
  public Rodent(float weight)
    {
      this.weight = weight;
      age = 0;
      alive=true
      declawed=false;    
    }

  /** Once a year */
  public age()
    {
      age++;
      if (age>4)
	alive=false;
      if (alive)
        weight = weight * 1.1;
    }
 
}

Rodent r = new Rodent(0.25);
r.age();

Next | Previous