Results 1 to 2 of 2
  1. #1
    silvia is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default Help with static variable counter

    Hi, i have a pretty easy program, and i wanted to add one function - but it doesn't work. It's a little bit long,
    But can you please tell me why doesn't it work?
    I highlighted the problematic function. Basically it's supposed to present how many animals i've created, in this case - 4, but it gives me 0 instead.

    Java Code:
    public class Animal {
    //class variables
    String Name;
    int BirthYear, NumOfLegs, AYear=1970;
    boolean Vegetarian;
    public static int counter=0;
    Animal (String theName , int theBirthYear , int theNumOfLegs, boolean theVegetarian)
    {
    Name = theName;
    BirthYear = theBirthYear;
    NumOfLegs = theNumOfLegs;
    Vegetarian = theVegetarian;
    if (BirthYear < AYear)
    System.out.println( "The animal is old." );
    else
    System.out.println( "The animal is not old." );
    }
    //class methods
    public void printDetails ()
    {
    System.out.println( "Name: " + Name ) ;
    System.out.println( "Year of birth: " + BirthYear ) ;
    System.out.println( "Number of legs: " + NumOfLegs ) ;
    System.out.println( "Vegetarian: " + Vegetarian ) ;
    }
    public static int addAnimal(){
    counter=addAnimal();
    return counter++; }
    public void walk() {
    System.out.println("The animal is walking in the rain.");
    }
    public void run() {
    System.out.println("The animal is running right now.");
    }
    public void eat() {
    System.out.println("The animal is busy eating");
    }
    }
    the main:
    Java Code:
    public class MainAnimalDemo
    {
    public static void main(String[] args)
    {
    //Define & initialize Animal objects :
    Animal Frog = new Animal ("Crazy frog", 1971, 2, true);
    Frog.printDetails();
    Frog.run();
    System.out.println();
    Animal Dog = new Animal ("Bobik", 1965, 4, false);
    Dog.printDetails();
    Dog.eat();
    System.out.println();
    Animal Camel = new Animal ("Moses", 2000, 2, false);
    Camel.printDetails();
    Camel.eat();
    System.out.println();
    Animal Cow = new Animal ("Cow Angelika", 1954, 4, false);
    Cow.printDetails();
    Cow.walk();
    System.out.println("Current number of animals created is: " + Animal.counter);
    }
    }
    Thanks

  2. #2
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    U did add the addAnimal() method in the Animal class, the problem is, u didn't include it inside the constructor

    I presume u wanted the counter to add each time u create an animal right?
    if so u should add addAnimal(); line inside your constructor

    And also,
    Java Code:
    public static int addAnimal(){
    counter=addAnimal();
    return counter++; }
    What do u want from the
    Java Code:
    counter=addAnimal()
    line to do supposedly?

    To increment da animal counter, u don't need this line, it'll bring error toward ur work instead

    Good luck with the code :)

Similar Threads

  1. Replies: 3
    Last Post: 03-20-2009, 01:35 AM
  2. Using Static Variables
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 12:08 AM
  3. Replies: 0
    Last Post: 04-17-2008, 12:07 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •