Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

7 Generalization Generalization is a very powerful and extremely overused technique. In books, you will find many guidelines on when and how to use it,

7 Generalization

Generalization is a very powerful and extremely overused technique. In books, you will find many guidelines on when and how to use it, often contradictory. In this course I adopt a strictly object oriented approach to it. I am not condemning other approaches, just not using them here. In my not-so-humble opinion, one uses generalization when: There is a class with clearly defined objects (hey, if not, it would be a bad class!) There are other objects who you would definitely say that they are the same and something more o They could be the same, but with more specialized behaviour o Or they could have some more properties o Or they could know more objects to use in certain ways So, if I have object A, and object B can always be used in its place, and object B can even be used in more places, the class of B would be a subclass of A. This is called the Liskov Substitution Principle. Example: I have an object of class Submarine. I also have an object of class Ship. This ship can move, get fuel, sink, and get cargo. The submarine can do all of this, and also dive and emerge. You would say a submarine is a ship. Hence, Submarine (watch the capital!) is a subclass of Ship. A helicopter can fly, hoover, get fuel. A plane can do so as well, except for the hovering. Hence, Helicopter is a subclass of Plane. Following are exercises to reinforce this concept and see how it works out in Java programming.

7.1 Planting the first class Create a class Plant. Plant has attributes color (Color), timeSinceDying (int), commonName (String), calories and weight (both double), dead and spoiled (both Boolean.) Calories indicates how many calories eating the plant would yield. Create accessor methods (getters and setters) for the attributes. For dead and spoiled, do not create regular set methods. Instead, there is a method die() setting the dead attribute to true, and a spoil() method setting spoiled to true and calories to the negative of what it was before.

The reasoning behind this is: 1) once a plant died, it cannot be resurrected, and 2) eating spoiled food will cost you energy instead of feeding you. Implement a toString() method to make testing easier and a method equals(Object obj) returning true if the common names of the plants are equal. Draw the Class Diagram for the application you now have.

7.2 Enter the beast Create a class Animal. Animal has attributes currentFeedingStatus and dailyCaloriesNeeded (both ints), commonName (String), calories and weight (both double), dead and spoiled (both Boolean.) Calories indicates how many calories eating the animal would yield. currentFeedingStatus gives the amount of calories the animal ate today.

Create accessor methods (getters and setters) for the attributes. For dead and spoiled, do not create regular set methods. Instead, there is a method die() setting the dead attribute to true, and a spoil() method setting spoiled to true and calories to the negative of what it was before.

The reasoning behind this is: 1) once an animal died, it cannot be resurrected, and 2) eating spoiled food will cost you energy instead of feeding you. Implement a toString() method to make testing easier and a method equals(Object obj) returning true if the common names of the animals are equal.

7.3 Sensing the common theme There seems to be some overlap between Plant and Animal. Looking more closely, their behaviour is almost the same, except for plants having a color and a timeSinceDying, and animals having dailyCaloriesNeeded and currentFeedingStatus. The common theme is that they are both organisms.

Create a new class Organism. Move the attributes and methods they have the same to this new class. Then add extends Organism to the code for Plant and Animal. Most of the moving is straightforward. But what about the equals() method? Can you compare plants to animals? I would say you could; we defined the plants to be the same if the common name is equal, and we did the same for animals. Reasoning transitively, an organism is the same as another one if their common names are equal. Create the method this way. Expand the Class Diagram you made in exercise 7.1.

7.4 Sunny beaches for the plant Plants are organisms, but they can do things not all organisms can. Add a method void photosynthesis(int hoursOfSunlight). The comment in the sample code is:

// Plants can do photosynthesis, but only if the are green

// In that case, they will grow and give more calories

// Amount of this is depending on the weight itself Implement this method.

Make sure a dead plant cannot do any photosynthesis!

7.5 Feed the kitten (and other animals) Animals do not do photosynthesis, but they do eat.

Create a method eat. This method has one parameter, which is the thing being eaten. The method adds the calories gained by this to the 40 attribute keeping track of the feeding status, and prints a message to the console if the food was spoiled. After eating, check whether the animal has had its fill for the day. If so, print a message to the console and return true; if not, return false.

7.6 Taking a nap Animals also sleep occasionally. When sleeping, the feeding status goes to zero, and its weight increases. Implement this method.

7.7 Lazy bunch of animals It is time to write the first test. Create a class called LazyAnimals for this purpose. In it, have four animals (fox, rabbit, and sparrows?) and 150 organisms (dandelion, but also rabbits to eat.)

Create a loop which will have all animals eat, sleep when they ate enough, repeating until all plants have been eaten. Now expand the UML Class Diagram to show the current application. Hint: you will now also have a dependency in the diagram.

7.8 Time flies Organisms will die after some time. Define a method timeTick() in Organism, which will have it die 10% of the time it is being called. Find a place to make a plant spoil sometime after it died as well; animals do not spoil.

7.9 There are no Organisms .. There are plants, or animals, but I have never seen an organism as such.

Change the class Organism to reflect this. All organisms do grow, but how they grow, strongly depends on the type of organism.

Define a method grow in Organism, but do not implement it. Force the subclasses to implement it. Implement the grow method in Animal and in Plant. Plants grow by calling photosynthesis().

Change the method photosynthesis() to add weight as well. After calling the method, call timeTick(). Animals grow by converting the calories eaten to more weight. Add calling timeTick() to the sleep method of Animal. Adapt your testing class when necessary and run it again.

7.10 Animals are more intelligent than plants Animals will know what they can eat and what not.

Add another attribute to the class Animal called edibleFood; it is an ArrayList of type Organism.

Add the following methods as well:

public void addFood(Organism food) {

// Check to make sure the food is not added twice.

// There should be a more decent way to do this;

// shouldn't the list of edible foods be able to refuse duplicates?

// Postponed until we cover the Collections Framework

if (!edibleFood.contains(food)) { edibleFood.add(food); }

}

public boolean isEdible(Organism food) {

return edibleFood.contains(food);

}

public void removeFood(Organism food) {

edibleFood.remove(food);

}

Now change the method eat() in Animal to check whether the food is considered edible by the Animal. If not, he will not eat it. Copy the LazyAnimals class to SmartAnimals and adapt it to test the new situation.

7.11 Do not ever call him monkey A Primate is an Animal behaving like regular animals, except it will not eat spoiled food.

Create a class Primate, implement the necessary code, and test it.

7.12 Enter the Mad Scientist A Virus is nor an Animal nor a Plant. It is an Organism though. However, viruses do not grow; instead, they reproduce, giving a new virus as the result. A Virus has an attribute host of type Organism.

Create a new class Virus, extending another class (pick the correct one.) Change the grow() methods to return an Organism; for Animals and Plants, return the object itself. For a virus, return a copy of the virus instead. When creating a virus, the host is one of the parameters. It cannot be another virus; in that case, create a dinosaur as the host instead. Tinkering with viruses is dangerous, so we will not allow that. Make sure no one can override the behaviour of the class Virus. Draw the UML Class Diagram of the final application

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Effective Delivery Effective

Answered: 1 week ago