Results 1 to 4 of 4
  1. #1
    redapplesonly is offline Member
    Join Date
    Feb 2016
    Location
    New Jersey
    Posts
    7
    Rep Power
    0

    Default How to instruct two objects to "friend" one another?

    Hi Javapeople,

    I'm a C++ guy slowly learning Java. So far, so good.

    I'm working on a program where I want to create virtual people and allow them to keep lists of their friends. Its pretty easy to write code that creates "Betty" and "Suzie." I also want them to be friends, meaning I want Betty to keep Suzie in her "friends" list, and vice versa. I've written an "AddAFriend()" method. And I can instruct Betty to add Suzie to her friends list. Child's play. :)

    Here's the problem I'm scratching my head over... I can easily add a mirror statement in main() that essentially instructing Suzie to add Betty to her friends list. (Suzie.AddAFriend(Betty);) But that seems redundant. Because if Betty is Suzie's friend and therefore Suzie is Betty's friend, it would be great if my "AddAFriend()" method can do both actions. (See code below - the syntax I can't figure out is on line 14.) How can Betty add herself into Suzie's friends list?

    Thanks!
    -P



    Java Code:
    import java.util.ArrayList;
    
    public class PersonSim{
    	
    	String Name;
    	ArrayList<PersonSim> friends = new ArrayList<PersonSim>();
    	
    	public PersonSim(String a){
    		Name = a;
    	}
    
    	public void AddAFriend(PersonSim a){
    		this.friends.add(a);
    		a.friends.add(this.__??me??__);	// Problem is here -- how to add local object to remote object's friends list?
    	}
    }
    
    
    public class MyProgram {
    	
    	public static void main(String[] args) {
    		PersonSim Betty = new PersonSim("Betty");
    		PersonSim Suzie = new PersonSim("Suzie");
    		Betty.AddAFriend(Suzie);
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to instruct two objects to "friend" one another?

    You're close.
    It's just 'this'.
    So:
    Java Code:
    a.friends.add(this);
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    redapplesonly is offline Member
    Join Date
    Feb 2016
    Location
    New Jersey
    Posts
    7
    Rep Power
    0

    Default Re: How to instruct two objects to "friend" one another?

    ...ahhhHHHHHHHhhhhhh...!!!

    Thank you thank you thank you!!!

    (sheepish)
    -P

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: How to instruct two objects to "friend" one another?

    That's OK.
    That keyword does sometimes take a little while to fix in people's heads.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 09:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 04:06 PM
  3. Embarrisingly simple from "a friend"
    By v6g in forum New To Java
    Replies: 5
    Last Post: 11-26-2009, 12:48 AM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Tags for this Thread

Posting Permissions

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