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.2.
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 resources at once.
If your file explorer looks like this, you will not be able to visualize the runtime.
So please close this atrocity and launch the exact folder that contains your file.
Anyways, let us begin.
You start with $100 and your child wants you to purchase them a toy car.
So first we need to check if we have enough money to purchase this car.
Can we afford it?
So we'll say if the wallet if the money I have in my wallet is greater than or equal to the price of
the toy car, then we are going to print.
Sure.
Let's add a new line.
And then we need to pay for the toy car.
So we need to say wallet minus equal toy car.
So we're updating the variable on the left by subtracting from it the value on the right.
From the wallet.
We are subtracting the toy car and now we can say otherwise.
If we cannot afford the toy car, we're going to print.
Whatever this message is.
Sorry, I only have wallet left.
And I think we're good to go.
Let us visualize the runtime.
Let's put breakpoints everywhere.
All right.
Debug.
So first.
It creates two variables wallet, which equals 100 and toy car which equals 599.
Now we reach the if statement which will only execute if wallet is greater than or equal to toy car.
Wallet is 100, Toy car is 599.
This condition is true.
So the if statement is indeed going to execute it prints.
Sure.
Followed by a new line.
And here we're updating the variable on the left by subtracting from it the value on the right.
So $100 -599.
But if I press continue.
It terminates the runtime and we don't get to see the updated variable here.
That is a shame.
Let's do it again.
All right.
Stepping through it again, it creates two variables here.
It's just going to print a message.
And this condition is true.
So the if statement executes.
Prince Sure.
And now, because this is the last thing that is going to run, we don't want to press continue.
We want to just step over this line.
So press the step over button.
And now we can see the updated variable.
First, we had $100.
Now we have $94.01 and toy car obviously is still 599 Press continue and it terminates the runtime.
So what I'm going to do is let you pause the video and write the code for this one yourself.
All right.
I hope you figured it out.
Let's walk through the whole runtime.
Let me just add some breakpoints over here as well.
OC.
So over here, let's just step through everything.
Wallet 100 is clearly bigger than a toy car of 599.
This condition is true.
So the if statement executes prints?
Sure.
And here the variable on the left is going to update by subtracting the value on the right.
Let's see it in action.
Now wallet is $94.01.
And now it continues to the next breakpoint.
So now if I continue here.
The Nike shoes are 95.99 here at prints.
Can I get these Nike shoes?
All right.
Now we reach the if statement, which will only execute if wallet is greater than or equal to the Nike
shoes.
But here we can see that wallet is $94.01, whereas the Nike shoes are 9599.
So this condition is going to turn out to be false.
The statement will not execute.
So we default to the statement and it's going to print.
Sorry, I only have wallet left.
$94.01.
Now let's run a test case where this is 8999.
I'll remove all of these breakpoints.
And we will start by visualizing the runtime over here.
This time, the Nike shoes are only $89, but we have $94 in $0.01 in our wallet.
So this is indeed going to execute.
And after we purchase the Nike shoes, we're going to step over this line because it's the last thing
that will run.
And we want to visualize the update in our wallet variable.
It's going to update by subtracting the value on the right.
94 -89.99 is $4.02.
So after purchasing a toy car and Nike shoes, we have $4.02 left in our wallet.
Beautiful.
Let's clean up.
If you want to remove the breakpoints, press the x button.
And if you want to rerun your code normally using terminal commands, of course we can use the familiar
Java Sea Java Java Command to compile our code followed by Java Java Mart.
All right.