| | |
Mammal class
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 24
Reputation:
Solved Threads: 0
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.
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.
I have the following classes Leg and Arms which implement an interface Limb.
Some help in the above classes (Human and Mammal) ?
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)
import java.util.*; public abstract class Mammal { Head head; char gender; public String name; protected int age = 0; private List <Limb> limbs; String myArm,myLeg; protected Mammal() { this.limbs = new ArrayList<Limb>(); limbs.add(new Leg("Left")); limbs.add(new Leg("Right")); limbs.add(new Arms("Left")); limbs.add(new Arms("Right")); } protected Mammal(List<Limb> limbs) { this.limbs = limbs; } // adding limbs: protected void addLimbs(List<Limb>limbToAdd) { this.limbs.add((Limb) limbToAdd); } //remove limbs protected void removeLimbs(List<Limb> limbToRemove) { Object temp = null; Iterator iter = limbs.iterator(); while (iter.hasNext()) { temp = iter.next(); if (temp instanceof Arms) { //myArm = (Arms)temp; iter.remove(); // iter.remove(myArm); if (temp instanceof Leg) { //myLeg = (Leg)temp; iter.remove(); //iter.remove(myLeg); } } } } protected List<Limb> getLimbs() { return this.limbs; } public Mammal(String name) { this.name = name; } public void getOlder(int years) { age += years; } public void makeSound() { System.out.print(name+" Oooops "); } public void eat() { System.out.print("Some food please..."); } }
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)
import java.util.*; public abstract class Human extends Mammal { int MyAge,Age; String mrating; Arms[] arms = new Arms[2]; Leg[] legs = new Leg[2]; void walk() { legs[0].move("Right Leg"); legs[1].move("Left Leg"); } void swim() { arms[0].move("Right hand"); arms[1].move("Left hand"); } // default constructor public Human(String name) { super(name); gender = 'M'; // the default is M for Male } @Override public void makeSound() { System.out.print(name+" Oooops "); } @Override public void getOlder(int years) { age += years; } @Override public void eat() { System.out.print("Some food please..."); } // the getters and setters... public int getAge() { return this.MyAge; } public void setAge(int Age) { this.MyAge=Age; } //methods for iterating through the limbs public void getArms() { Limb limbs = new Arms(""); ArrayList myArms = new ArrayList(); // Populate the list using the .add() methods myArms.add("Right Hand"); myArms.add("Left Hand"); Collection myforeLimbs =myArms; if(limbs instanceof Arms) { System.out.println("\t"+myforeLimbs); } } public void getLegs() { Limb limbs = new Leg(""); ArrayList myLegs = new ArrayList(); // Populate the list using the .add() methods myLegs.add("Right Leg"); myLegs.add("Left Leg"); Collection myhindLimbs =myLegs; if(limbs instanceof Leg) { System.out.println("\t"+myhindLimbs); } } public abstract boolean canWatchMovie(MovieRating mRt); }
I have the following classes Leg and Arms which implement an interface Limb.
Some help in the above classes (Human and Mammal) ?
•
•
Join Date: Aug 2008
Posts: 1,162
Reputation:
Solved Threads: 137
In your instructions you have that you can't add a list of limbs, only single limbs, yet in your code you have.
you need to change it to
Java Syntax (Toggle Plain Text)
// adding limbs: protected void addLimbs(List<Limb>limbToAdd) { this.limbs.add((Limb) limbToAdd); }
you need to change it to
Java Syntax (Toggle Plain Text)
// adding limbs: protected void addLimb(Limb limbToAdd) { this.limbs.add(limbToAdd); }
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
![]() |
Similar Threads
- We need to laugh... (Geeks' Lounge)
- Java Help Needed....... (Java)
- How to "delete" this memory??? (C++)
- virtual methods and inheritance (C++)
Other Threads in the Java Forum
- Previous Thread: Illegal start of expression-Newbie here
- Next Thread: MovieRating class
| Thread Tools | Search this Thread |
-xlint add android api applet application array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component converter database digit eclipse equation error event exception fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying page pearl print problem program programming project qt recursion scanner screen scrollbar server set size sms sort spamblocker sql string superclass swing system thread threads time tree variablebinding windows xor






