Assignment 4: Abstracting over Data Definitions; Designing Games
Goals: Learn to design abstract classes and class hierarchies. Design an interactive graphics-based game: Using libraries.
1 Instructions
The names of the classes must be exactly the same as specified. The same is the case for the names and types of the fields within the class, as well as the order in which they are defined and listed as the constructor arguments. This allows us to design our own Examples class that tests your program.
Make sure you follow the style guidelines that WebCAT enforces. For now the most important ones are: using spaces instead of tabs, indenting by 4 characters, following the naming conventions (data type names start with a capital letter, names of fields and methods start with a lower case letter), and having spaces before curly braces.
You will submit this assignment by the deadline using the Web-CAT submission system. You may submit as many times as you wish. Be aware of the fact that close to the deadline the WebCAT system may slow down to handle many submissions - so try to finish early.
With each homework you will also submit your log file named pair-user1-user2.txt where you replace user1 and user2 with the usernames of the two partners.
On top of both files you will have five lines of comments as follows:
// assignment 1 |
// partner1-last-name partner1-first-name |
// partner1-username |
// partner2-last-name partner2-first-name |
// partner2-username |
(In the text file you do not need the two slashes)
The two submissions will be organized as follows:
Submission HW4P1: The log file and the Files.java file in one .zip file
Submission HW4P2: The following files combined into one .zip file: MyGame.java, UsersGuide.txt, Design.txt, Images.txt, and the file that contains the class diagram - if a separate file (see below for details).
Due Date: Thursday, February 6th, 2014 10:59 pm.
Practice Problems
Work out these problems on your own. Save them in an electronic portfolio, so you can show them to your instructor, review them before the exam, use them as a reference when working on the homework assignments.
Problems 18.1 - 18.4 on page 225
Problem 18.5 on page 229
Problem 18.6 on page 234
Problem 19.4 on page 263
Problem 1: Abstracting over Data Definitions
A file in a computer can contain either a text, or an image, or an audio recording. Every file has a name and the owner of the file. There is additional information for each kind of file as shown in the program Files.java
Download the file and work out the following problems:
Make one more example of data for each of the three classes and add the tests for the method size that is already defined.
Design the method downloadTime that determines how many seconds does it take to download the file at the given download rate.
The rate is given in bytes per second.
Design the method sameOwner that determines whether the owner of this file is the same as the owner of the given file.
Save the work you have done. Copy the file and continue. (Do not submit this part of your work.)
Possibly start a new project and import the file Files.java into it. Alternatively, save the a copy of the file you have been working on in another folder.
Hand in the result of this part of the assignment only.
Look at the code and identify all places where the code repeats —
the opportunity for abstraction. Lift the common fields to an abstract class AFile.
Make sure you include a constructor in the abstract class, and change the constructors in the derived classes accordingly. Run the program and make sure all test cases work as before.
For each method that is defined in all three classes decide to which category it belongs:
The method bodies in the different classes are all different, and so the method has to be declared as abstract in the abstract class.
The method bodies are the same in all classes and it can be implemented concretely in the abstract class.
The method bodies are the same for two of the classes, but are different in one class —
therefore we can define the common body in the abstract class and override it in only one derived class.
Now, lift the methods that can be lifted and run all tests again.
Note: You can lift the method sameOwner only if you change its contract.
Do so —
Problem 2: Using Libraries: Designing Interactive Games
Design a simple interactive graphics-based game that extends the World class in the javalib.funworld library and performs some actions on each tick, and responds to some user’s key events.
The game should have at least one collection of objects in it.
It should have some random behavior.
It should not be overly complicated, but have options for later additions.
For your submission besides the code for your game and all of the necessary examples and tests, you should include:
a copy of the class diagram - either a separate document, or included with the code. You may make a small photo of a hand-drawn document, create ’ASCII’ art like I do, or use some tool to draw nice pictures and produce a neat pdf document.
If you take a photo, make it small, so WebCAT does not have a problem with the size of the submission.
UserGuide.txt: a one page text document that explaing how to play the game for the game user
Design.txt: a one (or more) page text document that describes the responsibilities of each class in your program
Images.zip: a zipped collection of your images. Name all images with your and your partners’ initials preceeding teh image name. So, Peter Hynes and Jenny Smith would name the picture of an apple as ph-js-apple.png
This allows us to run all your games in one project and not confuse your images with another student’s creations.
Additional instructions for how to organize and submit your work will be available early next week.