Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0

    Java Composition


    I am new to java and trying to use composition for the first time. I built this class called Person, who takes as an attribute a string name, and an object pAddress. It has two constructors and one copy constructor, a method to set the PAddress attribute and a method String toString:

    Code:
    public class Person {
    	private String name;
    	private Address pAddress;
    	
        ///*constructor that gets the city, street, number, zip from    Address class*/
    
    	public Person (String name, String city, String street, int number, int zip)
    	{
    		this.name=name;
    		pAddress =new Address(city, street, number, zip);
    	}
    	public Person (String name, Address a)
    	{
    		this.name=name;
    		pAddress = new Address (a);
    	}
        //copy constructor
    	public Person (Person other){
    		this.name=other.name;
    		this.pAddress= new Address (pAddress);
    
    //set method for pAddress
        public void setPAddress(String city, String street, int    number, int zip)
    	{
    		pAddress.setCity(city);
    		pAddress.setNumber(number);
    		pAddress.setStreet(street);
    		pAddress.setZip(zip);
    	}
    	public String toString(){
    		return this.name  + ", " + pAddress;
    }
    However, when I try to print the String toString method, I get a NULL value for the object part. to be more specific:

    Code:
    public static void main (String[]args){
    		Address Home=new Address("New York","5th Avenue", 112, 111);
    		Person a=new Person("Carl",Home);
    		System.out.println(a);
    The last println comes out:

    Code:
    Carl, Address: nullst., 0, null
    Thanks very much for every help. sorry for the long code, I'm new to java and it's tough to be specific. I promise to learn next time
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,716
    Rep Power
    348
    What variable has the null value? Why doesn't it have a non-null value?


    Can you post the code for the Address class? That would be where the problem is.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by NormR
    What variable has the null value? Why doesn't it have a non-null value?


    Can you post the code for the Address class? That would be where the problem is.
    .

    Thanks for answering. The null value is received for the a value - an Address object.
    The Address class:
    Code:
    public class Address {
    private String city, street;
    private int number, zip;
    public Address (String c, String s, int n, int z){
        city=c;
        street=s;
        number=n;
        zip=z;
    }
    public Address (Address a)
    {
        this.city=city;
        this.street=street;
        this.number=number;
        this.zip=zip;
    }
    public Address (String c, String s, int n)
    {
        zip=0;
        city=c;
        street=s;
        number=n;
    }
    
    
    public String toString(){ 
    if (zip==0)
        return "Address: " + street + "st., " + number + ", " + city ;
    return "Address: " + street + "st., " + number + ", " + city  + ", " + zip;
    
    }
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,716
    Rep Power
    348
    null value is received for the a value - an Address object.
    If the Address variable was null, the print out would be:
    Carl, null

    but the print out was:
    Carl, Address: nullst., 0, null

    that shows that the Address object was NOT null because "Address" comes from the Address class's toString() method. So the problem must be inside the Address class.

    Try debugging the code by adding some printlns to the Address class to print out the values of the variables as they are set before they are used in the toString() method.


    NOTE: The posted code does not compile without errors.
    Please correct all the compiler errors and repost the code.
    Last edited by NormR; January 1st, 2013 at 09:37 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by NormR
    If the Address variable was null, the print out would be:
    Carl, null

    but the print out was:
    Carl, Address: nullst., 0, null

    that shows that the Address object was NOT null because "Address" comes from the Address class's toString() method. So the problem must be inside the Address class.

    Try debugging the code by adding some printlns to the Address class to print out the values of the variables as they are set before they are used in the toString() method.


    NOTE: The posted code does not compile without errors.
    Please correct all the compiler errors and repost the code.
    Thank you. I think I got the answer - the problem is in the Address class indeed - I had to add:
    Code:
    this.city=getcity();
        this.street=getstreet();
        this.number=getnumber();
        this.zip=getzip();
    In the code.

    Thank you
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,716
    Rep Power
    348
    Does the code work now?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by NormR
    Does the code work now?
    Yes, the output is:
    Carl, Address: 5th Avenuest., 112, New York, 111
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,716
    Rep Power
    348
    Interesting. I would not expect the code posted in post#5 to work as you expected.
    Can you post the new code.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by NormR
    Interesting. I would not expect the code posted in post#5 to work as you expected.
    Can you post the new code.
    It's exactly the same code, except this change:
    Code:
    public Address (Address a)
    		{
    		    this.city=a.getCity();
    		    this.street=a.getStreet();
    		    this.number=a.getNumber();
    		    this.zip=a.getZip();
    	
    		}
    Before the change, the getCity method wasn't there, just the "city" variable, and so on with the rest three variables.
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,716
    Rep Power
    348
    except this change:
    That's an important change.

    Comments on this post

    • nerazzurri10 agrees : Thanks for the help
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by NormR
    That's an important change.
    Yes, well now I've learned.

    Do I have to make this change every time I set a private attribute, or just when I'm giving the object as a parameter to a method in OTHER class?

    Thanks.
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,716
    Rep Power
    348
    Code:
    this.city=a.getCity();
    You need to use a reference variable (above it is a) to access the contents of another object.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Originally Posted by NormR
    Code:
    this.city=a.getCity();
    You need to use a reference variable (above it is a) to access the contents of another object.
    Ok, thank you very much for your help.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    25
    Rep Power
    0
    Hi, it's me again.

    I've added a method that compares values within the object:
    Code:
    	public boolean sameAddress(Person p){
    		Person b=new Person (p.getName(), p.getPAddress());
    		if (p.getName()==name)
    			return true;
    		return false;
    It's supposed to check if the Person's address is the name for both objects.

    the main is here:
    Code:
    public static void main (String[]args){
    		Address Home=new Address("New York","5th Avenue", 112, 111);
    		Person a=new Person("Carl",Home);
    		Person b=new Person ("Carl",a.getPAddress());
    		System.out.println(b.sameAddress(a));
    Even though they have the same address I get a FALSE answer to my method.

    Does anybody know what's wrong with my code?

    Thanks.
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,716
    Rep Power
    348
    What class variable(s) hold the addresses that you want to compare?

    You should use the equals() method to compare the contents of Strings, not the == operator.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo