Enum ShipType

java.lang.Object
java.lang.Enum<ShipType>
student.micro.battleship.ShipType
All Implemented Interfaces:
Serializable, Comparable<ShipType>, java.lang.constant.Constable

public enum ShipType extends Enum<ShipType>
An enumeration representing the five types of ships in a player's fleet in the game "Battleship".
  • Enum Constant Details

    • CARRIER

      public static final ShipType CARRIER
      Represents an Aircraft Carrier, which is 5 units long.
    • BATTLESHIP

      public static final ShipType BATTLESHIP
      Represents a Battleship, which is 4 units long.
    • SUBMARINE

      public static final ShipType SUBMARINE
      Represents a Submarine, which is 3 units long.
    • DESTROYER

      public static final ShipType DESTROYER
      Represents a Destroyer, which is 3 units long.
    • PATROL

      public static final ShipType PATROL
      Represents a Patrol Boat, which is 2 units long.
  • Method Details

    • values

      public static ShipType[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      Returns:
      an array containing the constants of this enum type, in the order they are declared
    • valueOf

      public static ShipType valueOf(String name)
      Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
      Parameters:
      name - the name of the enum constant to be returned.
      Returns:
      the enum constant with the specified name
      Throws:
      IllegalArgumentException - if this enum type has no constant with the specified name
      NullPointerException - if the argument is null
    • getLength

      public int getLength()
      Get the length of this type of ship.
      • Carriers are 5 cells long.
      • Battleships are 4 cells long.
      • Submarines are 3 cells long.
      • Destroyers are 3 cells long.
      • Patrol boats are 2 cells long.
      Returns:
      This ship type's length.
    • valueOf

      public static ShipType valueOf(int ordinal)
      Get the ship value corresponding to the specified ordinal value. Use the method ordinal() to get the ordinal value from an enumeration type (a built-in method for all enums).
      • CARRIER : 0
      • BATTLESHIP : 1
      • SUBMARINE : 2
      • DESTROYER : 3
      • PATROL : 4
      Parameters:
      ordinal - A value from 0-4 indicating the desired ship type.
      Returns:
      The ship type that corresponds to the specified numeric value.