Hi, I have an assignment that's giving me some problem:

"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 "

My code for the Human class is as:

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.

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

Arm[] arms = new Arm[2];
Leg[] legs = new Leg[2];

Chances are likely that your Instructor wishes for both your getArms and getLegs methods to return an array of Limbs--

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.

commented: This should hopefully give you four blocks of green :) +9

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

All this talk of passing limbs around is somewhat creepy :p

What you'll want in your "getLimbs()" method is for it to look like this I suspect:

Limb[] getLimbs(){
  Limb[] limbs = new Limbs[4];
  //Add arms and legs to limbs array
  return limbs;
}

That's where the interface comes in

How about the "instanceof" ? How can I create array of limbs and use the instanceof?

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.

How do I do this on the Human class? It's proving hard for me. And remember the "instanceof" part of it

if I understand correctly, you don't need a Arms[] array and a Legs[] array, just a Limbs[] array.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.