Question: Implement the following classes and interfaces( In Java ): An interface Drawable that contains a single method, draw() . This interface will be implemented by

Implement the following classes and interfaces(In Java):

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 dont 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.

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!

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().

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!

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!

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.

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.

Provide a client program that creates a new Spaceship, mounts some Weapons onto it, and then calls fireFastestWeapons.

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 ships array of weapons. Theres no need to change any other existing code. This isnt 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!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!