Results 1 to 20 of 41
Thread: Help with Assigment needed.
- 01-25-2010, 01:07 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 36
- Rep Power
- 0
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:
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"] public void selectRunners() { for (int number = 1; number <4; number++) { OUDialog.request("Is racingFrog1 taking part in the race?")[/COLOR]
In that case I could apply that to method that I need to come up with: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]
??? I am really confused. Can anyone please help me?Java Code:[COLOR="darkgreen"] public void selectRunners() { for (int number = 1; number <4; number++) { racingFrog1.setSelected(false) }[/COLOR]
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
- 01-25-2010, 05:30 PM #2
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
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
- 01-25-2010, 05:50 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 36
- Rep Power
- 0
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
- 01-25-2010, 05:52 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
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
- 01-25-2010, 05:56 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 36
- Rep Power
- 0
Hi Evant
Is your loop asking for individual Frogs ? As requested:
Is racingFrog2 takig part in race?
(using name of diferent Frog)
- 01-25-2010, 06:00 PM #6
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
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)
- 01-25-2010, 06:07 PM #7
Member
- Join Date
- Jan 2010
- Posts
- 36
- Rep Power
- 0
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.
- 01-25-2010, 06:11 PM #8
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
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
- 01-25-2010, 06:17 PM #9
Member
- Join Date
- Jan 2010
- Posts
- 36
- Rep Power
- 0
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.
- 01-25-2010, 06:20 PM #10
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
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.
- 01-25-2010, 06:21 PM #11
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.
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: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 } } }
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() { 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 } } }
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 }
- 01-25-2010, 06:27 PM #12
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
Hi hardwired, this is the code i have so far
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?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."); } }
- 01-25-2010, 07:39 PM #13
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 }
- 01-25-2010, 07:47 PM #14
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
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
- 01-25-2010, 07:56 PM #15
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 } } }
- 01-25-2010, 09:11 PM #16
Member
- Join Date
- Dec 2009
- Posts
- 33
- Rep Power
- 0
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
- 01-25-2010, 09:32 PM #17
With respect to the code in reply #12:
Either do this
or do thisJava 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
You can wrap these blocks with an outer loop that repeats until you have (later) the required two user–selections.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
It does no good to loop 3 times for/over the same code statements, viz,
This part of your assignment is poorly worded:Java Code:// this doesn't make any sense for (int number = 1; number < 4 ; number ++) { racingFrog1.setSelected(false); racingFrog2.setSelected(false); racingFrog3.setSelected(false); ...
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.
- 01-25-2010, 09:46 PM #18
Member
- Join Date
- Jan 2010
- Posts
- 36
- Rep Power
- 0
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:
But in our assigment book we are clearly instructed to:Java Code:RacingFrog[] frogs = new RacingFrog[3];
"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?
- 01-25-2010, 10:00 PM #19
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.
- 01-25-2010, 10:10 PM #20
Member
- Join Date
- Jan 2010
- Posts
- 36
- Rep Power
- 0
I have used array and applied to constructor, does that looks ok?
or should I build constructor in this way?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; }
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]; }
Similar Threads
-
my java assigment problem!
By lieni in forum New To JavaReplies: 16Last Post: 01-28-2010, 12:06 AM -
Help Needed!
By Lyricid in forum New To JavaReplies: 8Last Post: 12-09-2009, 09:34 PM -
Help needed
By harshalsakpal in forum CLDC and MIDPReplies: 0Last Post: 02-17-2009, 03:53 PM -
help needed
By amarchandar in forum New To JavaReplies: 3Last Post: 02-17-2009, 01:59 PM -
Help with variable assigment to String
By silvia in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks