Sale!

Homework 4: An interface Drawable

$30.00

Category:

Description

COMP 2150 –
Homework 4: Polymorphism
(25 points)
After you release the revised Super Happy Fun Cute Creatures
World Traveler
from Homework 3, the game gains worldwide
popularity. Kids and adults from Europe to Asia to North
America are playing it, and you even get fan mail from one
random scientist down in Antarctica.
But a game company’s work is never done! You decide it’s time
to break into the niche market of space combat simulators. The
basic idea behind your sim is that you control a spaceship that can
fire different types of weapons. Each weapon has its own attack
damage and load time. (Generally speaking, longer loading
weapons should give you higher damage, and vice versa.)
1.
(11 pts) Implement the following classes and interfaces:
a. An interface
Drawable that contains a single method, draw(). This interface will be implemented by any
classes representing game objects that you want to draw on the screen. This makes it so the game engine
(which you don’t need to write for this assignment…) can treat every object you want to show on the
screen as type
Drawable, and it can simply call draw() on each of those objects to render it.
b. A class
Spaceship that implements Drawable. For now, the draw() method can just display some text –
make this as simple or elaborate as you want!
c. An abstract class
Weapon that contains instance variables for damage and load time. Weapon should
also contain an abstract method
fire(), and it should implement a Comparable interface by providing a
compareTo(Comparable c) method based on the load time. Finally, Weapon should implement
Drawable with the abstract method draw().
d. A
Laser subclass of Weapon. Laser must implement the fire() and draw() methods inherited from
Weapon. Both methods can just display some text – be as simple or elaborate as you want!
e. A
SingularityCannon subclass of Weapon. SingularityCannon must implement the fire() and draw()
methods inherited from Weapon. Both methods can just display some text – be as simple or elaborate as
you want!
Include some way to specify the damage and load time of your
Weapon objects. This could be via setter
methods, constructors, randomly assigned values in some range, or some combination thereof.
Number of People: Individual. Feel free to ask me for help, or visit the Computer Science Learning Center
(
http://www.memphis.edu/cs/current_students/cslc.php).
Due: Wed., Oct. 4 by 5:30 pm
Submission: Zip all your Java source files (you can zip the entire project folder if using an IDE) into a single file and
upload it to the proper folder in the eCourseware dropbox at
https://elearn.memphis.edu.
Coding Style: Use consistent indentation. Use standard Java naming conventions for
variableAndMethodNames,
ClassNames, CONSTANT_NAMES. Include a reasonable amount of comments.
Grader: Lasang Tamang,
[email protected]. Questions about grading? Please contact him first!
2. (4 pts) Add the ability to mount Weapons onto your Spaceship. Start by adding an array of Weapon objects to
Spaceship as an instance variable. You then need some way to store Weapon objects into that array. You can do
this via an
add method (similar to the addCard method from our poker examples), within the Spaceship
constructor, or some combination thereof.
3.
(4 pts) Add a method, fireFastestWeapons(int n), to the Spaceship class. This method should allow the
Spaceship to fire its n fastest weapons, in order of increasing load time. You can do this by sorting your Weapon
array (make use of the sort method for Comparables that we discussed in class), then calling fire() on the first n
elements. If n is larger than the number of weapons actually mounted, the method should just fire all weapons in
order of increasing load time.
4.
(3 pts) Provide a client program that creates a new Spaceship, mounts some Weapons onto it, and then calls
fireFastestWeapons.
5.
(3 pts) Write the necessary code to support a new type of Weapon (maybe a GravitonBomb, PhaseDisruptor,
AntimatterBeam, or some other crazy thing). Because of the way the code is organized, this should be very
easy! You can just create a new subclass of
Weapon and add some objects of that class to your ship’s array of
weapons. There’s no need to change any other existing code. This isn’t a big deal for this toy example, but
imagine if your classes had thousands of lines of code each and were being worked on by different people. Being
able to add new functionality in a modular fashion like this is invaluable!