Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

URENT, PLEASE WRITE THESE METHODS IN JAVA. DO NOT IGNORE 3.1 Class Position Each square in the grid of the board game can be specified

URENT, PLEASE WRITE THESE METHODS IN JAVA. DO NOT IGNORE

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

3.1 Class Position Each square in the grid of the board game can be specified by two numbers: the row where the square is and the column where it is. An object of class Position represents the position of a square of the grid. This class must have two private integer variables: position Row and positionColumn. In this class you must implement the following public methods: Position(int row, int col): this is the constructor for the class. The value of the first parameter must be stored in position Row and the second in positionColumn. int getRow(); returns the value of positionRow. int getColl): returns the value of positionColumn. void setRowlint newRow): stores the value of newRow in position Row. void setCollint newCol): stores the value of newCol in positionColumn. boolean equals(Position other Position): returns true if this position object and other Position have the same values stored in position Row and positionColumn. 3.3 Class Snake The class stores the information about the snake as it moves around the board. This class will have two private instance variables: int snakeLength: this is the number of grid squares occupied by the snake. For example, the snake shown in the above figure has a length of 4. Position [] snakeBody: the grid squares occupied by the snake will be stored in this array. The grid square with the head of the snake will be stored in index 0 of the array; the grid square where the tail of the snake is will be stored in index snakeLength-1 of the array. For the snake in the figure, array snakeBody will store the following information: Position of Position of tail |(1, 2) head (1,1) (1,0) (2,0) 0 1 2 3 3 object of class Position In this class you need to implement the following public methods. Snake(int row, int col): this is the constructor for the class; the parameters are the coordinates of the head of the snake. Initially the snake has length 1, so in this method the value of the instance variable snakeLength will be set to 1. Instance variable snakeBody is initialized to an array of length 5 of Position objects. An object of class Position will be created storing the values of row and col and this Position object will then be stored in the first entry of array snakeBody. int getLength(): returns the value of instance variable snakeLength. Position getPosition(int index): returns the Position object stored in snakeBody[index]. It returns null if index = snakeLength. void shrink(): decreases the value of snakeLength by 1. boolean snakePosition(Position pos): returns true if pos is in array snakeBody, and it returns false otherwise. Notice that you must use method equals from class Position to compare two objects of the class Position. Position newHeadPosition(String direction): returns the new position of the head of the snake when the snake moves in the direction specified by the parameter. The values that direction can take are "right", "left", "up" and "down". If, for example, the head of the snake is at (2,3) and direction is "right" then the new position would be (2,4); if direction is "down" then the new position would be (3,3). If the head is at (0,0) and direction is "up" the new position would be (-1,0). void moveSnake(String direction): moves the snake in the specified direction; this means that array snakeBody must be updated so it contains the positions of the grid squares that the snake will occupy after it moves in the direction specified by the parameter. For example, for the snake in the above figure array snakeBody is as specified above. If direction = "up" then array snakeBody must be this (0, 2) 0 (1,2) 1 (1,1) 2 (1,0) 3 If direction is "up" again then array snakeBody must be this (-1,2) (0, 2) (1, 2) (1,1) 0 1 2 3 4 Notice that to determine the new array snakeBody what you must do is this: o shift one position to the right all values in the array stored in indices 0 to snakeLength - 2. For example, if the snake is as in the figure and direction = "right", then shifting produces this array: |(1,3)-(1, 2) > (1,1)+(1,0)| 0 1 2 3 |(1,3) 0 (1, 3) 1 (1, 2) 2 (1,1) 3 4 4 o and then store in index 0 of the array the new position of the snake's head (which you can compute using method newHeadPostion(); in the above example we would store (1,4) in the first entry of the array. void grow(String direction): increases the length of the snake by 1 and moves the snake's head in the direction specified. This method is very similar to method moveSnake, but instead of shifting the values in snakeBody from index 0 to snakeLength - 2, we need to shift all values from index 0 to index snakeLength - 1. For example, if the snake is as shown in the figure, and direction="right" then the new content of array snake Body is (1,3) 0 (1, 2) 1 (1,1) 2 (1,0) 3 (2,0) 4 Notice that since the length of the snake grows you need to make sure that array snakeBody is large enough to store the new information. If instance variable snakeLength has the same value as snakeBody.length(), the size of the array, then you must double the size of the array before storing the new positions of the snake in it. To do this you must use the below private method increaseArray Size(). void increaseArraySize(): this method doubles the size of array snakeBody preserving the information that was stored in it. 3.1 Class Position Each square in the grid of the board game can be specified by two numbers: the row where the square is and the column where it is. An object of class Position represents the position of a square of the grid. This class must have two private integer variables: position Row and positionColumn. In this class you must implement the following public methods: Position(int row, int col): this is the constructor for the class. The value of the first parameter must be stored in position Row and the second in positionColumn. int getRow(); returns the value of positionRow. int getColl): returns the value of positionColumn. void setRowlint newRow): stores the value of newRow in position Row. void setCollint newCol): stores the value of newCol in positionColumn. boolean equals(Position other Position): returns true if this position object and other Position have the same values stored in position Row and positionColumn. 3.3 Class Snake The class stores the information about the snake as it moves around the board. This class will have two private instance variables: int snakeLength: this is the number of grid squares occupied by the snake. For example, the snake shown in the above figure has a length of 4. Position [] snakeBody: the grid squares occupied by the snake will be stored in this array. The grid square with the head of the snake will be stored in index 0 of the array; the grid square where the tail of the snake is will be stored in index snakeLength-1 of the array. For the snake in the figure, array snakeBody will store the following information: Position of Position of tail |(1, 2) head (1,1) (1,0) (2,0) 0 1 2 3 3 object of class Position In this class you need to implement the following public methods. Snake(int row, int col): this is the constructor for the class; the parameters are the coordinates of the head of the snake. Initially the snake has length 1, so in this method the value of the instance variable snakeLength will be set to 1. Instance variable snakeBody is initialized to an array of length 5 of Position objects. An object of class Position will be created storing the values of row and col and this Position object will then be stored in the first entry of array snakeBody. int getLength(): returns the value of instance variable snakeLength. Position getPosition(int index): returns the Position object stored in snakeBody[index]. It returns null if index = snakeLength. void shrink(): decreases the value of snakeLength by 1. boolean snakePosition(Position pos): returns true if pos is in array snakeBody, and it returns false otherwise. Notice that you must use method equals from class Position to compare two objects of the class Position. Position newHeadPosition(String direction): returns the new position of the head of the snake when the snake moves in the direction specified by the parameter. The values that direction can take are "right", "left", "up" and "down". If, for example, the head of the snake is at (2,3) and direction is "right" then the new position would be (2,4); if direction is "down" then the new position would be (3,3). If the head is at (0,0) and direction is "up" the new position would be (-1,0). void moveSnake(String direction): moves the snake in the specified direction; this means that array snakeBody must be updated so it contains the positions of the grid squares that the snake will occupy after it moves in the direction specified by the parameter. For example, for the snake in the above figure array snakeBody is as specified above. If direction = "up" then array snakeBody must be this (0, 2) 0 (1,2) 1 (1,1) 2 (1,0) 3 If direction is "up" again then array snakeBody must be this (-1,2) (0, 2) (1, 2) (1,1) 0 1 2 3 4 Notice that to determine the new array snakeBody what you must do is this: o shift one position to the right all values in the array stored in indices 0 to snakeLength - 2. For example, if the snake is as in the figure and direction = "right", then shifting produces this array: |(1,3)-(1, 2) > (1,1)+(1,0)| 0 1 2 3 |(1,3) 0 (1, 3) 1 (1, 2) 2 (1,1) 3 4 4 o and then store in index 0 of the array the new position of the snake's head (which you can compute using method newHeadPostion(); in the above example we would store (1,4) in the first entry of the array. void grow(String direction): increases the length of the snake by 1 and moves the snake's head in the direction specified. This method is very similar to method moveSnake, but instead of shifting the values in snakeBody from index 0 to snakeLength - 2, we need to shift all values from index 0 to index snakeLength - 1. For example, if the snake is as shown in the figure, and direction="right" then the new content of array snake Body is (1,3) 0 (1, 2) 1 (1,1) 2 (1,0) 3 (2,0) 4 Notice that since the length of the snake grows you need to make sure that array snakeBody is large enough to store the new information. If instance variable snakeLength has the same value as snakeBody.length(), the size of the array, then you must double the size of the array before storing the new positions of the snake in it. To do this you must use the below private method increaseArray Size(). void increaseArraySize(): this method doubles the size of array snakeBody preserving the information that was stored in it

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions