A method that returns a collection

Thread Solved

Join Date: Oct 2008
Posts: 24
Reputation: Achupa is an unknown quantity at this point 
Solved Threads: 0
Achupa Achupa is offline Offline
Newbie Poster

A method that returns a collection

 
0
  #1
Nov 12th, 2008
Hi, I have an assignment that's giving me some problem:
"1.Add methods to your Human that return collections of arms and legs. implement these methods by iterating through the collection of limbs and checking what type of limb they are. Note that you will need to use the instanceOf keyword "
My code for the Human class is as:
  1. import java.util.*;
  2. public abstract class Human extends Mammal
  3. {
  4. int MyAge,Age;
  5. String mrating;
  6.  
  7. Arm[] arms = new Arm[2];
  8. Leg[] legs = new Leg[2];
  9.  
  10. void walk()
  11. {
  12. legs[0].move("Right Leg");
  13. legs[1].move("Left Leg");
  14. }
  15.  
  16. void swim()
  17. {
  18. arms[0].move("Right hand");
  19. arms[1].move("Left hand");
  20.  
  21. }
  22.  
  23. // default constructor
  24. public Human(String name)
  25. {
  26. super(name);
  27. gender = 'M'; // the default is M for Male
  28. }
  29.  
  30. public void makeSound(String sound)
  31. {
  32. System.out.print(name+" Oooops ");
  33. }
  34.  
  35. public void getOlder(int years)
  36. {
  37. age += years;
  38. }
  39.  
  40. public void eat(String food)
  41. {
  42. System.out.print("Some food please..."+food+"");
  43.  
  44. }
  45. // the getters and setters...
  46. public int getAge()
  47. {
  48. return this.MyAge;
  49. }
  50. public void setAge() //public void setAge(int Age)
  51. {
  52. this.MyAge=age;
  53. }
  54.  
  55.  
  56. //methods for iterating through the limbs
  57. public void getArms()
  58. {
  59. Limb limbs = new Arm("");
  60. ArrayList myArms = new ArrayList();
  61. // Populate the list using the .add() methods
  62. myArms.add("Right Hand");
  63. myArms.add("Left Hand");
  64. Collection myforeLimbs =myArms;
  65. if(limbs instanceof Arm)
  66. {
  67. System.out.println("\t"+myforeLimbs);
  68. }
  69.  
  70. }
  71. public void getLegs()
  72. {
  73. Limb limbs = new Leg("");
  74. ArrayList myLegs = new ArrayList();
  75. // Populate the list using the .add() methods
  76. myLegs.add("Right Leg");
  77. myLegs.add("Left Leg");
  78. Collection myhindLimbs =myLegs;
  79. if(limbs instanceof Leg)
  80. {
  81. System.out.println("\t"+myhindLimbs);
  82. }
  83.  
  84. }
  85. public abstract boolean canWatchMovie(String mRt);
  86.  
  87. }

However, my teacher tells me that:
Your methods return void . e.g
public void getArms()


How do I make these changes in getArms() and getLegs() methods?

NOTE: I have two classes Leg, Arm and interface Limb. The two classes implement Limb interface.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: A method that returns a collection

 
0
  #2
Nov 12th, 2008
if you are calling getArms or getLegs why should that return void? when you call a getter something should be returned, i guess he is just wanting output, but anyway beyond my rant

From i guess what he is wanting you need a list of Limbs in your abstract class and something to add them or add them in the constructor

why are you doing this?
this sort of defeats the purpose of your limb interface
  1. Arm[] arms = new Arm[2];
  2. Leg[] legs = new Leg[2];
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: A method that returns a collection

 
1
  #3
Nov 12th, 2008
Chances are likely that your Instructor wishes for both your getArms and getLegs methods to return an array of Limbs--

  1.  
  2. Arm[] getArms(){
  3.  
  4.  
  5. return arms;
  6. }
  7.  
  8.  
  9. Leg[] getLegs(){
  10.  
  11. return legs;
  12. }

-- however, something about the assignment suggests that your Human class doesn't simply hold an array of 2 arms and 2 legs, but instead a Limb array of 4 Limbs.

From there you would need to make create a Leg array and extract the instances of leg objects references from the Limb array and assign those references to the local array generated in getLegs. The same idea would hold true for getArms.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: A method that returns a collection

 
0
  #4
Nov 12th, 2008
same lines as i am thinking, don't understand the need for an interface when its not used, and a getter that is void

maybe it is these instructors now days
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 19
Reputation: bionicseraph is an unknown quantity at this point 
Solved Threads: 6
bionicseraph bionicseraph is offline Offline
Newbie Poster

Re: A method that returns a collection

 
0
  #5
Nov 12th, 2008
All this talk of passing limbs around is somewhat creepy :p
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 19
Reputation: bionicseraph is an unknown quantity at this point 
Solved Threads: 6
bionicseraph bionicseraph is offline Offline
Newbie Poster

Re: A method that returns a collection

 
0
  #6
Nov 12th, 2008
What you'll want in your "getLimbs()" method is for it to look like this I suspect:

  1. Limb[] getLimbs(){
  2. Limb[] limbs = new Limbs[4];
  3. //Add arms and legs to limbs array
  4. return limbs;
  5. }

That's where the interface comes in
Last edited by bionicseraph; Nov 12th, 2008 at 1:12 am. Reason: code tag fail
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 24
Reputation: Achupa is an unknown quantity at this point 
Solved Threads: 0
Achupa Achupa is offline Offline
Newbie Poster

Re: A method that returns a collection

 
0
  #7
Nov 12th, 2008
How about the "instanceof" ? How can I create array of limbs and use the instanceof?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 24
Reputation: Achupa is an unknown quantity at this point 
Solved Threads: 0
Achupa Achupa is offline Offline
Newbie Poster

Re: A method that returns a collection

 
0
  #8
Nov 12th, 2008
however, something about the assignment suggests that your Human class doesn't simply hold an array of 2 arms and 2 legs, but instead a Limb array of 4 Limbs.

From there you would need to make create a Leg array and extract the instances of leg objects references from the Limb array and assign those references to the local array generated in getLegs. The same idea would hold true for getArms.
How do I do this on the Human class? It's proving hard for me. And remember the "instanceof" part of it
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: A method that returns a collection

 
0
  #9
Nov 12th, 2008
if I understand correctly, you don't need a Arms[] array and a Legs[] array, just a Limbs[] array.

  1.  
  2. private Limb[] limbs = new Limb[4];
  3.  
  4. public ArrayList getArms(){
  5. ArrayList returnVal = new ArrayList();
  6. for(int i = 0; i < 4; i++){
  7. if (limbs[i] instanceOf Arm){
  8. returnVal.add((Arm)limbs[i]);
  9. }
  10. }
  11. return returnVal;
  12. }

and the same for legs

for getLimbs, you just return the limbs array
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC