Class Position

java.lang.Object
edu.uoc.pacman.model.utils.Position

public class Position extends Object
Stores a 2D position/point/coordinate.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private int
    Value of the attribute X axis
    private int
    Value of the attribute Y axis
  • Constructor Summary

    Constructors
    Constructor
    Description
    Position(int x, int y)
    Constructor with arguments.
  • Method Summary

    Modifier and Type
    Method
    Description
    static Position
    Creates a new Position object as a result to add p1 and p2.
    double
    Calculates the Euclidean distance from this point to other given point.
    boolean
    equals(Object other)
    Checks if two positions are equal.
    int
    Getter of the attribute x
    int
    Getter of the attribute y
    int
    Returns the hash of a Position object.
    void
    setX(int x)
    Setter of the attribute x
    void
    setY(int y)
    Setter of the attribute y
    Returns a comma-seperated string representation of a Position.
    Format: "x,y"
    Example: "3,4"

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • x

      private int x
      Value of the attribute X axis
    • y

      private int y
      Value of the attribute Y axis
  • Constructor Details

    • Position

      public Position(int x, int y)
      Constructor with arguments.
      Parameters:
      x - Initial value of the attribute x
      y - Initial value of the attribute y
  • Method Details

    • getX

      public int getX()
      Getter of the attribute x
      Returns:
      The current value of the attribute x
    • setX

      public void setX(int x)
      Setter of the attribute x
      Parameters:
      x - New value for the attribute x
    • getY

      public int getY()
      Getter of the attribute y
      Returns:
      The current value of the attribute y
    • setY

      public void setY(int y)
      Setter of the attribute y
      Parameters:
      y - New value for the attribute y
    • distance

      public double distance(Position other)
      Calculates the Euclidean distance from this point to other given point.
      Parameters:
      other - Second position to calculate distance.
      Returns:
      The Euclidean distance between the position which invokes the method and the other position. If other is null, then it returns 0.
    • add

      public static Position add(Position p1, Position p2) throws NullPointerException
      Creates a new Position object as a result to add p1 and p2.
      Parameters:
      p1 - First position
      p2 - Second position
      Returns:
      New Position with x = p1.x + p2.x and y = p1.y and p2.y.
      Throws:
      NullPointerException - When either p1 or p2 are null.
    • equals

      public boolean equals(Object other)
      Checks if two positions are equal.
      Overrides:
      equals in class Object
      Parameters:
      other - object to compare against.
      Returns:
      true if the type of other is Position and x == this.x and also y == this.y, false otherwise.
    • hashCode

      public int hashCode()
      Returns the hash of a Position object.
      Overrides:
      hashCode in class Object
      Returns:
      The result of applying Objects.hash on x and y.
    • toString

      public String toString()
      Returns a comma-seperated string representation of a Position.
      Format: "x,y"
      Example: "3,4"
      Overrides:
      toString in class Object
      Returns:
      "x,y"