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.

No comments:

Post a Comment