Sunday, February 12, 2017

Basic Debugging with Eclipse




##########################################
Yeah.
.
Welcome to dr. Piercy's basic debugging with eclipse in this video we're going to look at setting breakpoints starting a debugging session and using the debugging perspective to debug our program will accomplish this by going through a debugging process with an example program you'll find some other shorter videos on my channel that show you some of the parts in more detail of this process keep in mind that debugging is a two-part process in part 1 we determine the nature and location of a program error we'll look at some tools in this video for how we can do this part 2.
You need to actually fix the error I'll also do this by the time the video is complete for this video.
Our example will include the following classes.
The lone class stores the state of the loan using the fields principal annual percentage rate as percentage and a term in years along class also includes a method where we can calculate the monthly payment for the loan will also have an amortization class.
The purpose of the amortization class is to calculate and display and amortization table for alone to do this it will include a loan object as a field finally our application will include a controller class this will be a class with the main method that will set up alone and amortization object and call the appropriate methods let's have a quick look at the application and its component you'll note in eclipse that this is a simple java application under a package called logic I have the three classes is the amortization class notice it includes a loan field two constructors adieu amortization method and a getter and setter for the loan field also have a loan class you'll notice that a lone object will have three fields principal rate and term two constructors getters and setters one pair for each of the fields and a get monthly payment method.
The controller class will simply hold a main method to run the program will have a look at the code for these classes as we debug the program keep in mind that i'm familiar with the code already as I'm the one who created it and a good knowledge of your code is very important to debugging because you're the one who knows what your code is supposed to do.
One of the first things you need to do before you start to debug is to actually test your program this is where you get the idea of whether or not there are bugs - fixed to do that one of the best ways is to test your program using known inputs for which you know what the desired output should be in this chart I can see that if i start with the principle of 200,000 an APR of six percent and a term of 30 years.
I should be getting output that is monthly payment of 1199 dollar the time it takes to zero debt to being the 360 @ month the first term interest payment of a thousand a first-term principal payment of hundred and ninety-nine dollars in ten cent and at the end of the first term my ending balance should be 199 thousand eight hundred dollars and ninety sent there are many other desired outputs that i will see when i run the program but this should be enough to compare whether it's working correctly or not so let's run our current version and see what we get.
As usual I will run the program by right clicking on the project and choosing run as a java application this program i have set up to where i am going to enter the values using a scanner and the console so mine own value of two hundred thousand dollars for the principal a rate of six percent and 30 years for my term and let's see what I get after running the code i will see in the console and amortization table in a comma-separated format I see though that my desired output does not look as it should in particular from the output of the current program i see that the actual output is a monthly payment of 1 million two hundred thousand dollar doesn't make much sense on a.
Two-hundred-thousand-dollar loan does it turn 20.
Well it did not make it went to a month 30 and we still owed money.
Was the full amount of the principal and the first term principal paid 10 and the first term ending balance was 200 dolls.
These numbers are way out of whack.
So we have recognized a bug in our logic and we need to fix it incidentally there were no error messages when we ran the code this means that our problems are entirely problems of logic and not syntax compile or runtime errors.
In general its logic errors that often require us to work with special debugging tools like the Eclipse debugger in order to solve the problem.
To get started you want to first think about what is going on and what should happen compared to what is happening.
For example in this program it was obvious that our monthly payment was not being calculated correctly it was actually higher than the principal you know anything about alone that's not possible.
So we're going to have to look at the monthly payment or perhaps how it's being used inside of the amortization table method after you think about what's going on think about where in your code would it be beneficial to stop the program and actually see what is going on.
I'll show you how to do that a little bit later in this video and at those locations we're going to do something called setting a break . if i look at this output.
I see that the monthly payment is incorrect and all the other values here.
There's a particular method that creates this table so one thing I might think is let's start looking in that method for what's going on.
The method that creates the table is in the amortization class in the do amortization so here we go so one thought might be that i'm having problems starting here could be it started earlier or later but at least i think that the error is showing up here so it might be beneficial to stop when this method begins and see what are the values that are coming in so i'm going to create what's called a break . in this method a break point is simply a statement or line in your code where you want to stop execution of the program and kind of take a look around see what are the current values for any objects or variables that are currently active i'm going to set a breakpoint here for line 34 which is pretty much the first line in this method to do that in Eclipse.
Move your cursor over to the margin just to the left of the line number if you're displaying line numbers if you're not displaying line numbers the margin will still be there right click in them argent and you'll notice that the top option in the pop-up menu is toggle break . so let's do that toggle basically means to turn something on or off since it was off and i hit toggle.
You'll notice now that at that point in my code there is a little . to signify that i have a break . course if I wanted to turn it off I could right click on it and hit toggle and that would turn it off.
Think of toggle like a light switch turning something on or off.
Let's go ahead and put another break point in our code down here in the middle of the for loop.
And then I'm going to add one more at the return statement the reason i might want to have multiple break points is that sometimes I want to stop look at something momentarily and then resume so that it runs the rest of the code up until the next break . so it's kind of skipping ahead very quickly we'll see that in action in a little bit when we work with these break points.
I could think of other places but sometimes i think this is a good place to start.
So I'm going to start my debugging session with these three break points.
I have another shorter more detailed version that talks a little bit more about break points you can check out on my channel.
Now that we've set our break points were ready to start a debugging session to do this we're going to choose debug as rather than run as to run the program will debug it as a java application once we get started eclipse will switch to the debug perspective.
I'll talk a little bit about that as we see that in the next few minutes.
So to get started i'm going to right click on the project name itself instead of choosing run as as we did before to run the program will select debug as this one is a java application so i will select that option you'll notice that my perspective immediately changed on your eclipse.
You may see a message pop up asking you if that's what you want to do.
Generally you will want to do that so click yes.
Note also that there is a check box if you put a check in the check box it will never ask you again as I've set up on my eclipse.
Let's review a few parts of this I have a more detailed video available about this perspective on my channel in general i am going to use three primary windows of the debug perspective the others i find less useful but they do have their uses for more advanced debugging in particular note in the center we see our code we see actually one of the lines highlighted this is where we have our first break . the line that is highlighted has not actually been executed yet it's waiting for us to tell it to go ahead and run that line in this area we have the variables window at this point we can see values for variables that are currently active variables is very important because this is where we actually can see the state of values that are happening then down in the console.
I use this often for two things one in this particular program that's where we're entering our input but also any error messages will show up here the full debug perspective is available in another video on our Channel.
Another very important part of the debugging perspective are debugging controls which we can find near the top of the Eclipse window here we see a view of those controls blown up a little bit for easier viewing these controls consists of the follow step into when you click on step into eclipse will attempt to run the line of code that is currently active if there are any method calls that need to be resolved within that line you'll step further down and c code within that method.
This one is useful when you want to be able to go down and look at some of the methods that you wrote that you're calling upon within that line step over step over will do the same thing as step in to accept it will run the current line as a complete line without stepping further down into any of the methods needed to resolve that line this is the one that I use the most because I often want to just run that line completely and go on to the next one that I'm viewing step return this is kind of the opposite of the step into when you use step return you're moving back up to the code of the calling method resume this lets you resume executing your program without stepping one line at a time this can be very useful when you just wanted to run to the end of the program after debugging or run to the next breakpoint without having to step over or into the next few line suspend this is where you can pause the program perhaps you noticed something you want to edit and change you can pause make that edit and then resumed terminate this basically stops the execution of the program altogether.
We'll see those in action in the following debugging process let's get started by stepping over to execute the current line will enter a.
Two-hundred-thousand-dollar principle.
Will step over again and enter a rate of six.
Notice something else up in the variable section because the last few lines we created two new variables and provided values we can see the value of those variables so currently we see that principle has been entered his 200,000 and rate has been entered in his six so it's this process of stepping through our code and checking the values of the variables that we will use to solve this problem and find out what is going on in our code.
Another important thing to do is that before you run any line you pause and you reflect on what should this line be doing think about what it's telling you and what you're telling the program to do with this instruction the next line will prompt the user for the years let's step over and see it in action.
Enter a term of six years the next line will create a loan object using the input values this time let's use step into to have a look at the constructor one thing to be aware of with step into is that sometimes you will see code from JCL classes here we see some code in the JCL classloader class often you can continue clicking step into until you see code that you have written.
You see now that we have visible the loan class and we could start stepping over those lines now note up in the variable section here's something else that's interesting.
Currently there is a this object that has a value loan that is displayed in the variables let's expand that we can see the values of the loan field so far i have clicked through over the principal and we see that the principal field for the loan has been set.
Let's set the term I step over the term has now been set to six which is the value of the local variable if objects are currently active they can be displayed in the variable section and you can expand them to see the current state of those objects.
Let's see what happens if we do step return notice it ran the next line and now we have all of our values set.
A better way to get further inside your code is to set multiple break points in this case i have also set a breakpoint at the first line of the do amortization method of the advertised ation class we can click resume and our code will run until it encounters that next break . now we see that the execution has stopped at the next break . inside the do amortization method Eclipse has paused execution and is waiting for us to take control once again before resuming let's take a quick look at the current state.
Let's expand the object called this in the variables window we see that this contains a loan object further expanding lets us see the instance variable values of the loan object.
Let's step over the next few lines watch the list of variables in the variables window we see that a balanced variable and an amortization table string variable have been added to the list.
By the time we get to line 48 the balance has been set to the principal and the header line of our table has been added to the amortization table string okay that makes sense so far so good.
Not go into a loop where i'm going to create the numerical values displayed in the amortization table.
So what should happen when i click step over is that will start the loop and we'll set a variable called month equal to one will check it against the loan term which I can see a 6 ok now I see.
That month is now one which should be good i'm going to add that to the table.
The next line I should get the monthly payment my desired number is a 1199.
Let's see what happens here I check I've added that to the table i can see that no that's a million two hundred and something so that is definitely incorrect.
So I noticed that an error is showing up here but these these lines of code.
Look okay to me perhaps i need to be using some other values in these calculations or perhaps the problem is really in my loan this next line let's go a little bit deeper and step into we see as I step into that I'm getting into some of the stringbuilder functions notice i have the variable rate the rate is six that makes sense to know as I look at my monthly payment.
I'm seeing some errors basically my formula should be using a monthly rate recall that a PR is a yearly rate so my formula should be using a monthly rate and my term instead of years should be using a set of months so perhaps that's one thing I need to fix it often you'll be stepping through looking through code and one of the important things about be debugging is it makes you concentrate on the lines of code and what's happening.
The rate itself is not so bad if i want to monthly rate however in my monthly payment calculation what I noticed once i'm looking at loan is i'm using yearly values instead of monthly so let's stop the program i prefer to edit back in the Java so let's see if I can fix this a bit i'm going to change this formula to rate to a monthly rate.
Let's change that to divide by 12.
Another aspect of this is the six should also be a percent so let's also divide by 100 ok those changes are complete here.
Let's save the project to try running it again.
I usually like to straight-up run the program to see if it is fixed and then go to debugging mode again.
Only if it is not let's enter our input values.
Let's have a look at the results the monthly payment now is a consistent 1199 dollars in ten cents but the other values are still incorrect.
They are way too high looking at the results i suspect that we next we'll need to correct the interest paid formula it's gonna hit my first break point right here in the middle of here I'm gonna hit continue because I don't need to really stop in the main method let's enter 200,000 enter 6 enter years.
Okay while we're here let's check the value of this amortization object it has a loan and it has a principal 200,000 a rate six and a term 30 let's resume the program so that it stops inside of the loop because everything works up until that point or confident of that.
So I hit continue we can see that the amortization table string has been created up to a certain point we can see that the balance is currently 200,000 we have our 24 matters and we have our month equal to one so when i run this one it should add one to the amortization table string notice it did and a , next it should calculate and add a monthly payment and it did now it's going to get the rate recall that the rate is a yearly rate and it's not a percentage if we step over this should give us something less than one thousand one hundred and ninety-nine dollars but we see that it gave us 1.2 million so we know that that value is out of whack.
Let's stop the program let's make some corrections in this area and make sure that my rate is now divided by 12 and / 100 multiply it times the balance need to do that everywhere I have rate my 12 / 100 I can start a debugging session again from the debugging area once I've made some changes by clicking on debug controller notice it went there.
I'll resume enter in my values now let's see what's happening let's resume up to the next breakpoint.
A month is one was added the monthly payment of 1199 dollar was added correctly.
Look at that now a monthly rate into a monthly interest paid of a thousand dollars was added correctly and that is my correct value.
Next I should have about a hundred and ninety-nine dollars in ten cents that was added correctly now the balance should be roughly a hundred and ninety-nine thousand.
Let's click resume to cycle through the loop.
Going to stop every time I go there so i have to do that for a while.
Or i could even toggle the break ..
Resume now we got all of it all the numbers look good but notice the term is not going it's still going to 30 years.
So how can i change that well in this loop.
The term is specified by the loop constraint.
I'm going to stop I'm gonna fix that line of code by doing multiply x 12.
Now let's debug again it here let's resume.
Check out my values looking good so far.
Let's resume down to the bottom of the loop.
I check out what's been paid here oh look at that.
Everything's looking pretty good now up to 240 than not so let's resume so it will end the program and we should see down in the console that it is completed all the way up to 360 with a zero payment do so to summarize we noticed an error in a the logic of our program we decided where some good places would be in our code that we should stop and actually see what was going on and we set break points at that point then we entered a debugging session in the debugging session we were able to step through our code running exactly the lines of code that we wanted to and have it stopped at key locations so that we could review what values were going on.
We also saw that in the debugging session we can make some Corrections and then debug again and hopefully this example gives you an idea how you might debug your code using the Eclipse debugger.
Please check out the other videos that describe some of the components and tools we use from the debugger in more detail for more information please visit the references shown here this video was written narrated and produced by dr. Craig a Piercy background music includes various piano selections that are performed by Craig a Piercy this has been a Piercy production.


##########################################

No comments:

Post a Comment