Please complete my code to get it to output the correct data. Make sure I have f
ID: 3568830 • Letter: P
Question
Please complete my code to get it to output the correct data. Make sure I have finished all comments or change whatever necessary. My code gives the wrong output, but it is fairly close to being correct. Thank You
/**
* The Pad class is used to represent individual lily pads in the
* pond. Each pad keeps track of its starting status and it's
* status as the the game goes on (VACANT, FROG, TOAD, or INVALID
* - these are public symbolic constants that will also be used in
* other classes). Among other things, each Pad object needs to be
* capable of determining whether or not it is in the solved-puzzle
* configuration (ignoring all the other lily pads).
* Test app for the Pad class, verifies proper operation of a pad object
*
* @author **************
* @version 11/25/2014
**/
public class Pad
{
/** Symbolic constant representing a lilypad with a frog */
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;
}
/**
* public void reset()
* This method restores the lily pad to the state it was in when originally constructed.
*/
public void reset(){
currentState = startState;
}
/**
* setStatus public void setStatus(int status)
* Changes the current state of the lily pad. If the status is invalid (i.e., not VACANT, TOAD, or FROG),
* then setStatus sets the status of the lily pad to INVALID.
*/
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 "#";
}
}
/**
* Test app for the Pad class, verifies proper operation of a pad object
*
* @author ******************
* @version 11/25/2014
*/
public class PadTester
{
public static void main(String [] args){
Pad p = new Pad( Pad.FROG );
System.out.println("The current status of Pad p = " + p.getStatus() );
System.out.println("Pad p = " + p ); // invokes p.toString()
System.out.println("Is p solved? " + p.isSolved() ); // should say false
// TEST CASE 1 FROG start, TOAD finish
p.setStatus(Pad.TOAD);
System.out.println("Pad p = " + p ); // should say T
System.out.println("Is p solved? " + p.isSolved() ); // should say true
p.reset();
System.out.println("Pad p = " + p ); // should say F
System.out.println("Is p solved? " + p.isSolved() ); // should say false
// TEST CASE 2 TOAD start, FROG finish
Pad p2 = new Pad(Pad.TOAD);
System.out.println("Pad p2 = " + p2 ); // should say T
p2.setStatus(Pad.VACANT);
System.out.println("Pad p2 = " + p2 ); // should say O
System.out.println("Is p2 solved? " + p2.isSolved() ); // should say false
p2.setStatus(Pad.FROG);
System.out.println("Pad p2 = " + p2 ); // should say F
System.out.println("Is p2 solved? " + p2.isSolved() ); // should say true
p2.reset();
System.out.println("Pad p2 = " + p2 ); // should say T
System.out.println("Is p solved? " + p2.isSolved() ); // should say false
// TEST CASE 3 VACANT start, VACANT finish
// add your test statements here
// create a Pad VACANT. Test toStrin, isSolved and setStatus methods.
Pad p3 = new Pad(Pad.VACANT);
System.out.println("Pad p3 = " + p3 ); // should say O
p2.setStatus(Pad.VACANT);
System.out.println("Pad p3 = " + p3 ); // should say O
System.out.println("Is p3 solved? " + p3.isSolved() ); // should say true
p3.setStatus(Pad.FROG);
System.out.println("Pad p3 = " + p3 ); // should say F
System.out.println("Is p3 solved? " + p3.isSolved() ); // should say false
p3.reset();
System.out.println("Pad p3 = " + p3 ); // should say O
System.out.println("Is p3 solved? " + p3.isSolved() ); // should say true
}
}
/**
* This class represents a grid of lily pads in a pond
* for the purpose of playing Frogs and Toads.
*
* do not begin working on this until the Pad class is fully tested and debugged
* @author *************
* @version 11/25/2014
*/
public 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)
// XXXX 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 ne sted 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.FROG)
numOfSolved++;
}
}
if(numOfSolved == size*size)
return true;
return false;
}
/**
*
public Pad getPad(int row,
int column)
Returns a reference to the Pad object at the specified row and column of the pond. (note: rows and columns are counted starting from 0).
Parameters:
row - Row number (counting from 0)
column - Column number (counting from 0)
Returns:
A reference to the Pad at the specified coordinates.
*/
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);
}
/**
public int getSize()
Returns the width of the pond (which is the same as its height).
Returns: pond size
*/
public int getSize(){
return size;
}
/**
public void reset()
*Resets the pond to the state it was in when originally created.
*/
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;
}
}
/**
* Test app for the Pond class, verifies proper operation of a Pond object
*
* @author ****************
* @version 11/25/2014
*/
public class PondTester
{
public static void main(String [] args){
Pond pool = new Pond(3); // smallest size pool
System.out.println("Pond pool = " + pool ); // automatically invokes p.toString()
System.out.println("Is pool solved? " + pool.isSolved() );
//1) before starting you must fully complete and debug the Pad class first using PadTester
//2) Then in Pond class define the isSolved, getPad, getSize and reset methods, then test here
//3) activate the following test code and verify
// TEST CASE
System.out.println("Size of Pond pool = " + pool.getSize()); // should match the size you used in constructor
System.out.println("Pad 0,0 in pool is " + pool.getPad(0,0)); // should print F
System.out.println("Pad 2,2 in pool is " + pool.getPad(2,2)); // should print T
System.out.println("Pad 1,1 in pool is " + pool.getPad(1,1)); // should print V for vacant
// Let's solve the 3x3 case
//
pool.getPad(0,0).setStatus(Pad.TOAD);
pool.getPad(1,0).setStatus(Pad.TOAD);
pool.getPad(2,0).setStatus(Pad.TOAD);
pool.getPad(0,1).setStatus(Pad.FROG);
pool.getPad(2,1).setStatus(Pad.TOAD);
pool.getPad(0,2).setStatus(Pad.FROG);
pool.getPad(1,2).setStatus(Pad.FROG);
pool.getPad(2,2).setStatus(Pad.FROG);
System.out.println("Pond pool = " + pool ); // invokes p.toString()
System.out.println("Is pool solved? " + pool.isSolved() ); // should say true
pool.reset(); // back to start state
System.out.println("Pond pool = " + pool ); // invokes p.toString()
System.out.println("Is pool solved? " + pool.isSolved() ); // should say false
}
}
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;
}
}
/**
* Test app for the Pond class, verifies proper operation of a Pond object
*
* @author (your name)
* @version (a version number or a date)
*/
public class PondTester
{
public static void main(String [] args){
Pond pool = new Pond(3); // smallest size pool
System.out.println("Pond pool = " + pool ); // automatically invokes p.toString()
System.out.println("Is pool solved? " + pool.isSolved() );
//1) before starting you must fully complete and debug the Pad class first using PadTester
//2) Then in Pond class define the isSolved, getPad, getSize and reset methods, then test here
//3) activate the following test code and verify
pool.getPad(0,0).setStatus(Pad.FROG);
pool.getPad(2,2).setStatus(Pad.TOAD);
pool.getPad(1,1).setStatus(Pad.VACANT);
// TEST CASE
System.out.println("Size of Pond pool = " + pool.getSize()); // should match the size you used in constructor
System.out.println("Pad 0,0 in pool is " + pool.getPad(0,0)); // should print F
System.out.println("Pad 2,2 in pool is " + pool.getPad(2,2)); // should print T
System.out.println("Pad 1,1 in pool is " + pool.getPad(1,1)); // should print V for vacant
// Let's solve the 3x3 case
//
pool.getPad(0,0).setStatus(Pad.TOAD);
pool.getPad(1,0).setStatus(Pad.TOAD);
pool.getPad(1,1).setStatus(Pad.FROG);
pool.getPad(2,0).setStatus(Pad.TOAD);
pool.getPad(0,1).setStatus(Pad.FROG);
pool.getPad(2,1).setStatus(Pad.TOAD);
pool.getPad(0,2).setStatus(Pad.FROG);
pool.getPad(1,2).setStatus(Pad.FROG);
pool.getPad(2,2).setStatus(Pad.FROG);
System.out.println("Pond pool = " + pool ); // invokes p.toString()
System.out.println("Is pool solved? " + pool.isSolved() ); // should say true
pool.reset(); // back to start state
System.out.println("Pond pool = " + pool ); // invokes p.toString()
System.out.println("Is pool solved? " + pool.isSolved() ); // should say false
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.