• (2 Pages)
• 1
• 2

## 16 Replies - 4824 Views - Last Post: 25 September 2008 - 09:55 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=64997&amp;s=b7cecc89e95a2516c6a79257cfe694f9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 3vilpyro

Reputation: 0
• Posts: 7
• Joined: 21-September 08

Posted 24 September 2008 - 06:50 PM

This is my assignment for data structures: (due by midnight ><)
"Imagine a pond with a frog in it. The pond is a 9x9 array with the frog in the center. Each square in the array is a lilypad.

Part 1: Have the frog do a set of 20 random jumps to adjacent lilypads and see if the frog jumps out of the pond. Stop if the frog does jump out of the pond. Repeat this set of 100 many times, i.e. put the frog back to the center each time, and keep a record of what percentage of the time the frog manages to leap out of the pond. The frog may come back to any lilypad any number of times.

Random jumping: the frog can jump any of 8 directions and lets assume they are all equally likely. Note that this is a bit different from random choices of deltaX = -1, 0 ,1 and deltaY = -1, 0, 1, so how would you implement an eight-choice random move?

Part 2: Keep a record of where the frog ends up at the end of each set of 100 jumps and at the end print out an array of these results."

I've almost got it, but i keep getting a type mismatch when I try to set the frog's position in the array. Here's my code thus far:

```public class Pond {
//The frog's starting position
private int frogStartX = 5;
private int frogStartY = 5;
//The size of the pond
private int pondWidth = 9;
private int pondHeight = 9;
//Max number of jumps
private int maxJumps = 20;
//The frog's last known position
private int lastX;
private int lastY;
//Number of times the frog would escape the pond
private int timesEscaped;
//The frog
private static Frog froggy;
//The pond
//The results
private static int iterations = 1000;
String results[] [] = new String[3][iterations +1];

public Pond(){
//generates the frog
froggy = new Frog(frogStartX, frogStartY);
//create 2-D pond matrix
for (int i=0; i < pondWidth; i++){
for (int j=0; j < pondHeight; j++){
}
}
//puts the frog in middle of the pond
pond [frogStartX] [frogStartY] =
}

public void reportresults(){
System.out.println("The Results of the Frog Jumper Simulator:");
System.out.println("Total number of trials: " + iterations);
System.out.println("Total number of times the frog escaped the pond: " + timesEscaped);
System.out.println("Total number of times the frog remained in the pond: " + (iterations - timesEscaped));
System.out.println("Last known position of the frog at the end of each iteration: ");
}

//Runs one twenty jump iteration
public int startSimulation(){
String results[] [] = new String[3][iterations +1];
String trial = "trial number";
String lastknownPosition = "Last known coordinates";

results [0] [0] = trial;
results [0] [1] = lastknownPosition;

for(int iterations = 0; iterations<1000; iterations++){
for (int jumps = 0; jumps<maxJumps; jumps++){
froggy.jumpFrogjump(froggy.getNewXCoor(),froggy.getNewYCoor());
if (froggy.getNewXCoor() - froggy.getOldXCoor() < 0 || froggy.getNewXCoor() - froggy.getOldXCoor()>pondWidth || froggy.getNewYCoor() - froggy.getOldYCoor()<0 || froggy.getNewYCoor() - froggy.getOldYCoor()>pondHeight)
{
timesEscaped++;
int lastX = froggy.getOldXCoor();
int lastY = froggy.getOldYCoor();
results [iterations +1][1] = toString(lastX, lastY);
return lastX;
}
else
{
pond[froggy.getNewXCoor() - froggy.getOldXCoor()][froggy.getNewYCoor() - froggy.getOldYCoor()] = froggy;
int lastX = froggy.getOldXCoor();
int lastY = froggy.getOldYCoor();
results [iterations +1][1] = toString(lastX, lastY);
}

return lastX;
}
}
}
public String toString(int lastX, int lastY)
{
return "X=" + lastX + " " + "Y=" + lastY;
}

public static void main(String[] args) {
for (int iterations = 0; iterations<1000; iterations++){
Pond littlePond = new Pond();
littlePond.startSimulation();
//littlePond.reportResults();
}
}

}

```

Here is my frog class:

```import java.util.Random;

public class Frog {
int currentX;
int currentY;
int newXCoor;
int newYCoor;
int xCoor;
int yCoor;
boolean escaped;

public Frog(int xTemp, int yTemp)
{
xCoor = xTemp;
yCoor = yTemp;
}

public void setXPosition(int XCoor){
newXCoor = XCoor;
}

public void setYPosition(int YCoor){
newYCoor = YCoor;
}

public int getOldXCoor(){
return currentX;
}

public int getOldYCoor(){
return currentY;
}

public int getNewXCoor(){
return newXCoor;
}

public int getNewYCoor(){
return newYCoor;
}

public void jumpFrogjump(int xNewPosition, int yNewPosition){
Random directionPick = new Random();
if (newXCoor == 0 && newYCoor == 0){
int newXCoor = directionPick.nextInt(3)-1;
int newYCoor = directionPick.nextInt(3)-1;
}
}

//Brings the frog back to the center of the pond
public void reset(){
//pond[frogStartX][frogStartY] = froggy;

}

}

```

And here is my lilipad class:
```

private int x;
private int y;

{
x = xTemp;
y = yTemp;
}

public int getX()
{
return x;
}

public int getY()
{
return y;
}

public void setX(int xTemp)
{
x = xTemp;
}

public void setY(int yTemp)
{
y = yTemp;
}

public String toString()
{
return "X=" + x + " " + "Y=" + y;
}

}

```

I've never used coordinates before and I have no idea how to do it! =(

Is This A Good Question/Topic? 0

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Posted 24 September 2008 - 07:03 PM

This code is kind of useless

```	public void jumpFrogjump(int xNewPosition, int yNewPosition){
Random directionPick = new Random();
if (newXCoor == 0 && newYCoor == 0){
int newXCoor = directionPick.nextInt(3)-1;
int newYCoor = directionPick.nextInt(3)-1;
}
}

```

Without going through all the logic of your cute problem

You put the nextInt(3)-1; into 2 new int (newXCoor and newYCoor) that you don't use.
But you have instance variables newXCoor and newYCoor... you sure you didn't want to write

```	public void jumpFrogjump(int xNewPosition, int yNewPosition){
Random directionPick = new Random();
if (newXCoor == 0 && newYCoor == 0){
newXCoor = directionPick.nextInt(3)-1;
newYCoor = directionPick.nextInt(3)-1;
}
}

```

### #3 3vilpyro

Reputation: 0
• Posts: 7
• Joined: 21-September 08

Posted 24 September 2008 - 07:31 PM

Okay, now my frog never escapes! What am I doing wrong? =(

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Posted 24 September 2008 - 07:41 PM

3vilpyro, on 24 Sep, 2008 - 07:31 PM, said:

Okay, now my frog never escapes! What am I doing wrong? =(

I guess you'll have to repost your Pond class
You did something wrong in your cut & paste

```	//puts the frog in middle of the pond
pond [frogStartX] [frogStartY] =			<----- what is going here
}

```

pond[][] is a Lililpad... how can you put a Frog into it ?
I mean in the real life you can put a frog on a lilipad
but in Java, you cannot assign to a variable of type Lilipad a Grog object
```		else
{
pond[froggy.getNewXCoor() - froggy.getOldXCoor()][froggy.getNewYCoor() - froggy.getOldYCoor()] = froggy;
int lastX = froggy.getOldXCoor();

```

### #5 3vilpyro

Reputation: 0
• Posts: 7
• Joined: 21-September 08

Posted 24 September 2008 - 07:47 PM

Yeah, I figured that part out about the type mismatch, I'm still not sure if I'm doing it correctly though:

Pond class:
```//import java.util*;

public class Pond {
//The frog's starting position
private int frogStartX = 5;
private int frogStartY = 5;
//The size of the pond
private int pondWidth = 9;
private int pondHeight = 9;
//Max number of jumps
private int maxJumps = 20;
//Number of times the frog would escape the pond
private int timesEscaped;
//The frog
private static Frog froggy;
//The pond
//The results
private static int iterations = 1000;
String results[] [] = new String[3][iterations +1];

public Pond(){
//generates the frog
froggy = new Frog(frogStartX, frogStartY);
//create 2-D pond matrix
for (int i=0; i < pondWidth; i++){
for (int j=0; j < pondHeight; j++){
}
}
//puts the frog in middle of the pond
pond [frogStartX] [frogStartY] = froggy.getcurrentPosition();
}

public void reportresults(){
System.out.println("The Results of the Frog Jumper Simulator:");
System.out.println("Total number of trials: " + iterations);
System.out.println("Total number of times the frog escaped the pond: " + timesEscaped);
System.out.println("Total number of times the frog remained in the pond: " + (iterations - timesEscaped));
System.out.println("Last known position of the frog at the end of each iteration: ");
}

//Runs one twenty jump iteration
public void startSimulation(){
String results[] [] = new String[3][iterations +1];
String trial = "trial number";
String lastknownPosition = "Last known coordinates";

results [0] [0] = trial;
results [0] [1] = lastknownPosition;

for(int iterations = 1; iterations<10; iterations++){
for (int jumps = 0; jumps<maxJumps; jumps++){
froggy.jumpFrogjump(froggy.getNewXCoor(),froggy.getNewYCoor());
if (froggy.getNewXCoor() - froggy.getOldXCoor() < 0 || froggy.getNewXCoor() - froggy.getOldXCoor()>pondWidth || froggy.getNewYCoor() - froggy.getOldYCoor()<0 || froggy.getNewYCoor() - froggy.getOldYCoor()>pondHeight)
{
timesEscaped++;
int lastX = froggy.getOldXCoor();
int lastY = froggy.getOldYCoor();
results [1][iterations] = toString(lastX, lastY);
jumps = 0;
}
else
{
pond[froggy.getNewXCoor() - froggy.getOldXCoor()][froggy.getNewYCoor() - froggy.getOldYCoor()] = froggy.getcurrentPosition();
int lastX = froggy.getOldXCoor();
int lastY = froggy.getOldYCoor();
results [1][iterations +1] = toString(lastX, lastY);
}

}
}
reportresults();
for (int row = 0; row<results.length; row++){
System.out.print(row + 1);
for (int column = 0; column<results[row].length; column++){
System.out.println(results[row][column]);
}
}
}
public String toString(int lastX, int lastY)
{
return "X=" + lastX + " " + "Y=" + lastY;
}

public static void main(String[] args) {
Pond littlePond = new Pond();
littlePond.startSimulation();
}

}

```

My Frog Class:
```import java.util.Random;

public class Frog {
int currentX;
int currentY;
int newXCoor;
int newYCoor;
int xCoor;
int yCoor;

public Frog(int xTemp, int yTemp)
{
xCoor = xTemp;
yCoor = yTemp;
}

return currentPosition;
}

public void setXPosition(int XCoor){
newXCoor = XCoor;
}

public void setYPosition(int YCoor){
newYCoor = YCoor;
}

public int getOldXCoor(){
return currentX;
}

public int getOldYCoor(){
return currentY;
}

public int getNewXCoor(){
return newXCoor;
}

public int getNewYCoor(){
return newYCoor;
}

public void jumpFrogjump(int xNewPosition, int yNewPosition){
Random directionPick = new Random();
if (newXCoor == 0 && newYCoor == 0){
newXCoor = directionPick.nextInt(3)-1;
newYCoor = directionPick.nextInt(3)-1;
}
}

}

```

```

private int x;
private int y;

{
x = xTemp;
y = yTemp;
}

public int getX()
{
return x;
}

public int getY()
{
return y;
}

public void setX(int xTemp)
{
x = xTemp;
}

public void setY(int yTemp)
{
y = yTemp;
}

public String toString()
{
return "X=" + x + " " + "Y=" + y;
}

}

```

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Posted 24 September 2008 - 07:51 PM

Come on... you are posting code that does not even compile

```		//puts the frog in middle of the pond
pond [frogStartX] [frogStartY] = froggy.getcurrentPosition();

```

Frog does not have a method getCurrentPosition();

```pond[froggy.getNewXCoor() - froggy.getOldXCoor()][froggy.getNewYCoor() - froggy.getOldYCoor()] = froggy.getcurrentPosition();

```

### #7 3vilpyro

Reputation: 0
• Posts: 7
• Joined: 21-September 08

Posted 24 September 2008 - 07:55 PM

My code does compile and it runs, the results are just null which they should not be. And I do have a getcurrentPosition method in Frog:

return currentPosition;
}

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Posted 24 September 2008 - 08:05 PM

3vilpyro, on 24 Sep, 2008 - 07:55 PM, said:

My code does compile and it runs, the results are just null which they should not be. And I do have a getcurrentPosition method in Frog:

return currentPosition;
}

OK I guess my version of Frog was outdated
I'll have a look
Give me 10 minutes

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Posted 24 September 2008 - 08:21 PM

One thing for sure you only assign values to results[1][iteration]

don't expect to have anything else than null in all result[0][*] and result[2][*]

a few println with smaller values show what is happening

```public class Pond {
//The frog's starting position
private int frogStartX = 2;	// 5
private int frogStartY = 2;	// 5
//The size of the pond
private int pondWidth = 9;
private int pondHeight = 9;
//Max number of jumps
private int maxJumps = 5; // 20;
//Number of times the frog would escape the pond
private int timesEscaped;
//The frog
private static Frog froggy;
//The pond
//The results
private static int iterations = 5; /// 1000;
String results[] [] = new String[3][iterations +1];

public Pond(){
//generates the frog
froggy = new Frog(frogStartX, frogStartY);
//create 2-D pond matrix
for (int i=0; i < pondWidth; i++){
for (int j=0; j < pondHeight; j++){
}
}
//puts the frog in middle of the pond
pond [frogStartX] [frogStartY] = froggy.getcurrentPosition();
}

public void reportresults(){
System.out.println("The Results of the Frog Jumper Simulator:");
System.out.println("Total number of trials: " + iterations);
System.out.println("Total number of times the frog escaped the pond: " + timesEscaped);
System.out.println("Total number of times the frog remained in the pond: " + (iterations - timesEscaped));
System.out.println("Last known position of the frog at the end of each iteration: ");
}

//	Runs one twenty jump iteration
public void startSimulation(){
String results[] [] = new String[3][iterations +1];
String trial = "trial number";
String lastknownPosition = "Last known coordinates";

results [0] [0] = trial;
results [0] [1] = lastknownPosition;

for(int iterations = 1; iterations<10; iterations++){
for (int jumps = 0; jumps<maxJumps; jumps++){
froggy.jumpFrogjump(froggy.getNewXCoor(),froggy.getNewYCoor());
if (froggy.getNewXCoor() - froggy.getOldXCoor() < 0 || froggy.getNewXCoor() - froggy.getOldXCoor()>pondWidth || froggy.getNewYCoor() - froggy.getOldYCoor()<0 || froggy.getNewYCoor() - froggy.getOldYCoor()>pondHeight)
{
timesEscaped++;
int lastX = froggy.getOldXCoor();
int lastY = froggy.getOldYCoor();
results [1][iterations] = toString(lastX, lastY);
System.out.println("Setting results[1][" + iterations + "] to that value");
jumps = 0;
}
else
{
pond[froggy.getNewXCoor() - froggy.getOldXCoor()][froggy.getNewYCoor() - froggy.getOldYCoor()] = froggy.getcurrentPosition();
int lastX = froggy.getOldXCoor();
int lastY = froggy.getOldYCoor();
results [1][iterations +1] = toString(lastX, lastY);
System.out.println("Setting results[1][" + iterations+1 + "] to that value");
}

}
}
reportresults();
for (int row = 0; row<results.length; row++){
System.out.print(row + 1);
for (int column = 0; column<results[row].length; column++){
System.out.println("result[" + row + "][" + column + "] : " + results[row][column]);
}
}
}
public String toString(int lastX, int lastY)
{
String str = "X=" + lastX + " " + "Y=" + lastY;
System.out.println("toString returns: " + str);
return str;
}

public static void main(String[] args) {
Pond littlePond = new Pond();
littlePond.startSimulation();
}

}

```

### #10 3vilpyro

Reputation: 0
• Posts: 7
• Joined: 21-September 08

Posted 24 September 2008 - 08:30 PM

Okay, I solved that problem =)
Do you know why my frog isn't moving? =S

### #11 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Posted 24 September 2008 - 08:55 PM

3vilpyro, on 24 Sep, 2008 - 08:30 PM, said:

Okay, I solved that problem =)
Do you know why my frog isn't moving? =S

A few other println() show what is happening:

```	public void jumpFrogjump(int xNewPosition, int yNewPosition){
Random directionPick = new Random();
System.out.println("In jumpFrogPosition: newXCoor: " + newXCoor + " newYCoor: " + newYCoor);
if (newXCoor == 0 && newYCoor == 0){
newXCoor = directionPick.nextInt(3)-1;
newYCoor = directionPick.nextInt(3)-1;
System.out.println("Frog new position: " + newXCoor + "," + newYCoor);
}
}

```

newXCoor and newYCoor are always -1 so the if is never executed
Question: why does jumpFrogPosition receives 2 parameters if you never used them ?

By the way... put the creation of

Random directionPick = new Random();

in the constructor or Frog and make directionPick an instance variable... no need to create a new Random object every time this methos is called

### #12 3vilpyro

Reputation: 0
• Posts: 7
• Joined: 21-September 08

Posted 24 September 2008 - 09:01 PM

Why are the only results 1 and -1? Shouldn't the random generator be giving me 0, 1 or 2 every time?

### #13 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Posted 24 September 2008 - 09:05 PM

3vilpyro, on 24 Sep, 2008 - 09:01 PM, said:

Why are the only results 1 and -1? Shouldn't the random generator be giving me 0, 1 or 2 every time?

Yes it will but you never enter in

if (newXCoor == 0 && newYCoor == 0){

so it is never called.

I think you made this simple problem a lot to much complicated.

You do not need a Lilipad class just an array of positions [9][9]

Do you have half an hour ?

### #14 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Posted 24 September 2008 - 09:34 PM

OK I have your Frog moving
Now the second part... the stats after whatever number of trials...

Are you still there ?

```import java.util.Random;

public class Pond {

private final int POND_WIDTH = 9, POND_HEIGHT = 9;

private int frogX, frogY;
private Random ran;

public Pond(){
// init frog position
frogX = POND_WIDTH / 2;
frogY = POND_HEIGHT / 2;
// create Random generator
ran = new Random();
}

public void startSimulation() {
System.out.println("Frog starts at [" + frogX + "][" + frogY + "]");
// lets try 20 jumps
for(int i = 0; i < 20; i++) {
if(moveFrog()) {
System.out.println("Frog exits pond after " + i + " jumps it would be at [" + frogX + "][" + frogY + "]");
break;
}
System.out.println("Frog is now at [" + frogX + "][" + frogY +"]");
}
}

// move the frog in a delta X and delta Y position
// return true if out of pond, else return false
private boolean moveFrog() {
int deltaX = 0;
int deltaY = 0;
// make sure both delta are not 0
while(deltaX == 0 && deltaY == 0) {
deltaX = ran.nextInt(2) - 1;
deltaY = ran.nextInt(2) - 1;
}
// update frog position
frogX += deltaX;
frogY += deltaY;
System.out.print("Direction: ");
// display direction
switch(deltaX) {
case -1:
System.out.print("left ");
break;
case 0:
System.out.print("same X ");
break;
case 1:
System.out.print("right ");
break;
}
switch(deltaY) {
case -1:
System.out.print("up ");
break;
case 0:
System.out.print("same Y ");
break;
case 1:
System.out.print("down ");
break;
}
// check if out of pound
if(frogX < 0 || frogX >= POND_WIDTH)
return true;
if(frogY < 0 || frogY >= POND_HEIGHT)
return true;
return false;
}

public static void main(String[] args) {
Pond littlePond = new Pond();
littlePond.startSimulation();
}
}

```

This post has been edited by pbl: 24 September 2008 - 09:51 PM

### #15 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Posted 24 September 2008 - 09:47 PM

Here it is with 10 simulations
I am afraid your frog always manage to exit

I let you complete the code for the recall history
Should be a lot clearer now
You can remove a lot of the print() and println() statements

```import java.util.Random;

public class Pond {

private final int POND_WIDTH = 9, POND_HEIGHT = 9;
private final int NB_TRIAL = 10;

private int frogX, frogY;
private Random ran;
private int manageToExit;
private int stayedInPond;

public Pond(){
// create Random generator
ran = new Random();
manageToExit = 0;
stayedInPond = NB_TRIAL;
}

public void startSimulation() {

for(int j = 0; j < NB_TRIAL; j++)
{
// init frog position
frogX = POND_WIDTH / 2;
frogY = POND_HEIGHT / 2;

System.out.println(" Simul #" + (j+1) + " Frog starts at [" + frogX + "][" + frogY + "]");
// lets try 20 jumps
for(int i = 0; i < 20; i++) {
if(moveFrog()) {
System.out.println("Frog exits pond after " + i + " jumps it would be at [" + frogX + "][" + frogY + "]");
manageToExit++;
stayedInPond--;
break;
}
System.out.println("\tFrog is now at [" + frogX + "][" + frogY +"]");
}
}

System.out.println("After " + NB_TRIAL + " Frog stayed in pond " + stayedInPond +" times " +
" and exited " + manageToExit + " times");

}

// move the frog in a delta X and delta Y position
// return true if out of pond, else return false
private boolean moveFrog() {
int deltaX = 0;
int deltaY = 0;
// make sure both delta are not 0
while(deltaX == 0 && deltaY == 0) {
deltaX = ran.nextInt(2) - 1;
deltaY = ran.nextInt(2) - 1;
}
// update frog position
frogX += deltaX;
frogY += deltaY;
System.out.print("Direction: ");
// display direction
switch(deltaX) {
case -1:
System.out.print("left ");
break;
case 0:
System.out.print("same X ");
break;
case 1:
System.out.print("right ");
break;
}
switch(deltaY) {
case -1:
System.out.print("up ");
break;
case 0:
System.out.print("same Y ");
break;
case 1:
System.out.print("down ");
break;
}
// check if out of pound
if(frogX < 0 || frogX >= POND_WIDTH)
return true;
if(frogY < 0 || frogY >= POND_HEIGHT)
return true;
return false;
}

public static void main(String[] args) {
Pond littlePond = new Pond();
littlePond.startSimulation();
}
}

```

This post has been edited by pbl: 24 September 2008 - 09:50 PM