Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application."— Presentation transcript:

1 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application 5.2 Variables 5.3 Handling the keyPressed event for a JTextField 5.4 Memory Concepts 5.5 Arithmetic 5.6Using the Debugger: Breakpoints and the run, stop, cont and print Commands 5.7Internet and Web Resources 5.8Wrap-Up Tutorial 5 – Enhancing the Inventory Application Introducing Variables, Memory Concepts, Arithmetic and Keyboard Events

2 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Declare variables. –Handle the keyPressed event for a JTextField. –Apply basic memory concepts using variables. –Use the precedence rules of arithmetic operators. –Set breakpoints to debug applications. –Use the debugger’s run, stop, cont and print commands.

3 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 5.1 Test-Driving the Enhanced Inventory Application

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 Figure 5.1 Running the enhanced Inventory application. 5.1 Test-Driving the Enhanced Inventory Application (Cont.) Run the Inventory application – cd c:\Examples\Tutorial05\ CompletedApplication\Inventory3 – java Inventory

5 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 5.1 Test-Driving the Enhanced Inventory Application (Cont.) Figure 5.2 Calculating the number of items in the shipment. Enter 5 in Cartons per shipment: JTextField Enter 6 in Items per carton: JTextField Click Calculate Total JButton

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 5.1 Test-Driving the Enhanced Inventory Application (Cont.) Figure 5.3 Enhanced Inventory application clears output JTextField (to avoid confusion) after new input. Total is removed when JTextField is changed Cleared output JTextField

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 5.2 Variables Figure 5.4 Declaring variables in event handler calculateJButtonActionPerformed. Variable declarations Variables – Storage location that holds data – Allows storage without a GUI component – Type – Initial value

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 5.2 Variables (Cont.) Figure 5.5 Retrieving numerical input from JTextField s in event handler calculateJButtonActionPerformed. Assigning user input to variables Assignment operator – copies value on right side into variable on left side

9 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 5.2 Variables (Cont.) Figure 5.6 Updated Inventory application. Save the changes to your code Compile and run in the Command Prompt

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 5.2 Variables (Cont.) Type – specifies the type of data a variable stores – primitive type – specific type built in by Java – primitive type names are also keywords

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 5.2 Variables (Cont.) Figure 5.8 Multiplying two variables in an assignment statement. Figure 5.9 Displaying the result of the calculation. Multiply cartons and items, then assign the result to the variable result Display result of calculation in totalResult JTextField

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 5.2 Variables (Cont.) Figure 5.10 Displaying the multiplication result. Result of calculation

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 5.3 Handling the keyPressed event for a JTextField Figure 5.11 keyPressed event handler for Cartons per shipment: JTextField. keyPressed event handler Clear totalResult JTextField keyPressed event – triggered when key is pressed in JTextField

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 5.3 Handling the keyPressed event for a JTextField (Cont.) Figure 5.12 keyPressed event handler for Items per carton: JTextField. Clear totalResult JTextField empty string – string value containing no characters

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 5.3 Handling the keyPressed event for a JTextField (Cont.) Figure 5.13 Testing the completed Inventory application. Output JTextField clears when value in either input JTextField is changed

16  2004 Prentice Hall, Inc. All rights reserved. Outline 16 Inventory.java (1 of 8) 1 // Tutorial 5: Inventory.java 2 // Calculates the number of items in a shipment based on the number 3 // of cartons received and the number of items per carton. 4 import java.awt.*; 5 import java.awt.event.*; 6 import javax.swing.*; 7 8 public class Inventory extends JFrame 9 { 10 // JLabel and JTextField for cartons per shipment 11 private JLabel cartonsJLabel; 12 private JTextField cartonsJTextField; 13 14 // JLabel and JTextField for items per carton 15 private JLabel itemsJLabel; 16 private JTextField itemsJTextField; 17 18 // JLabel and JTextField for total items per shipment 19 private JLabel totalJLabel; 20 private JTextField totalResultJTextField; 21 22 // JButton to initiate calculation of total items per shipment 23 private JButton calculateJButton; 24

17  2004 Prentice Hall, Inc. All rights reserved. Outline 17 Inventory.java (2 of 8) 25 // no-argument constructor 26 public Inventory() 27 { 28 createUserInterface(); 29 } 30 31 // create and position GUI components; register event handlers 32 public void createUserInterface() 33 { 34 // get content pane for attaching GUI components 35 Container contentPane = getContentPane(); 36 37 // enable explicit positioning of GUI components 38 contentPane.setLayout( null ); 39 40 // set up cartonsJLabel 41 cartonsJLabel = new JLabel(); 42 cartonsJLabel.setText( "Cartons per shipment:" ); 43 cartonsJLabel.setBounds( 16, 16, 130, 21 ); 44 contentPane.add( cartonsJLabel ); 45

18  2004 Prentice Hall, Inc. All rights reserved. Outline 18 Inventory.java (3 of 8) 46 // set up cartonsJTextField 47 cartonsJTextField = new JTextField(); 48 cartonsJTextField.setText( "0" ); 49 cartonsJTextField.setBounds( 148, 16, 40, 21 ); 50 cartonsJTextField.setHorizontalAlignment( JTextField.RIGHT ); 51 contentPane.add( cartonsJTextField ); 52 cartonsJTextField.addKeyListener( 53 54 new KeyAdapter() // anonymous inner class 55 { 56 // method called when user types in cartonsJTextField 57 public void keyPressed( KeyEvent event ) 58 { 59 cartonsJTextFieldKeyPressed( event ); 60 } 61 62 } // end anonymous inner class 63 64 ); // end call to addKeyListener 65 66 // set up itemsJLabel 67 itemsJLabel = new JLabel(); 68 itemsJLabel.setText( "Items per carton:" ); 69 itemsJLabel.setBounds( 16, 48, 104, 21 ); 70 contentPane.add( itemsJLabel );

19  2004 Prentice Hall, Inc. All rights reserved. Outline 19 Inventory.java (4 of 8) 71 72 // set up itemsJTextField 73 itemsJTextField = new JTextField(); 74 itemsJTextField.setText( "0" ); 75 itemsJTextField.setBounds( 148, 48, 40, 21 ); 76 itemsJTextField.setHorizontalAlignment( JTextField.RIGHT ); 77 contentPane.add( itemsJTextField ); 78 itemsJTextField.addKeyListener( 79 80 new KeyAdapter() // anonymous inner class 81 { 82 // method called when user types in itemsJTextField 83 public void keyPressed( KeyEvent event ) 84 { 85 itemsJTextFieldKeyPressed( event ); 86 } 87 88 } // end anonymous inner class 89 90 ); // end call to addKeyListener 91

20  2004 Prentice Hall, Inc. All rights reserved. Outline 20 Inventory.java (5 of 8) 92 // set up totalJLabel 93 totalJLabel = new JLabel(); 94 totalJLabel.setText( "Total:" ); 95 totalJLabel.setBounds( 204, 16, 40, 21 ); 96 contentPane.add( totalJLabel ); 97 98 // set up totalResultJTextField 99 totalResultJTextField = new JTextField(); 100 totalResultJTextField.setBounds( 244, 16, 86, 21 ); 101 totalResultJTextField.setHorizontalAlignment( 102 JTextField.RIGHT ); 103 totalResultJTextField.setEditable( false ); // output only 104 contentPane.add( totalResultJTextField ); 105 106 // set up calculateJButton 107 calculateJButton = new JButton(); 108 calculateJButton.setText( "Calculate Total" ); 109 calculateJButton.setBounds( 204, 48, 126, 24 ); 110 contentPane.add( calculateJButton ); 111 calculateJButton.addActionListener( 112

21  2004 Prentice Hall, Inc. All rights reserved. Outline 21 Inventory.java (6 of 8) 113 new ActionListener() // anonymous inner class 114 { 115 // method called when calculateJButton is pressed 116 public void actionPerformed( ActionEvent event ) 117 { 118 calculateJButtonActionPerformed( event ); 119 } 120 121 } // end anonymous inner class 122 123 ); // end call to addActionListener 124 125 // set properties of application’s window 126 setTitle( "Inventory" ); // set title bar text 127 setSize( 354, 112 ); // set window size 128 setVisible( true ); // display window 129 130 } // end method createUserInterface 131 132 // calculate the total items in the shipment 133 private void calculateJButtonActionPerformed( ActionEvent event ) 134 {

22  2004 Prentice Hall, Inc. All rights reserved. Outline 22 Inventory.java (7 of 8) 135 // declare variables 136 int cartons; // stores number of cartons in shipment 137 int items; // stores number of items per carton 138 int result; // stores product of cartons and items 139 140 // retrieve numbers from text fields 141 cartons = Integer.parseInt( cartonsJTextField.getText() ); 142 items = Integer.parseInt( itemsJTextField.getText() ); 143 144 // multiply values input 145 result = cartons * items; 146 147 // display result in totalResultJTextField 148 totalResultJTextField.setText( String.valueOf( result ) ); 149 150 } // end method calculateJButtonActionPerformed 151 Use keyword int to declare variables inside an event handler Assigning a value to a variable Calculating a result Displaying a variable’s value

23  2004 Prentice Hall, Inc. All rights reserved. Outline 23 Inventory.java (8 of 8) 152 // clear totalResultJTextField because the value is now invalid 153 private void cartonsJTextFieldKeyPressed( KeyEvent event ) 154 { 155 totalResultJTextField.setText( "" ); // clear output JTextField 156 157 } // end method cartonsJTextFieldKeyPressed 158 159 // clear totalResultJTextField because the value is now invalid 160 private void itemsJTextFieldKeyPressed( KeyEvent event ) 161 { 162 totalResultJTextField.setText( "" ); // clear output TextField 163 164 } // end method itemsJTextFieldKeyPressed 165 166 // main method 167 public static void main( String[] args ) 168 { 169 Inventory application = new Inventory(); 170 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 171 172 } // end method main 173 174 } // end class Inventory Setting a JTextField ’s text property to the empty string

24 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 5.4 Memory Concepts Figure 5.15 Memory location showing name and value of variable cartons. locations – variable names correspond to locations in computer’s memory

25 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 5.4 Memory Concepts (Cont.) Figure 5.16 Memory locations after assigning values to cartons and items. 10 items cartons 12

26 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 5.4 Memory Concepts (Cont.) Figure 5.17 Memory locations after a multiplication operation. cartons items result 120 10 12

27 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27 5.5 Arithmetic Arithmetic operators – special symbols to perform arithmetic functions – binary operators: require two operands – unary operators: require one operand

28 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28 5.5 Arithmetic (Cont.)

29 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 5.6 Using the Debugger: Breakpoints and the run, stop, cont and print Commands Debugger – analyze behavior of applications to locate and remove logic errors – logic error: application compiles successfully, but produces erroneous results – break points: markers set at any executable line of code

30 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30 5.6 Using the Debugger: Breakpoints and the run, stop, cont and print Commands (Cont.) Figure 5.20 Starting the Java debugger. Start the Java debugger Running the debugger – type javac –g ApplicationName.java – type jdb

31 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 31 5.6 Using the Debugger: Breakpoints and the run, stop, cont and print Commands (Cont.) Figure 5.21 Running the Inventory application through the debugger. Run the Inventory application Running the debugger (cont.) – type run ApplicationName Inventory application

32 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32 5.6 Using the Debugger: Breakpoints and the run, stop, cont and print Commands (Cont.) Figure 5.22 Restarting the debugger. Restart the debugger Restarting the debugger –type jdb

33 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33 5.6 Using the Debugger: Breakpoints and the run, stop, cont and print Commands (Cont.) Figure 5.23 Setting two breakpoints. Set a breakpoint at line 148 Set a breakpoint at line 145 Setting breakpoints – type stop at ApplicaitonName:line#

34 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 34 5.6 Using the Debugger: Breakpoints and the run, stop, cont and print Commands (Cont.) Figure 5.24 Restarting the Inventory application. Restart the Inventory application Inventory application

35 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35 5.6 Using the Debugger: Breakpoints and the run, stop, cont and print Commands (Cont.) Figure 5.25 Reaching the first breakpoint. Next line of code to be executed Breakpoint is reached Paused Inventory application

36 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 36 5.6 Using the Debugger: Breakpoints and the run, stop, cont and print Commands (Cont.) Figure 5.26 Execution reaches the second breakpoint. Another breakpoint is reached After breakpoint is reached, type cont to continue running application

37 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 37 5.6 Using the Debugger: Breakpoints and the run, stop, cont and print Commands (Cont.) Figure 5.27 Examine the values of three variables. The value of items The value of cartons The value of result The print command – prints current value held by variable – type print variableName

38 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38 5.6 Using the Debugger: Breakpoints and the run, stop, cont and print Commands (Cont.) Figure 5.28 Execution resumes. Execution resumes

39 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 5.6 Using the Debugger: Breakpoints and the run, stop, cont and print Commands (Cont.) Figure 5.29 Remove the breakpoints. Both breakpoints are removed Clear breakpoint at line 148 Clear breakpoint at line 145 Two breakpoints are set

40 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 5.6 Using the Debugger: Breakpoints and the run, stop, cont and print Commands (Cont.) Figure 5.30 Application executes with no breakpoints set.

41 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 41 5.6 Using the Debugger: Breakpoints and the run, stop, cont and print Commands (Cont.) Figure 5.31 Exiting the debugger.


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application."

Similar presentations


Ads by Google