January 1st, 2013, 08:57 AM
-
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
January 1st, 2013, 09:16 AM
-
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.
January 1st, 2013, 09:24 AM
-
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;
}
January 1st, 2013, 09:30 AM
-
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.
January 1st, 2013, 09:38 AM
-
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
January 1st, 2013, 09:46 AM
-
January 1st, 2013, 09:47 AM
-
Originally Posted by NormR
Does the code work now?
Yes, the output is:
Carl, Address: 5th Avenuest., 112, New York, 111
January 1st, 2013, 09:49 AM
-
Interesting. I would not expect the code posted in post#5 to work as you expected.
Can you post the new code.
January 1st, 2013, 10:11 AM
-
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.
January 1st, 2013, 10:13 AM
-
That's an important change.
Comments on this post
January 1st, 2013, 10:33 AM
-
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.
January 1st, 2013, 10:38 AM
-
Code:
this.city=a.getCity();
You need to use a reference variable (above it is a) to access the contents of another object.
January 1st, 2013, 10:46 AM
-
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.
January 1st, 2013, 03:50 PM
-
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.
January 1st, 2013, 04:10 PM
-
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.