Discuss Java Composition in the Java Help forum on Dev Shed. Java Composition Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
Posts: 14
Time spent in forums: 2 h 2 m 14 sec
Reputation 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
Posts: 14
Time spent in forums: 2 h 2 m 14 sec
Reputation Power: 0
Quote:
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;
}
Posts: 2,955
Time spent in forums: 1 Week 6 Days 2 h 51 m 13 sec
Reputation Power: 345
Quote:
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.
Posts: 14
Time spent in forums: 2 h 2 m 14 sec
Reputation Power: 0
Quote:
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:
Posts: 14
Time spent in forums: 2 h 2 m 14 sec
Reputation 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.