Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA 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

JAVA 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. There are some compilation errors present. 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 and mutator 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 function 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 numTorpedoes 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 after adding the polymorphic methods described below.

Add the following code in (Lab06.java) to create the ships and fire the weapons for both a Constitution and a Galaxy class StarShip:

// Step 1: Instantiate one of each subclass and fire weapon

// create and fire weapons for a Constitution starship

Constitution myShip2 = new Constitution("Enterprise NCC-1701-A");

System.out.println(" " + myShip2.getCaptain() + " commanding "

+ myShip2.getName() + " says: " + myShip2.fireWeapon());

// create and fire weapons for a Galaxy starship

Galaxy myShip3 = new Galaxy("Galaxy NCC-1701-D", "Jean-Luc Picard",

"Dr. Leah Brahms");

System.out.println(" " + myShip3.getCaptain() + " commanding "

+ myShip3.getName() + " says: " + myShip3.fireWeapon());

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, and Dr. Leah Brahms for the sponsor.

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? Take a look at the Constitution class constructor.)

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 the following code to (Lab06.java) in order to print the average crew size for your Constitution and Galaxy class StarShips. Use the getAvgCrewSize() method for both.

// Step 2: Print average crew size

System.out.println(" For " + myShip2.getName()

+ " the average crew size is: " + myShip2.getAvgCrewSize());

System.out.println(" For " + myShip3.getName()

+ " the average crew size is: " + myShip3.getAvgCrewSize());

You will notice errors when you try to compile this (assuming you did not make any other changes).

One of the compile errors is caused because although both the crewSize instance variable and the getAvgCrewSize() method are properly declared in the Galaxy class, they are not in the Constitution class. Fix the problem with the Constitution class by adding the following code:

// additional Constitution class instance variable:

private int crewSize = 430;

also add the missing getAvgCrewSize() as a new method to the Constitution class:

public int getAvgCrewSize()

{

return crewSize;

}

The other compiler error will be fixed in step 6.

Make two polymorphic methods. Because you have implemented getAvgCrewSize() and fireWeapon() somewhat differently for the 2 subclasses, these methods can support polymorphism (used in step 7 below). 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 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 try to recompile. You should get an error in (StarShip.java) (unless you made more changes than those described above). Determine the cause of the problem and fix this error (i.e., by making the StarShip class abstract), 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 (i.e., comment out the code that created an object of the StarShip class that was provided in the starter code). Specifically, this is the code that should be commented out in (Lab06.java):

/*

StarShip myShip = new StarShip("Orville");

System.out.println(" " + myShip.getName() + " says "

+ myShip.fireWeapon());

*/

You should have two remaining errors, this time in (Galaxy.java). Read the first compiler error message carefully, it should say something like:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - getAvgCrewSize() in Lab06.Galaxy cannot override getAvgCrewSize() in Lab06.StarShip - overriding method is static

The getAvgCrewSize() method in the Galaxy class cannot override getAvgCrewSize() if it is declared as static. It must match the superclass declaration. To fix the problem, remove the static modifier for the getAvgCrewSize() method in Galaxy class and recompile. This should correct all remaining compile errors.

Enter the following code to your driver (Lab06.java) that creates an array of starships and simulates firing all the weapons of both ships to exhaustion. Be sure that your output matches the program output provided in the screen capture below for reference.

ArrayList myFleet = new ArrayList();

myFleet.add( myShip2 );

myFleet.add( myShip3 );

while (myShip2.getPercentCharge() > 0 || myShip3.getNumTorpedoes() > 0)

{

for ( StarShip ship : myFleet )

{

System.out.println(ship.getCaptain() + " commanding "

+ ship.getName() + " says: " + ship.fireWeapon());

}

}

Remember to import the ArrayList package.

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 or Eclipse.

JAVA CODE FILES:

CONSTITUTION:

package Lab06Starter;

/**

*

* @author ????

*/

// ****************************************************************

// Constitution.java

//

// A class derived from StarShip that holds information about

// a Constitution class starship. Overrides StarShip fireWeapon method.

//

// ****************************************************************

public class Constitution extends StarShip

{

// *** PLACE ADDITIONAL INSTANCE VARIABLE HERE.

public Constitution(String name)

{

super(name);

// *** MODIFY THIS CONSTRUCTOR TO ACCOMMODATE THE NEW INSTANCE VARIABLE.

}

// ------------------------------------------------------------

// standard phasers -- overrides fireWeapon method in StarShip

// ------------------------------------------------------------

public String fireWeapon()

{

// *** MODIFY THIS METHOD TO MODEL PHASER DRAIN.

return "Phaser #1 - FIRE!";

}

// *** ADD APPROPRIATE ACCESSOR METHODS FOR THE NEW INSTANCE VARIABLE.

}

GALAXY:

package Lab06Starter;

/**

*

* @author ????

*/

// ****************************************************************

// Galaxy.java

//

// A class derived from StarShip that holds information about

// an advanced StarShip of the Galaxy class of vessels. Overrides StarShip

// fireWeapon method and includes additional information about

// this specialized class of StarShip.

//

// ****************************************************************

public class Galaxy extends StarShip

{

private int crewSize = 1012;

// *** PLACE ADDITIONAL INSTANCE VARIABLES HERE.

public Galaxy(String name, String captain, String sponsor)

{

this.captain = captain;

this.sponsor = sponsor;

// *** ADD CODE TO INITIALIZE THE NEW INSTANCE VARIABLES

}

// ------------------------------------------------------------

// advanced weaponry -- overrides fireWeapon method in StarShip

// ------------------------------------------------------------

public String fireWeapon()

{

// *** MODIFY THIS METHOD TO MODEL TORPEDO EXPENDITURES.

return "Photon Torpedoes, Full Spread - FIRE!";

}

// ------------------------------------------------------------

// Returns average crew complement

// ------------------------------------------------------------

public static int getAvgCrewSize()

{

return crewSize;

}

// *** PLACE ADDITIONAL METHODS HERE.

}

LAB06:

package Lab06Starter;

/**

*

* @author ????

*/

public class Lab06 {

/**

* @param args the command line arguments

*/

public static void main(String[] args)

{

StarShip myShip = new StarShip("Orville");

System.out.println(" " + myShip.getName()

+ " says " + myShip.fireWeapon());

// *** PLACE ADDITIONAL STATEMENTS PER PART II HERE.

// STEP 1: Instantiate one of each subclass and fire weapon

// 1.a. Create and fire weapons for a Constitution starship

// 1.b. Create and fire weapons for a Galaxy starship

// STEP 3: Print average crew size

// 3.a. Print average crew size for Constitution starship

// 3.b. Print average crew size for Galaxy starship

// STEP 7: Fire all weapons

System.out.println(" *** COMMENCE FIRE. Fire at will. ***");

// 7.a. Fire phasers until exhaustion (Constellation)

// 7.b. Launch torpedo salvos until all torpedoes expended. (Galaxy)

}

}

STARSHIP:

package Lab06Starter;

/**

*

* @author ????

*/

// ****************************************************************

// StarShip.java

//

// A class that holds a starShip's name and can make it fire Weapons.

//

// ****************************************************************

public class StarShip

{

protected String name;

// *** PLACE ADDITIONAL INSTANCE VARIABLES HERE.

// ------------------------------------------------------------

// Constructor -- store name

// ------------------------------------------------------------

// *** MODIFY THIS CONSTRUCTOR FOR THE ADDITIONAL INSTANCES VARIABLES.

public StarShip(String name)

{

this.name = name;

}

// ------------------------------------------------------------

// Returns the myShip's name

// ------------------------------------------------------------

public String getName()

{

return name;

}

// ------------------------------------------------------------

// Returns a string with the starShip's weapons volley

// ------------------------------------------------------------

public String fireWeapon()

{

return "Laser Canon - Fire!";

}

// *** PLACE ADDITIONAL METHODS HERE.

}

SAMPLE RUN SAMPLE RUN SAMPLE RUN SAMPLE RUN

image text in transcribed

Lab06 - NetBeans IDE 8.2 File Edit View Navigate Source Refactor Run Debug Profile Team Tools Window Help th 29 ? ?e : ) . T C) TicketTypes.java xLab06.javaStarShip.java xGalaxyavaConstitution.java x ource History package lab06; Eg Output Lab06 (run) X run: 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 Spread-FIRE! 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 will. Randy Pausch commanding Enterprise NCC-1701-A says: Phaser #1 - FIRE ! Phaser banks now at 40.0% Jean-Luc Picard commanding Galaxy NCC-1701-D says: Photon Torpedoes, Full Spread-FIRE! 12 torpedoes remaining Randy Pausch commanding Enterprise NCC-1701-A says: Phaser #1 - FIRE! Phaser banks now at 10.0% Jean-Luc Picard commanding Galaxy NCC-1701-D says: Photon Torpedoes, Full Spread-FIRE! 2 torpedoes remaining Randy Pausch commanding Enterprise NCC-1701-A says: Phaser #1 - FIRE ! ***Phaser Banks Exhausted! Jean-Luc Picard commanding Galaxy NCC-1701-D says: 2 Photon torpedoes fired. Al1 Photon Torpedoes Expended! BUILD SUCCESSFUL (total time: 0 seconds) Lab06 - NetBeans IDE 8.2 File Edit View Navigate Source Refactor Run Debug Profile Team Tools Window Help th 29 ? ?e : ) . T C) TicketTypes.java xLab06.javaStarShip.java xGalaxyavaConstitution.java x ource History package lab06; Eg Output Lab06 (run) X run: 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 Spread-FIRE! 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 will. Randy Pausch commanding Enterprise NCC-1701-A says: Phaser #1 - FIRE ! Phaser banks now at 40.0% Jean-Luc Picard commanding Galaxy NCC-1701-D says: Photon Torpedoes, Full Spread-FIRE! 12 torpedoes remaining Randy Pausch commanding Enterprise NCC-1701-A says: Phaser #1 - FIRE! Phaser banks now at 10.0% Jean-Luc Picard commanding Galaxy NCC-1701-D says: Photon Torpedoes, Full Spread-FIRE! 2 torpedoes remaining Randy Pausch commanding Enterprise NCC-1701-A says: Phaser #1 - FIRE ! ***Phaser Banks Exhausted! Jean-Luc Picard commanding Galaxy NCC-1701-D says: 2 Photon torpedoes fired. Al1 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

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions

Question

What is database?

Answered: 1 week ago

Question

What are Mergers ?

Answered: 1 week ago

Question

Explain the function and purpose of the Job Level Table.

Answered: 1 week ago