1 / 18

Chapter 7 – Control Structures

Chapter 7 – Control Structures.

tejana
Download Presentation

Chapter 7 – Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7 – Control Structures A payroll company calculates the gross earnings per week of employees. Employees’ weekly salaries are based on the number of hours they worked and their hourly wages. Create an application that accepts this information and calculates each employee’s total (gross) earnings. The application assumes a standard work week of 40 hours. The wages for 40 or fewer hours are calculated by multiplying the employee’s hourly salary by the number of hours worked. Any time worked over 40 hours in a week is considered “overtime” and earns time and a half. Salary for time and a half is calculated by multiplying the employee’s hourly wage by 1.5 and multiplying the result of that calculation by the number of overtime hours worked. The total overtime earned is added to the user’s gross earnings for the regular 40 hours of work to calculate the total earnings for that week.

  2. Introduction • Getting more complicated so we need structured approach to programming • Structured Programming: techniques that bring clarity in developing and modifying programs. • Algorithms: Set of instructions executed in a particular order (e.g. showerdressed) • Pseudocode: informal language to develop algorithms. • E.g. Assign 0 to counter vs. counter=0

  3. Control Structures • Sequence • E.g. A  B  C  D • Selection (IF…Then, IF…Then…Else, Select case) • If (income>expenses) THEN buy ipod ELSE wait • Returns boolean data type (True/False) • Iteration (or repetition) • While…End While,Do While…Loop, Do…Loop While, Do Until…Loop, Do…Loop Until, For…Next, and For Each…Next

  4. Operators • In order to evaluate a condition for a selection statement need relational and equality operators > < >= <= = <> += -= *= /= \= ^= • Control structures can be nested and stacked.

  5. Back to application Problem: Input  (Hours/wages)  Calculate (salary)  Display (earnings) Pseudocode: -Get hours worked and hourly wages from the textbox -If hours worked are less than or equal to 40 Gross earnings equals hours worked times hourly wages -Else Gross earnings equals 40 times hourly wage plus hours above 40 times wage and a half -Display gross earnings

  6. Designing Application Dim wage, gross As Decimal Dim hours As Double Const HOUR_LIMIT As Integer = 40 wage = Val(wageTextBox.Text) hours = Val(hoursTextBox.Text) If hours <= HOUR_LIMIT Then gross = wage * hours Else gross = HOUR_LIMIT * wage gross += (hours - HOUR_LIMIT) * wage * 1.5 End If ‘formatting the output earningsResultLabel.Text = gross[(String.Format(“{0:C}” , earnings)] Remember to insert comments

  7. Formatting Text • Output needs to be formatted • String.Format(formatControlString, variable) • String.Format("{0:c}", gross)

  8. Final tips • Debugger watch window • Available only in break mode • Can see contents of variables • Debug->windows->watch->type in name of variable or an expression • Tells you variable value at that point of execution and data type • Can use watch window to change var. values to text program

  9. Chapter 8: Error message and Checkboxes • Continuing with previous example. The application should give an error message if user does not input anything. If wageTextBox.Text = "" OrElse hoursTextBox.Text = "" Then MessageBox.Show("Please enter wage and hours", "Missing information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End If • Also, create a check box that checks eligibility for overtime. E.g. if 1.5 x overtime is checked then employee eligible for 1.5 times the salary. If 1 x checked then employee eligible only for 1x the salary. If not eligible checked then employee gets paid only for 40 hours. • Add a groupbox around overtime options

  10. How it will look?

  11. Checking errors Private Sub calculateButton_Click(ByVal sender As System.EventArgs) Handles calculateButton.Click Dim wage As Double = Val(wageTextBox.Text) Dim hours As Double = Val(hoursTextBox.Text) Dim pay As Double If wageTextBox.Text = "" OrElse hoursTextBox.Text = "" Then MessageBox.Show("Please enter wage and hours", "Missing information", _ MessageBoxButtons.OK, MessageBoxIcon.Exclamation) ElseIf OTCheckBox.Checked = False AndAlso RegularCheckBox.Checked = False AndAlso _ NotCheckBox.Checked = False Then MessageBox.Show("Please check overtime eligibility ", "Missing information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Else If OTCheckBox.Checked = True Then If hours <= 40 Then pay = wage * hours Else pay = wage * (hours - 40) * 1.5 + wage * 40 End If resultLabel.Text = String.Format("{0:c}", pay) End If If RegularCheckBox.Checked = True Then pay = wage * hours resultLabel.Text = String.Format("{0:c}", pay) End If If NotCheckBox.Checked = True Then If hours <= 40 Then pay = wage * hours Else pay = wage * 40 End If resultLabel.Text = String.Format("{0:c}", pay) End If End If End Sub Based on checkbox selected calculate the pay. Checkbox is a bad example here. Why? Radio buttons should be used instead. Why?

  12. Chapter 8: In-Class Exercise Dental Payment Application (Introducing checkboxes and message dialogs) A dentist’s office administrator wishes to create an application that employees can use to bill patients. The application must allow users to enter the patient’s name and specify which services were performed during the visit. The application will then calculate the total charges.If a user attempts to calculate a bill before any services are specified, or before the patient’s name is entered, an error message informing the user of that necessary input is missing will be displayed.

  13. How will it look

  14. Pseudocode and ACE table When user clicks calculate Clear previous total If user has not entered name or selected a checkbox Display error message Else Initialize the total to zero If “Cleaning” Checkbox is selected Add cost of a cleaning to total If “Cavity filling” Checkbox is selected Add cost of receiving filling to the total If “X-Ray” Checkbox is selected Add cost of receiving X-Ray to the total Format total to display as currency Display total

  15. Logical Operators

  16. Checkboxes • Add checkboxes Private Sub calculateButton_Click totalResultLabel.Text = "" Dim total As Decimal If cleanCheckBox.Checked = True Then total += 35 End If If cavityCheckBox.Checked = True Then total += 150 End If If xrayCheckBox.Checked = True Then total += 85 End If totalResultLabel.Text = String.Format("{0:c}", total) End Sub

  17. Problem • If no checkboxes or no name, no error • Doesn’t clear results on changing the checkbox. If (((nameTextBox.Text = "“))OrElse((cleanCheckBox.Checked = False AndAlsocavityCheckBox.Checked = False AndAlso xrayCheckBox.Checked = False))) Then MessageBox.Show("Please enter patients name", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) End if

  18. Homework • Problem 8.11 Download the template from the website and make the necessary changes. • As usual submit screen shot and a printout of your program with your name commented

More Related