Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hi I have problem with Java task Need to modify below code becouse breaks the rules of encapsulation. It cannot be that I have direct
Hi
I have problem with Java task
Need to modify below code becouse breaks the rules of encapsulation. It cannot be that I have direct access to the fields of Animal Class. I cant refer to e.g. c.sterilized.
I am asking for help in modifying the code so that the rules of encapsulation are preserved and that the code will work properly after modification
public class Animal { public String type; public String name; public int age; public boolean sterilized; }
import java.util.Random; public class Dog extends Animal { public Dog() { Random r = new Random(); /* a Dog's life span is 20 years, we generate a random number between 0 and 20 */ age = (Math.abs(r.nextInt())) % 20; /* generating random 0 or 1 and assigning false to 0 and true to 1 */ sterilized = ((Math.abs(r.nextInt()) % 2) == 0) ? false : true; } }
import java.util.Random; public class Cat extends Animal { public Cat() { Random r = new Random(); /* a Cats life span is 20 years, we generate a random number between 0 and 20 */ age = (Math.abs(r.nextInt())) % 20; /* generating random 0 or 1 and assigning false to 0 and true to 1 */ sterilized = ((Math.abs(r.nextInt()) % 2) == 0) ? false : true; } }
import java.util.Comparator; import java.util.List; import java.util.Optional; import java.util.Random; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.IntStream; class AnimalFactory { private final Random random = new Random(); public Animal createRandomAnimal() { if (random.nextBoolean()) { return new Cat(); } else { return new Dog(); } } } class ListGenerator { Listgenerate(Supplier supplier, int count) { return IntStream.range(0, count) .mapToObj(x -> supplier.get()) .collect(Collectors.toList()); } } class MyComparator implements Comparator { @Override public int compare(Animal o1, Animal o2) { // TODO Auto-generated method stub if(o1.age < o2.age) return -1; else if(o1.age > o2.age) return 1; else return 0; } } 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()); } /* Question 1 : how many cats are there * from the stream of Animal objects we filter out those objects where the object is an instance of Cat class */ long count = animals.stream() .filter(c -> (c instanceof Cat)) .count(); System.out.println("Number of Cats : " + count); /* Question 2 : Check how many of these cats are unsterilized * from the stream of objects we filter out cats and check the sterilized boolean variable to be false */ count = animals.stream() .filter(c -> c instanceof Cat && !c.sterilized) .count(); System.out.println("Number of Cats Not Sterilized : " + count); /* Question 3 : sterilize all the unsterilzed Cats */ System.out.println("Sterilizing all the unsterilzed Cats ..."); /* after filtering all the cats who are not sterilized we assign true to all Cat objects sterilized variable */ animals.stream().filter(c -> c instanceof Cat && !c.sterilized) .forEach(c -> c.sterilized = true); count = animals.stream() .filter(c -> c instanceof Cat && !c.sterilized) .count(); /* should be 0 */ System.out.println("Number of Cats Not Sterilized : " + count); /*Question 4 : how many dogs are there * from the stream of Animal objects we filter out those objects where the object is an instance of Dog class */ count = animals.stream() .filter(c -> (c instanceof Dog)) .count(); System.out.println("Number of Dogs : " + count); /* Question 5 : Check how many of these dogs are unsterilized * from the stream of objects we filter out dogs and check the sterilized boolean variable to be false */ count = animals.stream() .filter(c -> c instanceof Dog && !c.sterilized) .count(); System.out.println("Number of Dogs Not Sterilized : " + count); /* Question 6 : sterilize all the unsterilzed Cats */ System.out.println("Sterilizing all the unsterilzed Dogs ..."); /* after filtering all the dogs who are not sterilized we assign true to all Dog objects sterilized variable */ animals.stream().filter(c -> c instanceof Dog && !c.sterilized) .forEach(c -> c.sterilized = true); count = animals.stream() .filter(c -> c instanceof Dog && !c.sterilized) .count(); /* should be 0 */ System.out.println("Number of Dogs Not Sterilized : " + count); /* Question 7 : find the oldest Cat and Dog */ /* create a new MyComparator class object that compares the ages of two Animals */ Optional oldestCat = animals.stream().filter(c-> c instanceof Cat).max(new MyComparator()); System.out.println("Oldest Cat Age : " + oldestCat.get().age); Optional oldestDog = animals.stream().filter(c-> c instanceof Dog).max(new MyComparator()); System.out.println("Oldest Dog Age : " + oldestDog.get().age); /* Question 8 : find the youngest Cat and Dog */ /* create a new MyComparator class object that compares the ages of two Animals */ Optional youngestCat = animals.stream().filter(c-> c instanceof Cat).min(new MyComparator()); System.out.println("Youngest Cat Age : " + youngestCat.get().age); Optional youngestDog = animals.stream().filter(c-> c instanceof Dog).min(new MyComparator()); System.out.println("Youngest Dog Age : " + youngestDog.get().age); /* Question 9 : difference between oldestCat and Youngest Dog */ System.out.println("Difference between Oldest Cat and Youngest Dog : " + (oldestCat.get().age - youngestDog.get().age)); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started