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

    Default

    Quote Originally Posted by hardwired View Post
    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.
    There is 3 classes as part of that project.

    (1) Frog Class


    (2) RacingFrog Class (subclass of Frog Class)

    (3) Race Class

    Our job was to write a public instance method called moveToPosition() for RacingFrog Class - I have successfully done that (I have tested that method) and now RacingFrog Class is complete:

    Java Code:
    /**
     * Class RacingFrog - A racingFrog is like a frog but it has an
     * additional boolean instance variable, selected, which indicates whether the
     * racingFrog has been selected to take part in a race.
     * 
     * @M255 CT
     * @version 1.0
     */
    public class RacingFrog extends Frog
    {
       /* instance variables */
       private boolean selected; // indicates whether a racingFrog has been selected for a race
    
    
       /**
        * Default constructor for objects of class RacingFrog.
        */
       public RacingFrog()
       {
          super();
          selected = false;
       }
    
       /* instance methods */
       
       /**
        * Moves RacingFrog to aPosition one stone at a time and then jump once.
        */
       public void moveToPosition(int aPosition)
       {
           while (this.getPosition() < aPosition)
           {
               this.right(); 
            }
           while (this.getPosition() > aPosition)
           {
               this.left(); 
            }
            
            this.jump();
        }
       
       
    
       /**
        * Sets the value of the selected instance variable to the value
        * of the argument.
        */
       public void setSelected(boolean aValue)
       {
           this.selected = aValue;
       }  
       
       /**
        * Returns the value of the selected instance variable.
        */
       public boolean getSelected()
        {
           return this.selected;
        }
    }
    I'm not sure will that help?

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

    Default

    With regard to reply #20:
    I have used array and applied to constructor, does that looks ok?
    Yes, this looks okay.
    or should I build constructor in this way?
    Not so good.

    About the first one:
    You could instantiate the three RacingFrogs along with the member variable declarations
    Java Code:
    class Race
        private RacingFrog racingFrog1 = new RacingFrog();
        private RacingFrog racingFrog2 = new RacingFrog();
        private RacingFrog racingFrog3 = new RacingFrog();
        boolean selected = false;
    
        ...
    or you could instantiate them in your constructor
    Java Code:
    class Race
        private RacingFrog racingFrog1;
        private RacingFrog racingFrog2;
        private RacingFrog racingFrog3;
        boolean selected;
    
        public Race() {
            racingFrog1 = new RacingFrog();
            racingFrog2 = new RacingFrog();
            racingFrog3 = new RacingFrog();
            selected = false;
        }
    With regard to post #21:
    Yes, this post helps with perspective.
    I may have confused you with the talk about arrays.
    Looks like you're headed for some thread work with this.
    To loop through the member variables without using an array you could try something like this:
    Note: I altering your RacingFrog class and stubbed in a Frog class.
    This should run okay.
    Java Code:
    public class RaceTest {
        public static void main(String[] args) {
            Race race = new Race();
            for(int i = 0; i < race.numFrogs; i++) {
                RacingFrog rf = race.getNextFrog();
                System.out.println(rf);
            }
        }
    }
    
    class Race {
        RacingFrog racingFrog1 = new RacingFrog("one");
        RacingFrog racingFrog2 = new RacingFrog("two");
        RacingFrog racingFrog3 = new RacingFrog("three");
        int numFrogs = 3;
        int frogIndex = 0;
    
        public RacingFrog getNextFrog() {
            RacingFrog nextFrog = getRacingFrog(frogIndex);
            frogIndex = (frogIndex+1) % numFrogs;
            return nextFrog;
        }
    
        private RacingFrog getRacingFrog(int index) {
            switch(index) {
                case 0: return racingFrog1;
                case 1: return racingFrog2;
                case 2: return racingFrog3;
                default: return null;
            }
        }
    }
    
    class RacingFrog extends Frog {
       private boolean selected;
    
       public RacingFrog(String name) {
          super(name);
          selected = false;
       }
       ... the rest is unchanged
    }
    
    class Frog {
        String name;
    
        public Frog(String name) {
            this.name = name;
        }
    
        public int getPosition() { return 0; }
        public void left() { }
        public void right() { }
        public void jump() { }
    
        public String toString() {
            return getClass().getName() + "[name:" + name + "]";
        }
    }

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

    Default

    Thanks for helping me to decide which constructor was correctly build.

    In regards to the rest of your post: classes Frog and RacingFrog should not be changed (that what it says in instructions) the only change I should do to RacingFrog Class is to add moveToPosition() method:

    Java Code:
       /**
        * Moves RacingFrog to aPosition one stone at a time and then jump once.
        */
       public void moveToPosition(int aPosition)
       {
           while (this.getPosition() < aPosition)
           {
               this.right(); 
            }
           while (this.getPosition() > aPosition)
           {
               this.left(); 
            }
            
            this.jump();
        }
    I have come up with code for selectRunners() method in Race Class:

    Purpouse of that method is to:
    (1) turn all the three frogs boolean values to false
    (2) ask user whitch RacingFrogs are taking part in race (bolean = true) and if less than 2 frogs are selected loops should repeat it self.

    Java Code:
    public void selectRunners{}
        int numberOfSelected = 0;
            // loop to reset selected field in RacingFrog instances
            for (int number = 0; number < frogs.length; number++)
            {
                frogs[number].setSelected(false);
            }
                    for (int number = 0; number < frogs.length; number++)
                    {
                        RacingFrog frog = frogs[number];
                        // does this next call return a yes/no?
                        boolean result;
                        result = OUDialog.request("Is frog"+ [number] + "taking part in the race?")
                        if (result);
                        {
                            frogs[number].setSelected(true);
                            numberOfSelected++;
                        }
                    }
                    if (numberSelected <= 2)
                    {
                        OUDialog.alert("A race must have at least two runners.");
                    }
    Do you recon that would work fine now???

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

    Default

    In regards to my previous email.

    Apparently I don't need an array for selectRunners() method (that is confirmed by my tutor) and now I'm lost. I have been advised that I have call them (RacingFrog) individually and set to true if they were to take part.

    Is there a way of doing this without altering class Frog and RacingFrog ?

    I am looking at post 22 and I cant work that out:
    Where in that code is selectRunners() method that I should create?

    (Purpose of that method is to:
    (1) turn all the three frogs boolean values to false
    (2) ask user which RacingFrogs are taking part in race (bolean = true) and if less than 2 frogs are selected loops should repeat it self.)
    Last edited by darkblue24; 01-26-2010 at 10:11 AM.

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

    Default

    classes Frog and RacingFrog should not be changed (that what it says in instructions)
    I changed RacingFrog and made up a Frog class so that they would compile and I could ude them in the Race class; all this to demonstrate a way to call the individual racingFrog instances in a loop without using an array. I wasn't suggesting that you alter RacingFrog. It was a demo app probably too complicated for now.

    Apparently I don't need an array for selectRunners() method (that is confirmed by my tutor)
    So the assignment wording was confusing.
    and now I'm lost
    Sorry, I'm somewhat responsible for this.

    I have been advised that I have call them (RacingFrog) individually and set to true if they were to take part.
    Post #13 showed something like this.

    Is there a way of doing this without altering class Frog and RacingFrog ?
    Yes, of course.

    I am looking at post 22 and I cant work that out:
    Where in that code is selectRunners() method that I should create?

    I left it out. The app was intended to demonstrate a way to use a loop without using an array.

    Now you have confirmed the confusion in the assignment statement and have settled on not trying to use a loop and, instead, calling each instance individually.
    Okay.

    I changed RaceTest around to use your RacingFrog class as posted, no changes, and JOptionPane for the dialogs. It compiles and runs okay.
    To (hopefully) avoid confusion I'll substitute the dialog code from posts #12 and #13 which should compile and run okay for you. (I can't compile this since I don't have your OUDialog class.)
    Java Code:
    public class RaceTest {
        public static void main(String[] args) {
            Race race = new Race();
            race.selectRunners();
        }
    }
    
    class Race {
        RacingFrog racingFrog1 = new RacingFrog();
        RacingFrog racingFrog2 = new RacingFrog();
        RacingFrog racingFrog3 = new RacingFrog();
    
        public void selectRunners() {
            boolean selecting = true;
            do {
                racingFrog1.setSelected(false);
                racingFrog2.setSelected(false);
                racingFrog3.setSelected(false);
    
                int count = 0;
                boolean result =
                    OUDialog.confirm("is racingFrog1 taking part in the race?");
                if(result) {
                    racingFrog1.setSelected(true);
                    count++;
                }
                result =
                    OUDialog.confirm("is racingFrog2 taking part in the race?");
                if(result) {
                    racingFrog2.setSelected(true);
                    count++;
                result =
                    OUDialog.confirm("is racingFrog3 taking part in the race?");
                if(result) {
                    racingFrog3.setSelected(true);
                    count++;
                }
    
                if(count == 2) {
                    // we have two selections, okay to exit do-while loop
                    selecting = false;
                } else {
                    // tell user and allow another pass through do-while loop
                    OUDialog.alert("A race must have at least two runners.");
                }
            } while(selecting);
        }
    }

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

    Default

    Thanks for the help hardwired, works great now, was just wondering though, is there anyway it would work without the "Do" and just a while loop?

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

    Default

    Sure, try it.

  8. #28
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Did you folks know that the chance a thread ends successfully (i.e. it is finallly answered) is inversely proportional to the length of the thread?

    kind regards,

    Jos ;-)

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

    Default

    nice on Jos, its about the same chance of my assignment being completed successfully too

  10. #30
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by evant View Post
    nice on Jos, its about the same chance of my assignment being completed successfully too
    Yep, in this particular case an ordinary while loop would've worked just as well:

    Java Code:
    boolean selecting = true;
    while (selecting) {
       // all that frog leg stuff here ...
    }
    kind regards,

    Jos

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

    Default

    Yea, got that bit to work.. now onto the next and have come across another hurdle. I have to create a method to check what frogs have been selected to race, get the selected frog to move to a certain stone, change colour etc.

    for some reason i can't figure out how to check which has been selected.

    there is get selected method in the RacingFrog class...

    here is the line of code where the problem is..

    Java Code:
    if(racingFrog1.getSelected()=true) <--- [I]shows unexpected type?[/I]
             {
                racingFrog1.MoveToPosition(1);
                racingFrog1.green();
             }
    Last edited by evant; 01-26-2010 at 09:31 PM.

  12. #32
    owlsno1 is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    Hi darkblue and evant

    Looks like everybody is sturggling, what did you put down for your getter methods for the instance variables, cant seem to nail, dont know if its is void, boolean or int

  13. #33
    pje
    pje is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default evant does this work?

    public void prepareForRace()
    {
    raceOver = false;
    while(racingFrog1.getSelected())
    {
    racingFrog1.moveToPosition(1);
    racingFrog1.jump();
    racingFrog1.setColour(OUColour.GREEN);
    }
    while(racingFrog2.getSelected())
    {
    racingFrog2.moveToPosition(1);
    racingFrog2.jump();
    racingFrog2.setColour(OUColour.YELLOW);
    }
    while(racingFrog3.getSelected())
    {
    racingFrog3.moveToPosition(1);
    racingFrog3.jump();
    racingFrog3.setColour(OUColour.BROWN);
    }

    }

  14. #34
    Pandemonium is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default Hiya

    Sounds like we are all in the same boat. ha!

    I have got about as far as you guys not even passed question 1. I thought about using an array as I have too studied T175 and M150. but decided against it as we are supposed to use the material taught to us (i imagine). When I tested my code it was not understood by the class... any thoughts where i might have gone wrong?

  15. #35
    Ragnar is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default My solution

    Hey guys,

    I used a for loop to run 3 times to fulfill the first requirement of the question.
    I then used 3 if statements, inside the loop body, to compare the int I used as a counter in the for loop header against a number from 1 to 3 for each if statement. If the number matched then the if statement would assign the value of 'true' to each frogs 'selected' variable.



    This has the effect of running the loop 3 times but only executing the code corresponding to the current value of the counter.

    Hope that helps.

  16. #36
    kjh1 is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default

    Hi

    Thanks to everybody who has contributed to this thread, very helpful hints and tips!

    Has anybody got as far as 'advanceAllRacingFrogs' method yet?

    My code is as follows:

    public void advanceAllRacingFrogs()
    {
    this.setRaceOver(false);

    while(this.getRaceOver())
    {
    if (racingFrog1.getSelected())
    {
    this.advanceRacingFrog(racingFrog1);
    }
    if (racingFrog2.getSelected())
    {
    this.advanceRacingFrog(racingFrog2);
    }
    if (racingFrog3.getSelected())
    {
    this.advanceRacingFrog(racingFrog3);
    }
    }
    }

    According to the assignment, frog1 should move first then the remaining frog(s) should take a turn and return back to frog1 in a loop. My problem is the frogs can move to the finish line, but only after the previous frog has finished.

    Any ideas for switching between turns?

    Regards

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

    Default

    Hi kjh,

    i believe we have to use the numberGenerator. I will be on this evening having a go at it, i have a pretty good idea at how to get it working.

    Can't believe i'm still on question 1 and it has to be in tomorrow lol, question 2 looks hard.

    Has anyone finished Q1 and tried the others?

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

    Default

    ok i cant work it out! grrr!

    i have this code but it says found int but expected boolean, i'm probably being dumb but i can't see why thats happening.

    Java Code:
        public void advanceRacingFrog(RacingFrog racingFrog)
        {
          int randomInt = randomNumberGenerator.nextInt(4);
          while (randomInt = 0)
          {
             randomInt= randomNumberGenerator.nextInt(4);
          }
             racingFrog.moveToPosition(racingFrog.getPosition()+randomInt);
          }

  19. #39
    owlsno1 is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default error message

    Hi
    right i have followed the ideas placed on here for the selectRunners method but when it comes to executing the line RacingFrog = new race(rf1, rf2, rf3) i receive a volley of errors. These are:
    "Semantic error: Typed variable decleration: undefined argument: rf1
    Semantic error: Typed variable declaration: Constructor error: Can't find constructor: Race( RacingFrog, RacingFrog, RacingFrog) in class: race

    I'm :confused:

  20. #40
    Proton is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    This does the job
    public void selectRunners()
    {
    boolean selectedFrogs = true;
    while(selectedFrogs)
    {
    racingFrog1.setSelected(false);
    racingFrog2.setSelected(false);
    racingFrog3.setSelected(false);

    int countSelectedFrogs = 0;
    boolean result =
    OUDialog.confirm("Is racingFrog1 taking part in the race?");
    if(result)
    {
    racingFrog1.setSelected(true);
    countSelectedFrogs++;
    }
    result =
    OUDialog.confirm("Is racingFrog2 taking part in the race?");
    if(result)
    {
    racingFrog2.setSelected(true);
    countSelectedFrogs++;
    }
    result =
    OUDialog.confirm("Is racingFrog3 taking part in the race?");
    if(result)
    {
    racingFrog3.setSelected(true);
    countSelectedFrogs++;
    }

    if(countSelectedFrogs >= 2)
    {

    selectedFrogs = false;
    }
    else
    {

    OUDialog.alert("A race must have at least two runners.");
    }
    }

Page 2 of 3 FirstFirst 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
  •