Welcome to Dream.In.Code
Become a Java Expert!

Join 150,201 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,018 people online right now. Registration is fast and FREE... Join Now!




Frog Jumper Program PLEASE HELP!

2 Pages V  1 2 >  
Reply to this topicStart new topic

Frog Jumper Program PLEASE HELP!

3vilpyro
24 Sep, 2008 - 05:50 PM
Post #1

New D.I.C Head
*

Joined: 21 Sep, 2008
Posts: 7

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:

CODE

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
    private static Lilipad lilies;
    Lilipad pond[][] = new Lilipad[pondWidth][pondHeight];
    //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++){
              pond [i] [j]= new Lilipad(i,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:

CODE

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:
CODE



public class Lilipad {

       private int x;
       private int y;

       public Lilipad(int xTemp, int yTemp)
       {
               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! =(
Please help
User is offlineProfile CardPM
+Quote Post

pbl
RE: Frog Jumper Program PLEASE HELP!
24 Sep, 2008 - 06:03 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
This code is kind of useless

CODE

    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

CODE

    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;
        }
    }

User is offlineProfile CardPM
+Quote Post

3vilpyro
RE: Frog Jumper Program PLEASE HELP!
24 Sep, 2008 - 06:31 PM
Post #3

New D.I.C Head
*

Joined: 21 Sep, 2008
Posts: 7

Okay, now my frog never escapes! What am I doing wrong? =(
User is offlineProfile CardPM
+Quote Post

pbl
RE: Frog Jumper Program PLEASE HELP!
24 Sep, 2008 - 06:41 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(3vilpyro @ 24 Sep, 2008 - 07:31 PM) *

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
Look at your original post:

CODE

    //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
CODE

        else
            {
            pond[froggy.getNewXCoor() - froggy.getOldXCoor()][froggy.getNewYCoor() - froggy.getOldYCoor()] = froggy;
            int lastX = froggy.getOldXCoor();

User is offlineProfile CardPM
+Quote Post

3vilpyro
RE: Frog Jumper Program PLEASE HELP!
24 Sep, 2008 - 06:47 PM
Post #5

New D.I.C Head
*

Joined: 21 Sep, 2008
Posts: 7

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

Pond class:
CODE


//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
    Lilipad pond[][] = new Lilipad[pondWidth][pondHeight];
    //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++){
              pond [i] [j]= new Lilipad(i,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:
CODE

import java.util.Random;


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


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


public Lilipad getcurrentPosition(){
    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;
    }
}


}


My Lilipad Class:
CODE



public class Lilipad {

       private int x;
       private int y;

       public Lilipad(int xTemp, int yTemp)
       {
               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;
           }

}

User is offlineProfile CardPM
+Quote Post

pbl
RE: Frog Jumper Program PLEASE HELP!
24 Sep, 2008 - 06:51 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Come on... you are posting code that does not even compile

CODE

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


Frog does not have a method getCurrentPosition();

CODE

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

User is offlineProfile CardPM
+Quote Post

3vilpyro
RE: Frog Jumper Program PLEASE HELP!
24 Sep, 2008 - 06:55 PM
Post #7

New D.I.C Head
*

Joined: 21 Sep, 2008
Posts: 7

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:

public Lilipad getcurrentPosition(){
return currentPosition;
}

User is offlineProfile CardPM
+Quote Post

pbl
RE: Frog Jumper Program PLEASE HELP!
24 Sep, 2008 - 07:05 PM
Post #8

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(3vilpyro @ 24 Sep, 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:

public Lilipad getcurrentPosition(){
return currentPosition;
}

OK I guess my version of Frog was outdated
I'll have a look
Give me 10 minutes
User is offlineProfile CardPM
+Quote Post

pbl
RE: Frog Jumper Program PLEASE HELP!
24 Sep, 2008 - 07:21 PM
Post #9

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
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

CODE

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
    Lilipad pond[][] = new Lilipad[pondWidth][pondHeight];
    //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++){
                pond [i] [j]= new Lilipad(i,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();
    }

}

User is offlineProfile CardPM
+Quote Post

3vilpyro
RE: Frog Jumper Program PLEASE HELP!
24 Sep, 2008 - 07:30 PM
Post #10

New D.I.C Head
*

Joined: 21 Sep, 2008
Posts: 7

Okay, I solved that problem =)
Do you know why my frog isn't moving? =S
User is offlineProfile CardPM
+Quote Post

pbl
RE: Frog Jumper Program PLEASE HELP!
24 Sep, 2008 - 07:55 PM
Post #11

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(3vilpyro @ 24 Sep, 2008 - 08:30 PM) *

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

A few other println() show what is happening:

CODE

    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





User is offlineProfile CardPM
+Quote Post

3vilpyro
RE: Frog Jumper Program PLEASE HELP!
24 Sep, 2008 - 08:01 PM
Post #12

New D.I.C Head
*

Joined: 21 Sep, 2008
Posts: 7

Why are the only results 1 and -1? Shouldn't the random generator be giving me 0, 1 or 2 every time?
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:04AM