| | |
Iterating through a collection
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2008
Posts: 24
Reputation:
Solved Threads: 0
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.
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?
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 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?
•
•
•
•
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.
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 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
![]() |
Similar Threads
- Help me out with Type casting (Java)
- How to implement a binary tree Using HashMap Collection in java (Java)
- Iterating a collection without using loop (Community Introductions)
- LinkedHashSet Itearator problem (Java)
Other Threads in the Java Forum
- Previous Thread: Recursion problem...
- Next Thread: what are "prejava" files?
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows





