0
Help with writing a Java method that includes a loop and array
I am trying to write a Java method code as follows: A recordNewRun() method which can be called each time a runner completes a park run. This method increments an instance variable that counts the number of runs completed by a runner. The method is passed the name of the park in which the run took place and the time the runner took to complete a 5km run. Data for each new run should be stored in an array for runParks and runTimes. Include methods like - fastest,slowest and average run times.
34 Answers
+ 2
@ Gordie. thanks .
but next time first notify me , before changing my posts š
I know the risks
+ 2
. sorry I thought it was u ....
I really don't like that someone else can change the postes
+ 1
i don't know how u wrote the code . but I think maybe it's better if u create a class for run (that has fields for time , park .....) then create an array of that class's type to store the runs.
then for calculating the fastest , slowest u can iterate through the array , getting the time of each run an comparing them
+ 1
Ginjahh Freshh Gyal yes u can (it's called an inner class)
if u want , u can mail it to me ....
but if your code is not commented , there is not much that I could do to help
+ 1
right now I'm heading to work.
I'll take a look at it whenI get the chance.
hope I could help and learn something :)
here:
<removed>
+ 1
@Farshaad. Okay, I will send you an email now
+ 1
@ Gordie. Thanks for your advise but sorry I donāt understand how to do that?
+ 1
@ Gordie. Sorry, I am new on here but that sounds like a good idea actually ... I may give that a try. If I can successfully do that, will you take out some time to review my code and see if you could help me with the problem I have?
+ 1
@ Gordie. Hmm okay, thank you for your advice. Iāll have this in mind
+ 1
@Gordie. Hmm okay, I shall try what you just said. Hopefully it works out well for me .... Nevertheless, Thank you very much for your help and inputs on this. I appreciate it!
0
Thereās more to this but I have already written the code and it runs successfully ... it is also pretty long, I donāt Think I have enough space to write it in here
0
then where do u have troubles?!
0
@farshaad. Iām having troubles calculating the fastest, slowest and average run times inside the recordNewRun() method using a loop and storing the data in the runParks and runTimes arrays
0
@ farshaad. Is it possible to create the class in another class? Because I already have a class called ParkRunner with a constructor and Get and Set Methods. I donāt know if it is possible to create the Run class below the ParkRunner class? ... I would love to show you my code so far but it is too long to write it here
0
@ farshaad. Yes the code is commented, at least most part of it ... I can email it to you yes. Whatās your email please?
0
@ Gordie. I wrote the ParkRunner code myself but I donāt know how to write it in here along with the ParkRunnerTest for people to see. Iām new here ... And writing to a file is part of what I have to do. The RecordNewRun is not a class, it is a method I need to implement in the ParkRunner which I donāt know how to.
0
@Gordie. Okay I shall check your Code Playground. & yes those 2 lines of code in the ParkRunnerTest is a class. But it has to be a method in my ParkRunner which will calculate the fastest, slowest and average time of a park run ... it is really more understanding if you saw my ParkRunner code and the description brief of what I have to do though.
0
@Gordie. Yes the information is about several runners but Iāll be just fine if it represents for just 1 runner. Yeah I did do methods for sum, average and fastest/slowest. I just did that on my laptop. Iāll just paste my ParkRunner code in my Code Playground for you to see. Give me a few minutes to do that
0
@ Gordie. Okay Iāve managed to paste in my ParkRunner code in my ParkRunner Java Program in my Code Playground ... it is the first pieces of code you see and then when you scroll down, the ParkRunnerTest code is under it. Hope it helps you understand better.