0% found this document useful (0 votes)
41 views

Debugging: Appendix A

The document discusses various debugging techniques such as using print statements to output markers and variable values throughout code execution. This helps identify where issues are occurring. Selecting appropriate variable values and debugging layout placement are also covered. Debuggers in integrated development environments can further aid in determining what is happening in code by allowing step-through execution and value inspection.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Debugging: Appendix A

The document discusses various debugging techniques such as using print statements to output markers and variable values throughout code execution. This helps identify where issues are occurring. Selecting appropriate variable values and debugging layout placement are also covered. Debuggers in integrated development environments can further aid in determining what is happening in code by allowing step-through execution and value inspection.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

Debugging

Appendix A
Errors
 When we have errors in our code, we need to debug
our code to find and fix those errors
 Types of errors
 Syntax

 Run-time

 Logical

2
Problem Solving
 Debugging :

 System.out.println prints to the console window

 use print statements to public void init( )


see where you are: {
System.out.println( "A" );
 System.out.println( “a” ); setLayout( new FlowLayout( ) );
 System.out.println( “b” ); System.out.println( "B" );
setupList( );
System.out.println( "C" );
 use print statements textarea = new JTextArea( 5,10 );
to print out values add(textarea);
as you go System.out.println( "D" );
addListItemsToTextarea( );
System.out.println( "E" );
}

3
Problem Solving
 Use print statements to print out values as you go

 If ‘C’, ‘D’, and ‘E’ don’t print, then there’s a problem in the
method setupList( )
public void init( )
{
System.out.println( "A" );
setLayout( new FlowLayout( ) );
System.out.println( "B" );
setupList( );
System.out.println( "C" );
textarea = new JTextArea( 5,10 );
add(textarea);
System.out.println( "D" );
addListItemsToTextarea( );
System.out.println( "E" );
}

4
Printing Values
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; public void itemStateChanged( ItemEvent ie )
public class DebugValue extends JApplet {
implements ItemListener Object src = ie.getSource( );
{ if( ie.getStateChange( )==ItemEvent.SELECTED)
{
double cat = 4;
if( src == list )
double dog = 10; {
double horse = 350; quantity = Integer.parseInt( qty.getText( ) );
double costOfItem = 0; double totalCost = costOfItem * quantity;
double quantity; price.setText( "" + totalCost );
JComboBox list; }
JTextField qty; }
JLabel price; }
public void init( ) }
{
setLayout( new FlowLayout( ) );
qty = new JTextField( 4 );
list = new JComboBox( );
price = new JLabel( "0.0" );
list.addItem( "cat" );
list.addItem( "dog" );
list.addItem( "horse" );
list.addItemListener( this );
add( qty );
add( list );
add( price );
}

5
Printing Values
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public void itemStateChanged( ItemEvent ie )
public class DebugValue extends JApplet {
implements ItemListener Object src = ie.getSource( );
{ if( ie.getStateChange( )==ItemEvent.SELECTED)
double cat = 4; {
double dog = 10; if( src == list )
double horse = 350; {
double costOfItem = 0; quantity = Integer.parseInt( qty.getText( ) );
double quantity; System.out.println( "qty=" + quantity );
System.out.println( "cost="+costOfItem);
JComboBox list;
double totalCost = costOfItem * quantity;
JTextField qty; System.out.println( "total=" + totalCost );
JLabel price; price.setText( "" + totalCost );
public void init( ) }
{ }
setLayout( new FlowLayout( ) ); }
qty = new JTextField( 4 ); }
list = new JComboBox( );
price = new JLabel( "0.0" );
list.addItem( "cat" );
list.addItem( "dog" );
list.addItem( "horse" );
list.addItemListener( this );
OUTPUT:
add( qty ); qty=3.0
add( list );
add( price ); cost=0.0
}
total=0.0

6
Printing Values - fix
public void itemStateChanged( ItemEvent ie )
{
Object src = ie.getSource( );
if( ie.getStateChange( )==ItemEvent.SELECTED)
{
if( src == list )
{
quantity = Integer.parseInt( qty.getText( ) );
String sel = (String )list.getSelectedItem( );
if ( sel.equals( “dog” ) )
costOfItem = dog;
else if ( sel.equals( “cat” ) )
costOfItem = cat;
else if ( sel.equals( “horse” ) )
costOfItem = horse;
double totalCost = costOfItem * quantity;
price.setText( "" + totalCost );
}
}
}

7
Debugging Layouts

8
Debuggers
 Debuggers are programs that help you determine what is
going on in your code.
 Use it to:

 And lots more

 Graphical IDE’s such as Eclipse have debuggers you


can use.

9
Summary

 Methods for Debugging


 Print out markers throughout code
 Print out values of variables
 Color-code panels
 Use a debugger

10

You might also like