Please fix bold comments . If they do not need to be fixed please tell me why..
ID: 3568742 • Letter: P
Question
Please fix bold comments. If they do not need to be fixed please tell me why..
class Pad
{
public static final int FROG = 1;
// add the remaining 3 constants WITH JAVADOCS descriptors
public static final int TOAD = 2;
public static final int VACANT = 3;
public static final int INVALID = 4;
// INSTANCE VARIABLES
private int currentState; // the current state of lilypad
private int startState; // The original state of lilypad
/**
* Creates a new lily pad with a given initial state (the state
* when the game begins).
* @param startState - the starting state of the lilypad (FROG, TOAD, VACANT or INVALID)
*/
public Pad(int startState)
{
// initialise instance variables
this.currentState = startState;
this.startState = startState;
}
/**
* Returns the current state of the lily pad; either VACANT, TOAD, FROG, or INVALID.
*
* @return the currentState
*/
public int getStatus()
{
return currentState;
}
/**
* Returns true if this lily pad is in the correct state for the solved puzzle and
* false otherwise.
*/
public boolean isSolved()
{ // use the following algorithm:
// if the startState is FROG and the current state is TOAD return true
// else if the startState is TOAD and the current state is FROG return true
// else if the startState is VACANT and the current state is VACANT return true
if(startState == FROG && currentState == TOAD)
return true;
else if(startState == TOAD && currentState == FROG)
return true;
else if(startState == VACANT && currentState == VACANT)
return true;
// otherwise ...
return false;
}
/**
* copy the javadocs here for the reset method, then write the method
*/
public void reset(){
currentState = startState;
}
/**
* copy the javadocs here for the setStatus method, then write the method
*/
public void setStatus(int i){
currentState = i;
}
/**
* Returns the string representation of the lily pad.
*/
public String toString()
{
if (currentState == FROG)
return "F";
// add symbols for the other possibilities
else if(currentState == TOAD)
return "T";
else if(currentState == VACANT)
return "O";
else // must be INVALID
return "#";
}
}
class Pond
{
private Pad [][] grid; // the 2D array of Pad objects that make up our pond
int size; // the number of Pads in a row or column
/**
* Creates a grid of lily pads with height and width both equal
* to the given size; each lily pad is constructed with the appropriate
* status (frog, toad, or a vacant spot) for starting a game of
* Frogs and Toads.
*
* The starting configuration consists of a vacant
* lily pad in the center and the remaining lily pads being half
* occupied by frogs and half occupied by toads. Each (vertical)
* column with a higher index than the middle column starts with
* all toads, and each column with an index lower than the middle
* column starts with all frogs. The center column has frogs in
* the low row indices, toads in the high row indices, and a vacant
* spot in the middle.
*
* Internally, an instance of this class contains "size * size"
* instances of the Pad class (stored in a 2D array). The legal
* range of size is odd numbers from 3 up to 9, which is assumed
* by the constructor as a precondition.
*
* @param size - the height and width of the grid (assumed to be
* an odd number at least 3 and at most 9 as a precondition)
*/
public Pond(int size)
{
// check that size is an odd number between 3 and 9
// HINT size%2 computes the remainder of size/2.
// if (size%2 == 1) that means size is odd
// so there are three conditions on size: must be >=3, <=9 AND odd
// if this is NOT true, print an error message and return. Otherwise...
if(size >=3 && size <= 9 && size%2 != 0 )
{
this.size=size;
grid = new Pad[size][size]; // declare the 2D array of Pad
for(int i = 0 ; i < size ; i++){
for(int j = 0 ; j< size ; j++){
grid[i][j] = new Pad(Pad.FROG);
}}
}
else
{
System.out.println("Invalid size");
}
// now fill each cell with a new Pad(Pad.XXXX)
// XXX will be FROG, TOAD, or VACANT
}
/**
* Indicates whether or not all lily pads in the pond are in the solved state.
*
* @return true if the puzzle is solved and false otherwise.
*/
public boolean isSolved()
{ // use the following algorithm:
// create an int variable to count numSolved and set it to 0
// use a nested for loop to visit each cell in grid.
// If the cell at row, col is solved, then add one to numSolved
//
// after the loop, if numSolved == size*size then it's solved (return true)
// otherwise it's not (false)
int numOfSolved = 0;
for(int i = 0 ; i < size ; i++){
for(int j = 0 ; j< size ; j++){
if(grid[i][j].getStatus() != Pad.INVALID)
numOfSolved++;
}}
if(numOfSolved == size*size)
return true;
return false;
}
/**
* copy the javadocs here for the getPad method, then write the method
*/
public Pad getPad(int row, int col) {
// if row & col are between 0 and size-1,
// return grid[row][col]
// else return a reference to an INVALID Pad
}
/**
* copy the javadocs here for the getSize method, then write the method
*/
public int getSize(){
return size;
}
/**
* copy the javadocs here for the reset method, then write the method
*/public void reset(){
for(int i = 0 ; i < size ; i++){
for(int j = 0 ; j< size ; j++){
grid[i][j].setStatus(Pad.FROG);
}}
}
// call Pad reset() method on all Pad objects in array grid.
/**
* Returns the current state of the pond as a String (with the VACANT, FROG,
* TOAD, and VACANT Pads represented as defined by the toString() method of
* the Pad class).
*/
public String toString(){
String result="";
for (int row=0; row < size; row++){
for (int col=0; col < size ; col++){
result = result + grid[row][col].toString(); // add the next grid Pad in a row
}
result = result + " "; // go to a new line after each row
}
return result;
}
}
Explanation / Answer
class Pad {
public static final int FROG = 1;
// add the remaining 3 constants WITH JAVADOCS descriptors
public static final int TOAD = 2;
public static final int VACANT = 3;
public static final int INVALID = 4;
// INSTANCE VARIABLES
private int currentState; // the current state of lilypad
private int startState; // The original state of lilypad
/**
* Creates a new lily pad with a given initial state (the state when the
* game begins).
*
* @param startState
* - the starting state of the lilypad (FROG, TOAD, VACANT or
* INVALID)
*/
public Pad(int startState) {
// initialise instance variables
this.currentState = startState;
this.startState = startState;
}
/**
* Returns the current state of the lily pad; either VACANT, TOAD, FROG, or
* INVALID.
*
* @return the currentState
*/
public int getStatus() {
return currentState;
}
/**
* Returns true if this lily pad is in the correct state for the solved
* puzzle and false otherwise.
*/
public boolean isSolved() { // use the following algorithm:
// if the startState is FROG and the current
// state is TOAD return true
// else if the startState is TOAD and the current state is FROG return true
// else if the startState is VACANT and the current state is VACANT return
// true
if (startState == FROG && currentState == TOAD)
return true;
else if (startState == TOAD && currentState == FROG)
return true;
else if (startState == VACANT && currentState == VACANT)
return true;
// otherwise ...
return false;
}
/**
* copy the javadocs here for the reset method, then write the method
*/
public void reset() {
currentState = startState;
}
/**
* copy the javadocs here for the setStatus method, then write the method
*/
public void setStatus(int i) {
currentState = i;
}
/**
* Returns the string representation of the lily pad.
*/
public String toString() {
if (currentState == FROG)
return "FROG ";
// add symbols for the other possibilities
else if (currentState == TOAD)
return "TOAD ";
else if (currentState == VACANT)
return "VACANT ";
else
// must be INVALID
return "#";
}
}
//---------------------------------------------------------------------------------------------------------------------------------------
class Pond
{
private Pad [][] grid; // the 2D array of Pad objects that make up our pond
int size; // the number of Pads in a row or column
/**
* Creates a grid of lily pads with height and width both equal
* to the given size; each lily pad is constructed with the appropriate
* status (frog, toad, or a vacant spot) for starting a game of
* Frogs and Toads.
*
* The starting configuration consists of a vacant
* lily pad in the center and the remaining lily pads being half
* occupied by frogs and half occupied by toads. Each (vertical)
* column with a higher index than the middle column starts with
* all toads, and each column with an index lower than the middle
* column starts with all frogs. The center column has frogs in
* the low row indices, toads in the high row indices, and a vacant
* spot in the middle.
*
* Internally, an instance of this class contains "size * size"
* instances of the Pad class (stored in a 2D array). The legal
* range of size is odd numbers from 3 up to 9, which is assumed
* by the constructor as a precondition.
*
* @param size - the height and width of the grid (assumed to be
* an odd number at least 3 and at most 9 as a precondition)
*/
public Pond(int size)
{
// check that size is an odd number between 3 and 9
// HINT size%2 computes the remainder of size/2.
// if (size%2 == 1) that means size is odd
// so there are three conditions on size: must be >=3, <=9 AND odd
// if this is NOT true, print an error message and return. Otherwise...
if(size >=3 && size <= 9 && size%2 != 0 )
{
this.size=size;
grid = new Pad[size][size]; // declare the 2D array of Pad
for(int i = 0 ; i < size ; i++){
for(int j = 0 ; j< size ; j++){
grid[i][j] = new Pad(Pad.VACANT);
}}
}
else
{
System.out.println("Invalid size");
}
// now fill each cell with a new Pad(Pad.XXXX)
// XXX will be FROG, TOAD, or VACANT
}
/**
* Indicates whether or not all lily pads in the pond are in the solved state.
*
* @return true if the puzzle is solved and false otherwise.
*/
public boolean isSolved()
{ // use the following algorithm:
// create an int variable to count numSolved and set it to 0
// use a nested for loop to visit each cell in grid.
// If the cell at row, col is solved, then add one to numSolved
//
// after the loop, if numSolved == size*size then it's solved (return true)
// otherwise it's not (false)
int numOfSolved = 0;
for(int i = 0 ; i < size ; i++){
for(int j = 0 ; j< size ; j++){
if(grid[i][j].getStatus() != Pad.INVALID && grid[i][j].getStatus() != Pad.VACANT)
numOfSolved++;
}}
if(numOfSolved == size*size)
return true;
return false;
}
/**
* copy the javadocs here for the getPad method, then write the method
*/
public Pad getPad(int row, int col) {
// if row & col are between 0 and size-1,
// return grid[row][col]
// else return a reference to an INVALID Pad
if(row>=0 && row <size && col >= 0 && col <size)
return grid[row][col];
else
return new Pad(4);
}
/**
* copy the javadocs here for the getSize method, then write the method
*/
public int getSize(){
return size;
}
/**
* copy the javadocs here for the reset method, then write the method
*/public void reset(){
for(int i = 0 ; i < size ; i++){
for(int j = 0 ; j< size ; j++){
grid[i][j].setStatus(Pad.VACANT);
}}
}
// call Pad reset() method on all Pad objects in array grid.
/**
* Returns the current state of the pond as a String (with the VACANT, FROG,
* TOAD, and VACANT Pads represented as defined by the toString() method of
* the Pad class).
*/
public String toString(){
String result="";
for (int row=0; row < size; row++){
for (int col=0; col < size ; col++){
result = result + grid[row][col].toString(); // add the next grid Pad in a row
}
result = result + " "; // go to a new line after each row
}
return result;
}
}