Hello,

i'm justarting out with OOP with Java and i'm looking for a bit of help with a bit of code as i can't quite work out why it wont work as i'm still learning.

Here's what im being asked to do

write a public method called requestPositionsAndMove() which takes no arguments and returns no value. The method should request a use to enter a number for the position of startFrog using a dialogue box with the appropriate text. It should then do the same for the position of stopFrog. In each case it should assume that the user enters an integer, but if it is outside the range 1 to 11 inclusive a message informing the user that the positions must be in the range 1 to 11 inclusive should be displayed in a dialogue box and te request to re enter a number repeated. This should continue for as long as the user enters a number that is out of range. You should assume that the user does not press the cancel button. The method should then position startFrog and stopFrog according to the numbers entered or use a dialogue box to indicate an error has occurred and that this error is because the start and stop positions are the same.

Here is what i have so far. The code works a bit but not as it should;

public void requestPositionsAndMove()
{
String input;
int startpos = Integer.parseInt(OUDialog.request("Enter a start number between 1 & 11"));
int stoppos = Integer.parseInt(OUDialog.request("Enter a stop number between 1 & 11"));

if (startpos >11)
{
OUDialog.alert("The start number entered is not between 1 & 11 ");
OUDialog.request("Enter a start Number between 1 & 11");
}

if (stoppos >11)
{
OUDialog.alert("The stop number entered is not between 1 & 11 ");
OUDialog.request("Enter a stop Number between 1 & 11");
}
if (startpos==stoppos)
{
OUDialog.alert("An error has occurred as your start and stop positions are the same");
}
if ((startpos >=1) && (startpos <=11))
{
startFrog.setPosition(startpos);
}
if ((stoppos >=1) && (stoppos <=11))
{
stopFrog.setPosition(stoppos);
}
}

any help would be appreciated as i'm keen to learn but it's a bit of a struggle!

that code looks good, but it seems your not setting the start/stop pos to the new value when you request a new number. try something like this:

if (startpos >11)
{
OUDialog.alert("The start number entered is not between 1 & 11 ");
startpos = Integer.parseInt(OUDialog.request("Enter a start Number between 1 & 11"));

}

OOP requires elevation to a higher level of abstraction, while maintaining the natural rules of everyday life.

package frog;

/*
 * InputPosition.java
 *
 * Created on 2011-01-16, 22:22:41
 */
/**
 *
 * @author j3c
 */
public class InputPositions {

    private static final int START = 1;
    private static final int STOP = 11;
    private int i;//startpos
    private int j;//stoppos
    private final Frog frog;

    InputPositions(Frog frog) {
        this.frog = frog;
        // both i,j set to not valid 
        i = -1;
        j = -1;
    }

    private boolean isIValid() {
        return (i >= START) && (i <= STOP);
    }

    private boolean isJValid() {
        return (j >= START) && (j <= STOP);
    }

    private boolean isSame() {
        return i == j;
    }

    private boolean isBothValid() {
        return isIValid() && isJValid() && !isSame();
    }

    public void requestPositionsAndMove() {
        // once more, needed fo for next use
        // both i,j set to not valid 
        i = -1;
        j = -1;
        while (!isBothValid()) {
            //1. i
            while (!isIValid()) {
                i = Integer.parseInt(OUDialog.request("Enter a START number between " + START + " & " + STOP));
                if (!isIValid()) {
                    OUDialog.alert("The START number entered is not between " + START + " & " + STOP);
                }
            }
            // i valid
            //------------
            //2. j
            while (!isJValid()) {
                j = Integer.parseInt(OUDialog.request("Enter a STOP number between " + START + " & " + STOP));
                if (!isIValid()) {
                    OUDialog.alert("The STOP number entered is not between " + START + " & " + STOP);
                }
            }
            // j valid
            //------------
            if (isSame()) {
                OUDialog.alert("An error has occurred as your start and stop positions are the same");
            }
        }
        // both valid
        frog.setStartPosition(i);
        frog.setStopPosition(j);
    }
}

I hope that the analysis of this example will bring you to this way of thinking.
I left a small mistake, I think You catch it.

thanks quuba,

i'll have a go at rewriting the code and let you know how i get on

Main logic of program is:
START <= i < j <= STOP
This mean that j can't be == START and i can't be == STOP.
Be sure, that all dependencies are checked.
Monkeeboy, you can use in place of while(condition){body} structure, another one do{body}while(condition)

Hi Guys,

thanks for your help but i'm just not getting it to work . I need to use a while loop but it's just endlessly looping and i can't figure it out

here my new code the bit i'm struggling with is the while codes at the end the part where the user enters a start position and a stop position.

import ou.*;
/**
 * Class Workout - orchestrates the collaboration of three frogs. 
 * Two frogs act as fitness coaches to a third frog that must
 * walk between them.
 * 
 * @author M255 CT
 * @version version 1.0
 */

public class Workout
{
   /* instance variables */
    
   private Frog startFrog;
   private Frog stopFrog;
   private Frog exercisingFrog;
     
     
   
  /**
   * Constructor for objects of class Workout
   */
   public Workout(Frog start, Frog stop, Frog walker)
   {
      super();
      this.startFrog = start;
      this.startFrog.setColour(OUColour.BLUE);
      this.stopFrog = stop;
      this.stopFrog.setColour(OUColour.RED);
      this.exercisingFrog = walker;
      this.exercisingFrog.setColour(OUColour.YELLOW);
          
   }

  /* instance methods */
  
   /** 
    * Returns the receiver's startFrog
    */
   public Frog getStartFrog()
   {
      return this.startFrog;
   }

   /** 
    * Returns the receiver's stopFrog
    */
   public Frog getStopFrog()
   {
      return this.stopFrog;
   }
   
   /** 
    * Returns the receiver's exercisingFrog
    */
   public Frog getExercisingFrog()
   {
      return this.exercisingFrog;
   }
   
   /** 
    * moves the receivers startFrog and StopFrog to the value of the arguments. 
    */   
   public boolean moveCoachFrogs(int begin,int end)
   {                   
      if (begin==end)
      {
         return false;
      }
      if (begin!=end)
      {
        startFrog.setPosition(begin);
        stopFrog.setPosition(end);
        exercisingFrog.setPosition(startFrog.getPosition());
      
        while (exercisingFrog.getPosition()>stopFrog.getPosition())
        {
           exercisingFrog.left();
        }
        while (exercisingFrog.getPosition()<stopFrog.getPosition())
        {
            exercisingFrog.right();
        }               
      }
         return true;
   }
   
   /** 
    * moves the receiver exercisingFrog directly to the position of the receiver startFrog then to the position of the 
    * receiver stopFrog one stone at a time. 
    */   
   public void moveBetween()
   {                   
        exercisingFrog.setPosition(startFrog.getPosition());
        while (exercisingFrog.getPosition()>stopFrog.getPosition())
        {
           exercisingFrog.left();
        }
        while (exercisingFrog.getPosition()<stopFrog.getPosition())
        {
            exercisingFrog.right();
        }    
 
    }
   
   /** 
    * asks the user to enter a start and stop number for the receivers startFrog and stopFrog respectively and moves the 
    * receivers to these positions providing the numbers entered are between 1 & 11 otherwise an alert message is given advising the user
    * that the numbers enteres are outwith this range. 
    */   
   public void requestPositionsAndMove()
   {
      String input;
      int startpos = Integer.parseInt(OUDialog.request("Enter a start number between 1 & 11"));
    
          while (startpos <1 || startpos >11) 
          {
            OUDialog.alert("The start number entered is not between 1 & 11 ");
            startpos = Integer.parseInt(OUDialog.request("Enter a start Number between 1 & 11"));
          }
 
         int stoppos = Integer.parseInt(OUDialog.request("Enter a stop number between 1 & 11"));
         while (startpos == stoppos)
         {
         
            while (stoppos <1 || stoppos >11)
            {
               OUDialog.alert("The stop number entered is not between 1 & 11 ");
               stoppos = Integer.parseInt(OUDialog.request("Enter a stop Number between 1 & 11"));
            }
            startFrog.setPosition(startpos);
            stopFrog.setPosition(stoppos);
            {
               OUDialog.alert("An error has occurred as your start and stop positions are the same");
            }
         }
    }
}

i'm trying hard to learn but its a real struggle so any help you can offer will be really appreciated

thanks

Thanks for all your help, i've got it working now. all it needed was an if statement!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.