On This Page
Overview
This assignment explores the use of polymorphism and abstract classes. The lab exercies lead you through the basics of polymorphism. The assignment project uses polymophism and abstract classes to implement a simple polymorphic game. During the assignment, you will:
- Develop an inheritance hierarchy
- Create an extensible design using polymorphism
^ top
Lab Exercises
In these exercises you explore polymorphism and abstract classes.
Specifications
- Prepare a single text file named:
exercises.txt. Record answers to all the exercises in this file, labeling each answer with the exercise number.
- Complete self-test exercises 1 and 4 on page 485 of your textbook.
- Complete self-test exercises 5, 7, and 8 on pages 492 and 493 of your textbook.
- Complete self-test exercises 10, 11, 12, 13 and 14 on page 506 of your textbook.
- When finished, do not forget to upload your file to Blackboard as part of this assignment
^ top
Project Specifications
- Complete programming project 3 from chapter 7 of the textbook (pg. 469-471).
To assist with developing the project, I provide a sample game class in Blackboard named AlienAttack. Download this class so you can run the game when you are completed.
- When you complete your project you must have four classes in your inheritance hierarchy with
Alien as the superclass and three subclasses named: SnakeAlien, OgreAlien and MarshmallowAlien, as shown in the class diagram below.

- All the instance variables used in the entire
Alien hierarchy must be declared private and be contained in the Alien superclass. None of the subclasses may declare any instance variables, though you may declare local variables and constants as needed.
- Your rewritten
Alien superclass must be made abstract since there will never be a need to create an instance of it, only instances of the Alien subclasses.
- Add a
toString() method to your Alien superclass that returns the class name of the object, the given name of the alien, and the health of the alien.
Hint: use reflection to determine the class name of the object.
- Also in the
Alien superclass, make the getDamage() method abstract.
- In addition to your
Alien hierarchy, you must use the AlienPack class from the textbook. This class contains the calculateDamage() method that returns the damage inflicted by all aliens in the pack. Leave all the existing constructors and methods as they are except the calculateDamage() method. Modify the calculateDamage() method to use polymorphism when calculating the overall damage.
- To the
AlienPack class, add a method named getTotalHealth() that returns the sum of the health of the aliens in the pack.
- You must document and organize your code following the requirements in the page How To Document and Organize Java Code. Your program must pass CheckStyle configured with the grade_checks.xml for full credit. Note that the CTC has this configured on their computers and that I have instructions for setting up TextPad to use CheckStyle.
- Do not use any
package statements.
- Make an executable JAR of your program containing all the files described in the section of this document: What to Turn In. Set the
Main-Class of the JAR file to the AlienAttack class.
^ top
Example Operation
During this project you will develop some of the classes for a simple video game. You can see how the game operates in the example run shown below.
Aliens are attacking!!
Current health: 100
Choose an action
D: duck and hope they miss
S: shoot at the aliens
Q: quit
Choice: s
Shooting at the alien pack!
Firing at alien brutus.
Caused 8 damage (health remaining: 92)
Firing at alien slimy.
Caused 8 damage (health remaining: 92)
Firing at alien puffy.
Caused 9 damage (health remaining: 91)
Return fire from pack caused 13 damage (health remaining: 87)
Current health: 87
Choose an action
D: duck and hope they miss
S: shoot at the aliens
Q: quit
Choice: d
Ducking (I hope they miss!)
They hit you!
Total alien damage: 11 (health remaining: 76)
Current health: 76
Choose an action
D: duck and hope they miss
S: shoot at the aliens
Q: quit
Choice: q
Game over
Also, you can download and run the asn07demo.jar file to see how it operates. Note that you cannot double-click the JAR file and must run the demo from the command line:
java -jar asn07demo.jar
Also note that the JAR file may contain some extra credit features.
Note that you will need the following code in Alien to run the example game:
public static final int HEALTH = 100;
While the game is text based, you can extend it to a graphical game for the class project.
^ top
Test Cases
Testing is an important part of software development. Usually, programmers develop tests for each unit of code they produce. This is know as "unit testing" and allows the programmer to verify that the code they develop works correctly.
To assist you in developing your code, I provide tests for this assignment. These tests, known collectively as "test cases", help to make sure that your assignment meets its requirements. I will use these test cases (and perhaps others) to test your assignment, and you should too. If the test cases do not pass, then you will lose points for the assignment.
The test class is posted in Blackboard. Install the tests by downloading the test file into the same directory as your .java source code files. Then compile and run the test class as you would any other Java program.
^ top
Extra Credit
The following are worth extra credit points:
- To show the extensibility of a polymorphic design, create one or more additional subclasses of the
Alien superclass beyond those required in the assignment. Update the AlienAttack game class to use your new subclasses. (1 point for each additional subclass, 2 points maximum)
- Improve the sample game I provided to provide a more interesting and fun game! Put the code for your improved game in a file named
AlienPlus.java. (1 to 2 points, depending on the improvements made.)
Make certain that your README.txt file lists any extra credit attempted.
^ top
Grading Criteria
The instructor will evaluate your assignment using the following criteria. Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive.
Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.
For information on grading policies, including interpretation of scores, see the course information page.
Lab Exercises
- 2: All exercises attempted and turned in
- 1: Some exercises completed and turned in
- 0: No exercises completed or turned in
Program Compilation
- 4: Source code compiles with no errors or warnings
- 2: Source code compiles with warnings
- 0: Does not compile
Functionality
- 10: Demonstrates mastery of the assignment
- Has extra features or demonstrates techniques beyond the assignment
- Applies concepts from the lessons appropriately
- Meets all specifications (see above) with particularly elegant solutions
- Runs to completion with no abnormal error conditions
- 8: Has all the functionality expected of the assignment
- Demonstrates many techniques from the lesson
- Meets all specifications (see above)
- Implementation seems more complicated than necessary.
- May have one minor error
- 6: Has most of the functionality expected of the assignment
- Demonstrates some techniques from the lesson
- Meets all but one of the specifications (see above)
- Implementation seems excessively complicated.
- May have 2-3 minor errors
- 4: Has some of the functionality expected of the assignment
- Demonstrates some techniques from the lesson
- Meets at least 1/2 of the specifications (see above)
- Implementation seems excessively complicated.
- May have more than 3 minor errors
- 2: Serious functional problems but shows some effort and understanding
- Meets less than 1/2 of the of the specifications (see above)
- Has a major error or many minor errors
- Implementation seems very convoluted
- Demonstrates few techniques from the lesson
- 0: Does not execute
Program Style
- 4: Code is well-documented
- 3: Code has a minor documentation error
- 2: Code has some documentation errors
- 1: Code has many documentation errors
- 0: No apparent attempt to write documentation comments
Maximum Score: 22, plus extra credit
^ top
What to Turn In
Submit your assignment following the instructions for homework. Include the following items for grading:
README.txt file
- All Java source code and class files in a single executable JAR file
You must submit all the files needed for your assignment to compile and work correctly. Do not assume that the instructors has any files except AlienTester.java and AlienAttack.java. Also, do not turn in files that are not part of your assignment, especially ones that do not compile. Your assignment must compile after removing all the .class files and running: javac *.java. In addition, your code must work as submitted.
^ top
Home
| Blackboard
| Schedule
| Room Policies
| Syllabus
Help
| FAQ's
| HowTo's
| Links
Last Updated: April 28 2010 @16:04:16
|