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.