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.

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.

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

instanceOf

does that mean the way i've done it is okay? or what can i change?

did you run the code and did it do what you want it to do?

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.