Let's get started with the Animal class
public class Animal {
private String name;
private int speed;
private boolean flyAble;
public Animal() {
}
public String getName() {
return name;
}
public int getSpeed() {
return speed;
}
public boolean isFlyAble() {
return flyAble;
}
@Override
public String toString() {
return name + " {" +
"speed= " + speed +
", flyAble= " + flyAble +
"}"';
}
}
The Animal class represents an animal with properties such as name, speed, and flyAble. It provides methods to access these properties and overrides the toString() method to generate a readable representation of an Animal object.
Then, create a Controller
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AnimalController {
public void winnerSpeedWithoutWings(List<Animal> animalList){
Map<Integer,String> mapWinnerList = new HashMap<>();
for (Animal animal : animalList) {
if(!animal.isFlyAble()){
mapWinnerList.put(animal.getSpeed(), animal.getName());
}
}
int maxSpeed = Collections.max(mapWinnerList.keySet());
for (Map.Entry<Integer, String> entry : mapWinnerList.entrySet()) {
if(entry.getKey() == maxSpeed){
System.out.printf("===> Winner is %s, with speed: %d\n", entry.getValue(), entry.getKey());
}
}
}
}
The AnimalController class contains a method winnerSpeedWithoutWings to determine the fastest non-flying animal among a list of animals. It utilizes a HashMap to store the speed and name of non-flying animals, finds the maximum speed using Collections.max(), and then prints the winner(s).
Now, let's run the example:
import java.security.SecureRandom;
import java.util.Arrays;
public class TestAnimal {
public static void main(String[] args) {
Animal horse = new Animal("Horse", new SecureRandom().nextInt(100), false);
System.out.println(horse.toString());
Animal dog = new Animal("Dog", new SecureRandom().nextInt(50), false);
System.out.println(dog.toString());
Animal eagle = new Animal("Eagle", new SecureRandom().nextInt(100), true);
System.out.println(eagle.toString());
Animal duck = new Animal("Duck", new SecureRandom().nextInt(50), true);
System.out.println(duck.toString());
Animal lion = new Animal("Lion", new SecureRandom().nextInt(100), false);
System.out.println(lion.toString());
AnimalController animalController = new AnimalController();
animalController.winnerSpeedWithoutWings(Arrays.asList(horse, dog, eagle, duck, lion));
}
}
The TestAnimal class contains the main method to run the race simulation. It creates instances of various animals and passes them to the AnimalController to determine the winner among non-flying animals.
Conclusion
Advantages:
Clarity and Readability: The code is well-structured and easy to understand, making it accessible even to those with limited programming experience. Clear variable names and comments enhance its readability.
Modular Design: By dividing functionality into separate classes (Animal and AnimalController), the code follows the principles of modularity and encapsulation, promoting maintainability and reusability.
Effective Use of Collections: The use of a HashMap to store animal speeds and names facilitates efficient retrieval of data and enables straightforward determination of the fastest non-flying animal.
Limitations:
Static Test Data: The hardcoded creation of animal instances in the TestAnimal class limits the code's flexibility and scalability. In a practical application, data should ideally be sourced dynamically from external sources or user input.
Limited Extensibility: While the current codebase caters to races among animals with predefined properties, extending it to accommodate additional attributes or race conditions may require significant modifications, potentially impacting code maintainability.
Comments