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
- 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started