I have the following problem:
Animal
, Cat
, and Bug
.color
and leg_number
on the relevant and necessary classes. Have them be initialized within a constructor.move
with the Cat
and Bug
classes. Have the method return a string "I'm moving with leg_number
legs!", with the " leg_number
" being leg_number
as set on the class.Bird
. Add the property wing_number
. Add the functionality that would allow us to call a method move
with the Bird
class. Have the method return the string "I'm moving with leg_number
legs!" if wing_number
doesn't have an applicable value. If wing_number
does 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.
You don't need String color
and int leg_number
in your sub-classes. You also don't need to override move()
method in all sub-classes, except Bird
. Additionally, your move()
method in Bird
shouldn't take any parameters but use wing_number
property instead. Having said that, try the following:
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 {
public Cat(String color, int leg_number) {
super(color, leg_number);
}
}
class Bug extends Animal {
public Bug(String color, int leg_number) {
super(color, leg_number);
}
}
class Bird extends Animal {
private int wing_number;
public Bird(String color, int leg_number, int wing_number) {
super(color, leg_number);
this.wing_number = wing_number;
}
@Override
public void move() {
if (wing_number > 0) {
System.out.println("I'm flying");
} else {
super.move();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.