I have the following problem:
- Build the classes
Animal,Cat, andBug. - Define the properties
colorandleg_numberon the relevant and necessary classes. Have them be initialized within a constructor. - Add the functionality that would allow us to call a method
movewith theCatandBugclasses. Have the method return a string "I'm moving withleg_numberlegs!", with the "leg_number" beingleg_numberas set on the class. - Add a new class called
Bird. Add the propertywing_number. Add the functionality that would allow us to call a methodmovewith theBirdclass. Have the method return the string "I'm moving withleg_numberlegs!" ifwing_numberdoesn't have an applicable value. Ifwing_numberdoes have an applicable value, return the string "I'm flying".
My code thus far is as follows:
class Animal {
protected String color;
protected int leg_number;
public Animal(String color, int leg_number) {
this.color = color;
this.leg_number = leg_number;
}
public void move() {
System.out.println("I'm moving with " + leg_number + " legs!");
}
}
class Cat extends Animal {
String color;
int leg_number;
public Cat(String color, int leg_number) {
super(color, leg_number);
this.color = "orange";
this.leg_number = 4;
}
@Override
public void move() {
System.out.println("I'm moving with " + leg_number + " legs!");
}
}
class Bug extends Animal {
String color;
int leg_number;
public Bug(String color, int leg_number) {
super(color, leg_number);
this.color = "green";
this.leg_number = 16;
}
@Override
public void move() {
System.out.println("I'm moving with " + leg_number + " legs!");
}
}
class Bird extends Animal {
String color;
int leg_number;
int wing_number;
public Bird(String color, int leg_number, int wing_number) {
super(color, leg_number);
this.color = "yellow";
this.leg_number = 2;
this.wing_number = wing_number;
}
public void move(int wing_number) {
if (wing_number > 0) {
System.out.println("I'm flying");
} else {
System.out.println("I'm moving with " + leg_number + " legs!");
}
}
}
I think I did what the instructions called for in setting the value for leg_number in the constructor. I'm not sure how to use it in the constructor call, though. It seems that if I have Animal myCat = new Cat("orange", 4); that I'm not following the instructions for the assignment. But I'm not sure what else to put there. I've tried using (color, leg_number) and (this.color, this.leg_number), but as expected, they didn't work (I assume because the object hasn't been instantiated yet). I tried setting the values in the parameter list of the constructor itself, but that didn't work either. I tried having the constuctor take no parameters, but I have to call the super constructor which does require parameters, so I get an error that way.
What am I missing here?
EDIT: Where I'm trying to instantiate these is in the Main method.