Debugging 9/22/15 & 9/23/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Exam I Next week. Tu 9/29/15 & W 9/30/15
Objectives Define the terms bug and debugging. Explain what a compile-time error is. Explain what an execution-time error is. Give examples of compile-time errors. Give example of execution-time errors.
Bugs and Debugging Bug Debugging Debugger Any error in a program. Grace Hopper's team logged a "bug" in 1945 Debugging Fixing errors. Debugger Special software to help fix programs. Usually part of IDEs like eclipse.
Syntax Errors Error that keeps the program from compiling. Missing or Extra Semicolons Spelling and Capitalization Missing quote, parenthesis, or braces Missing operators Missing concatenation in println Unintialized Variables Not assigned value Example:DebugSyntax
Execution-Time Errors Logical Errors in a program Examples Incorrect Arithmetic. Divide by zero. Bad input. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010
Debugging Techniques Visually inspect the code (not every test requires modifying/running the code). Manually set a variable to a value. Insert print statements to observe variable values. Comment out unused code. Example in DebugExecution.java
Using eclipse Debugger Can set breakpoints. Right-click in bar left of code. “Debug as”. Execution stops at breakpoint. Green arrow to continue execution. Can inspect variable contents. “variables” tab Java button to get back to normal mode.
Questions What is the difference between a syntax error and a execution error? Does Java always give helpful error messages? What can you do to help debug execution errors?
Class DecimalFormat Class DecimalFormat enables control of output In java.text package. Specify a string pattern to describe desired format DecimalFormat formatter = new DecimalFormat("0.000"); System.out.println(formatter.format(val));
Class DecimalFormat 0 – digit, leading and trailing zeros # -- digit, no leading and trailing zeros % -- percentage $ -- at beginning displays dollar sign
Class DecimalFormat DecimalFormat
Program Problem Write a program to input a temperature between 30.0 and 40.0 degrees. Output the temperature to the nearest tenth degree My version: DecimalDemo.java
Participation Write a program to input a cash award between $0.00 and $100.00. Use a DecimalFormat to control the output. Output the number to the nearest cent. While writing the program, try setting a breakpoint and inspecting some variables.