Page 1 of 3 123 LastLast
Results 1 to 20 of 41
  1. #1
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Post Help with Assigment needed.

    Hi All

    I need to Write a public instance method called selectRunners() which takes no arguments and returns no message answer. Within an outer loop (that will execute over and over until at least two racingFrogs are selected) the method should first set each racingFrog’s selected instance variable to false.
    Then within an inner loop that should execute three times, once for each racingFrog, the method should display a dialogue box to confirm if that racingFrog is taking part in the race. The dialogue box should have the following form:

    Is racingFrog2 takig part in race?

    If a racingFrog is selected to take part in the race, the value of its instance variable selected should be set to true.
    Once the inner loop has executed three times and finished, if the user has selected less than two runners, the message "A race must have at least two
    runners." should be displayed in an appropriate dialogue box. Then the outer
    loop should execute again (setting each racingFrog’s selected instance variable to false once more) followed by the inner loop. This repetition should continue until at least two racingFrogs have been selected.

    I have three three instance variable that I need to use in that loop:

    Java Code:
    [COLOR="DarkGreen"]    private RacingFrog racingFrog1;
        private RacingFrog racingFrog2;
        private RacingFrog racingFrog3;[/COLOR]

    I have started creating this method:

    Java Code:
    [COLOR="darkgreen"]
        public void selectRunners()
        {
            for (int number = 1; number <4; number++)
            {
                OUDialog.request("Is racingFrog1 taking part in the race?")[/COLOR]
    I understand it have to be loop that sets all racing Frogs boolean variables to false and I have that method within racingFrog class that I can use:


    Java Code:
    [COLOR="darkgreen"]   /**
        * Sets the value of the selected instance variable to the value
        * of the argument.
        */
       public void setSelected(boolean aValue)
       {
           this.selected = aValue;
       }  [/COLOR]
    In that case I could apply that to method that I need to come up with:

    Java Code:
    [COLOR="darkgreen"]
        public void selectRunners()
        {
            for (int number = 1; number <4; number++)
            {
                racingFrog1.setSelected(false)
             }[/COLOR]
    ??? I am really confused. Can anyone please help me?
    Should I use an array to store values of racingFrog’s ? But I was not thought anything about arrays during my course and I have been asked to declare 3 private variables within that class: racingFrog1, racingFrog2, racingFrog3.

    Tom

  2. #2
    evant is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    Hi Darkblue, im in the same situation as you.

    I have all the code written for the select runners method, the only problem i have is working out how to count how many frogs have been selected etc

  3. #3
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    Quote Originally Posted by evant View Post
    Hi Darkblue, im in the same situation as you.

    I have all the code written for the select runners method, the only problem i have is working out how to count how many frogs have been selected etc

    Hi

    Did you used an array to store three frogs variable? When constructing selectRunners() did you use an array?
    I have looked thru units 1 - 6 today and I did not found any information about using arrays there (I know how to use them from other courses) so Im not sure are we alowed to use them (And if - how?).

    Darkblue

  4. #4
    evant is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    I haven't used an array no, my code is looping but not doing as it should, i can't figure out how to execute the outerloop again if the inner is false, must be a line of code for this. just not sure what

  5. #5
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    Hi Evant

    Is your loop asking for individual Frogs ? As requested:

    Is racingFrog2 takig part in race?

    (using name of diferent Frog)

  6. #6
    evant is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    Yes it is, , i have written 3 confirmation dialogue boxes each with an if statement after which sets the selected variable to true.

    not entirely sure if it is setting each one to true or how to test if it is, if i confirm only 1 frog is racing, it loops again, if i cause it to fail again it stops, rather than looping again (if that makes sense)

  7. #7
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    Thanks.

    So it is possible to ask for them with out using an array. I will sit down to that tonight straight after work if Ill come up with solution of outer loop I will let you know here.

    I have send question in regards to that method to the tutor this morning and Im still waiting for reply.

    How did you get on with rest of the questions? The deadline is on thursday and I stucked on Question 1 - that makes me lil bit stressed.

  8. #8
    evant is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    Yea, mine asks if each is in the race, i think my selection process is wrong though as it only loops twice, i'm guessing the outer loop isn't setting the variables back to false after selection.

    I tried question 2, but that's even worse so came back to Q1 lol

  9. #9
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    Well I have asked tutor in message sent this morning 9 am - do I need to use and array in that method and also can I poined to revelant resoucrs in our sutdy materials and still no reply and it's nearly 6pm. How much time do they have to reply to us? I have contacted tutors from diferent courses and always recived reply within and hour or max two.
    I dont think the study materials are relevant to the assigments that we been provided with and this is veryyyyyy frustraiting.

  10. #10
    evant is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    Me too, i struggled with TMA01 as i thought the text books don't quite give you enough to complete the TMA's.

    My tutor replied pretty quick but what he said didnt help much.

    I done the M150 and T175, the T175 i completed the TMAs with ease and that was with just skimming through the course books.

  11. #11
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    I am really confused
    Seems like you're on the right track so far.
    Should I use an array to store values of racingFrog’s
    Seems like you'll need some kind of collection of RacingFrogs for your loops to work with.
    Java Code:
    public class YourClass
    {
        private RacingFrog racingFrog1;
        private RacingFrog racingFrog2;
        private RacingFrog racingFrog3;
        RacingFrog[] frogs = new RacingFrog[3];
    
        public YourClass()
        {
            frogs[0] = racingFrog1;
            ...
        }
    
        public void selectRunners()
        {
            // loop to reset [i]selected[/i] field in RacingFrog instances
            for (int number = 0; number < frogs.length; number++)
            {
                frogs[number].setSelected(false)
            }
            // loop for frog race selection by user
            for (int number = 0; number < frogs.length; number++)
            {
                RacingFrog frog = frogs[number];
                // does this next call return a yes/no?
                OUDialog.request("Is frog taking part in the race?")
                // if user says yes, set [i]selected[/i] field in the
                // frog instance to [i]true[/i]
            }
            // did we get two frogs selected?
            // loop thru the frogs and count how many are selected
            // if 2 are not selected:
            // show dialog telling user '...must have at least two...'
            // start this over: reset selected fields
            // and query user for frog selections
            if 2 are selected
            {
                // you can either call this method again
                selectRunners();
                // or you could wrap everything above in a [i]while[/i]
                // or [i]do while[/i] loop and repeat the process until 
                // you get the two selections, then [i]break[/i] out of
                // the outer loop
            }
        }
    }
    The assignment describes an outer and an inner loop but the details don't match well with how they are described. In (more) java (pseudocode) we could say:
    Java Code:
    void selectRunners()
    {
        boolean outerLoopCondition = true
        // outer loop
        while(outerLoopCondition)
        {
            // inner loop
            // reset [i]selected[/i] field for all frogs
            // ask user to select 2 frogs for the race
            // did we get 2 frogs selected?
            if yes
            {
                // either
                break;
                // or
                outerLoopCondition = false
            }
        }
    }
    If you do not want to use a collection/array for the frogs you could do this with no for loop and a seperate dialog request for each frog:
    Java Code:
    void selectRunners()
    {
        frog1.setSelected(false);
        frog2....
        ...
        dialogRequest(frog1);
        dialogRequest(frog2);
        ...
        // now check each frog to determine how many
        // have been selected
        // then either call method again or
        //             repeat all above in a [i]while[/i] loop
        // or allow execution to exit method
    }

  12. #12
    evant is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    Hi hardwired, this is the code i have so far

    Java Code:
        public void selectRunners()
        {
           
          for (int number = 1; number < 4 ; number ++)
          {  
            racingFrog1.setSelected(false);
            racingFrog2.setSelected(false);
            racingFrog3.setSelected(false);               
            {
            boolean result;
            result = OUDialog.confirm("is racingFrog1 taking part in the race?");
            if (result)
            {
             racingFrog1.setSelected(true);
            }
            result = OUDialog.confirm("is racingFrog2 taking part in the race?");
            if (result)
            {
             racingFrog2.setSelected(true);
            }
            result = OUDialog.confirm("is racingFrog3 taking part in the race?");
            if (result)
            {
               racingFrog3.setSelected(true);
            }         
            }
             if (number ++ < 2)
          {
            OUDialog.alert("A race must have at least two runners.");          
          }
           
          }
    Now i am guessing the if(number ++ <2) line isn't checking how many frogs have been selected as the loop doesn't behave as it should. any ideas?

  13. #13
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    If you're going to reference each frog individually, vis–a–vis using an array, then you do not need a loop — you only run through the individual frog calls one time. A loop is necessary if you are using an array for the frog instances.
    Java Code:
        public void selectRunners()
        {
            // reset selected field in each frog instance
            racingFrog1.setSelected(false);
            racingFrog2.setSelected(false);
            racingFrog3.setSelected(false);               
            // ask user to select each frog instance
            // you can count in 2 ways
            // 1 — you can count each 'yes' as you go
            int numberSelectd = 0;
            boolean result;
            result = OUDialog.confirm("is racingFrog1 taking part in the race?");
            if (result)
            {
                racingFrog1.setSelected(true);
                numberSelected++;
            }
            result = OUDialog.confirm("is racingFrog2 taking part in the race?");
            if (result)
            {
                racingFrog2.setSelected(true);
                numberSelected++;
            }
            result = OUDialog.confirm("is racingFrog3 taking part in the race?");
            if (result)
            {
                racingFrog3.setSelected(true);
                numberSelected++;
            }
            // check numberSelected
            if (numberSelected != 2)
            {
                OUDialog.alert("A race must have at least two runners.");          
            }
    
            // or,
            // 2 — you could declare [i]numberSelected[/i] here and
            // query each frog for selection and add them up:
            int numberSelected = 0;
            if(racingFrog1.isSelected())
            {
                numberSelected++;
            }
            if(frog2....
               ...
            // ie, you can separate the setting and the counting operations
        }

  14. #14
    evant is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    Thanks, it's becomming a little clearer now, although i am required to have an outer an inner loop, hence why i used a for loop in the code above.. the question is as follows.

    Write a public instance method called selectRunners() which takes no
    arguments and returns no message answer. Within an outer loop (that will execute
    over and over until at least two racingFrogs are selected) the method should first
    set each racingFrog’s selected instance variable to false.
    Then within an inner loop that should execute three times, once for each
    racingFrog, the method should display a dialogue box to confirm if that racingFrog
    is taking part in the race.


    I will try and use the numberSelected within my code to see if it works with using the for loop

  15. #15
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    From the middle code block in reply #11:
    Java Code:
    void selectRunners()
    {
        boolean outerLoopCondition = true
        // outer loop
        while(outerLoopCondition)
        {
            // inner loop
            // reset selected field for all frogs
    
            // ask user to select 2 frogs for the race
            // here you can use a [i]for[/i] loop to loop
            // through the elements, viz, the 3 frog member
            // variables (racingFrog1, et al), of an array or
            // do 3 individual calls as in replies #12 and #13
    
            // did we get 2 frogs selected?
            if yes
            {
                // either
                break;
                // or
                outerLoopCondition = false
            }
        }
    }

  16. #16
    evant is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    Sorry me again, my code is back to how it was in reply #12.

    How do i go about resetting the selected field for all frogs

  17. #17
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    With respect to the code in reply #12:
    Either do this
    Java Code:
    public class YourClass
    {
        private RacingFrog racingFrog1;
        private RacingFrog racingFrog2;
        private RacingFrog racingFrog3;
        RacingFrog[] frogs = new RacingFrog[3];
    
        public YourClass()
        {
            // instantiate array elements
            frogs[0] = racingFrog1;
            frogs[1] = racingFrog2;
            frogs[2] = racingFrog3;
        }
    
        public void selectRunners()
        {
            // loop over array elements
            for (int number = 0; number < frogs.length; number++)
            {  
                frogs[number].setSelected(false);
            }
            ...
            // on to user selections
    or do this
    Java Code:
    public class YourClass
    {
        private RacingFrog racingFrog1;
        private RacingFrog racingFrog2;
        private RacingFrog racingFrog3;
    
        public void selectRunners()
        {
            // setSelected for each instance
            racingFrog1.setSelected(false);
            racingFrog2.setSelected(false);
            racingFrog3.setSelected(false);
            ...
            // on to user selections
    You can wrap these blocks with an outer loop that repeats until you have (later) the required two user–selections.

    It does no good to loop 3 times for/over the same code statements, viz,
    Java Code:
          // this doesn't make any sense
          for (int number = 1; number < 4 ; number ++)
          {  
            racingFrog1.setSelected(false);
            racingFrog2.setSelected(false);
            racingFrog3.setSelected(false);
            ...
    This part of your assignment is poorly worded:
    Then within an inner loop that should execute three times, once for each
    racingFrog
    . It indicates that you should use an array.

    Whether you loop and call each array element (3 total) one time
    or you (do not loop and) make individual calls to the 3 member variables, viz, racingFrog1, racingFrog2, racingFrog3, it is the same result.

  18. #18
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    hi hardwired

    Thanks for helping us.
    I understand what your trying to say abot not looping code 3 times when is turning 3 variables to false (cause its like doing same job 3 times when its already done).

    What it warring me is the bit that says: "Then within an inner loop that should execute three times, once for each racingFrog, the method should display a dialogue box to confirm if that racingFrog is taking part in the race."
    I think if the loop have to be repeated 3 times asking for each racingFrog than there is no other way to do that without using an array. And that's where it goes complicated.

    To create new array we could use:

    Java Code:
    RacingFrog[] frogs = new RacingFrog[3];
    But in our assigment book we are clearly instructed to:

    "Within Race Class Write declarations for three private instance variables of type RacingFrog called:
    racingFrog1, racingFrog2 and racingFrog3 and provide public getter
    methods for these instance variables."

    It dos not sounds like we should create array for them, does it?

  19. #19
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    The question is "if you do loop 3 times how will you access the next of the 3 racingFrogs in each successive pass through the loop?"
    Maybe you have other classes or means that I don't know about. If you can do a loop and in each pass through the loop access the next of the 3 frogs then you're set.

  20. #20
    darkblue24 is offline Member
    Join Date
    Jan 2010
    Posts
    36
    Rep Power
    0

    Default

    I have used array and applied to constructor, does that looks ok?

    Java Code:
        /* instance variables */
        private boolean raceOver; // false if a race has been started but is not over, otherwise true
        private RacingFrog winner; // holds the winning racingFrog
        private Random randomNumberGenerator;    
        
        private RacingFrog racingFrog1;
        private RacingFrog racingFrog2;
        private RacingFrog racingFrog3;
        RacingFrog[] frogs = new RacingFrog[3]
    
        /**
         * Constructor for objects of class Race.
         */
        public Race(RacingFrog raceArg1, RacingFrog raceArg2, RacingFrog raceArg3)
        {
            super();
            this.raceOver = true;
            this.randomNumberGenerator = new Random();      
            this.racingFrog1 = raceArg1;
            this.racingFrog2 = raceArg2;
            this.racingFrog3 = raceArg3;
            
            frogs[0] = racingFrog1;
            frogs[1] = racingFrog2;
            frogs[2] = racingFrog3;
        }
    or should I build constructor in this way?

    Java Code:
        /**
         * Constructor for objects of class Race.
         */
        public Race(RacingFrog frogs[0], RacingFrog frogs[1], RacingFrog frogs[2])
        {
            super();
            this.raceOver = true;
            this.randomNumberGenerator = new Random();      
            this.racingFrog1 = frogs[0];
            this.racingFrog2 = frogs[1];
            this.racingFrog3 = frogs[2];
            
    
        }

Page 1 of 3 123 LastLast

Similar Threads

  1. my java assigment problem!
    By lieni in forum New To Java
    Replies: 16
    Last Post: 01-28-2010, 12:06 AM
  2. Help Needed!
    By Lyricid in forum New To Java
    Replies: 8
    Last Post: 12-09-2009, 09:34 PM
  3. Help needed
    By harshalsakpal in forum CLDC and MIDP
    Replies: 0
    Last Post: 02-17-2009, 03:53 PM
  4. help needed
    By amarchandar in forum New To Java
    Replies: 3
    Last Post: 02-17-2009, 01:59 PM
  5. Help with variable assigment to String
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:43 AM

Posting Permissions

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