Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: return help

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default return help

    Hello
    I try to return this but i really dont know what i need to type to get it to return. its for p1 and p2 i want help with the return.

    PHP Code:
    import cs1.Keyboard;
    import java.util.Scanner;
    class 
    Person 
    {
        private 
    String name;
        private 
    String persnr;
        private 
    String adress;
        private 
    int age;

        public 
    Person(String _nameString _persnrString _adressint _age
        {
            
    name name;
            
    persnr persnr;
            
    adress adress;
            
    age age;
        }
        
        public 
    void byterNamn(String _name)
        {
            
    name _name;
        }
        
        public 
    void byterAdress(String _adress)
        {
            
    adress _adress;
        }
        
        public 
    void fyllerAr()
        {
            
    age age 1;
        }
        
        public 
    String hamtaNamn()
        {
            return 
    name;
        }
        
        public 
    String hamtaPersonnmmer()
        {
            return 
    persnr;
        }
        
        public 
    String hamtaAdress()
        {
            return 
    adress;
        }
        
        public 
    int hamtaAlder()
        {
            return 
    age;
        }
        
        public 
    String toString()
        {
        
    String _toString;
        
    _toString "Namn: " name "\nÅlder: " age;
        
    _toString _toString +  "\nPersonnummer: " persnr "\nAdress: " adress;
        return 
    _toString;
        }
         
         public 
    p1()
         {
           
    System.out.print("namn: ");
            
    name Keyboard.readString();    
            
            
    System.out.print( "adress: " );
            
    String adress Keyboard.readString();

            
    System.out.print( "ålder: " );
            
    Integer age = new Integer(); 
            
    age.parseInt(Keyboard.readint());

            
    System.out.print( "personnummer: " );
            
    String persnr Keyboard.readString();

         }
         public 
    p2()
         {
           
    System.out.print("namn: ");
            
    name Keyboard.readString();    
            
            
    System.out.print( "adress: " );
            
    String adress Keyboard.readString();

            
    System.out.print( "ålder: " );
            
    Integer age = new Integer(); 
            
    age.parseInt(Keyboard.readint());

            
    System.out.print( "personnummer: " );
            
    String persnr Keyboard.readString();
         }
         
        public static 
    void main(String[] args
        {
             
    String name Keyboard.readString();
            
    String persnr Keyboard.readString();
            
    String adress Keyboard.readString();
            
    int age Keyboard.readint(); 
            
    Person p1 = new Person(nameageadresspersonnummer);
            
            
    String name Keyboard.readString();
            
    String persnr Keyboard.readString();
            
    String adress Keyboard.readString();
            
    int age Keyboard.readint(); 
            
    Person p2 = new Person(nameageadresspersonnummer);

        }



  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: return help

    The question you are asking does not specify where you need help in your code,

    but a method can return 1 thing/object
        public int fyllerAr()
        {
            age = age + 1;
            return age;
        }

    this is how you would return an int variable from a method.
    the part that says "int" can be changed to any variable. (String, int[], Object, etc)

    if you want to return something from a class, you can use a new method to do this task specificly
    people refer to this as a getter method.
        public int getAge()
        {
            return age;
        }

    but you seem to use these methods already, so I am not sure what you need help with.
    Jonathan
    Last edited by JonLane; February 21st, 2012 at 03:20 PM.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: return help

    im sorry i meen help white the return on public p1 and public p2

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: return help

    the question is still a little unclear,

    How to make a copy of an object: (so a new person with all the same things)
    write a copy() method in your Person Class, that calls all your variable getters from your Person Class
    and calls the constructor() to make a new Person Object with them, then returns that Person Object.

    if you want to ask a person to tell you everything about it
    you can write a method to ask for everything. since you can return only 1 thing, you should use a generic Array. (perhaps of type object)
    then you can return that Array to who ever needs it, and it will be up to them to know how to split that information up.
    if you are past basic programming, you will need to have error checking to make sure the information you pull out of the Array is of the type you expect it to be.

    if you just need to print out a persons information
    you can write a method that one by one gets information and prints it
    E.G.
    system.out.println("my age is: " + age);

    Hope one of these are what you needed,
    Jonathan

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: return help

    yes you right that i whant the information to com out. that i meen is you can type in the information om p1 then on p2 and then its going to print that information out but now it telling me that i need a return on p1 and p2 and i dont know how im going to do it but i can try thet you say

  6. #6
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: return help

    if you can translate(to English) and post this part of the assignment for me, I can maybe clarify what your instructor is looking for you to do.

    but by your wording it sounds like your teacher wants you to make a "toString()" method.

    normaly what you would do for this, is have return type string,
    then inside the method you use all your getters to one by one add to your print string.

    something like

    [code]
    public String toString()
    {
    String printable = "";
    // use your getters to one by one ask for thier information and convert it into type String.

    // each time add it to your string printable.
    return printable;
    }

    got to go.

    Goodluck,
    Jonathan

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: return help

    my teacher wants me to do two person and then when im going to start the program im going to enter age, name, address, day of birth and after i type that information in to both person it going to print out that information i type in on the two

  8. #8
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: return help

    Then my suggestions are what you need.

    Goodluck,
    Jonathan

Similar Threads

  1. return;
    By tarkal in forum Java Theory & Questions
    Replies: 3
    Last Post: November 26th, 2011, 10:53 AM
  2. Return variable from an URL.
    By ur2cdanger in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: October 15th, 2011, 07:31 AM
  3. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:24 AM
  4. how to return value ..
    By gr8mind in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 11th, 2011, 05:27 AM
  5. return a day of the year
    By lipry in forum Java Theory & Questions
    Replies: 3
    Last Post: January 23rd, 2011, 05:10 PM

Tags for this Thread