1
\$\begingroup\$

I was assigned the following problem:

You've gone back in time to 500BC Athens and Socrates wants you to build him an app to help classify animals.

  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 for that is as follows:

public class SocratesApp {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Cat myCat = new Cat("orange", 4);
        int catLegs = myCat.leg_number;
        myCat.move(catLegs);
    
        Bug myBug = new Bug("green", 16);
        int bugLegs = myBug.leg_number;
        myBug.move(bugLegs);
    
        Bird bird1 = new Bird("yellow", 2, 2);
        int bird1Legs = bird1.leg_number;
        int bird1Wings = bird1.wing_number;
        bird1.move(bird1Legs, bird1Wings);
    
        Bird bird2 = new Bird("brown", 2, 0);
        int bird2Legs = bird2.leg_number;
        int bird2Wings = bird2.wing_number;
        bird1.move(bird2Legs, bird2Wings);

    }

}

class Animal {

    private String color;
    private int leg_number;

    public Animal(String color, int leg_number) {
        this.color = color;
        this.leg_number = leg_number;
    }

    public void move(int leg_number) {
        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 = color;
        this.leg_number = leg_number;
    }

    @Override
    public void move(int leg_number) {
        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 = color;
        this.leg_number = leg_number;
    }

    @Override
    public void move(int leg_number) {
        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 = color;
        this.leg_number = leg_number;
        this.wing_number = wing_number;
    }

    public void move(int leg_number, int wing_number) {
        if (wing_number > 0) {
            System.out.println("I'm flying");
        } else {
            System.out.println("I'm moving with " + leg_number + " legs!");
        }
    }
}

The code works fine. However, I am concerned about whether I should have used an interface or an abstract class instead of a super class. I'm not sure I understand how to determine which to use when. If I should have used something different, can you explain and tell me how I should be implementing the move method or the properties?

I am also concerned about my use/lack of use of public and private in different places. I'm a little confused about if and where I should have used those.

Of course, I am open to comments on anything else that I could have/should have done better and/or more concisely.

I appreciate the feedback everyone!

\$\endgroup\$
8
  • \$\begingroup\$ Welcome to Code Review! I'm tempted to vote to close, since your code does not appear to have implemented the move method the way the problem requires. Can you clarify the requirements, or update the code to state the correct requirements? \$\endgroup\$ Oct 25 at 16:14
  • \$\begingroup\$ Why would you vote to close? That's exactly the reason I'm asking for help, to learn what I need to do differently. If I've done something wrong, the answer is to tell me what I've done wrong, not close the question. \$\endgroup\$ Oct 25 at 16:18
  • \$\begingroup\$ On Code Review, we require that code be functional and working as intended. In this case, I think there is an argument that "not doing what you were asked to do" counts as not working as intended. Notably, I didn't vote to close, because I was unsure and was interested in getting input from other reviewers. \$\endgroup\$ Oct 25 at 16:20
  • \$\begingroup\$ So you're saying if I didn't understand something correctly I can't get any help? No offense, but that seems rather stupid. Why would I ask for help if I've done everything right? I posted the question on Stack Overflow also. They told me that I needed to post it here. If they won't help and you won't help, where do I go for help? I would note that you have answered twice, and you apparently know what I've done wrong, and you have not helped. So tell me, where do I go where someone will tell me what I've done wrong? \$\endgroup\$ Oct 25 at 16:24
  • 1
    \$\begingroup\$ This should be tagged as homework. Apart from that, I'd leave this open as the requirement for move() seems poorly written to me and one could argue that this fulfills it. \$\endgroup\$ Oct 26 at 7:59
4
\$\begingroup\$

I'm not sure how to approach this. You say you've been assigned a problem, so this feels very much like coursework/homework.

Assuming this homework is to be assessed, it's more helpful for the assessor to see your work than mine - they will then know how successful the tuition has been, and where your understanding needs more work.

On that basis I'm not going to give an alternative solution, but will make some general comments.

The intent of the question is clearly for you to show that you've grasped Object-oriented concepts, in Java terms, such as encapsulation, inheritance, polymorphism and so on. The assignment naturally lends itself to such an approach, but your solution largely misses the point in so many ways that I wonder whether you've been taught anything at all about these concepts.

Here's a few points to think about, and review in your learning material.

  1. An instance of an Animal knows how what color it is. It also knows many legs (and possibly wings) it has. What does that tell you about the move() method? (Keyword: Encapsulation.)
  2. Any Animal - whether Cat, Bug or Bird - has legs and a color. (Keyword: Inheritance - you probably should also review the concept of public/protected/private fields and methods.).
  3. Cats, Bugs and wingless Birds all move in the same way, but Birds with wings move differently. (Keyword: Polymorphism)
  4. What does "return a string" mean? Is that what you are doing?

I would suggest you review these points, and rework your code in line with your (hopefully improved) understanding of them. If you'd care to show us your revised code, please don't edit the original post but instead post your updated code as an answer or perhaps a new question.

\$\endgroup\$
2
  • \$\begingroup\$ +1 According to the description of the homework tag, this is how you are approach such questions. \$\endgroup\$ Oct 26 at 7:56
  • \$\begingroup\$ Agree with your answer without code for the OP's homework. \$\endgroup\$ Oct 26 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.