Monday, 6 December 2010

Chapter 10 Test

6. Now run your simulation. How does the introduction of gender impact the changing populations? Make a quantitative comparison of V2 and V3.


Here it is possible to see the differences between breeding rates in v2 and v3 quantitatively:

These screenshots are the numbers of rabbits after 200 steps (male vs. female)

v2:


v3:


7. Given what you have learned from V3, describe one way of extending the simulation in "V4". What hypothesis will you be testing with V4?
     For V4 it is possible to implement a class Grass off of which the rabbit survives. Just as the fox class searches for rabbits, rabbits should, too, search for herbs and food, as without food they will not be able to live, and assuming that they don't need to eat as we do right now is not correct.
     Another possible extension of such a simulation would be to make the foxes move more steps if they wish, because in reality the rabbits have much less mobility when compared with the foxes who can easily catch up to them. Therefore it is unreasonable to assume that they both move at the same right, and changing that could help our simulations validity.

Sunday, 5 December 2010

Chapter 10 36-49


36
Using your latest version of the project (or the foxes-and-rabbits-V2 project in case you have not done all the exercises), move the canBreed() method from Fox and Rabbit to Animal and rewrite it as shown in code 10.8. Provide appropriate versions of getBreedingAge() in Fox and Rabbit. Are those changes sufficient to recompile the project? If not, what is missing from the Animal class?ced any new participant types.
 Done, I had to also much an abstract method which accesses the age variable in the subclasses as it is also not defined in the Animal class.
37
Move the incrementAge() method from Fox and Rabbit to Animal by providing an abstract getmaxAge() method in Animal and a concrete version in Fox and Rabbit.
 I now realized that I could move the age field into the superclass, and did so. I also had to change the incrementAge() method from being private to protected because the subclassese need access to it.
38
Can the breed() method be moved to Animal?

If so, make this change.
>
 Yes it is possible, here is some of the code, to show how I did it:

39
In the light of all the changes you have made to these three classes, reconsider the visibility of each method and make any changes you feel are appropriate.
 As I stated earlier, I already recognized the need for these methods to either be public or protected, and I made them protected as it seemed they weren't supposed to be public earlier.
40
Was is possible to make these changes without having any impact on any other classes in the project? If so, what does this suggest about the degrees of encapsulation and coupling that were present in the original version?
 Well, at times these changes could have had an indirect impact, but generally it is the same exact project, with a more sophisticated structure. This suggests that the degrees of encapsulation and coupling were large, and therefore each class or hierarchy of classes didn't necessarily affect the other classes.
41
Define a completely new type of animal for the simulation as a subclass of Animal. You will need to decide what sort of impact its existence will have on the existing animal types. For instance, your animal might compete with foxes as a predator on the rabbit population, or your animal might prey on foxes but not on rabbits. You will probably find that you need to experiment quite a lot with the configuration settings you use for it. You will need to modify the populate() method to have some of your animals created at the start of a simulation. You should also define a new color for your new animal class. You can find a list of pre-defined color names on the API page documenting the Color class in the java.awt package.
 Done. I defined a made up creature that I named "KillerMouse" which is a super animal. It breeds super-fast, but has a very late breeding age. It lives basically for eternity (2000 years). It also feeds off of foxes and rabbits. I was truly messing around, but It's really an interesting situation because they breed so late. Once they actually breed, however, it's like a plague.
42
Introduce the Actor class into your simulation. Rewrite the simulateOneStep() method in Simulator to use Actor instead of Animal. You can do this even if you have not introduced any new participant types.

Does the Simulator class compile? Or is there something else that is needed in the Actor class?
 I'm having serious compiling issues with this. After creating the actor class I tried changing the simulateOneStep() method, and now I might have permanently damaged the project....
43
Redefine the abstract class Actor in your project as an interface.

Does the simulation still compile?

Does it run?
 My guess is that it would work had I not had these compiling issues.
44
Are the fields in the following interface static fields or instance fields?

public interface Quiz
{
int CORRECT = 1;
int INCORRECT = 0;
...
}

What visibility do they have?
 The fields in the interface have a public, static, and final visibility. But they aren't instance fields, as this class is an interface and not a normal class, and therefore they aren't directly the fields of an instance or object.
45
What are the errors in the following interface?

public interface Monitor
{
private static final int THRESHOLD = 50;
public Monitor (int initial);
public int getThreshold()
{
return THRESHOLD;
}
...
}
 Firstly, an interface cannot use a private field, so the visibility for it must be changed, also the user shouldn't write the visibility as it is implied, and rather it won't compile with them, and lastly there shouldn't be a method body in getThreshold() as it is an abstract method.
46
Add a non-animal actor to the simulation. For instance, you could introduce a Hunter class with the following properties.

Hunters have no maximum age and neither feed nor breed. At each step in the simulation, a hunter moves to a random location anywhere in the field and fires a fixed number of shots into random target locations around the field. Any animal in one of the target locations is killed.

Place just a small number of hunters in the field at the start of the simulation. Do the hunters remain in the simulation throughout or do they ever disappear?

If they do disappear, why might that be, and does that represent realistic behavior?

What other classes required changing as a result of introducting hunters?

Is there a need to introduce further decoupling to the classes?
 Done, I made the hunter act like a fox when it evokes its act() method, in that it uses the same procedure to check, except I made the movement random.

The hunters remain in the simulation because I put no age limit or restriction with food level onto them.

I believe there is no longer a need to introduce further decoupling, this is far extended, in fact I believe some methods we used are simply redundant.
47
Which methods do ArrayList and LinkedList have that are not defined in the List interface?

Why do you think that these methods are not included in List?
  getFirst(), getLast(), removeRange(), and trimToSize() are methods not defined in the List interface. This is probably because they don't necessarily apply to other possible implementations of the List class, or maybe because they simply apply to ArrayList and LinkedList, while they don't necessarily apply to List.
48
Read the API description for the sort methods of the Collections class in the java.util package.

Which interfaces are mentioned in the descriptions?
 It mentions implementing the Comparable interface.
49
Investigate the Comparator interface.
Define a simple class that implements Comparator.
Create a collection containing objects of this class and sort the collection.
 Done, this was a fairly simple task, I just made use of subtraction with two integers x and y, and later checked if they are equal to another integer z.

Wednesday, 1 December 2010

Chapter 10 20-35


20
Identify the similarities and differences between the Fox and the Rabbit classes.
Make separate lists of the fields, methods and constructors, and distinguish between the class variables (static fields) and instance variables.

 They each have the isAlive boolean field, the age integer field, a location field, a breeding age, a max age, a breeding probability, and a MAX_LITTER_SIZE field, which is the same name, but used differently in each class. Their constructors are primarily the same, except the fox class has the food level component in its constructor. As for methods, they have many in common such as IsDead(), giveBirth(), setDead(), incrementAge(), breed(), canBreed(), setLocation(), and getLocation(). Their run() and hunt() methods are similar in the sense that they move the instance, but are different in their purpose.
21
Candidate methods to be placed in a superclass are those that are identical in all subclasses.
Which methods are truly identical in the Fox and Rabbit classes?
In reaching a conclusion, you might like to consider the effect of substituting the values of class variables into the bodies of the methods that use them.

 IsDead(), giveBirth(), setDead(), incrementAge(), breed(), canBreed(), setLocation(), and getLocation(). Those methods are absolutely identical as has been asked.
22
In the current version of the simulation, the values of all similarly named class variables are different.
If the two values of a particular class variable (BREEDING_AGE, say) were identical, would it make any difference to your assessment of which methods are identical?

 No because the class variable would still be the same. It would still serve the same purpose, and would have the same functionality, it will only produce different results with different actual parameters.
23
What sort of regression-testing strategey could you establish before undertaking the process of refactoring on the simulation?
Is this something that you could conveniently automate?

 What is regression testing?
24
Create the Animal superclass in your version of the project. Make the changes dicussed in the text. Ensure that the simulation works in a similar manner as before.
 I'm constantly getting this error, I've been working on fixing it for a while, but I don't seem to see why it doesn't work:

25
How has using inheritance improved the project so far?
Discuss.

 There was no need for the code duplication that we had in the classes of Rabbit and Fox, and as a result the project is now more simplified, easier to use, and takes up less space.
26
Although the body of the loop in Code 10.6 no longer deals with the Fox and Rabbit types, it still deals with the Animal type.
Why is it not possible for it to treat each object in the collection simply using the Object type?
 If it were to treat each object in the collection using the Object type, theoretically, it could place anything into the collection, as almost everything is a sub-class of the Object class (except for primitive types). As a result we could potentially get unexpected and unwanted results.
27
Is it necessary for a class with one or more abstract methods to be defined as abstract?

If you are not sure, experiment with the source of the Animal class in the foxes-and-rabbits-v2 project.
 No, having an abstract method simply means that it would be excluded from any instance of that type, and that it would have to be overridden by one of the classes sub-classes if it has any.
28
Is it possible for a class that has no abstract methods to be defined as abstract?

If you are not sure, change act() to be a concrete method in the Animal class by giving it a method body with no statements.
 Yes it is. Being abstract would mean that no instance could be made from the given class; however, since it would have no abstract methods no method would have to necessarily be overridden in its sub-classes. This also means that it would be a super-class for one or more sub-classes, as creating an abstract class at the end of the hierarchy would be wasted code, and effort, since no instance of or relating to the class could be created.
29
Could it ever make sense to define a class as abstract if it has no abstract methods? Discuss this.
 Yes it could, this is largely discussed above, but basically if a user simply doesn't want any objects being created from that class he would define it as abstract. This wouldn't necessarily mean that any methods would have to overridden as they would if the class had abstract methods.
30


31


32
Which of the other simulation classes do not need to be aware of whether they are specifically dealing with foxes or rabbits? Could they be rewritten to use the Animal class instead? Would there be any particular benefits in doing this?/P>
 The field and location classes can use the abstract class of Animal which would benefit the project by having less code duplication.
33
Review the overriding rules for methods and fields discussed in Chapter 9. Why are they particularly significant in our attempts to introduce inheritance into this application?
 They are significant because with the new idea of having abstract methods it is important to know how to override methods, because otherwise the subclasses of the abstract method's class will also unintentionally be made into abstract classes.
34
The changes made in this text section have removed the dependence (couplings) of the simulateOneStep() method to the Fox and Rabbit class. The Simulator class however, is still coupled to Fox and Rabbit, because these classes are referenced in the populate() method. There is no way to avoid this: when we create Animal instances, we have to specify exactly what kind of animal to create. This could be improved by splitting the Simulator into two classes: one class Simulator, which runs the simulation and is completely decoupled from the concrete animal classes, and one class, PopulationGenerator (created and called by the simulator), which create the population. Only this class is coupled to the concrete animal classes, making it easier for a maintenance programmer to find places where change is necessary when the application is extended. Try implementing this refactoring step. The PopulationGenerator class should also define the colors for each type of animal.>
 Done, however, I can't see the purpose of this:
35
The canBreed() method of Fox and Rabbit are textually identical, yet we chose not to move them to the Animal class. Why is this? Try moving the methods from Fox and Rabbit and making them protected. Is there any way to make the resulting classes compile and, even if there is, does the resulting simulation work as it should? How can you tell?
 No, because each class has its own value for the BreedingAge, field and moving it up would require one to be set for the Animal class even though the fox and rabbit breeding ages are not the same. Since there is no way to have a universal BreedingAge, since that would defeat the purpose of the simulation, it is safe to assume that the canBreed() method cannot be moved up in the hierarchy.