• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with calculating age from birth year.  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to be able to have any birth year be entered into the console and to have the age be displayed.

I also need to have a string of text that tells how many decades the person has been alive.

here is the code I have so far:





Also, here are the instructions just in case:

"
Write either an application or an applet that allows the user to give their year of birth.
Given their year of birth, calculate their age.
So that you do not have to worry about issues like Y2K, use the class java.util.Calendar.
Try these calls to instantiate and get the current year (of course you will need to import the package too):
Calendar rightNow = Calendar.getInstance();
System.out.println(rightNow.get(Calendar.YEAR));

Provide a nested if (or case statement) of 10 different statements of what the person can do depending on their age. Ranges of capabilities are from 0-9, 10-19, 20-29, ... 90-99. Given their age, provide output of how many decades they have been alive and then give them some words of wisdom about what they can or should do during this time of their life."
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, and what's your question? What specifically are you having trouble with?
 
Mack Grill
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How do I determine the age from any birth year entered into the console and how do I display how many decades they have been alive based on their age.
 
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well, you don't have much so far. if it was me i would write a GUI app(or applet:should be extra credit for that ) instead of a console app.
 
Mack Grill
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't have that much because I can't continue without the statements for determining the age based on birth year and also how many decades they've been alive.

I've tried searching on google but all I found was how to determine the age based on a fixed birth year

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mack Grill wrote:

How do I determine the age from any birth year entered into the console and how do I display how many decades they have been alive based on their age.


You're asking multiple things at once here. You need to break it down into independent pieces, and solve them one at a time. For example:

  • Reading the birth year from the console.
  • Turning the String read for brith year into a number (may or may not be a separate step).
  • Determining the current year.
  • Doing the math to compute age in years based on the above two years.

  • and so on.

    You can pick any one of those, write code for it, test that code, debug it, and get it working all by itself, without any thought to the other steps. Then you move on to another step, write, test, debug code for just that step. Then maybe you combine the two, or maybe you move on to another step.

    What you don't do is try to solve "read the birth year, calculate the age, and display it" all at once, write all the code, and then try to compile and run it. That way lies madness.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Randall Twede wrote:i would write a GUI app(or applet:should be extra credit for that ) instead of a console app.


    Definitely not a path the OP should follow. At least not until he gets it working as a console app first.
     
    Mack Grill
    Greenhorn
    Posts: 29
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Could I get an example of the math involved for calculating the age?

     
    Randall Twede
    Ranch Hand
    Posts: 4696
    8
    Java Scala
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    [Deleted. Please, let's encourage the OP to work as much of it out himself as possible.]
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mack Grill wrote:
    Could I get an example of the math involved for calculating the age?



    What year were you born? How old are you now? How did you work that out?

    So, as an example of the step-by-step work I described before, you might do something like this:



    Compile, test, run.

    Then add a tiny bit:



    Compile, test, run...

    And so on. Then you hardcode the current year and the birth year and you do the math in that "calculate" method, and you see if the result is what you expect. Then you move on to taking the real current year, and so on.
     
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    [Moderator action: Solution deleted. This site is NotACodeMill.(⇐click)]
     
    sangram nani
    Greenhorn
    Posts: 22
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    sangram nani wrote:[Moderator action: Solution deleted. This site is NotACodeMill.(⇐click)]



    Ok. I'll follow.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!