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

Slides:



Advertisements
Similar presentations
Basic Java – Interface design. Understand: How to use TextPad for Java How to define classes and objects How to create a GUI interface How event-driven.
Advertisements

Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Outline DivideByZeroTes t.java 1 // Fig. 15.1: DivideByZeroTest.java 2 // An exception-handling example that checks for divide-by-zero. 3 import java.awt.*;
 2003 Prentice Hall, Inc. All rights reserved. Chapter 15 – Exception Handling Outline 15.1 Introduction 15.2 Exception-Handling Overview 15.3 Exception-Handling.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 15.1 Test-Driving the Craps Game Application.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 14 - Exception Handling Outline 14.1Introduction 14.2When Exception Handling Should Be Used 14.3Other.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Objectives –Display a date and time. –Use a JPasswordField.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 6.1 Test-Driving the Wage Calculator Application.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Wage Calculator Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Fund Raiser Application Introducing Scope, Pass-by-Reference and Option Strict.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Wage Calculator Application: Introducing.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
 2005 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 18.1 Test-Driving the Microwave Oven Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 23.1 Test-Driving the Screen Scraping Application.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 25 – Ticket Information Application Introducing.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 7 – Class Average Application: Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 22.1 Test-Driving the Typing Skills Developer.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 18 – Student Grades Application Introducing.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 15 – Fund Raiser Application Introducing.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 11.1 Test-Driving the Security Panel Application.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 10.1 Test-Driving the Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 26.1 IBM Cloudscape Database 26.2 Test-Driving.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 13.1 Test-Driving the Enhanced Interest Calculator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 – Methods Part I.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 17.1 Test-Driving the Student Grades Application.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 7.1 Test-Driving the Dental Payment Application.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
Chapter 14 – Exception Handling
“Form Ever Follows Function” Louis Henri Sullivan
Java Programming: From Problem Analysis to Program Design,
CiS 260: App Dev I Chapter 6: GUI and OOD.
Chapter 15 – Exception Handling
Presentation transcript:

© Copyright 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

© Copyright 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.

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

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

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

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

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

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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Conversions

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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.

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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.

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

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

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

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

 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; // JLabel and JTextField to display amount after expenses 16 private JLabel afterExpensesJLabel; 17 private JTextField afterExpensesJTextField; // JLabel and JTextField to display total amount raised 20 private JLabel totalRaisedJLabel; 21 private JTextField totalRaisedJTextField; // JButton to allow user to enter donation 24 private JButton donateJButton; 25

 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; // no-argument constructor 30 public FundRaiser() 31 { 32 createUserInterface(); 33 } // 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(); // enable explicit positioning of GUI components 42 contentPane.setLayout( null ); // 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

 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( 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 } } // end anonymous inner class ); // end call to addKeyListener // set up afterExpensesJLabel 70 afterExpensesJLabel = new JLabel(); 71 afterExpensesJLabel.setBounds( 16, 48, 98, 20 ); 72 afterExpensesJLabel.setText( "After expenses:" ); 73 contentPane.add( afterExpensesJLabel ); 74

 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 ); // set up totalRaisedJLabel 85 totalRaisedJLabel = new JLabel(); 86 totalRaisedJLabel.setBounds( 16, 80, 88, 20 ); 87 totalRaisedJLabel.setText( "Total raised:" ); 88 contentPane.add( totalRaisedJLabel ); // 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

 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( 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 } } // end anonymous inner class ); // end call to addActionListener // 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 } // end method createUserInterface

 2003 Prentice Hall, Inc. All rights reserved. Outline 26 FundRaiser.java (6 of 8) // returns donation amount after operating expenses 126 private double calculateDonation( int donatedAmount ) 127 { 128 final double NET_PERCENTAGE = 0.83; // calculate amount of donation for charity 131 return NET_PERCENTAGE * donatedAmount; } // end method calculateDonation // 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() ); // obtain donation amount after operating expenses deduction 143 double netDonation = calculateDonation( grossDonation ); // 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

 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 ) ); // update total amount of donations received 153 totalNetDonations += netDonation; // display total amount collected for charity 156 totalRaisedJTextField.setText( 157 dollars.format( totalNetDonations ) ); } // end method donateJButtonActionPerformed // clear afterExpensesJTextField 162 private void donationJTextFieldKeyPressed( KeyEvent event ) 163 { 164 // clear afterExpensesJTextField 165 afterExpensesJTextField.setText( "" ); } // 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

 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 ); } // end method main } // end class FundRaiser

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Conversions (Cont.) Figure 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

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