Tutorial 9 Income Tax Calculator Application: Introducing the switch Multiple-Selection Statement

Similar documents
Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

C++ Language Tutorial

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

6. Control Structures

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Sources: On the Web: Slides will be available on:

An Incomplete C++ Primer. University of Wyoming MA 5310

Appendix K Introduction to Microsoft Visual C++ 6.0

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Pemrograman Dasar. Basic Elements Of Java

Object Oriented Software Design

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

7.7 Case Study: Calculating Depreciation

Chapter 2: Elements of Java

Basics of I/O Streams and File I/O

Object Oriented Software Design

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

The programming language C. sws1 1

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

C++ Input/Output: Streams

13 Classes & Objects with Constructors/Destructors

Chapter 8 Selection 8-1

Conditions & Boolean Expressions

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

Member Functions of the istream Class

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Sequential Program Execution

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Lecture 2 Notes: Flow of Control

PIC 10A. Lecture 7: Graphics II and intro to the if statement

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Chapter 5. Selection 5-1

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

- Hour 1 - Introducing Visual C++ 5

1. The First Visual C++ Program

Appendix M: Introduction to Microsoft Visual C Express Edition

Lab 2 - CMPS 1043, Computer Science I Introduction to File Input/Output (I/O) Projects and Solutions (C++)

Introduction to Java. CS 3: Computer Programming in Java

Statements and Control Flow

Chapter One Introduction to Programming

Numbering Systems. InThisAppendix...

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Java Programming Fundamentals

Answers to Review Questions Chapter 7

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

A brief introduction to C++ and Interfacing with Excel

The C Programming Language course syllabus associate level

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Creating a Simple Visual C++ Program

Introduction to Java

Formatting Numbers with C++ Output Streams

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Chapter 5 Functions. Introducing Functions

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

The While Loop. Objectives. Textbook. WHILE Loops

Visual Studio 2008 Express Editions

Passing 1D arrays to functions.

The Payroll Program. Payroll

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Comp151. Definitions & Declarations

J a v a Quiz (Unit 3, Test 0 Practice)

AP Computer Science Java Subset

Syllabus for CS 134 Java Programming

Software Development Phases

Visual C Tutorial

C++ INTERVIEW QUESTIONS

The little endl that couldn t

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Introduction to Python

Java (12 Weeks) Introduction to Java Programming Language

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Fondamenti di C++ - Cay Horstmann 1

Computer Programming C++ Classes and Objects 15 th Lecture

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Ch 7-1. Object-Oriented Programming and Classes

Tutorial on C Language Programming

JavaScript: Control Statements I

Ubuntu. Ubuntu. C++ Overview. Ubuntu. History of C++ Major Features of C++

Syllabus OBJECT ORIENTED PROGRAMMING C++

Introduction to Visual C++.NET Programming. Using.NET Environment

Java Interview Questions and Answers

Wage Calculator Application

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true.

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Java Types and Enums. Nathaniel Osgood MIT April 25, 2012

Objective-C Tutorial

Lecture 22: C Programming 4 Embedded Systems

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

Compiler Construction

VHDL Test Bench Tutorial

Lecture 5: Java Fundamentals III

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Transcription:

Tutorial 9 Income Tax Calculator Application: Introducing the switch Multiple-Selection Statement Outline 9.1 Test-Driving the Income Tax Calculator Application 9.2 Introducing the switch Multiple-Selection Statement 7.3 Implicit (promotion) and Explicit (cast) conversion 9.3 Constructing the Income Tax Calculator Application 9.4 Wrap-Up

Objectives In this tutorial, you will learn to: Use the switch multiple-selection statement to choose among many alternative actions. Use case labels to identify the alternative actions in switch statements. Understand the short and long data types to represent small and large integers.

9.1 Test-Driving the Income Tax Calculator Application Applic ation Requirements An accounting firm wants an application that estimates the amount of Federal income tax that a client must pay, depending upon that client s annual salary. The application should use the following income ranges and corresponding tax rates: Under $25,000 = 15% income tax $25,000 74,999 = 25% income tax $75,000 149,999 = 28% income tax $150,000 299,999 = 33% income tax $300,000 and over = 35% income tax Your application should display an error message if the user enters an annual salary that is less than or equal to zero. [Note: The actual U.S. Federal income tax rates vary based on many factors. For more information, see the information in IRS Form 1040-ES, which is located at www.irs.gov/pub/irs-pdf/f1040e03.pdf. In Exercise 9.12, you will create an application that calculates a closer approximation of an individual s Federal income tax.]

9.1 Test-Driving the Income Tax Calculator Application (Cont.) Figure 9.3 Income Tax Calculator application output for a $100,000 yearly salary.

9.2 Introducing the switch Multiple-Selection Statement The syntax of the switch statement is a bit peculiar. Its objective is to check several possible constant values for an expression. Something similar to what we did at the beginning of this section with the concatenation of several if and else if instructions (source: cplusplus.com) switch (expression) { case constant1: group of statements 1; break; case constant2: group of statements 2; break;.. default: default group of statements }

9.2 Introducing the switch Multiple-Selection Statement switch multiple-selection statements switch keyword Controlling expression Must be a value of the char, short, int or long types or a type that can be converted to these types case labels case keyword Constant expression Can be a character literal, an integer literal or a variable containing a character or integer constant Cannot be a floating-point value

9.2 Introducing the switch Multiple-Selection Statement (Cont.) break statement transfers control to the first statement after the switch statement If a case label has no break statement, execution falls through default case Executes if no constant expression matches the controlling expression A switch statement cannot contain two case labels with the same constant expression or two default cases If the controlling expression does not match any constant expression and there is no default case, none of the statements in the switch body are executed

9.2 Introducing the switch Multiple-Selection Statement (Cont.) Figure 9.4 switch multiple-selection statement UML activity diagram.

7.3 Implicit (promotion) and Explicit (cast) conversion Implicit conversion (promotion) Values are automatically converted to larger data types (types at top of the list in Fig. 7.12) when necessary No loss of data Explicit conversion (casting) Use unary cast operator static_cast< datatype > Prevents compiler warnings when converting to a smaller data type Converting to a smaller data type may cause a loss of data

Fundamental Data Types (Week 3 Review) Summary of the basic fundamental data types in C++, as well as the range of values that can be represented with each one.

7.3 Implicit (promotion) and Explicit (cast) conversion Primitive typ e long double double float unsigned long int long int unsigned int int unsigned short int short int char unsigned char signed char (synonymous with unsigned long) (synonymous with long) (synonymous with unsigned) (synonymous with unsigned short) (synonymous with short) bool (false becomes 0, true becomes 1) Figure 7.12 Promotion hiera rc hy for C++ p rimitive typ es.

9.3 Constructing the Income Tax Calculator Application Prompt the user for and input the yearly salary If the salary is less than or equal to zero Display error message Else Switch based on the integer result of the yearly salary divided by 25,000 If the result is 0 (salary is less than $25,000) Set the tax rate to 15% If the result is 1 or 2 (salary is between $25,000 and $74,999, inclusive) Set the tax rate to 25% If the result is 3, 4 or 5 (salary is between $75,000 and $149,999, inclusive) Set the tax rate to 28% If the result is 6, 7, 8, 9, 10 or 11 (salary is between $150,000 and $299,999, inclusive) Set the tax rate to 33% Otherwise (salary is greater than or equal to $300,000) Set the tax rate to 35% Calculate and display the income tax Figure 9.5 Pseudocode for the Income Tax Calculator application.

9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.6 Defining variables to contain the tax rate and the yearly salary. Defining variables to store user input

9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.7 Prompting the user for and inputting the yearly salary. Prompting the user for and inputting the yearly salary

9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.8 Adding an if...else statement to the function. Displaying an error message if the user enters an invalid salary

9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.9 Adding a switch statement to the function. Beginning of the switch statement

9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.10 Adding a case label to the switch statement. Sets the tax rate to 15% if salary is less than 25000

9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.11 Finishing the switch statement. Multiple case labels result in the same taxrate (28%) Sets tax rate to 25% if salary is between 25000 and 74999 Multiple case labels result in the same taxrate (33%)

9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.12 Adding a default case to the switch statement. default case at the end of the switch statement When the default case is at the end of the switch body, a break statement is unnecessary

9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.13 Calculating and displaying the income tax. Calculating the income tax Displaying the income tax

9.3 Constructing the Income Tax Calculator Application (Cont.) Figure 9.14 Completed Income Tax Calculator application.

1 // Tutorial 9: IncomeTaxCalculator.cpp 2 // Calculates a person's Federal income tax 3 // depending on that person's salary. 4 #include <iostream> // required to perform C++ stream I/O 5 #include <iomanip> // required for parameterized stream manipulators 6 7 using namespace std; // for accessing C++ Standard Library members 8 9 // function main begins program execution 10 int main() 11 { 12 // define variables 13 double taxrate; // tax rate 14 double salary; // yearly salary 15 16 // prompt user for and input yearly salary 17 cout << "\nenter yearly salary: "; 18 cin >> salary; // get yearly salary 19 20 // ensure that the user entered a value greater than zero 21 if ( salary > 0 ) 22 { Define variables to store user input IncomeTaxCalculator. cpp (1 of 4) Prompt the user for and input the yearly salary

23 // determine appropriate tax rate based on yearly salary 24 switch ( static_cast< int > ( salary ) / 25000 ) 25 { 26 case 0: // yearly salary under $25,000 27 taxrate = 0.15; // 15% tax rate 28 break; 29 30 case 1: // yearly salary in range $25,000-49,999 31 case 2: // yearly salary in range $50,000-74,999 32 taxrate = 0.25; // 25% tax rate 33 break; 34 35 case 3: // yearly salary in range $75,000-99,999 36 case 4: // yearly salary in range $100,000-124,999 37 case 5: // yearly salary in range $125,000-149,999 38 taxrate = 0.28; // 28% tax rate 39 break; 40 Beginning of the switch statement IncomeTaxCalculator. cpp (2 of 4) Set the tax rate to 15% if salary is less than 25000 Set the tax rate to 25% if salary is between 25000 and 74999 Set the tax rate to 28% if salary is between 75000 and 149999

41 case 6: // yearly salary in range $150,000-174,999 42 case 7: // yearly salary in range $175,000-199,999 43 case 8: // yearly salary in range $200,000-224,999 44 case 9: // yearly salary in range $225,000-249,999 45 case 10: // yearly salary in range $250,000-274,999 46 case 11: // yearly salary in range $275,000-299,999 47 taxrate = 0.33; // 33% tax rate 48 break; 49 50 default: // yearly salary $300,000 or higher 51 taxrate = 0.35; // 35% tax rate 52 53 } // end switch 54 55 double incometax = salary * taxrate; // calculate taxes 56 57 // display income tax 58 cout << "\nincome tax: $" << fixed << setprecision( 2 ) 59 << incometax << endl; 60 } // end if IncomeTaxCalculator. cpp (3 of 4) Set the tax rate to 33% if salary is between 150000 and 299999 default case at the end of the switch statement Calculate the income tax Display the income tax

61 else // display error message 62 { 63 cout << "\nerror: Income must be greater than 0" << endl; 64 } // end else 65 66 cout << "\n"; 67 68 return 0; // indicate that program ended successfully 69 70 } // end function main Display an error message if annual salary is not greater than zero IncomeTaxCalculator. cpp (4 of 4)

Lab and Homework Assignment Tutorial 4 Wage Calculator. Turn in annotated source file with your own comments. Answer and Turn-in Tutorial 4 Questions 4.1, 4.2, 4.7, 4.8, 4.9, 4.10 at the end of the Chapter 4 plus Tutorial 9 Questions 9.1, 9.2, 9.5, 9.7, 9.10 at the end of Chapter 9. Always write the question followed by the answer. Remember to highlight the answer. Exercises 4.11, 9.11, and 4.17 Due next Wednesday