The Person class implements MyOwnInterface which means that it must
have a method with the signature:
public AddressInterface getAddress();
This is a clue to you that the person class will contain some sort of
address object which implements the AddressInterface. In the homework
this is an AddressImpl object, so you need to add code to the Person
class to create that object too. You will need to add something like
the following to the Person class:
public class Person implements PersonInterface,
AnotherInterfaceExample, MyOwnInterface {
...
AddressImpl address; //this declaration is at the class level (you
could specify access if you like).
...
// this code is the constructor with arguments
Person(int cashSaving, int retirementFund,
String firstName, String lastName,
int streetNumber, String streetName, String country) {
this.cashSaving = cashSaving;
this.retirementFund = retirementFund;
this.firstName = firstName;
this.lastName = lastName;
address = new AddressImpl(streetNumber, streetName, country);
}
...
// this code implements the getAddress() method specified in the
interface
public AddressInterface getAddress() {
return address;
}
So now when you create a Person object in your main method you will
need to pass in some extra arguments to the constructor for the
address fields that have been added: int streetNumber, String
streetName, and String country. You can see in my example that these
are then passed along to the AddressImpl constructor from the Person
constructor.
After all this, the only thing left to do is to decide how you're
going to display the address (along with the other Person object
details such as the name, wealth and intelligence which you should
already have examples of from the base code provided by Sang Shin). So
you have two options (at least) for displaying the address details:
1. You could create an AddressImpl object reference in main to hold
the AddressImpl object returned by the getAddress() method. For
example in the main method you could use something like this which
calls getAddress just once:
AddressImpl tempAddress = person1.getAddress();
and then use tempAddress.getStreetNumber() etc. in your
System.out.print statements.
OR
2. You can access the object held by the Person object by using the
getAddress() method each time you want to access any of the
AddressImpl getter/setter methods. So you would just use getAddress()
every time like this in your System.out.print statements:
person1.getAddress.getStreetNumber()
Hope this explanation is clear enough. There are of course alternative
solutions.
On 29/12/2009, at 6:13 AM, nn roh wrote:
> Ok .
>
> I did create 2 classes AddressImpl and person .now in the main
> method how i can use Addresimpl classs ?
> and how i can use the method getaddress with it return an interface??
>
> Thanks.
>
>
>
> On Mon, Dec 28, 2009 at 9:32 PM, Stephen Hunter
> <[email protected]> wrote:
> Need more info on what you are having trouble with.
>
> Sent from my iPhone
>
> On Dec 28, 2009, at 7:24 AM, nn roh <[email protected]> wrote:
>
>> Hi ,
>> Still stuck on 1027 hw :(
>>
>> What is the relation between AddressImpl class and person class. ?
>>
>> Can any one help on this!!
>>
>>
>> Thanks
>> Nada
>>
>> On Wed, Dec 23, 2009 at 5:44 AM, Nic Fox <[email protected]> wrote:
>> 1. Usually methods are declared public so that they can be called
>> by other objects without concern for their parent package or
>> inheritance. If you have specific reasons to restrict the access to
>> the methods then you would select a suitable access modifier. If
>> you don't select an access modifier then the implicit access is at
>> the package level.
>>
>> On the other hand, if you have what are often referred to as
>> 'helper methods' in a class then often those are declared as
>> private or protected because they are used internally by the class
>> to perform some common task that the class performs. It is an
>> example of 'Don't Repeat Yourself' and encapsulation.
>>
>> 2. the method call to getAddress() should return a reference to an
>> object instantiated from the AddressImpl class. The AddressImpl
>> class implements AddressInterface.
>>
>> On 23/12/2009, at 8:15 AM, nn roh wrote:
>>
>>> Hi all,
>>>
>>> from homework 1027
>>>
>>> 1-Why i should define the implemented methods as public ?
>>>
>>>
>>> 2-What i can write inside the method AddressInterface
>>> getAddress() which return an interface ?
>>>
>>>
>>> Thanks in advance,
>>>
>>> Nada
>>>
>>>
>>>
>>> --
>>> To post to this group, send email to
>>> [email protected]
>>> To unsubscribe from this group, send email to
>>> [email protected]
>>> For more options, visit this group at
>>> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>>
>>
>>
>> --
>> To post to this group, send email to
>> [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>
>
> --
> To post to this group, send email to
> [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en