Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
Welcome to Work Book 4.5.
In this workbook, we're going to print the areas for a square rectangle, triangle and circle and task
one.
We have to define all the functions been defined.
We just have to write the logic for it first.
Inside the function, we check if the side that was passed in is negative, in which case we need to
print impossible and terminate the Java program.
We cannot let them proceed.
So we'll say if side is smaller than zero.
Then print error and possible.
And then terminate the runtime system Exit zero OC.
But otherwise, if the side that they pass in is good, then we're going to calculate the area of the
square and return it.
Now, if you want to do something to the power of something else, you can use the math power function.
Power is a built in Java function that we can get from the math class and it expects to values.
The first value is the base.
The second value is the exponent.
Our base is side and the exponent is going to be two.
So this function is going to return the double result from doing side to the power of two.
And that's all.
All right.
And now the remaining functions are going to follow a very similar logic.
So I'll give you the chance to pause the video and try to complete them on your own.
All right.
Welcome back.
So this one is fairly easy.
First, you check if the length or width are negative.
If they are, you say not possible and terminate the application.
But if this doesn't execute, then runtime will keep going, in which case you're going to return the
area of a rectangle which is length times width.
I will delete the dot comment.
And moving on to task number three, which was to calculate the area of a triangle.
Again, we do a safeguard.
If they pass in an invalid base or height, we terminate the application.
Otherwise, runtime continues and we return the area of a triangle.
And now in task number four, your function needs to return the area of a circle.
First, you check if the radius is negative.
If that happens to be the case, we print not possible and terminate the application.
Otherwise we return pi.
So pi is a field from the math class that equals 3.14159, etc. It is better practice to reference pie
from the math class as opposed to actually writing 3.1 for yourself.
And then all we're doing is multiplying that by radius to the power of two as you were instructed to
do so inside of learn the part.
The area of a circle is equal to pi times radius to the power of two.
And that is all.
All right.
Task number five is to call the area functions.
So here we'll say, Well, you know what?
Let's just uncomment these here.
We can calculate the area of a square.
With a side of two.
Here we calculate the area of a rectangle with a length of one and a width of two.
Here we calculate the area of a triangle.
With a base of one and a height of two.
And here we calculate the area of a circle with a radius of what was it again?
A radius of two.
All right.
That was task number five.
Task number six was to define a function that prints every single area.
So the function is going to be public static.
As always, it's going to be void because no return type was specified in the doc comment and we're
not returning anything because all we'll be doing is printing a bunch of areas.
It's going to receive four parameters.
Double square.
Double rectangle.
Double triangle.
And double circle.
So when called this function expects to receive four values, we're going to print all of these values
in accordance with what we have specified over here.
Looking good.
Deleting this.
And I'm pretty sure we're all done.
The next step is to call a function that prints all the areas, and that function is print areas.
It's time to run the code.
But as always, I would prefer to visualize the runtime.
So I will place breakpoints over here and that will be all.
Let us now visualize it.
Dismissing this debug console.
Minimizing this window.
So first, we're calling the function area square, passing in a side of to the parameter side stores.
The value that was passed in two is not smaller than zero.
So this gets skipped and two to the power of two is going to return for.
The square variable equals the return value of four.
Beautiful.
And now we're calling area rectangle, passing in two values.
The first parameter store is the first value, lowest pass, then the second parameter store is the
second value that was passed in.
None of the parameters are less than zero.
So this statement is going to get skipped and here it returns the area of a rectangle length times width.
And now we're setting the rectangle variable equal to the return value of two.
All right.
Now, we've calculated two areas, one for the square, one for the rectangle.
Now we're calling a function that calculates the area of a triangle passing in two values.
The first parameter store is the first value that was passed in.
The second parameter stores.
The second value that was passed in.
Here we're checking if either value is less than zero.
That is not the case.
So this if statement gets skipped and here we return the area of a triangle.
The function call retains this return value.
And here we set our variable equal to an area of one.
All right, Now we've got three areas.
Stepping inside the function area circle.
Here.
We check if the radius that was passed in is smaller than zero.
That is not the case.
So the if statement gets skipped.
And now here we're doing 3.1, four times two to the power of two, which is going to result in a return
value of 12.56.
And then we're setting the variable equal to it.
All right.
And then print areas is just going to print all of these areas.
Beautiful.
This was the last workbook for the function section, and I really hope you've been enjoying these breakpoint
sessions.