E1:
Answers for five questions in Question 1 of tutorial 2 a)
According to the condition that the classes: Lion, Wolf, Bear, Cat, implement the interface Animal, the method that these classes required to implement is ¡°talk()¡± method, as the ¡°talk()¡± method is the only method that declared in interface Animal.
b)
If the method makeNoise() is invoked, due to the fact that the method makeNoise() receives a List of Animal objects, therefore, the the method makeNoise() may take Lists of all the objects that implement the Animal interface that consists of Lion, Wolf, Bear and Cat.
c)
If the method outputToString() is invoked, due to the fact that the method outputToString() receives a List of type Object, that is, the method outputToString() may take objects of any type because Object is the super class of every class. Therefore, the object types can be contained within objects can be List of floats, integers, doubles, Lion, Wolf, Bear, Cat or any class.
d)
No, not each object in method outputToString() may call the method talk(). The reason is that the method talk() is defined in the interface Animal, the Object class doesn¡¯t implement the interface Animal, therefore, only the objects that belong to the interface Animal¡¯s subclasses can call the method talk().
e)
If the method addAnimal() is invoked, due to the fact that the method addAnimal() may take objects of the classes that implement interface Animal as parameters, therefore, the types that can be passed to a can be a object of the classes that implement interface Animal.
Besides, during line 4 of above code, the method makeNoise() is called, list of type Animal makes during first three lines of above code. In that list, there are a Lion object and a Wolf object. The list is iterated over,then the method talk() is called for every object that belongs to interface Animal. Due to the fact that Lion and Wolf are subclasses of interface Animal and the method talk() is overridden by class Lion and Wolf, in line 2 of above code, Lion¡¯s talk() is invoked, in line 3 of above code, Wolf¡¯s talk() is called.
E2:
Answers for Question 4 of tutorial 2:
List
The open-closed principle is violated,according to open-closed principle, if we want to change the Book class, everything in that class required to change, therefore another abstract class account should be added, and add another account type(such as superannuation account). The interface segregation principle is also violated, because in the Customer interface, it¡¯s a large general purpose interface, we should breaks it into many interfaces such as withdraw and save. On the other hand, I think the single responsibility principle is not violated, as each class is assigned a responsibility.
E3:
In class BlockedBackground, the method draw() draws the game background in the pane area; the
method update() updates the game background in the pane when the pane moves left or right in
the map area, however, the game background is the same in any area of the map, this method does
not need to do anything.
In class EntityViewImpl, the method getViewOrder() calculates view order by layer; the method
update() updates x,y,width,height and imagePath of the node according to the offset parameter and
the entity instance variables; the method matchesEntity() determines whether the entity passed in
the parameter is the same as the member variable entity; the method markForDelete() marks the
current EntityView status as deleted; the method getNode() returns the instance variable ¡®node¡¯; the
method isMarkedForDelete() gets the status of deletion
In class GameWindow, the method getScene() returns scene; the method run() starts the game,
creates the timeline and calls the method draw() every 17ms; the method draw() draws the entities
in the game: first of all, get all entities from the current level, then update the x coordinate of hero,
if the hero is in the margin area, recalculate the offset(the purpose is to make the hero not in the
margin area),
In other words, when hero is in the margin area on the right of pane, the offset
increases, causing the screen to move to the right of the map.
When hero is located in the margin
area on the left of the pane, the offset decreases, making the screen move to the left of the map.
Then update the background,
traverse every entity, If it is already in the pane, update its coordinates
relative to the pane (because the offset may have changed),If it is not in the pane, add it to the pane.
Finally, delete the entities that should not exist in the pane
(For example, after entering the next
level, all the entities added to the pane from the original level need to be deleted).
In class KeyboardInputHandler, the method handlePressed(KeyEvent keyEvent) records the keys in
the pressedKeys member. This member is used to record all currently pressed keys; If the arrow key
is currently pressed, the model.jump() method is called and the jump MediaPlayer is played;
If the
right arrow key is currently pressed, call the model.moveRight() method, the effect should be to
move the hero to the right;
If the left arrow key is currently pressed, and the right arrow key is also
pressed at the same time, the stopMoving() method of the model is called, and the effect is to stop
the movement of the hero; If the left arrow key is currently pressed, and the right arrow key is not
pressed, the moveLeft() method of the model is called, and the effect should be to move the hero to
the left. the method handleReleased(KeyEvent keyEvent) deletes the key from the pressedKeys
member,
when the keyboard button is released, the moving is determined according to the
keyboard button currently being pressed.If the left arrow key is pressed and not released, the
method moveLeft() of the model is called; if the right arrow key is pressed and not released, the
method moveRight() of the model is called;if the left arrow key and right arrow key pressed at the
same time, the method stopMoving() is called;If neither the left arrow key nor the right arrow key is
pressed, the stopMoving() method of the model is called; if the left arrow key is pressed and right
arrow key is not pressed and the left arrow key is released, the stopMoving() method of the model is
called; if the right arrow key is pressed and left arrow key is not pressed and the right arrow key is
released, the stopMoving() method of the model is called.