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.
- Build the classes
Animal
,Cat
, andBug
.- Define the properties
color
andleg_number
on the relevant and necessary classes. Have them be initialized within a constructor.- Add the functionality that would allow us to call a method
move
with theCat
andBug
classes. Have the method return a string"I'm moving with " + leg_number + " legs!"
, with theleg_number
beingleg_number
as set on the class.- Add a new class called
Bird
. Add the propertywing_number
. Add the functionality that would allow us to call a methodmove
with theBird
class. Have the method return the string"I'm moving with " + leg_number + " legs!"
ifwing_number
doesn't have an applicable value. Ifwing_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!
move
method the way the problem requires. Can you clarify the requirements, or update the code to state the correct requirements? \$\endgroup\$move()
seems poorly written to me and one could argue that this fulfills it. \$\endgroup\$