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!
1 Answer 1
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.
- 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.)
- 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.).
- Cats, Bugs and wingless Birds all move in the same way, but Birds with wings move differently. (Keyword: Polymorphism)
- 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.
-
\$\begingroup\$ +1 According to the description of the homework tag, this is how you are approach such questions. \$\endgroup\$mindoverflow– mindoverflow2021年10月26日 07:56:40 +00:00Commented Oct 26, 2021 at 7:56
-
\$\begingroup\$ Agree with your answer without code for the OP's homework. \$\endgroup\$dariosicily– dariosicily2021年10月26日 13:54:12 +00:00Commented Oct 26, 2021 at 13:54
Explore related questions
See similar questions with these tags.
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\$