CSE 1302C Midterm_SP13.docx

advertisement
CSE 1302C Midterm
2/18/2013
Directions: You have 90 minutes to complete this exam. All answers are to be your own, without the
assistance of others. If you have a question, please raise your hand and the instructor will be with you shortly.
Question 1 (OOD – 25 pts): Imagine we are creating a game about a hero who must avoid touching a wide
variety of enemies. All of the enemies in this game can move and have a position, an amount of damage they
deal, and whether or not they can teleport. However, each kind of enemy moves differently, and some have
additional abilities beyond simply moving. Design a class hierarchy (i.e. a UML diagram like we did in class) of
at least two different kinds of enemies (of your choosing) that demonstrates all of these concepts.
Based on your hierarchy above:

(10 pts) Give a quick definition of “inheritance”, providing an example from this hierarchy (no code).

(10 pts) Using the hierarchy, explain what it means to “override” a method as well as when you
should do it.

(15 pts) Explain the term “polymorphism” as if to a programmer who isn’t familiar with the concept
using only words (i.e. no code). Then, give an example in code using the hierarchy from above. Hint:
the code should have 2 distinct parts to it.
Question 2 (Lists - 15 pts): Create a List of Enemies and then add 25 enemies to that List. All of the enemies
can be of the same type, if that helps. Then, tell each enemy in the List to move.
Question 3 (2D Arrays – 25 pts): We’ve talked about 2D arrays for storing 2D terrains – like in The Legend of
Zelda. To refresh your memory, we can store numbers in a 16x10 2D array – where the numbers represent
the terrain type (e.g. “sand” == 1, “rock”==2, “tree”==3, “grave”==4, etc…). Imagine we want to make
sure that a “grave” piece of terrain (#4) does not lie directly next to a “rock” piece of terrain (#2), like those
with the arrows pointing to them below; if a grave is next to a rock, the terrain is not “allowable”. Write a
function with the prototype (below) that determines whether or not a 2D array of ints violates this rule.
public boolean isGoodTerrain (int[,] terrainArray, int width, int height);
Hints: 1) if you get rows vs. columns confused, I don’t care; you can assume the width and height are the
same, if it helps. 2) it’s not necessary to check any terrain at the border of the room. 3) I don’t care about the
diagonals such as “NorthEast”; just check “North, South, East, West” for each terrain piece. Final hints: how
do you know if a terrain piece is a grave, and how would you access the terrain piece to the north of
terrainArray[x,y]?
(“illegal” terrain pieces)
Download