Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Java problem

  1. #1
    New Coder
    Join Date
    Mar 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java problem

    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, i'm sorry..
    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. Thank you in advance..
    It's the public class:
    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");
    }
    }

    And it's the main:
    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);
    }
    }

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    you never increase the static var counter. (actually you do it once, after you are trying to get the number of animals created)

    also, in the function where you add an animal to the counter, you assign the returnvalue of another call to the function to the counter variable. I don't see reason to do that. It seems incorrect to me.

    I would create an addAnimal() function to increase the number of created animals and a getAnimalCount function to get the current count.
    I am the luckiest man in the world

  • #3
    New Coder
    Join Date
    Mar 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks, but...

    can anyone please give me more specific advice about what exactly should i change / add?

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    change the addAnimal() function so that it just adds 1 to the static counter variable
    when creating an object of the type Animal (eg execute the constuctor) call the addAnimal() function
    create a new function (fe getAnimalCount()) which just returns the current counter variable

    I won't write the code because this is clearly a homework assignment. You should be able to figure the previous three steps out with the advice i gave you earlier
    I am the luckiest man in the world


  •  

    Posting Permissions

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