Question: Hi I have Java task : First need to be create a Superclass Animal with type, name, age, boolean sterilized, Dog and Cat subclasses. And
Hi
I have Java task :
First need to be create a Superclass Animal with type, name, age, boolean sterilized, Dog and Cat subclasses.
And then to generate a list of 100 cats and dogs with a generator below.
Next by Java stream Intermediate and Terminal method do :
-Check how many cats there are;
-Check how many of these cats are unsterilized;
- sterilize them; -Check how many dogs there are,
-Check how many of them are unsterilized;
-sterilize them;
-find the oldest cat and dog,
-Find the youngest cat and dog,
- calculate the difference in years between the oldest cat and the youngest dog
class AnimalFactory { private final Random random = new Random(); public Animal createRandomAnimal() { if (random.nextBoolean()) { return new Cat(); } else { return new Dog(); } } } class ListGenerator { List generate(Supplier supplier, int count) { return IntStream.range(0, count) .mapToObj(x -> supplier.get()) .collect(Collectors.toList()); } } public class Animals { public static void main(String[] args) { ListGenerator generator = new ListGenerator<>(); AnimalFactory factory = new AnimalFactory(); List animals = generator.generate(factory::createRandomAnimal, 50); for (Animal animal : animals) { System.out.println(animal.getClass().getSimpleName()); } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
