Skip to main content
edited title; shortened preface (your question is fine)
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Model animals using inheritance in Java Solution for an interview coding requirement, revised

Yesterday I was attemptingposted my first solution to get help with a problem that many people assumed was a homework question. In fact it is an interview problem here. And before you say it, yes, I I am now aware that I have many weak spots in Java and need to do extensive review before tackling any more interviews. Having said that, piecing together what people on here mentioned, I have come to what I hope is a good solution.

If you're concerned about giving help or advice for an interview question, please know that I have already submitted my proposed solution. Any help on here now is too late for this interview, but hopefully honing in on what I should have done here prepares me better for future problems.

If Iand am somehow breaking the rules with this submission, please tell me what I needasking to do so that I won't be breaking the ruleshave it reviewed again.

So hereHere is the 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 <number of legs> legs!", with the "<number of legs>" 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 <number of legs> legs!" if wing_number doesn't have an applicable value. If wing_number does have an applicable value, return the string "I'm flying".

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 <number of legs> legs!", with the "<number of legs>" 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 <number of legs> legs!" if wing_number doesn't have an applicable value. If wing_number does have an applicable value, return the string "I'm flying".

Java Solution for an interview coding requirement

Yesterday I was attempting to get help with a problem that many people assumed was a homework question. In fact it is an interview problem. And before you say it, yes, I am now aware that I have many weak spots in Java and need to do extensive review before tackling any more interviews. Having said that, piecing together what people on here mentioned, I have come to what I hope is a good solution.

If you're concerned about giving help or advice for an interview question, please know that I have already submitted my proposed solution. Any help on here now is too late for this interview, but hopefully honing in on what I should have done here prepares me better for future problems.

If I am somehow breaking the rules with this submission, please tell me what I need to do so that I won't be breaking the rules.

So here is the 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 <number of legs> legs!", with the "<number of legs>" 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 <number of legs> legs!" if wing_number doesn't have an applicable value. If wing_number does have an applicable value, return the string "I'm flying".

Model animals using inheritance in Java, revised

Yesterday I posted my first solution to an interview problem here. I am now aware that I have many weak spots in Java and need to do extensive review before tackling any more interviews. Having said that, piecing together what people on here mentioned, I have come to what I hope is a good solution, and am asking to have it reviewed again.

Here is the 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 <number of legs> legs!", with the "<number of legs>" 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 <number of legs> legs!" if wing_number doesn't have an applicable value. If wing_number does have an applicable value, return the string "I'm flying".
Tweeted twitter.com/StackCodeReview/status/1453240045670998024
Became Hot Network Question
edited tags
Link
Source Link
apex2022
  • 161
  • 1
  • 5

Java Solution for an interview coding requirement

Yesterday I was attempting to get help with a problem that many people assumed was a homework question. In fact it is an interview problem. And before you say it, yes, I am now aware that I have many weak spots in Java and need to do extensive review before tackling any more interviews. Having said that, piecing together what people on here mentioned, I have come to what I hope is a good solution.

If you're concerned about giving help or advice for an interview question, please know that I have already submitted my proposed solution. Any help on here now is too late for this interview, but hopefully honing in on what I should have done here prepares me better for future problems.

If I am somehow breaking the rules with this submission, please tell me what I need to do so that I won't be breaking the rules.

So here is the 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 <number of legs> legs!", with the "<number of legs>" 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 <number of legs> legs!" if wing_number doesn't have an applicable value. If wing_number does have an applicable value, return the string "I'm flying".

Here was my final solution that I submitted:

public class SocratesApp {

    public static void main(String[] args) {
        Cat myCat = new Cat();
        System.out.println(myCat.move());
    
        Bug myBug = new Bug();
        System.out.println(myBug.move());
    
        Bird myBird1 = new Bird(2);
        System.out.println(myBird1.move());
    
        Bird myBird2 = new Bird(0);
        System.out.println(myBird2.move());
    }
}

class Animal {
    protected String color;
    protected int leg_number;

    public Animal() {

    }

    public String move() {
        return "I'm walking with " + leg_number + " legs!";
    }

}

class Cat extends Animal {
    public Cat() {
        color = "orange";
        leg_number = 4;
    }
}

class Bug extends Animal {
    public Bug() {
        color = "green";
        leg_number = 6;
    }
}

class Bird extends Animal {
    private int wing_number;

    public Bird(int wing_number) {
        color = "yellow";
        leg_number = 2;
        this.wing_number = wing_number;
    }

    //  @Override
    public String move() {
        if (this.wing_number > 0) {
            return "I'm flying";
        } else {
            return "I'm walking with " + leg_number + " legs!";
        }
    }
} 

A couple of observations of my one:

First, since this consists of a superclass and subclasses, I thought I was required to reference the superclass constructor in my subclass constructors with super(). However, I found out the code worked without it. I don't know if I was wrong about this requirement or if it was an old requirement that changed with the evolution of Java.

Second, I thought that the @Override decoration was required for the move() method in the Bird class due to the move() method in the Animal superclass. However, the code works without it. I'm thinking now that because the move() method in Bird has a parameter and the one in Animal does not, the Bird one does not constitute a true override of the Animal one.

Third, I was sure that to instantiate my different objects, the instantiation would somehow have to include the Animal superclass. When reviewing code examples on the web, I would see superclasses referenced like Animal myCat = new Cat() or, I think, Cat myCat = new Animal(), maybe one other way of referencing Animal that I don't remember. It turns out that Cat myCat = new Cat() was the only one that worked correctly. I'm not sure what the difference is between the code examples I saw on the Internet and my code, but I went with what worked.

Any comments on my observations as well as my code are welcome.