• Post Reply Bookmark Topic Watch Topic
  • New Topic

am I old, young or teen?  RSS feed

 
adebari olalekan
Ranch Hand
Posts: 61
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wanted this code to evaluate on an input whether i am old , young or teen, but i think the logical operators are messing up; i need someone to debug the code for me(as i have done and cant figure what is happening), and tell what is keeping the code from running perfectly. this is how the logical operator is supposed to be :
the constructor ClassvsInstance() :  is supposed to check if i am old based on :if age is <=0 = age should be set to =0.
the method yearpasses(): is supposed to increment the instance variable by 1 at each run.
amIOld() : if age is > 13 = i am young. if age is >13 >18 = i am a teenager. else :i am old.
so, i just create a loop so that the first input should be the amount of times that the loop will run. the reason why i used a loop is to validate the yearpasses() method of incrementing the the  instance variable "age" and output the new age ; just to check if the person has passed a level of growth or another.



thanks in advance.
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your 'age' variable at the top (line 6) should not be static, and ought to be private.
Then, this is a problem part:


Here, you have declared your constructor to take in a parameter called 'initialage'.
Then you assign the value of 'age' to it.
At that point 'age' is zero, the default value for an int, as nothing has been assigned to it.
So that assignment should be the other way around.
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah,, hang on.
I see you use the static 'age' in main.

Do you really want a single value for age?
 
adebari olalekan
Ranch Hand
Posts: 61
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Ah,, hang on.
I see you use the static 'age' in main.

Do you really want a single value for age?


i declared it static because i wanted to output in in the mail method outside of those loops, so that i can check if the problem is caused because the variable: age; is not increasing. but it wasnt the case, and i later removed the static and declared it normal, still it fails to be accurate.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure your main method is doing what you think it is but as you are getting 2 inputs without having printed any text to say what the inputs are for it's hard to tell.

One thing that is clearly wrong is the first line of your constructor, the assignment is the wrong way around.

 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After fixing the constructor, you should look at these lines:

There is no need for the else block to test "age >= 13" since you can never get to that statement unless age is >= 13
 
adebari olalekan
Ranch Hand
Posts: 61
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i appreciate the replies, thanks . i swapped the code inside the constructor for each other and it worked .
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!