Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab 06 - Inheritance and Polymorphism The organizing committee for the HCC Celebrates Columbia's 50th Birthday, Inc., has planned a special event commemorating notable people

Lab 06 - Inheritance and Polymorphism

The organizing committee for the HCC Celebrates Columbia's 50th Birthday, Inc., has planned a special event commemorating notable people originally from Columbia, MD. The committee has invited the family of late Columbia native and Oakland Mills High School graduate, Dr. Randy Pausch, as special guests at the event. The event will honor the Carnegie Mellon University professors contributions to computer science, human-computer interaction, and computer graphics.

Dr. Pausch became nationally famous for his book, The Last Lecture, delivered at CMU after learning that he had terminal pancreatic cancer. His best-selling book was based on that lecture whose complete title was "The Last Lecture: Really Achieving Your Childhood Dreams". The lecture is available on YouTube - The Last Lecture .

A life-long Star Trek fan, Pausch was invited by film director J. J. Abrams to appear in a cameo role in the 2009 reboot of Star Trek. In addition to appearing in the film, he also has a line of dialogue at the beginning of the film. Dr. Pauschs blog entry (for 12/1/2007) gushes at the prospect of his appearing in the Star Trek movie:

https://www.cs.cmu.edu/~pauschews/index.html

You have been asked to write a program that will be demonstrated at the commemoration. It will pay homage to Dr. Pauschs Star Trek affinity. Specifically, you will build a simulation of his two favorite types of Federation vessels: the Constitution and Galaxy class starships.

File StarShip.java contains a declaration for a StarShip class. Save this file to your directory and study itnotice what instance variables and methods are provided. Files Galaxy.java and Constitution.java contain declarations for classes that extend the StarShip class. Save and study these files as well.

Part I: Develop the Design of the Classes:

In file (StarShip.java) enhance the StarShip class design by adding more fields. Specifically, add the following instance variables to the StarShip class using the appropriate declaration and access specifier. Modify the constructor to initialize the new fields to the default values listed in (a) and (b) below:

homeport - Earth McKinley Station

captain Randy Pausch

Add and implement the following accessor methods for the StarShip class in (a) through (d) below:

getHomeport() returns a single String value corresponding to the ships homeport.

setHomeport() accepts a single String value that sets the value of the ships homeport.

getCaptain() - returns a single String value containing the full name of the ships captain.

setCaptain() - accepts a single String value that sets the value of the ship captains full name.

In file (Constitution.java) specialize the Constitution class by adding another field that contains the percentage charge remaining on the ships phaser banks. Specifically, add the following instance variable using the appropriate declaration and initialize its value in an appropriate manner with the default value provided in (a) below. Also add the appropriate accessor functions for the percentCharge instance variable part (b). Also include additional code that reduces the charge on the ships phasers by 30% after each time the phasers are fired as in (c) below. When the phaser banks are at drained to 0% or lower, display a message stating:

*** Phaser Banks Exhausted! ***

percentCharge initial phaser charge is 100% (i.e., fully charged)

getPercentCharge() returns a double reflecting the current charge in the phasers.

fireWeapon() modify implementation to simulate the phaser drain.

In file (Galaxy.java) specialize the Galaxy class by adding another field to contain the number of photon torpedoes initially carried by this more advanced type of vessel. Specifically, add the following instance variable using the appropriate declaration and initialize in an appropriate manner with the default value provided in part (a) below. Add an instance variable to hold the name of the ships sponsor as in part (b) below. Also add an appropriate accessor method for the percentCharge instance variable as in part (c) below. Also include code that reduces the number of remaining photon torpedoes by 10 after each torpedo salvo is fired as in part (d) below. When all torpedoes have been fired, display a message stating:

*** All Photon Torpedoes Expended! ***

numTorpedoes initial weapons load is 32

sponsor a String variable to hold the name of the ships sponsor.

getNumTorpedoes() returns a single integer value corresponding to the current number of torpedoes remaining on the ship, i.e., the number of torpedoes remaining in the ships torpedo inventory.

fireWeapon() modify implementation to include photon torpedo depletion.

Draw a UML class diagram depicting the inheritance hierarchy described above and implemented in your Java source code.

Part II: Build and Operate your StarShips:

Note: After making the design enhancements in Part I above, you shouldnt need to make any major changes to the class files except to correct the errors discovered in this part and add the polymorphic methods described below.

Add the necessary statements in (Lab06.java) to create the ships and fire the weapons for both a Constitution and a Galaxy class StarShip. Mimic the existing code in the main method template that creates and fires weapons for an object of the base class. Note that the Galaxy class constructor takes three parameters: the name of the ship, her captain, and her sponsor (i.e., the persons name who christened the ship). All are declared as strings. For the Galaxy class ship, construct the object using Jean-Luc Picard for the captain, Dr. Leah Brahms for the sponsor, and a name of your choosing for both ship names.

Now recompile (Lab06.java); you should get a compiler error message referring to a problem with the constructor.

When you examine the offending line in Galaxy.java, you will note that the compiler can't find the StarShip() constructor. It is not called anywhere in this file.

Determine the root cause of this problem. (Hint: What call must always be made in the constructor of a subclass?)

Fix the problem (which really is in Galaxy.java) so that (Lab06.java) creates and makes StarShip, Galaxy, and Constitution objects all invoke the fireWeapon() method properly.

Add code to (Lab06.java) to print the average crew size for your Constitution and Galaxy class StarShips. Use the getAvgCrewSize() method for both. You will notice errors when you compile this (assuming you did not make any other changes). Determine the cause and fix the problem with the Constitution class first (i.e., printing the crew size) by adding the needed code to the Constitution class and making the necessary changes. Then fix the problem with the crewSize instance variable in (Galaxy.java). Initialize the crewSize instance variables for all Constitution class objects at 430.

Make two polymorphic methods. Because you have implemented getAvgCrewSize() and fireWeapon() differently for the 2 subclasses, these method exhibit polymorphism. Make the following changes.

Add the getAvgCrewSize() as an abstract method to the StarShip class. Remember that this means that the word abstract must appear in the method header after public, and that this method will not have a body (just a semicolon after the parameter list). It makes sense for this to be abstract, because the StarShip class has no reference to the crewSize instance variables in its subclasses. But now all subclasses that inherit from the StarShip base class must implement a getAvgCrewSize() method. If you added the getAvgCrewSize() method to the Constitution class in the last step, then you have complied with this requirement. Be sure to include the use of the @Override modifier where applicable (see page the bottom of page 381 in the textbook for more information on the use of the Override annotation for overridden and implemented classes).

Also make the existing fireWeapon() method abstract in the StarShip class in the same manner as you did for the getAvgCrewSize() method in part (a) above. Update the comments in the method subclasses to indicate that this method is now implemented as opposed to overridden.

Save these changes and recompile. You should get an error in (StarShip.java) (unless you made more changes than those described above). Determine the problem and fix this error, then recompile. You should get another error, this time in (Lab06.java). Read the compiler error message carefully; it tells you exactly what the problem is. Fix this by changing (Lab06.java) - which will mean commenting some things out.

Add the necessary code to your driver (Lab06.java) that simulates firing all the weapons of both ships to exhaustion. Be sure to match the program output provided below for reference.

image text in transcribed

Part III: Additional Requirements:

A Javadoc comment must appear at the top of the source code containing: Your name, course number and section, and a brief description of what the program does.

An end-of-line comment must appear after each closing brace describing the block being closed.

Proper indentation must be used.

The zip file containing all source files, UML diagram, and two screenshots: one showing your driver code in NetBeans and the other of your program running in NetBeans. The zip file must be named in the following format. You should replace the text in bold-italics with your information:

.

This file should be uploaded through CANVAS. I will not accept email attachments.

Randy Pausch commanding Enterprise NCC-1701-A says: Phaser #1 - FIRE ! Phaser banks now at 70.0% Jean-Luc Picard commanding Galaxy NCC-1701-D says: Photon Torpedoes, Full SpreadFIRE! 22 torpedoes remaining For Enterprise NCC-1701-A the average crew size is: 430 For Galaxy NCC-1701-D the average crew size is: 1012 *COMMENCE FIRE. Fire at wil1 Randy Pausch commanding Enterprise NCC-1701-A says: Phaser #1 - FIRE ! Phaser banks now at 40.0% Randy Pausch commanding Enterprise NCC-1701-A says Phaser #1 - FIRE ! Phaser banks now at 10.0% Randy Pausch commanding Enterprise NCC-1701-A says: Phaser #1 - FIRE ! **Phaser Banks Exhausted! Jean-Luc Picard commanding Galaxy NCC-1701-D says: Photon Torpedoes, Full SpreadFIRE! 12 torpedoes remaining Jean-Luc Picard commanding Galaxy NCC-1701-D says: Photon Torpedoes, Full Spread FIRE! 2 torpedoes remaining Jean-Luc Picard commanding Galaxy NCC-1701-D says: 2 Photon torpedoes fired All Photon Torpedoes Expended! * BUILD SUCCESSFUL (total time: 0 seconds) Randy Pausch commanding Enterprise NCC-1701-A says: Phaser #1 - FIRE ! Phaser banks now at 70.0% Jean-Luc Picard commanding Galaxy NCC-1701-D says: Photon Torpedoes, Full SpreadFIRE! 22 torpedoes remaining For Enterprise NCC-1701-A the average crew size is: 430 For Galaxy NCC-1701-D the average crew size is: 1012 *COMMENCE FIRE. Fire at wil1 Randy Pausch commanding Enterprise NCC-1701-A says: Phaser #1 - FIRE ! Phaser banks now at 40.0% Randy Pausch commanding Enterprise NCC-1701-A says Phaser #1 - FIRE ! Phaser banks now at 10.0% Randy Pausch commanding Enterprise NCC-1701-A says: Phaser #1 - FIRE ! **Phaser Banks Exhausted! Jean-Luc Picard commanding Galaxy NCC-1701-D says: Photon Torpedoes, Full SpreadFIRE! 12 torpedoes remaining Jean-Luc Picard commanding Galaxy NCC-1701-D says: Photon Torpedoes, Full Spread FIRE! 2 torpedoes remaining Jean-Luc Picard commanding Galaxy NCC-1701-D says: 2 Photon torpedoes fired All Photon Torpedoes Expended! * BUILD SUCCESSFUL (total time: 0 seconds)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems 6th Asian Conference Aciids 2014 Bangkok Thailand April 7 9 2014 Proceedings Part I 9 2014 Proceedings Part 1 Lnai 8397

Authors: Ngoc-Thanh Nguyen ,Boonwat Attachoo ,Bogdan Trawinski ,Kulwadee Somboonviwat

2014th Edition

3319054759, 978-3319054759

More Books

Students also viewed these Databases questions