1 Tutorial 6 Wage Calculator Application Introducing Algorithms

  • Slides: 44
Download presentation
1 Tutorial 6 – Wage Calculator Application Introducing Algorithms, Pseudocode and Program Control Outline

1 Tutorial 6 – Wage Calculator Application Introducing Algorithms, Pseudocode and Program Control Outline 6. 1 6. 2 6. 3 6. 4 6. 5 6. 6 6. 7 6. 8 6. 9 6. 10 6. 11 Test-Driving the Wage Calculator Application Algorithms Pseudocode Control Statements if Selection Statement if…else Selection Statement Constructing the Wage Calculator Application Assignment Operators Formatting Text Using the Debugger: The print and set Commands Wrap-Up © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

2 Objectives • In this tutorial, you will learn to: – – – Use

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

6. 1 Test-Driving the Wage Calculator Application © Copyright 1992 -2004 by Deitel &

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

6. 1 Test-Driving the Wage Calculator Application (Cont. ) Figure 6. 1 Running the Wage

6. 1 Test-Driving the Wage Calculator Application (Cont. ) Figure 6. 1 Running the Wage Calculator application. • Run the Wage Calculator application – cd c: ExamplesTutorial 06 Completed. ApplicationWage. Calculator – java Wage. Calculator © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4

6. 1 Test-Driving the Wage Calculator Application (Cont. ) Figure 6. 2 Calculating wages by

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

6 6. 2 Algorithms • Algorithms – procedure for solving a problem in terms

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

7 6. 3 Pseudocode • Pseudocode – informal language to help programmers develop algorithms – describes only executable statements: – actions performed when Java application is run © 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

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

9 6. 4 Control Statements (Cont. ) Figure 6. 3 Sequence statement activity diagram. © 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

10 6. 5 if Selection Statement If student’s grade is greater than or equal to 60 Display “Passed” if ( student. Grade >= 60 ) { grade. Display. JLabel. set. Text( "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 © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

11 6. 5 if Selection Statement (Cont. ) © Copyright 1992 -2004 by Deitel

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

12 6. 5 if Selection Statement (Cont. ) Figure 6. 5 if single-selection statement UML

12 6. 5 if Selection Statement (Cont. ) Figure 6. 5 if single-selection statement UML activity diagram. © 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

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

14 6. 6 if…else Selection Statement (Cont. ) Figure 6. 6 if…else double-selection statement UML

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

15 6. 6 if…else Selection Statement (Cont. ) Figure 6. 7 Pseudocode for an application

15 6. 6 if…else Selection Statement (Cont. ) Figure 6. 7 Pseudocode for an application that displays a student’s grades. 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” © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

16 6. 6 if…else Selection Statement (Cont. ) Figure 6. 8 Java code converted from

16 6. 6 if…else Selection Statement (Cont. ) Figure 6. 8 Java code converted from the pseudocode in Fig. 6. 7. if ( student. Grade >= 90 ) { display. Label. set. Text( "A" ); } else if ( student. Grade >= 80 ) { display. Label. set. Text( "B" ); } else if ( student. Grade >= 70 ) { display. Label. set. Text( "C" ); } else if ( student. Grade >= 60 ) { display. Label. set. Text( "D" ); } else { display. Label. set. Text( "F" ); } © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

17 6. 6 if…else Selection Statement (Cont. ) Figure 6. 9 Nested if…else statements with

17 6. 6 if…else Selection Statement (Cont. ) Figure 6. 9 Nested if…else statements with alternative indentation. if ( student. Grade >= 90 ) { display. Label. set. Text( "A" } else if ( student. Grade >= 80 { display. Label. set. Text( "B" } else if ( student. Grade >= 70 { display. Label. set. Text( "C" } else if ( student. Grade >= 60 { display. Label. set. Text( "D" } else { display. Label. set. Text( "F" } © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. ); ) );

6. 7 Constructing the Wage Calculator Application Calculate gross wages when the user clicks

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

6. 7 Constructing the Wage Calculator Application (Cont. ) © Copyright 1992 -2004 by

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

6. 7 Constructing the Wage Calculator Application (Cont. ) Figure 6. 11 Calculate JButton event

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

6. 7 Constructing the Wage Calculator Application (Cont. ) Figure 6. 12 Assigning user input

6. 7 Constructing the Wage Calculator Application (Cont. ) Figure 6. 12 Assigning user input to variables. Declare variable hourly. Wage and assign it the user input from hourly. Wage. JText Field Declare variable hours. Worked and assign it the user input from hours. Worked. JTex t. Field • type double © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. – represents floating point numbers – Double. parse. Double – converts a String to a double 21

6. 7 Constructing the Wage Calculator Application (Cont. ) Figure 6. 13 Creating a constant.

6. 7 Constructing the Wage Calculator Application (Cont. ) Figure 6. 13 Creating a constant. Constant declaration Figure 6. 14 Declaring a variable of type double. Declare double variable wages © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 22

6. 7 Constructing the Wage Calculator Application (Cont. ) Figure 6. 15 if…else statement that

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

6. 7 Constructing the Wage Calculator Application (Cont. ) Figure 6. 16 Assigning the result

6. 7 Constructing the Wage Calculator Application (Cont. ) Figure 6. 16 Assigning the result to gross. Wages. JText. Field. Displaying output © Copyright 1992 -2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 24

6. 7 Constructing the Wage Calculator Application (Cont. ) Figure 6. 17 Displaying unformatted number.

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

26 6. 8 Assignment Operators © Copyright 1992 -2004 by Deitel & Associates, Inc.

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

27 6. 8 Assignment Operators (Cont. ) Figure 6. 19 Using the addition assignment operator

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

28 6. 9 Formatting Text Figure 6. 20 Using the format method of Decimal. Format

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

29 6. 9 Formatting Text (Cont. ) Figure 6. 21 Displaying formatted number. Displaying formatted

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14

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

26 27 28 29 30 31 32 33 34 35 36 37 38 39

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

48 49 50 51 52 53 54 55 56 57 58 59 60 61

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 // set up hourly. Wage. Field hourly. Wage. JText. Field = new JText. Field(); hourly. Wage. JText. Field. set. Bounds( 120, 16, 90, 21 ); hourly. Wage. JText. Field. set. Horizontal. Alignment( JText. Field. RIGHT ); content. Pane. add( hourly. Wage. JText. Field ); // set up hours. Worked. JLabel = new JLabel(); hours. Worked. JLabel. set. Bounds( 16, 56, 90, 21 ); hours. Worked. JLabel. set. Text( "Hours worked: " ); content. Pane. add( hours. Worked. JLabel ); // set up hours. Worked. JText. Field = new JText. Field(); hours. Worked. JText. Field. set. Bounds( 120, 56, 90, 21 ); hours. Worked. JText. Field. set. Horizontal. Alignment( JText. Field. RIGHT ); content. Pane. add( hours. Worked. JText. Field ); Outline 32 Wage. Calculator. java (3 of 7) // set up gross. Wages. JLabel = new JLabel(); gross. Wages. JLabel. set. Bounds( 16, 90, 21 ); gross. Wages. JLabel. set. Text( "Gross wages: " ); content. Pane. add( gross. Wages. JLabel ); 2004 Prentice Hall, Inc. All rights reserved.

73 74 75 76 77 78 79 80 81 82 83 84 85 86

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 // set up gross. Wages. JText. Field = new JText. Field(); gross. Wages. JText. Field. set. Bounds( 120, 96, 90, 21 ); gross. Wages. JText. Field. set. Horizontal. Alignment( JText. Field. RIGHT ); gross. Wages. JText. Field. set. Editable( false ); content. Pane. add( gross. Wages. JText. Field ); // set up calculate. JButton = new JButton(); calculate. JButton. set. Bounds( 120, 136, 90, 24 ); calculate. JButton. set. Text( "Calculate" ); content. Pane. add( calculate. JButton ); calculate. JButton. add. Action. Listener( Outline 33 Wage. Calculator. java (4 of 7) new Action. Listener() // anonymous inner class { // event handler called when calculate. JButton is clicked public void action. Performed ( Action. Event event ) { calculate. JButton. Action. Performed( event ); } } // end anonymous inner class 2004 Prentice Hall, Inc. All rights reserved.

98 99 100 101 102 103 104 105 106 107 108 109 110 111

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 ); // end call to add. Action. Listener Outline 34 // set properties of application’s window set. Title( "Wage Calculator" ); // set title bar text set. Size( 230, 200 ); // set window size set. Visible( true ); // display window } // end method create. User. Interface // method called when user presses calculate. JButton private void calculate. JButton. Action. Performed( Action. Event event ) { // get hourly wage double hourly. Wage = Double. parse. Double( hourly. Wage. JText. Field. get. Text() ); // get number of hours worked this week double hours. Worked = Double. parse. Double( hours. Worked. JText. Field. get. Text() ); // constant for maximum hours employee can // work before being paid for overtime final double HOUR_LIMIT = 40. 0; Convert hourly wage to double by using Double. parse Wage. Calculator. java Double (5 of 7) Convert hours worked to double by using Double. parse. Do uble Keyword final specifies that HOUR_LIMIT is a constant 2004 Prentice Hall, Inc. All rights reserved.

123 124 125 126 127 128 129 130 131 132 133 134 135 136

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

148 } // end method calculate. JButton. Action. Performed 149 150 // main method

148 } // end method calculate. JButton. Action. Performed 149 150 // main method 151 public static void main( String args[] ) 152 { 153 Wage. Calculator application = new Wage. Calculator(); 154 application. set. Default. Close. Operation( JFrame. EXIT_ON_CLOSE ); 155 156 } // end method main 157 158 } // end class Wage. Calculator Outline 36 Wage. Calculator. java (7 of 7) 2004 Prentice Hall, Inc. All rights reserved.

6. 10 Using the Debugger: The print and set Commands Figure 6. 23 Setting breakpoints

6. 10 Using the Debugger: The print and set Commands Figure 6. 23 Setting breakpoints at lines 116 and 130. © 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 (Cont. ) Figure 6.

6. 10 Using the Debugger: The print and set Commands (Cont. ) Figure 6. 24 Suspended application execution. © 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.

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

6. 10 Using the Debugger: The print and set Commands (Cont. ) Figure 6. 26 Examining variable hourly. Wage. Value of variable hourly. Wage © 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.

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 © 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. ) 42 Figure

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

6. 10 Using the Debugger: The print and set Commands (Cont. ) Figure 6.

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 variable. Name = new. Value © 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.

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