So in this video we're going to actually talk about the Package Challenge.
So I've already actually pasted in the challenge for you to actually do.
And, going through it we need to create a suitably named package
containing a class called Series.
And that class should have the following static methods.
First one's nSum, which is actually going to end in parameter And
that actually returns the sum of all numbers from zero to n.
So the first ten numbers to give you
an idea of 0, 1, 3, 6, 10, 15, 21, 28, 36, 45 and 55.
The second static method is vectorial which
also accepts an interject and that should return the product
of all numbers from one to n and being the perimeter of course.
So here one times two times three times four.
Brought up into intake one times in.
As you can see on the screen there, multiplied.
So the first 10 factorials are 0, 1, 2, 6, 24, 120, 720, 540, and so on, and the last
one fibanache, the fibnache static method Return say the nth [INAUDIBLE] number.
These are defined, as you can see there with an algorithm.
F0 equals 0, F1 equals 1.
Then FN equals FN take one plus FN take two.
So there's actually the algorithm for the [INAUDIBLE] in the screen there.
And you can see in that case there, F2 is also one.
So the first ten [INAUDIBLE] numbers,
zero, one, one Then it's 2, 3, 5, 8, 13, 21, 34, 55, and so on.
So once you've actually tested your functions,
delete the main class and actually produce a jar file.
You want to create a new project and add to series library, to that project,
and then test the 3 methods in the main method of your new project.
And what you want to do is actually use this code which I'm gonna paste in,
when you actually test it.
That'll be the code there.
But that obviously won't work at the moment,
because I haven't actually created and added the series library yet.
But that would be how you actually go about actually testing your code.
And this, you can see, again, they are static methods.
So that's the challenge.
So now it's your turn to actually see if you can solve this challenge.
Go away and actually create the series class and the three static methods, and
do some testing.
Create a library and when you're ready to actually come back and
actually see the solution, start the video again and I'll go through it.
So pause the video now.
Okay so [INAUDIBLE] That you figure it out.
So let's actually go through that.
So, the first part was we needed to create this package, or this class I should say
called series, and with the three static methods in some factorial, and Fibonacci.
So what I'm going to do is create a new package.
I won't actually create it.
I'm going to come up to my source, and do right click it, new Java class,
and I'm going to actually call this [INAUDIBLE] Dot dot my library dot series.
So the class name is series and the package is come dot dot my library.
Click on ok, and you can see we've got the new package created and
the class also created.
So let's actually go back now creating the three methods.
So the first one we're gonna start with is the end sum.
And in sums a matter of it's the easiest of the three.
So it's public, static, turning along, in sum, int.
And returning a long because it's basically gotta accommodate
a larger number if we want to go higher.
And the actual method, let me go back to our current again, to our example.
In some [INAUDIBLE] sum of all numbers from zero to inter
to whatever was actually past in being the parameter.
So to turn that all we need to do is turn N mode applied by N plus one.
divided by 2, that should actually give us the answer.
And we'll test that shortly.
We'll actually write the 3 methods, test them, and
then we'll actually create the actual library.
So going back to our main, the second one was the, Factorial, return the product of
all numbers from one to n, I one time two times three times four etc right up until
intake one or modified by intake one times n as you can see there on the screen and
it gives you some examples of the first ten factorials.
So the code for that still pretty straightforward.
Public static.
Long factorial int in, cuz that's the parameter, of course.
Now first, we need to do a test for zero.
So if n is equal to zero, we can actually just return zero,
cuz obviously we can't calculate anymore.
and just a quick video edit here I'm
making a change after the original
recording and I made a change because
there's an error here
and the issue here is a factorial at 0
is actually 1 and not 0 and you can see
that in the code here I'm returning 0
which is wrong so the correct code you
can see on the screen now should be
returning 1 and not returning 0 so you
might want to make the change to return
1 in that scenario and the code will
then work properly
Otherwise, we're going to actually start with the value one And
then we're gonna do a full loop and actually go through and multiple that.
Add to that the multiplication to each one.
So to do that we're going to do four int I equals 1.
NR++.
And then we just do fact equals fact multiplied by I.
And then, we return fact and save it.
That's actually it.
That's the factorial And the last one we need to do now is the Fibonacci.
We're just going back to our overview, turns the [INAUDIBLE] Fibonacci number and
you can see there the [INAUDIBLE] calculator.
So fn equals fn take one plus fn take two [INAUDIBLE] F2 in this example
is also one and we've got the example of the first ten Fibonacci numbers then.
So, lets go ahead and do that Public static
long Fibonacci(int n).
So we start off doing a test for zero, so if n is equal to zero.
Just like the factorial, we can actually return zero.
Sorry return zero.
But else, if it's one, we can actually also return one.
So basically if it's zero or one, we're returning those hard coded numbers,
zero or one.
But if we get to here, we know that the number has to be two or higher.
So I'm going to put long and minus one.
One equals one.
Long n minus two equals zero so
that's the initial values because we're going to take the number minus one will be
one because we are going to get a number that is at least two or higher.
In n minus two.
It's going to start off zero.
Because of course n being two, n take two is going to be zero.
So, basically we're initializing that with the two values.
Assuming the number was two.
And then we run through our format.
So, we put four INT, i equals one.
i less than n, i plus plus.
In the case I have this fib equals m minus.
And I need to create a variable long for fib equals zero.
Fib equals n minus two plus n minus one.
And then we put mMinus2 = mMinus1 because we're incrementing the loop.
Then mMinus1 = fib.
So basically we're setting fib initially to mMinus2 + mMinus1,
then we're setting mMinus2 = mMinus1 And you can see in the last line,
nMinus1 then becomes the current value of fib.
And that's it.
So we let the for loop go through and complete, and then we return.
Fib.
So that should be our Fibonacci method.
So now we've actually done that, it's time to actually create the actual package.
So what I'm actually gonna do now is actually remove the main method.
'Cuz we dont want that in our file, our file, so delete that.
So then you're left with series.
And come up here now to file, project structure.
We're gonna choose artifacts, we're gonna add.
We'll click on plus.
We're gonna select from modules with dependencies.
We're gonna just leave the default set.
There's no main class, which is correct, cuz we're gonna write the main class and
do the testing.
And the Java file's okay.
Click on OK. And you can see it gives us a default
location, JavaProgramsPackageChallenge_Java.
And we're looking at this shortly to get the Java file so
we can actually include it in the new project we're going to create.
Click on OK to that.
And that's actually created, Then we can go up to Build,
and Build Artifacts to make sure that the jar file is built.
We're gonna select Build.
So that's now built.
And we'll just go back and check that.
So let's have a look now Package challenge.
Out.
Artifacts.
Package challenge I'm just going jar on there is a Java file so
that was built successfully.
Now what we want to do is create a new project that we're going to actually
import this series class are the package challenge Java file.
Into that new project.
So, I'm gonna back into here, File > New > New Project, a new Java project.
And we're gonna call this Series Test, but
you can call it obviously whatever you like.
But Series Test, com.timbuchalka.
So, what I'm going to do Is create something completely different.
[INAUDIBLE] let's actually try comm dot example dot series dot test like so.
It's a completely different base package.
Click on okay and we're going to use this one there so
we can let the other one close since we're finished with that project.
So we know about this one.
And the next one we want to do then is we want to we've got our main method.
Which we're going to write our test code in.
We want to actually go to project structures.
Lobberies.
And we want to actually add our artifact file.
So I'm going to click on Java.
And we'll go back to our challenge.
Package challenge.
Out folder, artifacts folder.
Package challenge forward, [INAUDIBLE] score,
then there it is our package challenge [INAUDIBLE] jar.
[INAUDIBLE] On OK for that.
And it's gonna be added to the series, to the selected modules,
that's click, click on OK to that, that's fine.
And click on OK.
So now we've got that, we can go into external libraries, and we can see we've
got package challenge, and you can see we've got our series class.
That's obviously [INAUDIBLE] so that's good.
So now we should be able to write some test code and
if you remember I actually had that test code from before.
Let's actually check this out so we're gonna go for(int i=0; i <=10; i++).
Now let's try the in sum first.
So that's gonna be System.out.
And I'll put series.nsumi.
[INAUDIBLE] actually found our class automatically,
and input to the imports that we started using, and
you can see now it's a different input altogether.
[INAUDIBLE] .mylibrary.series.
So I'm gonna run that.
And we've got the numbers, and at the start of the video, I mentioned that,
the numbers we're looking for, is, 0, 1, 3, 6, 10, 15, 21, 29, [INAUDIBLE], 45, 55.
So that's working correctly which is good.
The next one, factorial, let's do that.
And before int i=0, i<=10, i++.
And it'll print out the Series.factorial for i.
If you run that.
And actually what I might do there is just print out some asterisks there,
just to indicate that this is [INAUDIBLE] a separator between one
method's output and the next.
Obviously, what we're looking for was 012624 127, 25000, 40, 40320 and so on.
So that's obviously working for the factorial as well.
That's good, so let's go and do the last one now.
The last one, of course is going to be int I = 0.
[INAUDIBLE] equal to 10 I plus plus, and we're gonna
print out series.fibonocci for I.
The output we'll be wanting for Fibonacci 0, 1, 1, 2, 3,, 5, 8, 13, 21, 34, and
55 so let's run that.
And then we got 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.
So that's actually it.
So that's the actual solution.
So you're now able to, if you followed along, create your own Packages and
use them in other Java projects.
So, I hope you did well with that challenge.
It's now time to move on to the next part of this section and
that's all to do with scope and visibility.
So, I'll see you in the next video.