CSCI 3155: Object-Oriented Programming II Exercise Sheet #13, Assignments of Programming Languages

An exercise sheet for csci 3155: principles of programming languages, focusing on object-oriented programming ii. Instructions for various programming exercises, such as implementing a text adventure game, using java or c++ and eclipse or axon.cs.colorado.edu. The exercises cover topics like inheritance, rooms and items, and implementing methods like go, status, and use.

Typology: Assignments

Pre 2010

Uploaded on 02/13/2009

koofers-user-yvo
koofers-user-yvo 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI 3155: Principles of Programming Languages
Exercise sheet #13
2007
Group name:
Object-Oriented Programming II
This exercise is largely a lab exercise.
You may use Eclipse (which is preinstalled on our machines) or EMACS/vim
on axon.cs.colorado.edu for the programming exercises. Submit the resulting
programs via e-mail (to [email protected].colorado.edu) or leave them in your
home directory on axon, indicating (on the exercise sheet) where you stored
them.
Exercise 1.Java’s instanceof feature allows us to inspect the dynamic type of
an object. Specifically, ainstanceof Ais true iff a:Tand T <:A.
(a) Give an example where such a facility might be useful. You may find
it helpful to consider instanceof in the proximity of explicit narrowing
conversions.
(b) Assume that you only want to test instanceof for one or two classes in
your program (though possibly for many objects). Further assume that
all of the classes you care about are (direct or indirect) subclasses of C.
How could you extend Cto achieve an effect similar to instanceof, albeit
limited to only one or two types?
1
pf3
pf4

Partial preview of the text

Download CSCI 3155: Object-Oriented Programming II Exercise Sheet #13 and more Assignments Programming Languages in PDF only on Docsity!

CSCI 3155: Principles of Programming Languages

Exercise sheet

Group name:

Object-Oriented Programming II

This exercise is largely a lab exercise. You may use Eclipse (which is preinstalled on our machines) or EMACS/vim on axon.cs.colorado.edu for the programming exercises. Submit the resulting programs via e-mail (to [email protected]) or leave them in your home directory on axon, indicating (on the exercise sheet) where you stored them.

Exercise 1. Java’s instanceof feature allows us to inspect the dynamic type of an object. Specifically, a instanceof A is true iff a : T and T <: A.

(a) Give an example where such a facility might be useful. You may find it helpful to consider instanceof in the proximity of explicit narrowing conversions. (b) Assume that you only want to test instanceof for one or two classes in your program (though possibly for many objects). Further assume that all of the classes you care about are (direct or indirect) subclasses of C. How could you extend C to achieve an effect similar to instanceof, albeit limited to only one or two types?

Exercise 2. Object-oriented programming has many practical uses. However, one of its most pleasant attributes is that it maps neatly to many aspects of the “physical world”. Illustrate your prowess in Object-Oriented Programming by implementing a text adventure game^1. The instructor has provided you with the parser part (both as C++ and as Java code), so you can focus on the game content. You may implement the system either in C++ or in Java. If in doubt, use Java. The following contains a step-by-step instruction to implement the game. Be advised that the steps become less and less concrete and require you to make more decisions by yourself towards the end.

(a) Set up your development environment to use the skeleton program for the language you picked. Make sure that it compiles. Use the command “quit” to exit. (b) Examine the source code. You will find a class AdvParser, which parses the input and calls various handler methods (look, go,... ). Create a subclass Game of AdvParser and override the method status to print your own status/welcome message. Make sure to adjust the class Adv to use your newly overridden Game class. (c) We will populate our game world with rooms and items. We want to be able to (i) look at all three of these (this should print a message describing the object), and (ii) to get the name of the object. Implement an appropriate inheritance hierarchy that ensures that all three have a common supertype that allows us to look at them. Make sure that you use an appropriate “concept” to implement the common supertype and its subtypes. (d) Rooms should have four exits, to the north, south, east, and west. Im- plement facilities to set the exits (pointing to other rooms). Implement a single room StartRoom with no exits, and give the room some description. Make sure that the Game object has a reference to the current room (i.e., the StartRoom) and prints the room’s name after every command. Change the implementation of look to print the room’s description when- ever the string parameter is null(Java)/NULL(C++). (e) Implement a second room of your own design and attach it to the Start- Room. Make sure that the exits of the two rooms match up. Try to ensure that your implementation enforces that the exits match up. Now, implement the method go in class Game that allows you to move around in rooms. Make sure that you can move around among the rooms. Implement this method with a helper method adjacent in Room that tells you what room there is in a given direction. (^1) Or “interactive fiction”, as some people prefer to call it.

interactively ask the user for something that it should affect. The user should input a name, leading to an appropriate effect:

i) When applied to the portal room, the wand should connect the portal room to the goal room. ii) When applied to the wand, the wand should make itself disappear (making the game potentially unwinnable). iii) Feel free to add further effects. In particular, ensure that it is easy to add further effects.

Since this is a “magic wand”, you don’t need to be near the object you affect. Initialise the wand with an array of all the things it can affect. Do not use narrowing conversions. Do not add any new methods (abstract or concrete) to unrelated objects (such as Item).