1

I have the following problem:

  1. Build the classes Animal, Cat, and Bug.
  2. Define the properties color and leg_number on the relevant and necessary classes. Have them be initialized within a constructor.
  3. Add the functionality that would allow us to call a method 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.
  4. Add a new class called 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.

4
  • Don't repost questions. Just edit the original. Commented Oct 25, 2021 at 19:09
  • That question was deleted. Commented Oct 25, 2021 at 19:10
  • It shouldn't have been. Commented Oct 25, 2021 at 19:10
  • Why do you repeat the color and leg_number in the child classes? They are already declared in Animal. Commented Oct 25, 2021 at 19:24

1 Answer 1

4

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

the problem is that I need to set the values in the class, not as arguments when calling the constructor.
You clearly wrote "Have them be initialized within a constructor.". What do you mean by "I need to set the values in the class, not as arguments when calling the constructor."?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.