Вместо
if (hasFur == true) {
Просто
if (hasFur) {
В Bird не надо переопределять весь getDescription, копируя базовую логику. Ее можно использовать и сократить до одной строки @Override
String getDescription() {
return super.getDescription() + " Moreover, it has 2 wings and can fly.";
}
ошибок компиляции нет, но не могу набрать достаточно балловДа кто его знает какие там критерии. Попробуй расставить модификаторы доступа
class Animal {
private String color;
private int numberOfPaws;
private boolean hasFur;
public Animal(String color, int numberOfPaws, boolean hasFur) {
this.color = color;
this.numberOfPaws = numberOfPaws;
this.hasFur = hasFur;
}
public String getDescription() {
String paw = (numberOfPaws == 1) ? "paw" : "paws";
String fur = (hasFur) ? "a" : "no";
return "This animal is mostly " + color +
". It has " + numberOfPaws + " " + paw + " and " + fur + " fur.";
}
protected String getColor() {
return color;
}
protected int getNumberOfPaws() {
return numberOfPaws;
}
protected boolean hasFur() {
return hasFur;
}
}
class Dog extends Animal {
public Dog() {
super("brown", 4, true);
}
}
class Bird extends Animal {
public Bird() {
super("blue", 2, false);
}
@Override
public String getDescription() {
String paw = (getNumberOfPaws() == 1) ? "paw" : "paws";
String fur = (hasFur()) ? "a" : "no";
return "This animal is mostly " + getColor() +
". It has " + getNumberOfPaws() + " " + paw + " and " + fur +
" fur. Moreover, it has 2 wings and can fly.";
}
}
public class A1 {
public static void main(String[] args) {
Dog dog = new Dog();
Bird bird = new Bird();
System.out.println("Dog description:");
System.out.println(dog.getDescription());
System.out.println();
System.out.println("Bird description:");
System.out.println(bird.getDescription());
}
}
Кажется, все сделал согласно заданию, output такой, как надо, ошибок компиляции нет, но не могу набрать достаточно баллов...
Задание такое:
Task 1
Work with class Animal.
Provide it with 3 private fields - color(String), numberOfPaws(int), hasFur(boolean).
Add constructor with full parameters. Save the parameter order and names as it is listed in a second paragraph.
Add a methods getDescription(), witch would use class fields and return a string with such pattern "This animal is mostly (color). It has (numberOfPaws) paws and ('a'/'no' -> depends on value of hasFur) fur."
(Optional) In the method getDescription() change the word 'paw' depending on the numberOfPaws: number of paws is 1 -> 'paw', number of paws is different from 1 -> 'paws'.
Task 2
Work with classes Dog and Bird. Extend them with the help of Animal.
Create no-args constructor for each where provide all necessary information for Animal constructor by super() method:
for Dog: color - brown, numberOfPaws - 4, hasFur - true;
for Bird: color - blue, numberOfPaws - 2, hasFur - false.
Override getDescription() method for class Bird: add one more sentence to the description. The result must be "This animal is mostly blue. It has 2 paws and no fur. Moreover, it has 2 wings and can fly."
(Optional) Create an object of each class and call getDescription() method for both of them. Try to explain the output results.
Вот мой код:
public class Animal {
private String color;
private int numberOfPaws;
private boolean hasFur;
Animal(String color, int numberOfPaws, boolean hasFur) {
this.color = color;
this.numberOfPaws = numberOfPaws;
this.hasFur = hasFur;
}
String getDescription() {
String paw;
if(numberOfPaws == 1) {
paw = "paw";
} else {
paw = "paws";
}
String fur;
if(hasFur == true) {
fur = "a";
} else {
fur = "no";
}
return "This animal is mostly " + color +
". It has " + numberOfPaws + " " + paw + " and " + fur + " fur.";
}
public String getColor() {
return color;
}
public int getNumberOfPaws() {
return numberOfPaws;
}
public boolean getHasFur() {
return hasFur;
}
}
class Dog extends Animal {
Dog() {
super("brown", 4, true);
}
}
class Bird extends Animal {
Bird() {
super("blue", 2, false);
}
@Override
String getDescription() {
String paw;
if(getNumberOfPaws() == 1) {
paw = "paw";
} else {
paw = "paws";
}
String fur;
if(getHasFur() == true) {
fur = "a";
} else {
fur = "no";
}
return "This animal is mostly " + getColor() +
". It has " + getNumberOfPaws() + " " + paw + " and " + fur + " fur. Moreover, it has 2 wings and can fly.";
}
}