Frog.java:
public class Frog {
    private int myCoin;     // current coin toss (0 = heads, 1 = tails)
    private int myHome;     // number of times frog visits 0
    private int myMax;      // most positive position so far
    private int myMin;      // most negative position so far
    private int myPosition; // current position
    private int mySteps;    // total hops so far

    // constructor
    public Frog() {
       myCoin = 0;
       myHome = 0;
       myMax = 0;
       myMin = 0;
       myPosition = 0;
       mySteps = 0;
    }

    //accessor methods
    public int GetHome() {
       return myHome;
    }

    public int GetMax() {
       return myMax;
    }

    public int GetMin() {
       return myMin;
    }

    public int GetPosition() {
       return myPosition;
    }

    public int GetSteps() {
       return mySteps;
    }

    //modifier methods
    public void Hop() {
       myCoin = (int)(2*Math.random());  // Flip the coin   
       if (myCoin == 0)                  // Update the position
          myPosition = myPosition + 1;
       else
          myPosition = myPosition - 1;
       mySteps = mySteps + 1;            // Update the number of steps
       if (myPosition == 0)              // Update home visits
          myHome = myHome + 1;
       if (myPosition > myMax)           // New position biggest so far
          myMax = myPosition;
       if (myPosition < myMin)           // New position smallest so far
          myMin = myPosition;
    }
}
  • FrogMain.java
    public class FrogMain {
    
        /**
         * Constructor
         */
        public FrogMain () {
        }
    
        public static void main (Strings args[]) {
           Frog kermit = new Frog();
           kermit.Hop();
           kermit.Hop();
           System.out.println("Kermit has hopped " + kermit.GetSteps()
                     + " times and is now at " + kermit.GetPosition());
           Frog kirby = new Frog();
           kirby.Hop();
           kermit.Hop();
        }
    }