Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-04-2006, 10:00 AM   PM User | #1
Cat22
New Coder

 
Join Date: Mar 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Cat22 is an unknown quantity at this point
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);
}
}
Cat22 is offline   Reply With Quote
Old 04-04-2006, 10:16 AM   PM User | #2
Roelf
Senior Coder

 
Join Date: Jun 2002
Location: Zwolle, The Netherlands
Posts: 1,110
Thanks: 2
Thanked 28 Times in 28 Posts
Roelf is on a distinguished road
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
Roelf is offline   Reply With Quote
Old 04-04-2006, 05:39 PM   PM User | #3
Cat22
New Coder

 
Join Date: Mar 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Cat22 is an unknown quantity at this point
Thanks, but...

can anyone please give me more specific advice about what exactly should i change / add?
Cat22 is offline   Reply With Quote
Old 04-05-2006, 06:43 AM   PM User | #4
Roelf
Senior Coder

 
Join Date: Jun 2002
Location: Zwolle, The Netherlands
Posts: 1,110
Thanks: 2
Thanked 28 Times in 28 Posts
Roelf is on a distinguished road
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
Roelf is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:43 AM.


Advertisement
Log in to turn off these ads.