In this lab, we will learn the following computer science concepts:
- Introducing the DateTime Library
- How to use the debugger tool in Sodbeans
Computer Science Principles Curriculum
- Big Idea: Programming: EK 5.1.2B, EK 5.1.2E, EK 5.2.1F, EK 5.1.2J, EK 5.2.1C, EK 5.3.1D, EK 5.3.1I, EK 5.4.1
Common Core Standards
- English Language Arts Standards � Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.3, CCSS.ELA-Literacy.RST.9-10.4, CCSS.ELA-Literacy.RST.9-10.5, CCSS.ELA-Literacy.RST.9-10.6, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3
- Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8
- DateTime Library
- Run Back To Cursor
- Run To Cursor
- Step Back Into
- Step Back Over
- Step Into
- Step Over
A list of Hotkeys can be found on the Getting Started with NetBeans page.
For this lab, you will learn a different type of library and how to use it. You will also learn how to use the debugger to test your code.
Goal 1: Introducing the DateTime Library
Before you start writing any code, let's understand some of the
DateTime class. The
DateTime class is used to get the date/time information from the computer.
Example: Use the
DateTime Library to output today�s date.
//Lab3_3 Date and Time
output timeDate:GetMonth() + "/" + timeDate:GetDayOfMonth() + "/" + timeDate:GetYear()
On the second line, the
DateTime object is used to get the month, day of month, and year. All three methods return an integer value from the system.
In addition to
GetYear() the DateTime Library also have actions to get the time that are:
GetSecond(). All three of these methods are used to get the time information from the system.
Use a library to output the current time
The output should be in hour:minute:second format. For more information about the DateTime class, check out the documentation for the DateTimeclass.
Goal 2: Use the Debugger
It�s time to try out the Sodbeans debugger! Our goal is to help you become familiar with the functionality of the debugger in Sodbeans. The debugger has controls, such as pause, continue, breakpoints, and step over, to navigate through the code in debugger mode.
The debugger is a tool in Quorum that helps you solve complicated logic problems in your code, also known as a bug. It executes the program line by line so that you can keep track of what is going on at runtime and observe the behavior of your code. The debugger issues steps to check the current state of a program and the value of variables. Steps are commands that execute one line of code or part of the program. Quorum's debugger offers different types of functionality to help you debug your code.
- Breakpoint: Inserts a stop location into the debugger at a specific line number in the editor to tell the debugger where to stop.
- Continue: Runs the program or continues the debugger until a breakpoint is found or the end of the program is reached.
- Rewind: Moves the debugger back to the beginning of the program or back to the last breakpoint that was found.
- Run Back To Cursor: Moves the debugger backwards to the point in the program that matches the line the cursor is currently located on.
- Run To Cursor: Keeps moving the debugger forward to the end of the program or to the cursor.
- Start: Starts the debugger and runs the program after the build is successful and the focus is set to the editor.
- Step Back Into: Moves the debugger's active line into part of the previous line of code.
- Step Back Over: Moves the debugger's active line over one complete previous line of code.
- Step Into: Moves the debugger's active line into part of the next line of code. If that line has an action call the debugger steps into that action.
- Step Over: Moves the debugger's active line over one complete next line of code. If that line has an action call the debugger executes over all of the code in that action.
- Stop: Turns off the debugger and stops the program from running.
This section of the lab was divided into smaller parts to help you accomplish a specific task with the debugger. For your reference, the debugger keyboard shortcuts are in the beginning of this lab and can also be found in Lab 1.1.
Start and stop debugger
First, you need to start the debugger. To start the debugger, do one of the three following options:
- Visually click on the debugger button on the toolbar.
- Use the menu bar by going to Debugger -> Debug Main Project.
- Use the keyboard shortcut.
To finish the debugger session, either click on the Finish Debugger Session button, go to the menu, or use the keyboard shortcuts.
Now, let�s try setting breakpoints. Set at least three breakpoints in the editor, preferably at the start of each conditional and loop. To set a breakpoint, click on the line number on the side of the editor or use the shortcut. For the sighted users, a colored line will appear on the line where a breakpoint has been set. When you start debugging, the program will run until it hits a breakpoint that you set in the editor by using continue.
Start the debugger. Tell the debugger to continue and the program will then halt at the first breakpoint you just set.
Use the Step Over to move the active line to the next line. Use the step controls, such as Step Back Over, Step Back Into, Step Over, and so on, to move the active line around in the editor. Watch and/or listen to how the active line moves into the part of the code. For instance, notice how it jumps into the proper conditional. It only skips a block of the code in the conditional because it was checked as false, and it only enters a block of the code in the conditional that returns true. In the case of a loop, notice that the active line keeps moving around until the proper action in the program has been taken.
You can also use the Variables window to observe the use of variables in the case of lexical scoping. Open the Variables window and use any of the step controls in the debugger to observe variables and their respective values. The Variables window can be helpful in some cases; for example, keeping track of each time the program goes through a loop. Try this in the loop to see what happens in the Variables window.
Step through all lines in the editor to finish the debugger, even though you could just simply stop the debugger without taking steps. The step controls in the debugger should be finished when the active line reaches the end of the program.
Below is an example of code that has a few logic errors. The program is supposed to take in four numbers as inputs, convert them to integers, and then call an action to perform a series of computations. The variables of userInput1-4 are supposed to correspond to integers w-z and the function parameters a-d, as you would likely expect. You may be able to see the logic errors just by reading through the code, but pretend that you cannot so you can practice using the debugger.
text userInput1 = input("Input the first number:")
text userInput2 = input("Input the second number:")
text userInput3 = input("Input the third number:")
text userInput4 = input("Input the fourth number:")
integer w = cast(integer, userInput1)
integer x = cast(integer, userInput2)
integer y = cast(integer, userInput2)
integer z = cast(integer, userInput4)
output "The final result is " + Computation(w, x, y, z) + "."
action Computation(integer a, integer d, integer c, integer b) returns integer
integer temp1 = a + b
integer temp2 = c * d
integer temp3 = a / d
integer temp4 = b - c
integer result = temp1 * (temp2 - temp3) / temp4
Hopefully you were able to spot at least the first two logic errors in this code using the debugger. The first is that integer y is assigned userInput2, but it should have been assigned userInput3. The second is that the parameters of the action are out of order: integers b and d should be swapped. The last logic error is not quite as obvious, but rather depends on the inputs you provide. For instance, try inputting 2 as both the second and fourth number (without fixing the other two logic errors). This will result in temp4 = 0, which will cause a runtime error because the final result computation tries to divide by zero.
Optionally, if there is enough time and if more practice is needed, you can practice more with the debugger by going through the Debugging Programs tutorial in the Sodbeans Tutorials.
In the next tutorial, we will discuss Lab 3.4, which describes how work Control the Audio in Quorum..