Question: QUESTION Write the program from today (in 2 separate classes/files: StarTrek and Spaceship, where StarTrek is the Driver or main class (with the main method)

QUESTION 
  1. Write the program from today (in 2 separate classes/files: StarTrek and Spaceship, where StarTrek is the Driver or main class (with the main method) and Spaceship is the Blueprint or data class), and update it so that it creates an additional instance of Spaceship, e.g. an object klingonShip, prints all its data, changes its captain, and prints just its new captain. You may do this by modifying StarTrek.java which uses Spaceship.java. To do this, you need not modify the Blueprint class Spaceship; you are just invoking the constructor again in the main method of the Driver class StarTrek. BELOW ARE BOTH PROGRAMS. PLEASE DO THIS IN JAVA
 STARTREK PROGRAM /** * Driver class = main class * * @author Dr. Chays, revised by ... * @version 2018 October 17 (to be revised) */ public class StarTrek { public static void main (String[] args) { // Create a spaceship. Spaceship enterprise = new Spaceship (40, 50, "Enterprise", "Kirk"); // enterprise is an object of type Spaceship, i.e. enterprise is an instance of the Spaceship class. // Print enterprise's data. System.out.println (enterprise.toString()); // OR System.out.println (enterprise); // Change captain to Picard. enterprise.setCaptain ("Picard"); // Print new captain. System.out.println("The new captain is " + enterprise.getCaptain()); } // end main } // end class 

SPACESHIP PROGRAM

/** * Blueprint class. A Spaceship has x and y coordinates,a name and a captain. * * @author Dr. Chays * @version 2018 October 17 (to be revised) */ public class Spaceship { // attributes = instance data = state variables = properties = characteristics private int x, y; private String name, captain; // Constructor initializes an object/instance of Spaceship by initializing all attributes. public Spaceship(int theX, int theY, String theName, String theCaptain) { // Initialize attributes. this.x = theX; this.y = theY; this.name = theName; this.captain = theCaptain; // Note: "this." is optional but makes the code more readable // by saying here that x, y, name, captain are attributes as opposed to some other kind of variable. } // toString: returns a String representation of this Spaceship, that includes the values of all its attributes. public String toString () { String result; result = "A Spaceship named " + this.name + " is at coordinate (" + this.x + ", " + this.y + "). " + "The captain is " + this.captain + ". "; return result; } // Accessor for captain. public String getCaptain () { return this.captain; } // Mutator for captain. public void setCaptain (String theNewCaptain) { //if (theNewCaptain.length() > 0) this.captain = theNewCaptain; } } // end class Spaceship

 

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!