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.

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.

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) ?

Recommended Answers

All 2 Replies

In your instructions you have that you can't add a list of limbs, only single limbs, yet in your code you have.

// adding limbs:
   protected void addLimbs(List<Limb>limbToAdd) 
   {
        this.limbs.add((Limb) limbToAdd);
   }

you need to change it to

// adding limbs:
   protected void addLimb(Limb limbToAdd) 
   {
        this.limbs.add(limbToAdd);
   }

and also same thing with the remove

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.