So in the Previous few video we talked about scope and
also about visibility in Java so now it's actually time for a challenge.
So I'm gonna paste a challenge in here then we'll just discuss it.
And then you can make a start on that.
now just before we start talking
about the challenge and going into the
details of it I want to make something
really clear here this is not good
coding technique what I've attempted to
do here with this challenge is to help
teach you scope so by going through the
challenge and completing it I believe
you'll come up with a firm understanding
a much better understanding of what
scope is and how it actually works but
going through the challenge and doing
things like I'm telling you to do
creating variables of the same name of
the letter X and a class with the same
name also with the letter X would be
very bad programming practice and you
would never do that in a production
environment I just wanted to make this
really clear that the challenge is
designed to help you understand scope do
not assume that the coding standards
here are standards that you would use
with the short variable names in this
case with the letter X that there again
purely to help you understand scope
alright so let's get back to discussing
what the challenge is
So here is the challenge.
It's to write a small program to read an integer from the keyboard using Scanner.
And of course we've used that many times in the course today.
And also to print out the times table for that number,
whatever the number was entered the keyboard.
And the table should run from 1 to 12.
Now you're allowed to use one variable called scan for your scanner instance.
And you can use as many other variables as you need, but they must all be called x.
So that includes any class instances and
loop control variables that you may decide to use.
Now if you use a class, the class should be called the X with a capital,
but any instances of it must be called x with a lower case.
So any methods, there's many methods you create must also be called x.
And as an optional extra, change a program so
that all variables including the scan instance are called x.
So what we're trying to do here Here is learn what scope is and
to use the appropriate scope to make this happen.
So by actually having you go through calling everything x,
you're going to really have to understand where to use things and
where the scope is appropriate to use to be able to do that.
So that's the challenge, now's the time to pause this video and
go away and have a go and see if you can come up with a solution to that.
And so when you're ready to see my solution come back and
we'll go through that.
Okay, so I gave you a minute or two to figure it out.
So let's go ahead now and implement the solution.
So the first thing we're going to do is we're going to create a class
image there that If you use a class, the class can be called x.
So we need that.
So I'm gonna create that.
I'm gonna go into our package.
Right click, new class.
X with a capital X.
And we're gonna create a field, int x [INAUDIBLE].
We're gonna create a constructor.
Which is going to accept that parameter and update the field we're using this so
dot x equals x as you can see there.
We're gonna create our function which actually does the times table and
it's gonna be public, void x,
four int x equals one, x less than 13.
X plus plus and I'm gonna print out x plus
times plus this dot x plus
equals plus x times this dot x and
what we need to do is go back to our main class now and actually call this so.
Back to my and
actually first things first we're going to imput the from the keyboard.
The actually number we're going to process so scanner scanner, equals new scanner.
System dot N,
and they're going to print out a message that says please enter number.
And X x = new
X(scanner.nextInt()); in
x.x So if we run that just to confirm that its actually going to work.
Trial two and
you can see we've got two work from one to 12 and that's working fine.
So you can see what we've done there, we've used x everywhere.
But we've managed to by using the appropriate scope
to get the program to work without it actually coming up with a problem
with the X being considered like a duplicate.
Of course the way we've done that is in the X class we've created
a field I've got them in a constructor that accepts that parameter and
updates the field in the mythadex we're using the integer x for the for loop but
we're using this x instead of obviously the x in the for loop to get it to work.
Now, as the optional extra, change program so that all variables,
including the scanner instance, are codecs.
And the way we can do that is we can go back here to our x-class,
and instead of having this dot x equals x, and having a parameter,
we'll change this so I'll actually pass a parameter of scanner, and
we'll accept that import And then we'll actually put a message here.
Please enter a number.
And we're going to accept the input.
Let's start I say, x start next int [INAUDIBLE] So now if we go back to mine.
We can change this a little bit and we can copy this new part cuz we're going to need
that, delete out those first two parts and x, x equals new x and
this time it's going to be new scanner dot system in like so and just x dot x.
So now we've literally got everything using x and we run that again.
Confirming that it works fine.
So I know it's a contrived example and
it's certainly not a way you'd normally code.
Cuz it's very hard to read with all the variables and even the methods and
the class being called X, but the point is by understanding scope and
visibility, we're actually able to achieve our goal of doing that.
So I hope that's actually helped, and I hope you did well on the challenge.
And we'll see you in the next section.