Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    OOP with Java help with if and while loops?

    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 user 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 you 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 the 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, but it does not work as it should;

    Code:
    }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);
    }
    }

  • #2
    New to the CF scene
    Join Date
    Jan 2011
    Location
    USA, California
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    String input; //non used variable
    I've only see, that here there's no one more check for the case when startpos < stoppos. In this case you'll gain undefined behaviour and it fails
    In another looks like compiliable.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This is close to the described method. You're just missing a loop.
    Use a do/while loop as it will allow you to write pretty much a single block of code. Do/While will always execute the do then evaluate, unlike the while where it needs to evaluate first before processing.
    PHP Code:
    String input;
    int startpos 0stoppos 0;
    boolean isError;
    do
    {
        
    isError false;
        
    startpos Integer.parseInt(OUDialog.request("Enter a start number between 1 & 11"));
        
    stoppos Integer.parseInt(OUDialog.request("Enter a stop number between 1 & 11"));

        if (
    startpos <= || startpos 11 || stoppos <= || stoppos 11)
        {
            
    OUDialog.alert("The start number entered is not between 1 & 11 ");
            
    isError true;
        }
    } while (
    isError); 
    I decided in the end to add an isError variable. This is only becuase I didn't want to re-type all the startpos / stoppos conditions into the while loop.

    Following this, you just need to check if startpos = stoppos, and set the values for startpos and stoppos (no need to check them, we know they are valid).
    Because there is no error checking (don't know if exceptions have been covered for you or not), and I don't know what that OUDialog will do in the event of hitting cancel, I cannot guarantee that startpos or stoppos will be properly populated (these are things you'll need to test).
    The description does not indicate that you are to handle the error on the startpos = stoppos, only to indicate that an error has occurred (which if you've covered exceptions is what I'd toss). If the intent is to re-ask the user for input on that event, you simply pack everything away into the do/while, and use a series of if checks against that isError (appropriately setting it to true when an error exists).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks Fou-lu,

    i've not covered exceptions or do/while loops so i'm not sure how to re-write the code but i've had a go with a while loop as suggested but it's not quite right, but it is getting there as it does everything it should do only while it should do nothing if the startpos and the stoppos are the same only give an alert message to indicate that an error has occurrede, the alert error message pops up, but the frogs still move to the positions the user has entered. Also i have to assume that "the user does not hit the cancel button"

    Here is what my new code looks like with my while loop;

    /**
    * 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 (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");

    }

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Thats the simple one - you are just missing a conditional check after verifying that the startpos and stoppos are valid.
    A simple if is all you need:
    PHP Code:
    if (startpos != stoppos)
    {
        
    startFrog.setPosition(startpos);
        
    stopFrog.setPosition(stoppos);
    }
    else
    {
       
    OUDialog.alert("They are the same");

    I'm not sure if the startFrog and stopFrog are valid as I don't see any particular construction of those. To me it seems that those should be the same object, but the two names would indicate you are working with two different objects.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •