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.
This video will present the solution to workbook 3.1.
I'm also going to show you how to visualize the runtime in VS code, so please make sure that you launch
the exact folder that contains your java file.
Do not under any circumstance launch all of the folders at once.
All of the resources.
If your file explorer looks like this, please launch the correct folder.
Otherwise you will not be able to visualize the runtime like this.
All right, let me zoom out a little.
The application starts by asking a question.
Hey, Java, do I have enough change to buy chips?
We can only say true if they have enough money.
So if the change that they have.
If the money that they have.
Is greater than or equal to price.
Then this will return true.
Otherwise it will return false.
All right.
And now, instead of just running our code, let's visualize the runtime.
Again, I repeat, this will only work if you launch the exact folder that contains your Java File workbook
3.1.
So please make sure your file explorer looks like mine.
You should never under any circumstance launch all of the folders at once.
So first put a breakpoint beside each line of code that we worked with so far.
We're essentially putting a breakpoint beside each line of code where we want the runtime to stop.
All right, now go ahead and press debug.
Looking good.
The only buttons we will use in this video are continue and stop.
We will cover the rest of these buttons throughout the course.
Don't worry.
So first I will press continue to execute this line of code.
And by doing so here, you can clearly see that Java creates a variable that equals 3.5.
Change equals 3.5.
Press continue again and in executes the next line of code.
Here you can see Java created another variable called price that equals a decimal of 2.5.
Step through this line and it prints the question.
Hi, Java.
Do I have enough chains to buy chips?
And this line is going to print the resulting boolean.
So right beside the comparison, it shows us the variables involved.
Change and price change of 3.5 is clearly bigger than the price of 2.5, which satisfies the greater
than or equal to comparison.
Which will result in a value of true.
Now, if I press next, because this is the last breakpoint, it's going to print everything else.
So we'll ignore everything and we will just focus on this one.
It clearly printed true in line number seven.
All right.
What if I change this to 250?
Let's visualize the runtime again.
Step through each line.
Again.
Here we see the variables involved in the comparison.
Change of 2.5 is equal to a price of 2.5, which satisfies once again the greater than or equal to comparison.
They clearly have enough to buy chips.
True.
Now, a lot of you have been asking me, when do I use greater than or equal to and when do I just use
greater then?
Well, in this case, if I put greater, then that would be incorrect.
Why do you ask?
Let's visualize it.
So if I step through the runtime change of 250.
First it creates the variables, prints a message.
And now here change of 250 is equal to the price of 250.
They clearly have enough to buy chips, but not according to our comparison.
It returns false, as you will see in just a moment, because change is not greater than price.
They are equal.
That's not good.
We're using the wrong operator.
We should be returning true if the change is greater than or equal to the price.
Perfect.
Now, I'm going to let you pause the video so that you can finish up the rest, and then we're going
to visualize the entire runtime together.
All right.
This is going to be really fun.
I went ahead and put breakpoints everywhere.
Feel free to pause the video and do that.
And let's walk through the runtime.
Let's debug.
I'll step through the first two lines here.
Java creates two variables change equaling 2.5 price in 2.5.
They clearly have enough to buy chips.
The variables involved in this comparison are change and price, which are definitely equal, which
satisfies once again the greater than or equal to comparison.
They clearly have enough to buy chips.
So Java replies true.
Here are the capacity is 12, but there are 15 people.
And here we can only return true if the capacity is greater than or equal to the number of people inside.
But 12, the capacity is smaller than 15.
So this comparison is going to, rightfully so, return false.
That would be the correct answer to give them.
So here the request was for a PlayStation five, but we got them a toy car.
Will they be happy?
In other words, will the request equal what they purchased?
Looking at the variables involved over here in the comparison, they do not equal each other.
So this will return false.
Beautiful.
By the way, before I continue.
Never, ever use the equal equal operator with strings.
I know I talked about this before, but I want to really emphasize it.
This compares internal references.
It doesn't actually compare the two values.
You'll learn more about references in module to object oriented programming.
But by using equal equal with strings, you're going to get bugs.
Please use dot equals with strings.
Always use dot equals with strings.
That's the golden rule.
All right.
Anyways, here we can only return true if there is enough space for all the guests.
So if space is greater than or equal to guests looking at the variables involved, nine is clearly greater
than eight.
So that's going to return true.
And indeed it does.
All right.
And now here.
I really hope you didn't use the greater than or equal to operator, because that would be wrong.
We can only return true if your votes is greater than your competitors votes.
But if we compare them side by side, you only have 24 votes and your competitor has 43 votes.
And accordingly this comparison is going to return false.
You lose the election.
Now, if you happen to have, let's say, 43 votes and your competitor also had 43 votes, let us remove
all of the breakpoints.
Using the x button I showed you and only put breakpoints here so that we can just start over here.
Because I have 43 votes, my competitor has 43 votes.
I do not win the election.
It's a tie.
So it's really important that you use the greater than operator, not greater than or equal to.
I hope you were able to make that distinction when solving this workbook.
And rightfully so.
It prints false.
All right, now let's clean up.
If you want to remove the breakpoints, press the X button.
And now we can write Java Sea.
Java Java as we would anyways.
Java.
Ask Java.
So yeah, we can still compile and run our code as normal.
We don't always have to visualize the runtime.
All right.
I hope you enjoyed this breakpoint session.
There will be plenty more to come.
I will keep repeating this for the remaining workbooks.