p_models
Class Paddle

java.lang.Object
  extended by p_models.Paddle
All Implemented Interfaces:
PaddleSpecification, HasInvariant
Direct Known Subclasses:
RunnableViewablePaddle

public class Paddle
extends java.lang.Object
implements HasInvariant, PaddleSpecification

Implements HasInvariant, PaddleSpecification

A simple paddle model for use in a video game.
For teaching MVC design pattern.

Version:
1.0.0
Author:
J Paul Gibson

Field Summary
 
Fields inherited from interface p_abstractions.PaddleSpecification
INVARIANT_OF_CLASS, MAXIMUM_position, MINIMUM_position
 
Constructor Summary
Paddle()
          Tested by JUnit_Paddle.testDefaultConstructor(), which guarantees that the Paddle is constructed in a safe state as specified by invariant().
Paddle(int pos)
          Tested by JUnit_Paddle.testNonDefaultConstructor(), which guarantees that the Paddle is constructed in a safe state as specified by invariant().
Paddle(java.util.Random rng)
          Tested by RandomTest_Paddle, which guarantees that the Paddle is constructed in a safe state as specified by invariant().
 
Method Summary
 void changeDirection()
          Tested by JUnit_Paddle.testChangeDirection(), which guarantees that the Paddle remains in a safe state as specified by invariant().
 boolean equals(java.lang.Object thing)
           
 int get_position()
           
 boolean goingRight()
           
 boolean invariant()
          The position must be within the defined limit: PaddleSpecification.MINIMUM_position ...
 java.lang.String toString()
           
 void updatePosition()
          Tested by JUnit_Paddle.testUpdatePosition(), which guarantees that the Paddle remains in a safe state as specified by invariant().
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Paddle

public Paddle()
Tested by JUnit_Paddle.testDefaultConstructor(), which guarantees that the Paddle is constructed in a safe state as specified by invariant().

As default the Paddle is at the leftmost position as defined by MINIMUM_position, and moving right


Paddle

public Paddle(int pos)
Tested by JUnit_Paddle.testNonDefaultConstructor(), which guarantees that the Paddle is constructed in a safe state as specified by invariant().

Parameters:
pos - specifies the initial horizontal position for the Paddle being constructed
  • if pos is smaller than minimum bound then the position is initialised to the minimum
  • if pos is bigger than maximum bound then the position is initialised to the maximum
  • the Paddle direction is initially set to the right irrespective of the position

Paddle

public Paddle(java.util.Random rng)
Tested by RandomTest_Paddle, which guarantees that the Paddle is constructed in a safe state as specified by invariant().

Parameters:
rng - is the random number generator to be used in the construction of a random Paddle:
  • Every valid position has an equal chance of being the initial value
  • Each direction (left and right) has an equal chance of being the initial value
Method Detail

invariant

public boolean invariant()
The position must be within the defined limit: PaddleSpecification.MINIMUM_position ... PaddleSpecification.MAXIMUM_position

Specified by:
invariant in interface HasInvariant
Returns:
if the object is in a safe (meaningful) state

get_position

public int get_position()
Specified by:
get_position in interface PaddleSpecification
Returns:
The position of the paddle - must be within the defined limit: MINIMUM_position ... MAXIMUM_position

goingRight

public boolean goingRight()
Specified by:
goingRight in interface PaddleSpecification
Returns:
true if the paddle is moving to the right and false otherwise

updatePosition

public void updatePosition()
Tested by JUnit_Paddle.testUpdatePosition(), which guarantees that the Paddle remains in a safe state as specified by invariant().

Specified by:
updatePosition in interface PaddleSpecification

changeDirection

public void changeDirection()
Tested by JUnit_Paddle.testChangeDirection(), which guarantees that the Paddle remains in a safe state as specified by invariant().

Specified by:
changeDirection in interface PaddleSpecification

equals

public boolean equals(java.lang.Object thing)
Specified by:
equals in interface PaddleSpecification
Overrides:
equals in class java.lang.Object
Parameters:
thing - is the input object to test for equality

toString

public java.lang.String toString()
Specified by:
toString in interface PaddleSpecification
Overrides:
toString in class java.lang.Object