All right so this is the video we're going to solve the challenge I presented to you in the last video.
We need to create two methods and then actually execute them four times each.
Now what I'm going to do is start off by creating the display high score position method but doesn't
really matter which order you create them in their work and there's no right or wrong answer here.
All right so if you recall again we look at the instructions here display high school position.
It's a name.
We have two parameters name and position.
Name is the player's name in the string and position is a value returned from the first method and represents
where in the high school what type of the Player Score belongs.
So we come down here outside of the main method.
So it's basically above the last right Kelly Price in this file to top public static.
And this one's void.
And the reason is we're displaying something we're not actually returning any information the name of
the method as display high school position parentheses.
And we need to have two parameters for this one.
We've got string player name you can call that play name in call it name doesn't really matter the second
one int position.
You could call that high school position or position or even process is an abbreviation for position
that doesn't really matter if the left or right curly braces to make the method valid.
So we need to output that string that I mentioned basically up here.
That's a to manage to get to position two on the high school table and we're going to achieve that.
But this is not out dot printed in can we drop a bit on the screen so it doesn't affect subtitles so
we'll start by putting the player name you can say that I'm on 32 what we're trying to do is I'll play
a name plus an edge to get to position spikes and that's that I'm putting a space after the word position
and I need to put a space here before the word managed so that the name and the managed text doesn't
get scrunched up together.
Position there.
I'm going to press enter there and tab over and put it plus next to return the position position plus
a space in between the double quotes on the high score table.
Okay so that's literally our first method.
We're just displaying the play and I managed to get to position the position number I actually got to
on the high score table.
So moving on now for a second method if you call that was calculate high school position so there's
a line then it has one parameter which is called Score and is it is based on the value or the player's
score.
So we come down here let's create that as well.
Public static this returns an integer.
So we need to put in there instead avoid the method name to calculate high score position and parentheses
and then it score being the player score and then we need to basically solve this they need to return
one suburb of Damascus take a copy that can be a useful thing to do if you're passing if you're trying
to write some code and you can put in comments as to what are you trying to achieve there.
Which can I do.
So from the top if parentheses score if it's greater than 1000 greater than equal to 1000 in fact what
we're going to do.
Can I put the left like the code back left right clearly prices are going to return one I'm gonna add
an LS here so else if score is this time is going to be greater than equal to 500 and so the end operator
it's less than one task so that's an hour and we're going to return to that challenge score then of
course you need to tell us what we're trying to compare to.
Moving on we need a fair comparison else if score to equal to 100 and score is less than 500 we're to
return three then the last scenario.
All other cases we need to return for.
So that's basically our code for our calculate high score position method if the score is greater than
equal to 1000 return returning one else if score is greater than equal to 500 and scores less than 1000
returned to the scores credit the record a hundred and scores less than five hundred return three otherwise
return for perhaps another we've done that created both our methods we now need to come down here under
the comments and we need to calculate Wendy to call these things four times we need to say to score
each time we call the calculate high score position so I'm going to top it playschool is equal to calculate
high school position and the first time we're calling it with fifteen hundred I'm going to do next then
it's called the display.
High school opposition methods that display high scores position method.
I'm gonna pass the name of Tim
comma I'm gonna pass play a score to it.
That's what I have it.
I'm gonna show you a different way of doing it just to be bit more concise to save a bit of space.
We can also do it the second way we'll do this for the nine 900.
I'm going to type display high school position.
Tim comma calculate high school position nine hundred I could say what a tad.
There is a basically decide to line the instead of actually storing the value returned from our calculate
high score position which we're doing a lot forty in the second call.
What I'm doing is calling the method calculate high score position as the second argument that has been
set to display high score position.
So what actually happens there in that scenario is the value from calculate high score position is returned
to display high score position.
So in terms of what happens here on line 43 they calculate the highest composition method is executed
by Java first.
It figures out the value and it then caused the display high score position with the parameters team
and what was returned from the core to calculate high score position.
So that's our second example.
What I'm going to do then is take a copy and paste both in 900 the third time we're calling it with
400 and the fourth time we're calling it with 50.
So I've now got our four calls and you might be wondering why we needed to call the methods four times
each.
Well if you look at the code you can say the first time I called it the code lines 40 to 41 K code high
score position was called and despite high score position was called clearly to call seven total for
each of the next three lines.
We've got both methods being caught on the same line.
So we end up calling in total.
Calculate high score position four times and display high score position.
Also four times.
So if we run this now let's say well it's worked for us
and you can see the airport to managed to get to a position of one on the high score table do manage
to get to position two on the high score table to maintain the position three of the high score table
and two managed to get to position four on the high score table.
Now if we look at the values that we pass to this method will come down here to have a look at again
at our scenario if the score was greater than equal to 1000 should return 1 in the first case our first
value was fifteen hundred.
So yes we should get the output position 1.
The second scenario a score that we pass to this method was 900.
So therefore the second marker current here was executed the code on line of 64 it scores greater than
equal to 500 which no 100 is and it's less than 1000 which of course 900 is returns too.
So a second line of output is position 2 and moving on in the third case we passed 400.
So that gets to the third block here.
Scores greater than equal to 100 which 400 is and it's less than 500 which of course 400 is returns
three then in the last case we passed 50 and we know that in all other cases this cause here is executed
and four is returned and that was reflected in the output.
The other thing that's very useful in scenarios like this especially when you're using greater than
an equal to is to make sure that you're actually checking the exact values I'm going to put up by say
just to make the card look a bit nicer.
So in other words we're checking whether scores greater than equal to 1000 would be a good thing to
test would be to display something else come down here and put display high school position but I'm
not playing I'm always 1000.
Let's see what happens there.
We'll always get to position on one theory she should because we've put the value 1000 in there so let's
actually check that well clearly that was a mistake because of past the position 1000 and it should
be quick calling the calculate High School method.
So let's fix that calculate high school position that 1000 tests Trump this time you can save about
Louise managing to position one on the high score table with a score of exactly 1000.
So always check that out when you're doing your testing and which we're doing here we're testing these
methods out.
Always be sure that you've checking the scenarios here we've got a greater than equal to.
So make sure you're checking the exact 31000 and also a value that's greater than 1000 and now that
we've got there five tests we've done that.
So that's an exercise you could go through and also test with the values of 500 and 100 to make sure
that they were also executed have inside a value 99 which should work anyway because we know the value
50 return for when we first read it.
And just one other thing we could do here.
When you put it it was like this had ends and there's no other code.
It's actually a little bit redundant to put it in the US what we can do is we can actually change this.
I could come up in a code out which we're doing now noting that when I've done that they've got an error
because I've got misplaced no braces.
I've got to put up what price back there to make it valid again.
But that's you still got an era error missing return statement and that's because the way that methods
return is no matter what program flow is executed.
It has to return a value.
So now I'll take it at the else.
There's no code to return.
So what I can do then is change this and put return for here making the code valid again.
Let's clean up some spaces there and that's another alternative which would work just as nicely as the
else that I originally put in online 73 72 right so that's my solution to the challenge.
I hope you get a lot out of it and I'll see you in the next video.