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 14.1 Test-Driving the Fund Raiser Application.

Similar presentations


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

1 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 14.1 Test-Driving the Fund Raiser Application 14.2 Constructing the Fund Raiser Application 14.3 Conversions 14.4 Wrap-Up Tutorial 14 – Fund Raiser Application Introducing Scope and Conversion of Primitive Types

2 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objective In this tutorial, you will learn to: –Create variables that can be used in all the application’s methods. –Assign a value from a variable of one type to a variable of another type, using implicit conversion.

3 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 14.1 Test-Driving the Fund Raiser Application

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 14.1 Test-Driving the Fund Raiser Application (Cont.) Figure 14.1 Running the completed Fund Raiser application.

5 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 14.1 Test-Driving the Fund Raiser Application (Cont.) Figure 14.2 Fund Raiser application with first donation entered.

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 14.1 Test-Driving the Fund Raiser Application (Cont.) Figure 14.3 Making further donations. Total of all donations (minus expenses)

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 14.2 Constructing the Fund Raiser Application When the user changes the current donation amount in the JTextField Clear the JTextField that displays the current donation after expenses When the user clicks the Donate JButton Obtain the current donation from the JTextField Calculate and display the current donation after expenses Update and display the total amount raised for the charity

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 14.2 Constructing the Fund Raiser Application (Cont.)

9 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 14.2 Constructing the Fund Raiser Application (Cont.) Scope –Every variable declaration has a scope –Scope of an instance variable –Scope of a local variable

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 14.2 Constructing the Fund Raiser Application (Cont.) Figure 14.5 Declaring an instance variable in the application. Declaring an instance variable Instance variables –Declared inside a class but outside any method declarations of that class –Scope is the entire body of the class

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 14.2 Constructing the Fund Raiser Application (Cont.) Figure 14.6 Declaring local variables in the Fund Raiser application. Right brace ends the scope of the local variables Declaration of a local variable is the beginning of the variable’s scope Local variables –Declared in the body of a method (such as an event handler) –Scope is from the point at which the declaration appears in the block to the end of that block

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 14.2 Constructing the Fund Raiser Application (Cont.) Java can only evaluate arithmetic expressions in which the operands’ types are identical. Implicit conversion –Ensures that the operands are of the same type Hidden instance variable

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 14.2 Constructing the Fund Raiser Application (Cont.) Figure 14.7 Value of donatedAmount converted to double to perform the calculation. Value of donatedAmount implicitly converted to double

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 14.3 Conversions

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 14.3 Conversions (Cont.) Widening conversion –Conversion that changes a value of a “smaller” type to a value of a “larger” type (e.g. int to a double ) –Will not lose information –Also called implicit conversion –Does not require a cast operator (or cast) A type contained in parentheses.

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 14.3 Conversions (Cont.) Narrowing conversion –Conversion that changes a value of a “larger” type to a value of a “smaller” type (e.g. double to an int ) –Information could be lost –Also called explicit conversion (or casting) –Requires a cast operator (or cast) A type contained in parentheses.

17 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 14.3 Conversions (Cont.) Figure 14.9 Displaying the donation amount after operating expenses are deducted. Display the donation amount after expenses are subtracted

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 14.3 Conversions (Cont.) Figure 14.10 Updating and displaying the total amount raised for charity. Update instance variableDisplay total amount raised for charity

19 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 14.3 Conversions (Cont.) Figure 14.11 Clearing the After Expenses: JTextField.

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 14.3 Conversions (Cont.) Figure 14.12 Running the completed application.

21  2003 Prentice Hall, Inc. All rights reserved. Outline 21 FundRaiser.java (1 of 8) 1 // Tutorial 14: FundRaiser.java 2 // Calculates the amount of a donation after expenses and then 3 // totals repeated donations. 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.text.*; 7 import javax.swing.*; 8 9 public class FundRaiser extends JFrame 10 { 11 // JLabel and JTextField to hold donation 12 private JLabel donationJLabel; 13 private JTextField donationJTextField; 14 15 // JLabel and JTextField to display amount after expenses 16 private JLabel afterExpensesJLabel; 17 private JTextField afterExpensesJTextField; 18 19 // JLabel and JTextField to display total amount raised 20 private JLabel totalRaisedJLabel; 21 private JTextField totalRaisedJTextField; 22 23 // JButton to allow user to enter donation 24 private JButton donateJButton; 25

22  2003 Prentice Hall, Inc. All rights reserved. Outline 22 FundRaiser.java (2 of 8) 26 // instance variable stores total raised for charity 27 private double totalNetDonations = 0.00; 28 29 // no-argument constructor 30 public FundRaiser() 31 { 32 createUserInterface(); 33 } 34 35 // create and position GUI components; register event handlers 36 private void createUserInterface() 37 { 38 // get content pane for attaching GUI components 39 Container contentPane = getContentPane(); 40 41 // enable explicit positioning of GUI components 42 contentPane.setLayout( null ); 43 44 // set up donationJLabel 45 donationJLabel = new JLabel(); 46 donationJLabel.setBounds( 16, 16, 80, 20 ); 47 donationJLabel.setText( "Donation:" ); 48 contentPane.add( donationJLabel ); 49 Declaring instance variable

23  2003 Prentice Hall, Inc. All rights reserved. Outline 23 FundRaiser.java (3 of 8) 50 // set up donationJTextField 51 donationJTextField = new JTextField(); 52 donationJTextField.setBounds( 122, 16, 120, 21 ); 53 donationJTextField.setHorizontalAlignment( JTextField.RIGHT ); 54 contentPane.add( donationJTextField ); 55 donationJTextField.addKeyListener( 56 57 new KeyAdapter() // anonymous inner class 58 { 59 // event handler called when donationJTextField is edited 60 public void keyPressed( KeyEvent event ) 61 { 62 donationJTextFieldKeyPressed( event ); 63 } 64 65 } // end anonymous inner class 66 67 ); // end call to addKeyListener 68 69 // set up afterExpensesJLabel 70 afterExpensesJLabel = new JLabel(); 71 afterExpensesJLabel.setBounds( 16, 48, 98, 20 ); 72 afterExpensesJLabel.setText( "After expenses:" ); 73 contentPane.add( afterExpensesJLabel ); 74

24  2003 Prentice Hall, Inc. All rights reserved. Outline 24 FundRaiser.java (4 of 8) 75 // set up afterExpensesJTextField 76 afterExpensesJTextField = new JTextField(); 77 afterExpensesJTextField.setBounds( 122, 48, 120, 20 ); 78 afterExpensesJTextField.setText( "$0.00" ); 79 afterExpensesJTextField.setEditable( false ); 80 afterExpensesJTextField.setHorizontalAlignment( 81 JTextField.RIGHT ); 82 contentPane.add( afterExpensesJTextField ); 83 84 // set up totalRaisedJLabel 85 totalRaisedJLabel = new JLabel(); 86 totalRaisedJLabel.setBounds( 16, 80, 88, 20 ); 87 totalRaisedJLabel.setText( "Total raised:" ); 88 contentPane.add( totalRaisedJLabel ); 89 90 // set up totalRaisedJTextField 91 totalRaisedJTextField = new JTextField(); 92 totalRaisedJTextField.setBounds( 122, 80, 120, 20 ); 93 totalRaisedJTextField.setText( "$0.00" ); 94 totalRaisedJTextField.setEditable( false ); 95 totalRaisedJTextField.setHorizontalAlignment( 96 JTextField.RIGHT ); 97 contentPane.add( totalRaisedJTextField ); 98

25  2003 Prentice Hall, Inc. All rights reserved. Outline 25 FundRaiser.java (5 of 8) 99 // set up donateJButton 100 donateJButton = new JButton(); 101 donateJButton.setBounds( 63, 112, 122, 24 ); 102 donateJButton.setText( "Donate" ); 103 contentPane.add( donateJButton ); 104 donateJButton.addActionListener( 105 106 new ActionListener() // anonymous inner class 107 { 108 // event handler called when donateJButton is clicked 109 public void actionPerformed( ActionEvent event ) 110 { 111 donateJButtonActionPerformed( event ); 112 } 113 114 } // end anonymous inner class 115 116 ); // end call to addActionListener 117 118 // set properties of application’s window 119 setTitle( "Fund Raiser" ); // set title bar string 120 setSize( 263, 174 ); // set window size 121 setVisible( true ); // display window 122 123 } // end method createUserInterface

26  2003 Prentice Hall, Inc. All rights reserved. Outline 26 FundRaiser.java (6 of 8) 124 125 // returns donation amount after operating expenses 126 private double calculateDonation( int donatedAmount ) 127 { 128 final double NET_PERCENTAGE = 0.83; 129 130 // calculate amount of donation for charity 131 return NET_PERCENTAGE * donatedAmount; 132 133 } // end method calculateDonation 134 135 // calculate the donation and fill the JTextFields 136 private void donateJButtonActionPerformed( ActionEvent event ) 137 { 138 // get donation amount 139 int grossDonation = 140 Integer.parseInt( donationJTextField.getText() ); 141 142 // obtain donation amount after operating expenses deduction 143 double netDonation = calculateDonation( grossDonation ); 144 145 // specify display format 146 DecimalFormat dollars = new DecimalFormat( "$0.00" ); 147 Declaring the method calculateDonation Obtaining the donation amount Calling method calculateDonation to obtain donation amount after deducting operating expenses

27  2003 Prentice Hall, Inc. All rights reserved. Outline 27 FundRaiser.java (7 of 8) 148 // display amount of donation after expenses 149 afterExpensesJTextField.setText( 150 dollars.format( netDonation ) ); 151 152 // update total amount of donations received 153 totalNetDonations += netDonation; 154 155 // display total amount collected for charity 156 totalRaisedJTextField.setText( 157 dollars.format( totalNetDonations ) ); 158 159 } // end method donateJButtonActionPerformed 160 161 // clear afterExpensesJTextField 162 private void donationJTextFieldKeyPressed( KeyEvent event ) 163 { 164 // clear afterExpensesJTextField 165 afterExpensesJTextField.setText( "" ); 166 167 } // end method donationJTextFieldKeyPressed 168 Displaying the donation amount Displaying the total amount of donations to the charity after expenses Clearing afterExpenses- JTextField Updating the total amount of donations received

28  2003 Prentice Hall, Inc. All rights reserved. Outline 28 FundRaiser.java (8 of 8) 169 // main method 170 public static void main( String args[] ) 171 { 172 FundRaiser application = new FundRaiser(); 173 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 174 175 } // end method main 176 177 } // end class FundRaiser

29 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 14.3 Conversions (Cont.) Figure 14.14 Attempting to access donateJButtonActionPerformed ’s local variable grossDonation when it is out of scope. Try to access local variable grossDonation, which is out of scope

30 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 30 14.3 Conversions (Cont.) Figure 14.15 Attempting to access donateJButtonActionPerformed ’s local variable grossDonation causes a compilation error.


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

Similar presentations


Ads by Google