Results 1 to 7 of 7
  1. #1
    mukul9999 is offline Member
    Join Date
    Feb 2009
    Posts
    2
    Rep Power
    0

    Default java return help

    i am asked to use a method to return the age of a user who entered a year between 1960 and 2000.
    Can someone design the method ONLY showing the exact code used for retruning the age.

  2. #2
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    i can do this in c++.. i cant tell you how to do it in java.

    int year;
    int age;

    cout<<What year were you?<<endl;
    cin>>year;

    if ((year>=1960) && (year<=2000))
    {
    cout<<you are this old=;
    cout<<2009-year<<endl;
    }

    its not perfect but thats how it will look.. the if statement will work in java

  3. #3
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    How are they to enter the code?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    pseudocode
    Java Code:
    public return_data_type getAge(data_type birthYear){
      currentYear = 2009; // look into calendar class.
    
      age = currentYear - birthYear;
    
      return age;
    }
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    ramsrocker is offline Member
    Join Date
    Feb 2009
    Posts
    48
    Rep Power
    0

    Default

    hope we could help man..

    and sorry i cant help to much with java code.. all ive ever learned was applets. c++ i can do it ; ]

    good luck brah.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Sounds too much like homework here. What you have you done yourself to solve this?

  7. #7
    veens4444 is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default

    public int getAge(int cage)
    {
    Date d=new Date();
    int cryear=d.getYear()+1900;
    return (cryear-cage);
    }

    This will work fine ,it will work for any year,don't hard code the current year ,this is not good practice ,u can put the condition to check birth year is in between 1960-2000 ok.

    this will work fine for who born after 1900 only.
    Thank you

Similar Threads

  1. Java Native Access (JNA) return types of void *
    By burnumd in forum Advanced Java
    Replies: 5
    Last Post: 01-15-2010, 01:09 AM
  2. Using int/int, 7/5 would return 1
    By zoe in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 12:25 PM
  3. about 'return'.
    By helloworld in forum New To Java
    Replies: 9
    Last Post: 11-28-2008, 05:08 AM
  4. if..else..return
    By mqdias in forum New To Java
    Replies: 1
    Last Post: 08-10-2007, 05:20 PM
  5. Return value of method
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 09:23 AM

Posting Permissions

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