| | |
A method that returns a collection
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 24
Reputation:
Solved Threads: 0
Hi, I have an assignment that's giving me some problem:
My code for the Human class is as:
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.
•
•
•
•
"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 "
Java Syntax (Toggle Plain Text)
import java.util.*; public abstract class Human extends Mammal { int MyAge,Age; String mrating; Arm[] arms = new Arm[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 } public void makeSound(String sound) { System.out.print(name+" Oooops "); } public void getOlder(int years) { age += years; } public void eat(String food) { System.out.print("Some food please..."+food+""); } // the getters and setters... public int getAge() { return this.MyAge; } public void setAge() //public void setAge(int Age) { this.MyAge=age; } //methods for iterating through the limbs public void getArms() { Limb limbs = new Arm(""); 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 Arm) { 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(String mRt); }
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.
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
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
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
Java Syntax (Toggle Plain Text)
Arm[] arms = new Arm[2]; Leg[] legs = new Leg[2];
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
Chances are likely that your Instructor wishes for both your getArms and getLegs methods to return an array of Limbs--
-- 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.
java Syntax (Toggle Plain Text)
Arm[] getArms(){ return arms; } Leg[] getLegs(){ return legs; }
-- 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.
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
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
maybe it is these instructors now days
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Nov 2008
Posts: 19
Reputation:
Solved Threads: 6
What you'll want in your "getLimbs()" method is for it to look like this I suspect:
That's where the interface comes in
java Syntax (Toggle Plain Text)
Limb[] getLimbs(){ Limb[] limbs = new Limbs[4]; //Add arms and legs to limbs array return limbs; }
That's where the interface comes in
Last edited by bionicseraph; Nov 12th, 2008 at 1:12 am. Reason: code tag fail
•
•
Join Date: Oct 2008
Posts: 24
Reputation:
Solved Threads: 0
•
•
•
•
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.
if I understand correctly, you don't need a Arms[] array and a Legs[] array, just a Limbs[] array.
and the same for legs
for getLimbs, you just return the limbs array
Java Syntax (Toggle Plain Text)
private Limb[] limbs = new Limb[4]; public ArrayList getArms(){ ArrayList returnVal = new ArrayList(); for(int i = 0; i < 4; i++){ if (limbs[i] instanceOf Arm){ returnVal.add((Arm)limbs[i]); } } return returnVal; }
and the same for legs
for getLimbs, you just return the limbs array
![]() |
Similar Threads
- CD Collection Program... (Java)
- Help needed with JTree (Java)
- Source Code that don't work? (Java)
- Draw the perimiter of a PointF collection? (VB.NET)
- SEO Confusion (Search Engine Optimization)
- Urgent Help Needed!!!! Plzzz (Java)
- Java practice questions (Java)
- help with arrays, methods (Java)
Other Threads in the Java Forum
- Previous Thread: read file to array
- Next Thread: creating a method for texts
| Thread Tools | Search this Thread |
2dgraphics 3d @param affinetransform android api applet application arc arguments array arrays automation banking binary bluetooth byte chat chatprogramusingobjects class client code color compare component count database design detection eclipse eclipsedevelopment encryption error fractal game givemetehcodez graphics gridlayout gui guitesting helpwithhomework html ide if_statement image input integer interface j2me java java.xls javadesktopapplications javaprojects jni jpanel julia keytool keyword linux list loop macintosh map method methods mobile netbeans newbie object os pong problem producer program programming project projectideas read recursion reference replaysolutions rim scanner server set size sms sort sql string swing terminal threads transforms tree ui unicode validation web windows






