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 6.1 Test-Driving the Wage Calculator Application.

Similar presentations


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

1 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 6.1 Test-Driving the Wage Calculator Application 6.2 Algorithms 6.3 Pseudocode 6.4 Control Statements 6.5 if Selection Statement 6.6 if … else Selection Statement 6.7Constructing the Wage Calculator Application 6.8Assignment Operators 6.9Formatting Text 6.10Using the Debugger: The print and set Commands 6.11Wrap-Up Tutorial 6 – Wage Calculator Application Introducing Algorithms, Pseudocode and Program Control

2 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives In this tutorial, you will learn to: –Use basic problem-solving techniques. –Use structured programming techniques. –Use control statements. –Use pseudocode as an application development tool. –Use the if and if … else selection statements to choose between alternative actions. –Use the assignment operators. –Use the debugger’s print command to evaluate expressions. –Use the debugger’s set command to change variable values during program execution.

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

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 Figure 6.1 Running the Wage Calculator application. 6.1 Test-Driving the Wage Calculator Application (Cont.) Run the Wage Calculator application – cd c:\Examples\Tutorial06\ CompletedApplication\WageCalculator – java WageCalculator

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

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 6.2 Algorithms Algorithms – procedure for solving a problem in terms of: – actions to be executed – order in which actions should be executed – Program control: ordering a program’s statements correctly

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 6.3 Pseudocode Pseudocode – informal language to help programmers develop algorithms – describes only executable statements: – actions performed when Java application is run

8 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 6.4 Control Statements Sequential execution – statements in an application executing one after another in the order they were written – Java runs sequentially unless a transfer of control is made Transfer of control – occurs when executed statement does not previously executed statement in written application – three different types of control: – sequence – selection – repetition

9 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 6.4 Control Statements (Cont.) Figure 6.3 Sequence statement activity diagram.

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 6.5 if Selection Statement If student’s grade is greater than or equal to 60 Display “Passed” if ( studentGrade >= 60 ) { gradeDisplayJLabel.setText( "Passed" ); } if selection statement – performs an action based on a condition Condition – expression with true or false value used to make a decision – true or false values are of type boolean

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 6.5 if Selection Statement (Cont.)

12 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12 Figure 6.5 if single-selection statement UML activity diagram. 6.5 if Selection Statement (Cont.)

13 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 13 6.6 if … else Selection Statement If student’s grade is greater than or equal to 60 Display “Passed” else Display “Failed” if ( studentGrade >= 60 ) { displayLabel.setText( "Passed" ); } else { displayLabel.setText( "Failed" ); } if…else selection statement – performs action if condition is true and a different action if condition is false

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 Figure 6.6 if … else double-selection statement UML activity diagram. 6.6 if … else Selection Statement (Cont.)

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 6.6 if … else Selection Statement (Cont.) If student’s grade is greater than or equal to 90 Display “A” else If student’s grade is greater than or equal to 80 Display “B” else If student’s grade is greater than or equal to 70 Display “C” else If student’s grade is greater than or equal to 60 Display “D” else Display “F” Figure 6.7 Pseudocode for an application that displays a student’s grades.

16 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 6.6 if … else Selection Statement (Cont.) if ( studentGrade >= 90 ) { displayLabel.setText( "A" ); } else if ( studentGrade >= 80 ) { displayLabel.setText( "B" ); } else if ( studentGrade >= 70 ) { displayLabel.setText( "C" ); } else if ( studentGrade >= 60 ) { displayLabel.setText( "D" ); } else { displayLabel.setText( "F" ); } Figure 6.8 Java code converted from the pseudocode in Fig. 6.7.

17 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 17 6.6 if … else Selection Statement (Cont.) if ( studentGrade >= 90 ) { displayLabel.setText( "A" ); } else if ( studentGrade >= 80 ) { displayLabel.setText( "B" ); } else if ( studentGrade >= 70 ) { displayLabel.setText( "C" ); } else if ( studentGrade >= 60 ) { displayLabel.setText( "D" ); } else { displayLabel.setText( "F" ); } Figure 6.9 Nested if … else statements with alternative indentation.

18 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18 6.7 Constructing the Wage Calculator Application Calculate gross wages when the user clicks the Calculate JButton: Input the hourly wage Input the number of hours worked If the number of hours worked is less than or equal to 40 hours Gross earnings equals hours worked times hourly wage else Gross earnings equals 40 times hourly wage plus hours above 40 times hourly wage times 1.5 Display gross wages

19 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 19 6.7 Constructing the Wage Calculator Application (Cont.)

20 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 Figure 6.11 Calculate JButton event handler. Calculate JButton event handler 6.7 Constructing the Wage Calculator Application (Cont.) “{“ and “}” symbols indicate beginning and end of event handler body

21 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 21 Figure 6.12 Assigning user input to variables. Declare variable hourlyWage and assign it the user input from hourlyWageJText Field Declare variable hoursWorked and assign it the user input from hoursWorkedJTex tField 6.7 Constructing the Wage Calculator Application (Cont.) type double – represents floating point numbers – Double.parseDouble – converts a String to a double

22 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22 Figure 6.13 Creating a constant. Figure 6.14 Declaring a variable of type double. Constant declarationDeclare double variable wages 6.7 Constructing the Wage Calculator Application (Cont.)

23 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 23 Figure 6.15 if … else statement that calculates gross wages. if … else statement to calculate wages 6.7 Constructing the Wage Calculator Application (Cont.)

24 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24 Figure 6.16 Assigning the result to grossWagesJTextField. Displaying output 6.7 Constructing the Wage Calculator Application (Cont.)

25 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 25 Figure 6.17 Displaying unformatted number. 6.7 Constructing the Wage Calculator Application (Cont.) Displaying unformatted number

26 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 26 6.8 Assignment Operators

27 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27 Figure 6.19 Using the addition assignment operator in a calculation. 6.8 Assignment Operators (Cont.) Addition assignment operator Using different assignment operator shortens code

28 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 28 Figure 6.20 Using the format method of DecimalFormat to display the result as currency. 6.9 Formatting Text DecimalFormat for dollar values Displays wages in dollar format Formatting text – makes output easier to read – DecimalFormat : used to format decimal numbers – format method

29 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 29 Figure 6.21 Displaying formatted number. 6.9 Formatting Text (Cont.) Displaying formatted number

30  2004 Prentice Hall, Inc. All rights reserved. Outline 30 WageCalculator.java (1 of 7) 1 // Tutorial 6: 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;

31  2004 Prentice Hall, Inc. All rights reserved. Outline 31 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

32  2004 Prentice Hall, Inc. All rights reserved. Outline 32 WageCalculator.java (3 of 7) 48 // set up hourlyWageField 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 );

33  2004 Prentice Hall, Inc. All rights reserved. Outline 33 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 is clicked 92 public void actionPerformed ( ActionEvent event ) 93 { 94 calculateJButtonActionPerformed( event ); 95 } 96 97 } // end anonymous inner class

34  2004 Prentice Hall, Inc. All rights reserved. Outline 34 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 text 103 setSize( 230, 200 ); // set window size 104 setVisible( true ); // display window 105 106 } // end method createUserInterface 107 108 // method called when user presses calculateJButton 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 // constant for maximum hours employee can 120 // work before being paid for overtime 121 final double HOUR_LIMIT = 40.0; 122 Convert hourly wage to double by using Double.parse Double Convert hours worked to double by using Double.parseDo uble Keyword final specifies that HOUR_LIMIT is a constant

35  2004 Prentice Hall, Inc. All rights reserved. Outline 35 WageCalculator.java (6 of 7) 123 // gross wages for week; calculated in if...else statement 124 double wages; 125 126 // determine gross wages 127 if ( hoursWorked <= HOUR_LIMIT ) 128 { 129 // regular wages for HOUR_LIMIT (40) hours or less 130 wages = ( hoursWorked * hourlyWage ); 131 } 132 else // worked more than HOUR_LIMIT (40) hours 133 { 134 // wage for first HOUR_LIMIT (40) hours 135 wages = HOUR_LIMIT * hourlyWage; 136 137 // add time-and-a-half for hours above HOUR_LIMIT (40) 138 wages += 139 ( hoursWorked - HOUR_LIMIT ) * ( 1.5 * hourlyWage ); 140 } 141 142 // specify output format 143 DecimalFormat dollars = new DecimalFormat( "$0.00" ); 144 145 // display gross wages 146 grossWagesJTextField.setText( dollars.format( wages ) ); 147 Variable to store gross wages Begin if … else statement End if part of if … else statement and begin else part; else body executes when condition in line 127 evaluates to false Assign to left operand the result of adding left and right operands End else part of if … else Format result as a dollar amount

36  2004 Prentice Hall, Inc. All rights reserved. Outline 36 WageCalculator.java (7 of 7) 148 } // end method calculateJButtonActionPerformed 149 150 // main method 151 public static void main( String args[] ) 152 { 153 WageCalculator application = new WageCalculator(); 154 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 155 156 } // end method main 157 158 } // end class WageCalculator

37 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 37 6.10 Using the Debugger: The print and set Commands Figure 6.23 Setting breakpoints at lines 116 and 130.

38 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 38 6.10 Using the Debugger: The print and set Commands (Cont.) Figure 6.24 Suspended application execution.

39 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 39 6.10 Using the Debugger: The print and set Commands (Cont.) Figure 6.25 Application execution suspended when debugger reaches the breakpoint at line 116.

40 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 40 6.10 Using the Debugger: The print and set Commands (Cont.) Figure 6.26 Examining variable hourlyWage. Value of variable hourlyWage

41 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 41 6.10 Using the Debugger: The print and set Commands (Cont.) Figure 6.27 Examining the values of expressions. Evaluating a boolean expression Evaluating an arithmetic expression

42 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 42 6.10 Using the Debugger: The print and set Commands (Cont.) Figure 6.28 Resuming execution and displaying the value of the variable hoursWorked. Display value of variable hoursWorked Resume execution

43 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 43 6.10 Using the Debugger: The print and set Commands (Cont.) Figure 6.29 Modifying values. Value modified in debugger set command – changes value of a variable from the debugger – type set variableName = newValue

44 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 44 Figure 6.30 Output displayed after the debugging process. 6.10 Using the Debugger: The print and set Commands (Cont.) Gross wages result based on altered value of variable hoursWorked


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

Similar presentations


Ads by Google