943,614 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 938
  • Java RSS
Oct 28th, 2008
0

Mammal class

Expand Post »
Can someone help me modify the code below to do the following:
Ensure that your mammal has a private property which is an collection of limbs and protected methods to add, remove and retrieve the limbs. You need to pass a limb not a list to the methods for adding and removing limbs.

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2. public abstract class Mammal
  3. {
  4. Head head;
  5. char gender;
  6. public String name;
  7. protected int age = 0;
  8. private List <Limb> limbs;
  9. String myArm,myLeg;
  10.  
  11. protected Mammal()
  12. {
  13. this.limbs = new ArrayList<Limb>();
  14. limbs.add(new Leg("Left"));
  15. limbs.add(new Leg("Right"));
  16. limbs.add(new Arms("Left"));
  17. limbs.add(new Arms("Right"));
  18. }
  19.  
  20. protected Mammal(List<Limb> limbs)
  21. {
  22. this.limbs = limbs;
  23. }
  24.  
  25. // adding limbs:
  26. protected void addLimbs(List<Limb>limbToAdd)
  27. {
  28. this.limbs.add((Limb) limbToAdd);
  29. }
  30.  
  31. //remove limbs
  32. protected void removeLimbs(List<Limb> limbToRemove)
  33. {
  34. Object temp = null;
  35. Iterator iter = limbs.iterator();
  36. while (iter.hasNext())
  37. {
  38. temp = iter.next();
  39. if (temp instanceof Arms)
  40. {
  41. //myArm = (Arms)temp;
  42. iter.remove(); // iter.remove(myArm);
  43. if (temp instanceof Leg)
  44. {
  45. //myLeg = (Leg)temp;
  46. iter.remove(); //iter.remove(myLeg);
  47. }
  48. }
  49. }
  50. }
  51. protected List<Limb> getLimbs()
  52. {
  53. return this.limbs;
  54. }
  55.  
  56. public Mammal(String name)
  57. {
  58. this.name = name;
  59. }
  60.  
  61. public void getOlder(int years)
  62. {
  63. age += years;
  64. }
  65.  
  66. public void makeSound()
  67. {
  68. System.out.print(name+" Oooops ");
  69. }
  70.  
  71. public void eat()
  72. {
  73. System.out.print("Some food please...");
  74. }
  75.  
  76. }


2. class Human modify to 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.

Java Syntax (Toggle Plain Text)
  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 the following classes Leg and Arms which implement an interface Limb.
Some help in the above classes (Human and Mammal) ?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Achupa is offline Offline
24 posts
since Oct 2008
Oct 28th, 2008
0

Re: Mammal class

In your instructions you have that you can't add a list of limbs, only single limbs, yet in your code you have.

Java Syntax (Toggle Plain Text)
  1. // adding limbs:
  2. protected void addLimbs(List<Limb>limbToAdd)
  3. {
  4. this.limbs.add((Limb) limbToAdd);
  5. }

you need to change it to
Java Syntax (Toggle Plain Text)
  1. // adding limbs:
  2. protected void addLimb(Limb limbToAdd)
  3. {
  4. this.limbs.add(limbToAdd);
  5. }
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Oct 28th, 2008
0

Re: Mammal class

and also same thing with the remove
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Illegal start of expression-Newbie here
Next Thread in Java Forum Timeline: MovieRating class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC