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 Tutorial 12 – Enhancing the Wage Calculator Application.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application."— Presentation transcript:

1 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application Introducing Methods Outline 12.1 Test-Driving the Enhanced Wage Calculator Application 12.2 Classes and Methods 12.3Method Declarations 12.4Finishing the Maximum Application 12.5Using Methods in the Wage Calculator Application 12.6Using the Debugger: Controlling Execution Using the step, step up and next Commands 12.7Wrap-Up

2 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Construct applications modularly from pieces called methods. –Work with “built-in” methods. –Create your own methods.

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

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 12.1Test Driving the Enhanced Wage Calculator Application (Cont.) Figure 12.1 Wage Calculator executing. Enter 10 in the Hourly wage: JTextField Enter 45 in the Hours worked: JTextField Click the Calculate JButton

5 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 12.2Classes and Methods Methods –Programmer-declared classes and methods –Pre-declared classes and methods –Code reuse –Divide and conquer Construct an application from smaller, more manageable pieces

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 12.2Classes and Methods (Cont.)

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 12.3Method Declarations Pythagorean theorem –The sum of the squares of the two smaller sides equals the square of the largest side (hypotenuse) of a right triangle Declaring a method –Method name –Return type –Parameter list (parameters) –Method header –Method body –Method declaration –Return statement / return keyword

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 12.3Method Declarations (Cont.) Figure 12.3 Running the Hypotenuse Calculator application.

9 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 12.3Method Declarations (Cont.) Figure 12.4 Hypotenuse Calculator template code. Lengths for sides A and B Message dialog displays if negative value (or zero) is entered

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 12.3Method Declarations (Cont.) Figure 12.5 Declaring method square. Method name: square Return type: double Parameter list: A double named side Method headerA right brace marks the end of a method declaration

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 12.3Method Declarations (Cont.) Figure 12.6 Coding the square method. The return statement returns the value of side * side The return statement sends a value back to the method’s caller

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 12.3Method Declarations (Cont.) Figure 12.7 Hypotenuse Calculator application updated.

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 12.3Method Declarations (Cont.) Figure 12.8 Invoking the square method. Call a method by using the method name followed by a set of parentheses containing the method’s argument ( sideA and sideB ) Calling method square for each side

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 12.3Method Declarations (Cont.) Invoking a method –A method is invoked by a method call Method call specifies the method name and arguments that the callee (method being called) receives in parameters –When a method is complete Return control to the caller (calling method)

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 12.3Method Declarations (Cont.) Figure 12.9 Completing the calculateJButtonActionPerformed method. Format and display the length of the hypotenuse Calling Math method sqrt The Math.sqrt method returns the square root of its argument

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 12.3Method Declarations (Cont.) Figure 12.10 Running the completed Hypotenuse Calculator application.

17 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 12.4Finishing the Maximum Application Figure 12.11 Maximum application executing.

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 12.4Finishing the Maximum Application (Cont.) Figure 12.12 Invoking the determineMaximum method. Store user input Calling method determineMaximum

19 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 12.4Finishing the Maximum Application (Cont.) Figure 12.13 Declaring the determineMaximum method. Empty method determineMaximum Arguments: three values of which to determine maximum Return value: double containing maximum value

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 12.4Finishing the Maximum Application (Cont.) Figure 12.14 Math.max returns the larger of its two arguments. Return maximum of all three values Calling Math.max twice to determine the maximum of three values Use the Math.max method

21 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 12.4Finishing the Maximum Application (Cont.) Figure 12.15 Maximum application executing.

22 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 12.5Using Methods in the Wage Calculator Application Figure 12.16 calculateJButtonActionPerformed calls the calculatePay method. Format and display the total wage Store user input Call to method calculatePay

23 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 12.5Using Methods in the Wage Calculator Application (Cont.) Figure 12.17 Declaring the calculatePay method. Method headerReturn wages for the week Calculate gross wages based on the number of hours worked

24 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 12.5Using Methods in the Wage Calculator Application (Cont.) Figure 12.18 Wage Calculator application executing.

25  2004 Prentice Hall, Inc. All rights reserved. Outline 25 WageCalculator.java (1 of 7) 1 // Tutorial 12: WageCalculator.java 2 // This application inputs the hourly wage and number of hours 3 // worked for an employee, then calculates the employee's gross 4 // wages (with overtime for hours worked over 40 hours). 5 import java.awt.*; 6 import java.awt.event.*; 7 import javax.swing.*; 8 import java.text.*; 9 10 public class WageCalculator extends JFrame 11 { 12 // JLabel and JTextField for wage per hour 13 private JLabel hourlyWageJLabel; 14 private JTextField hourlyWageJTextField; 15 16 // JLabel and JTextField for hours worked in a week 17 private JLabel hoursWorkedJLabel; 18 private JTextField hoursWorkedJTextField; 19 20 // JLabel and JTextField for gross wages 21 private JLabel grossWagesJLabel; 22 private JTextField grossWagesJTextField; 23 24 // JButton to initiate wage calculation 25 private JButton calculateJButton;

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

27  2004 Prentice Hall, Inc. All rights reserved. Outline 27 WageCalculator.java (3 of 7) 48 // set up hourlyWageJTextField 49 hourlyWageJTextField = new JTextField(); 50 hourlyWageJTextField.setBounds( 120, 16, 90, 21 ); 51 hourlyWageJTextField.setHorizontalAlignment( 52 JTextField.RIGHT ); 53 contentPane.add( hourlyWageJTextField ); 54 55 // set up hoursWorkedJLabel 56 hoursWorkedJLabel = new JLabel(); 57 hoursWorkedJLabel.setBounds( 16, 56, 90, 21 ); 58 hoursWorkedJLabel.setText( "Hours worked:" ); 59 contentPane.add( hoursWorkedJLabel ); 60 61 // set up hoursWorkedJTextField 62 hoursWorkedJTextField = new JTextField(); 63 hoursWorkedJTextField.setBounds( 120, 56, 90, 21 ); 64 hoursWorkedJTextField.setHorizontalAlignment( 65 JTextField.RIGHT ); 66 contentPane.add( hoursWorkedJTextField ); 67 68 // set up grossWagesJLabel 69 grossWagesJLabel = new JLabel(); 70 grossWagesJLabel.setBounds( 16, 96, 90, 21 ); 71 grossWagesJLabel.setText( "Gross wages:" ); 72 contentPane.add( grossWagesJLabel );

28  2004 Prentice Hall, Inc. All rights reserved. Outline 28 WageCalculator.java (4 of 7) 73 74 // set up grossWagesJTextField 75 grossWagesJTextField = new JTextField(); 76 grossWagesJTextField.setBounds( 120, 96, 90, 21 ); 77 grossWagesJTextField.setHorizontalAlignment( 78 JTextField.RIGHT ); 79 grossWagesJTextField.setEditable( false ); 80 contentPane.add( grossWagesJTextField ); 81 82 // set up calculateJButton 83 calculateJButton = new JButton(); 84 calculateJButton.setBounds( 120, 136, 90, 24 ); 85 calculateJButton.setText( "Calculate" ); 86 contentPane.add( calculateJButton ); 87 calculateJButton.addActionListener( 88 89 new ActionListener() // anonymous inner class 90 { 91 // event handler called when calculateJButton clicked 92 public void actionPerformed ( ActionEvent event ) 93 { 94 calculateJButtonActionPerformed( event ); 95 } 96 97 } // end anonymous inner class

29  2004 Prentice Hall, Inc. All rights reserved. Outline 29 WageCalculator.java (5 of 7) 98 99 ); // end call to addActionListener 100 101 // set properties of application's window 102 setTitle( "Wage Calculator" ); // set title bar string 103 setSize( 230, 200 ); // set window size 104 setVisible( true ); // display window 105 106 } // end method createUserInterface 107 108 // get user input and call displayPay 109 private void calculateJButtonActionPerformed( ActionEvent event ) 110 { 111 // get hourly wage 112 double hourlyWage = 113 Double.parseDouble( hourlyWageJTextField.getText() ); 114 115 // get number of hours worked this week 116 double hoursWorked = 117 Double.parseDouble( hoursWorkedJTextField.getText() ); 118 119 // gross wages for week; returned from method calculatePay 120 double totalWages = calculatePay( hoursWorked, hourlyWage ); 121 Calling method calculatePay Initializing variables

30  2004 Prentice Hall, Inc. All rights reserved. Outline 30 WageCalculator.java (6 of 7) 122 // specify output format 123 DecimalFormat dollars = new DecimalFormat( "$0.00" ); 124 125 // display gross wages 126 grossWagesJTextField.setText( dollars.format( totalWages ) ); 127 128 } // end method calculateJButtonActionPerformed 129 130 // calculate and display wages in grossWagesJTextField 131 private double calculatePay( double hours, double wages ) 132 { 133 // gross wages for week; calculated in if...else statement 134 double total; 135 136 // constant for maximum hours employee can 137 // work before being paid for overtime 138 final double HOUR_LIMIT = 40.0; // declare constant 139 140 // determine gross wages 141 if ( hours <= HOUR_LIMIT ) 142 { 143 // regular wages for HOUR_LIMIT (40) hours or less 144 total = wages * hours; 145 } Formatting and displaying value returned from the calculatePay method Method calculatePay takes two double parameters, returns a double Variable total will contain user’s wages for the week Overtime for more than 40 hours worked Calculate standard pay

31  2004 Prentice Hall, Inc. All rights reserved. Outline 31 WageCalculator.java (7 of 7) 146 else // worked more than HOUR_LIMIT (40) hours 147 { 148 // wages for first 40 hours with time and a half added 149 total = ( wages * HOUR_LIMIT ) + ( hours - HOUR_LIMIT ) * 150 ( 1.5 * wages ); 151 } 152 153 return total; 154 155 } // end method calculatePay 156 157 // main method 158 public static void main( String[] args ) 159 { 160 WageCalculator application = new WageCalculator(); 161 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 162 163 } // end method main 164 165 } // end class WageCalculator Calculate overtime pay Return total to calling method Closing brace ends method body

32 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 32 12.6Using the Debugger: Controlling execution using step, step up and next commands Figure 12.20 Setting a breakpoint in the Wage Calculator application. Setting a breakpoint Starting the debugger

33 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 33 Figure 12.21 Running the Wage Calculator application. 12.6Using the Debugger: Controlling execution using step, step up and next commands (Cont.) Running the application

34 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 34 Figure 12.22 Reaching the breakpoint in the Wage Calculator application. 12.6Using the Debugger: Controlling execution using step, step up and next commands (Cont.) Reaching a breakpoint

35 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 35 12.6Using the Debugger: Controlling execution using step, step up and next commands (Cont.) Step –Execute the next statement in the application stepUp –Execute the statements in the method and return control to the place where the method was called next –Like the step command, but if the next statement is a method call, execute the entire method

36 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 36 Figure 12.23 Stepping into the calculatePay method. 12.6Using the Debugger: Controlling execution using step, step up and next commands (Cont.) Application currently paused at line 138 in method calculatePay Stepping through an application

37 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 37 Figure 12.24 Stepping out of a method. 12.6Using the Debugger: Controlling execution using step, step up and next commands (Cont.) Executing remaining statements in current method

38 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38 Figure 12.25 Reaching the breakpoint in the Wage Calculator application. 12.6Using the Debugger: Controlling execution using step, step up and next commands (Cont.)

39 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 Figure 12.26 Stepping over a method call. 12.6Using the Debugger: Controlling execution using step, step up and next commands (Cont.) Using the next command

40 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 Figure 12.27 Exiting the debugger. 12.6Using the Debugger: Controlling execution using step, step up and next commands (Cont.)


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Enhancing the Wage Calculator Application."

Similar presentations


Ads by Google