Debugging: Appendix A
Debugging: Appendix A
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 :
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:
9
Summary
10