Iterating through a collection

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

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

Iterating through a collection

 
0
  #1
Nov 3rd, 2008
Hi, can someone help me modify this code to achieve the following:
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.
  1. import java.util.*;
  2. public abstract class Human extends Mammal
  3. {
  4. int MyAge,Age;
  5. String mrating;
  6.  
  7. Arms[] arms = new Arms[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. @Override
  30. public void makeSound()
  31. {
  32. System.out.print(name+" Oooops ");
  33. }
  34. @Override
  35. public void getOlder(int years)
  36. {
  37. age += years;
  38. }
  39. @Override
  40. public void eat()
  41. {
  42. System.out.print("Some food please...");
  43.  
  44. }
  45. // the getters and setters...
  46. public int getAge()
  47. {
  48. return this.MyAge;
  49. }
  50. 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 Arms("");
  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 Arms)
  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.  
  86. public abstract boolean canWatchMovie(MovieRating mRt);
  87.  
  88. }

I have class Arms and class Leg. Both two classes implement interface Limb.
Is there a way I can change my getLegs() and public void getArms() methods to achieve the above?
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: Iterating through a collection

 
0
  #2
Nov 3rd, 2008
Originally Posted by Achupa View Post
Hi, can someone help me modify this code to achieve the following:
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.
  1. import java.util.*;
  2. public abstract class Human extends Mammal
  3. {
  4. int MyAge,Age;
  5. String mrating;
  6.  
  7. Arms[] arms = new Arms[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. @Override
  30. public void makeSound()
  31. {
  32. System.out.print(name+" Oooops ");
  33. }
  34. @Override
  35. public void getOlder(int years)
  36. {
  37. age += years;
  38. }
  39. @Override
  40. public void eat()
  41. {
  42. System.out.print("Some food please...");
  43.  
  44. }
  45. // the getters and setters...
  46. public int getAge()
  47. {
  48. return this.MyAge;
  49. }
  50. 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 Arms("");
  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 Arms)
  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.  
  86. public abstract boolean canWatchMovie(MovieRating mRt);
  87.  
  88. }

I have class Arms and class Leg. Both two classes implement interface Limb.
Is there a way I can change my getLegs() and public void getArms() methods to achieve the above?

ehm yes, by using
Originally Posted by Achupa View Post
instanceOf
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 22
Reputation: janamrob is an unknown quantity at this point 
Solved Threads: 1
janamrob janamrob is offline Offline
Newbie Poster

Re: Iterating through a collection

 
0
  #3
Nov 3rd, 2008
does that mean the way i've done it is okay? or what can i change?
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: Iterating through a collection

 
0
  #4
Nov 3rd, 2008
did you run the code and did it do what you want it to do?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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