Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

OBJECTIVES To apply knowledge of the Conparable interface to a problem To unders

ID: 3854887 • Letter: O

Question

OBJECTIVES To apply knowledge of the Conparable interface to a problem To understand and override the toString and equals methods INSTRUCTIONS For this assignment, you will create a Spaceship class The class will contain the following fields: . classification The classification of the starship Valid values are based on an enumerated type, ShipType: o o - ScoutShip -Sciencevessel ConstructionVessel - WarShip o The name of the vessel captain The name of the captain Eg, Coptoin John Snogglefish o The class will contain the following methods: . Spaceship) o Constructor-initializes the fields to their default values -Classification set to Warship The ship's name set to U.S.S.Default The ship's captain set to Captain Krustache McGhee Spaceship(ShipType classification, string shipNane, string captain) o Constructor-initialize the fields to the values passed in by the client Getters and setter methods for each of the field names . tostring) o overridden from the Object class This method should return a string containing a reference to the value in the following format: The classification, shipName is a sturdy vessel, under the leadership of captain o An actual example might be The WarShip U.S.S. Dirtdragon is a sturdy vessel, under the leadership of Captain Picante o . equals() o overridden from the Object class o A ship is equal to another ship if the classifications match . compareTo(Spaceship otherShip) implemented from the interface Comparable Specifically, ComparablecSpaceship> should be used for the interface o The method returns the following o o 1 is the current ship object's classification is less than otherShip's classification - O i the ships have the same classification -1 if the current ship object's classification is greater than otherShip's classification o The ordering of the ship classifications is as follows

Explanation / Answer

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

enum ShipType {

ScoutShip(0), ScienceVessel(1), ConstructionVessel(2), WarShip(3);

private int val;

/**

* @param val

*/

private ShipType(int val) {

this.val = val;

}

public int getShipType() {

return val;

}

}

public class Spaceship implements Comparable<Spaceship> {

private ShipType classification;

private String shipName;

private String captain;

public Spaceship() {

// TODO Auto-generated constructor stub

this.classification = ShipType.WarShip;

this.shipName = "U.S.A Default";

this.captain = "Krustache McGhee";

}

/**

* @param classification

* @param shipName

* @param captain

*/

public Spaceship(ShipType classification, String shipName, String captain) {

this.classification = classification;

this.shipName = shipName;

this.captain = captain;

}

/**

* @return the classification

*/

public ShipType getClassification() {

return classification;

}

/**

* @return the shipName

*/

public String getShipName() {

return shipName;

}

/**

* @return the captain

*/

public String getCaptain() {

return captain;

}

/**

* @param classification

* the classification to set

*/

public void setClassification(ShipType classification) {

this.classification = classification;

}

/**

* @param shipName

* the shipName to set

*/

public void setShipName(String shipName) {

this.shipName = shipName;

}

/**

* @param captain

* the captain to set

*/

public void setCaptain(String captain) {

this.captain = captain;

}

/*

* (non-Javadoc)

*

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

return "The " + classification + ", " + shipName

+ "is a study vessel, under the leadership of Captain "

+ captain;

}

/*

* (non-Javadoc)

*

* @see java.lang.Object#hashCode()

*/

@Override

public int hashCode() {

final int prime = 31;

int result = 1;

result = prime * result

+ ((classification == null) ? 0 : classification.hashCode());

return result;

}

/*

* (non-Javadoc)

*

* @see java.lang.Object#equals(java.lang.Object)

*/

@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (!(obj instanceof Spaceship))

return false;

Spaceship other = (Spaceship) obj;

if (classification != other.classification)

return false;

return true;

}

/*

* (non-Javadoc)

*

* @see java.lang.Comparable#compareTo(java.lang.Object)

*/

@Override

public int compareTo(Spaceship other) {

// TODO Auto-generated method stub

if (this.getClassification().getShipType() < other.getClassification()

.getShipType())

return -1;

else if (this.getClassification().getShipType() > other

.getClassification().getShipType())

return 1;

else

return 0;

}

public static void main(String[] args) {

Spaceship spaceship1 = new Spaceship();

Spaceship spaceship2 = new Spaceship(ShipType.ScienceVessel,

"shipname2", "captainname2");

Spaceship spaceship3 = new Spaceship(ShipType.ConstructionVessel,

"shipname3", "captainname3");

Spaceship spaceship4 = new Spaceship(ShipType.ScoutShip, "shipname4",

"captainname4");

List<Spaceship> spaceships = new ArrayList<Spaceship>();

spaceships.add(spaceship1);

spaceships.add(spaceship2);

spaceships.add(spaceship3);

spaceships.add(spaceship4);

Collections.sort(spaceships);

for (int i = 0; i < spaceships.size(); i++) {

System.out.println(spaceships.get(i));

}

}

}

OUTPUT:

The ScoutShip, shipname4is a study vessel, under the leadership of Captain captainname4
The ScienceVessel, shipname2is a study vessel, under the leadership of Captain captainname2
The ConstructionVessel, shipname3is a study vessel, under the leadership of Captain captainname3
The WarShip, U.S.A Defaultis a study vessel, under the leadership of Captain Krustache McGhee