aspose file tools*
The moose likes Beginning Java and the fly likes exercise in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exercise in java" Watch "exercise in java" New topic
Author

exercise in java

Ricky James
Ranch Hand

Joined: Mar 26, 2007
Posts: 83
Hi,

I am working on this Java exercise that asked me to do the following:

Develop a class to represents a human. The head, arms and legs should be properties of the human and should be represented by separate objects. All of your objects should have at least three properties and one method. Ensure that the head, arms and legs can only be constructed from with the same package. The human should include a walk method that uses the legs to actually move.

In the main method of the human, create an instance of a human and set the properties so that it looks like you.

Create an interface called limb and modify your arm and leg classes so that they implement it.

Create a new abstract class called Mammal and move any properties and methods that all mammals have from your Human to the Mammal class.

Ensure that your mammal has a private property which is an collection of limbs and protected methods to add, remove and retrieve the limbs

Modify your Human to extend Mammal.


I worked on this and have produced the code which is pasted below.

But I need help with this part although I tried doing it:
Ensure that your mammal has a private property which is an collection of limbs and protected methods to add, remove and retrieve the limbs


Would be really thankful if ranchers helped me with this one.

Thanks
Ricky



EDIT by mw: Added Code Tags. (See how much better that looks? )
[ March 29, 2007: Message edited by: marc weber ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3553
    
  15
You will need to add a Limb parameter to the addLimb() method and then I guess you need to validate (by default) that you are not adding a fifth limb. If there are any mammals with more than four limbs, you will need to override this method in the specific class for that mammal. You could also override it in the Human class to check that the four limbs are made up of two arms and two legs.
Once you've validated, you then just add the Limb to your limbs ArrayList.

In the removeLimb and retrieveLimb (note spelling) methods, you will need to specify parameters that allow the method to indicate which limb is to be removed/retrieved. You will also need to change the return type from void to Limb.
In the case of the removeLimb method, you will need to remove the specified limb from your ArrayList.

You should find that the removeLimb and retrieveLimb methods are more or less identical, so you could implement the former one by calling the latter one and then just adding code to remove it from the ArrayList.


Joanne
Ricky James
Ranch Hand

Joined: Mar 26, 2007
Posts: 83
Hello Joanne,

Thanks for the reply. Much appreciated.

I am gonna incorporate all the advice you gave into the exercise and shall post here what I do for further advice.

Thanks again.

Ricky
Ricky James
Ranch Hand

Joined: Mar 26, 2007
Posts: 83
Originally posted by Joanne Neal:
You will need to add a Limb parameter to the addLimb() method and then I guess you need to validate (by default) that you are not adding a fifth limb. If there are any mammals with more than four limbs, you will need to override this method in the specific class for that mammal. You could also override it in the Human class to check that the four limbs are made up of two arms and two legs.

Once you've validated, you then just add the Limb to your limbs ArrayList.

In the removeLimb and retrieveLimb (note spelling) methods, you will need to specify parameters that allow the method to indicate which limb is to be removed/retrieved. You will also need to change the return type from void to Limb.

In the case of the removeLimb method, you will need to remove the specified limb from your ArrayList.

You should find that the removeLimb and retrieveLimb methods are more or less identical, so you could implement the former one by calling the latter one and then just adding code to remove it from the ArrayList.


Hello Joanne,

I tried what you suggested in the Mammal class and am attaching the code below.

Question: You advised me to change the return type from void to Limb. How can I get the method to return a Limb? For example, in the addLimb method I increased the ArrayList but what is the method going to return?

Thanks in advance.
Ricky

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3553
    
  15
It was only the removeLimb and retrieveLimb methods I advised you to change the return type for.
It's not absolutely necessary for the removeLimb method, but could be used as a confirmation that the Limb was removed. If the Limb was not removed for any reason (perhaps it didn't exist) then the method would return null. As i said, this functionality is not required - it would be okay to leave the return type of removeLimb as void.
What you have done in your code (returning the number of limbs remaining) is probably okay as well, but you will need to change the return type to int in this case.
Ricky James
Ranch Hand

Joined: Mar 26, 2007
Posts: 83
Originally posted by Joanne Neal:
It was only the removeLimb and retrieveLimb methods I advised you to change the return type for.
It's not absolutely necessary for the removeLimb method, but could be used as a confirmation that the Limb was removed. If the Limb was not removed for any reason (perhaps it didn't exist) then the method would return null. As i said, this functionality is not required - it would be okay to leave the return type of removeLimb as void.
What you have done in your code (returning the number of limbs remaining) is probably okay as well, but you will need to change the return type to int in this case.


I have changed the return type to int in all as I understand that way better at the moment.



Does it look good?
Thanks
Ricky
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3553
    
  15
A method called retrieveLimb should return a Limb object, not the number of Limbs that the Mammal has.
I'm also not sure that passing the object that you want to retrieve is a good idea. If you already have the object, why do you need to call a method to retrieve it ? I would ask your teacher what (s)he expects this method to do. Just retrieve a random limb (in which case you don't need any parameter) or retrieve a specific limb (in which case you will need a parameter of some kind to indicate which limb - maybe int values that indicate front left, front right, back left or back right).
I also think it's unlikely that your retrieveLimb method should also remove the limb.
Ricky James
Ranch Hand

Joined: Mar 26, 2007
Posts: 83
Originally posted by Joanne Neal:
A method called retrieveLimb should return a Limb object, not the number of Limbs that the Mammal has.
I'm also not sure that passing the object that you want to retrieve is a good idea. If you already have the object, why do you need to call a method to retrieve it ? I would ask your teacher what (s)he expects this method to do. Just retrieve a random limb (in which case you don't need any parameter) or retrieve a specific limb (in which case you will need a parameter of some kind to indicate which limb - maybe int values that indicate front left, front right, back left or back right).
I also think it's unlikely that your retrieveLimb method should also remove the limb.


Thank you so much again for the reply Joanne.

I've modified the code as you suggested:



And even I didn't understand the logic behind this method. I shall ask the tutor about this.

Thanks again.
Ricky
 
wood burning stoves
 
subject: exercise in java