An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 2 Applications and Data.

Slides:



Advertisements
Similar presentations
Programming Logic and Design Fourth Edition, Introductory
Advertisements

JavaScript, Fourth Edition
Chapter 2: Introduction to C++.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
VB .NET Programming Fundamentals
A First Book of ANSI C Fourth Edition
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs.
Programming.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
An Object-Oriented Approach to Programming Logic and Design Chapter 1 An Overview of Computers and Logic.
Introduction to Programming with RAPTOR
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Programming Logic and Design Seventh Edition
Neal Stublen What's a class?  A class is comprised of a set of data and the actions taken on that data  Each action is represented.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 1: Introduction to Computers and Programming
Chapter 2 Variables.
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
Chapter 2 Introduction to C++ Programming
Chapter 2: Introduction to C++
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2: Input, Processing, and Output
Structured Programming
Chapter 2: Problem Solving Using C++
ICS103 Programming in C Lecture 3: Introduction to C (2)
The Selection Structure
Variables, Expressions, and IO
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Introduction to C++
Designing and Debugging Batch and Interactive COBOL Programs
Topics Introduction to File Input and Output
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 3 – Introduction to C# Programming
Chapter 2: Introduction to C++.
Topics Introduction to Functions Defining and Calling a Function
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2: Input, Processing, and Output
12th Computer Science – Unit 5
Chapter 2 Variables.
Lexical Elements & Operators
Variables in C Topics Naming Variables Declaring Variables
Topics Introduction to File Input and Output
Chapter 4: Writing and Designing a Complete Program
Programming Logic and Design Eighth Edition
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 2 Applications and Data

Objectives In this chapter, you will learn about: Creating an application class with a main() method Programming languages reflecting logic Literals, variables, and named constants Assigning values to variables Arithmetic operations Features of good program design An Introduction to Structure 2An Object-Oriented Approach to Programming Logic and Design

Creating an Application Class with a main() Method Application – Program that executes to accomplish a task – Term used synonymously with program Object-Oriented Programming Languages – Every programming statement must be part of a class Method – Named set of statements that perform task(s) in an application – In a class, all statements are placed in a method – Application classes contain a main() method 3An Object-Oriented Approach to Programming Logic and Design

Creating an Application Class with a main() Method (cont’d) 4 An Object-Oriented Approach to Programming Logic and Design Figure 2-1 Flowchart and pseudocode for a class with a main() method that prints “Hello”

Creating an Application Class with a main() Method (cont’d) Application Class Begins class header with word class followed by class name Ends with word endClass – No object-oriented programming language uses endClass Class header and ending statement aligned vertically for easy reading Only one header and one endClass statement for each class 5An Object-Oriented Approach to Programming Logic and Design

Creating an Application Class with a main() Method (cont’d) Class name – Any legal identifier Identifier – Name of a programming object (class, method, or variable) – Different programming languages have different rules for naming identifiers 6An Object-Oriented Approach to Programming Logic and Design

Creating an Application Class with a main() Method (cont’d) Common identifier rules – Can contain letters and digits – Some languages allow special characters (Ex. hyphen or underscore) – May not begin with a digit (most languages) – May not contain white space (spaces, tabs, line breaks) – May not be a keyword – Are case sensitive ( Hello, hello, and HELLO are considered separate identifiers) Use descriptive names 7An Object-Oriented Approach to Programming Logic and Design

Understanding the main() Method Executable programs contain a main() method. Set of parentheses always follows method name Rules for naming method identifiers follow same basic rules as class identifiers Convention of this text – method names begin with a lowercase letter and method ends with a return statement – For readability, capitalize subsequent words in method header Examples: computePaycheck, startTheGame 8An Object-Oriented Approach to Programming Logic and Design

Understanding the main() Method (cont’d) Place method’s executable statements between method header and return statement Pseudocode indentation conventions Method header and return statement align vertically to show they are a pair Method statements are indented more than class and endClass statements but less than executable statements in code 9An Object-Oriented Approach to Programming Logic and Design

Understanding How Programming Languages Reflect Logic 10An Object-Oriented Approach to Programming Logic and Design Figure 2-2 The Hello class written in the Java programming language Figure 2-3 The Hello class written in the C# programming language

Understanding How Programming Languages Reflect Logic (cont’d) 11An Object-Oriented Approach to Programming Logic and Design Figure 2-4 The Hello class written in the Visual Basic programming language

Using Literals, Variables, and Named Constants Programs accept input in different ways – User enters data in an interactive program – Storage devices provide large amounts of data to batch programs Three different forms of data – Literals (unnamed constants) – Variables – Named constants 12An Object-Oriented Approach to Programming Logic and Design

Understanding Unnamed, Literal Constants and Their Data Types Two types of unnamed constants: numeric and text Numeric constant or literal numeric constant – Number without quotation marks Example: 613 – Cannot contain alphabetic characters String constant, or literal string constant – Text enclosed in quotation marks Example: “Jenna” – Also called alphanumeric values – Can contain digits, punctuation, and other characters 13An Object-Oriented Approach to Programming Logic and Design

Working with Variables Variables – Named memory locations – Contents can vary over time – Hold just one value at any given time Examine the doubling statements in the following pseudocode 14An Object-Oriented Approach to Programming Logic and Design Figure 2-5 Statements that input a number, double it, and display the results

Working with Variables (cont’d) 15An Object-Oriented Approach to Programming Logic and Design In most programming languages, must declare a variable before it can be used Variable declaration includes data type and identifier Data type describes the following: –What values the variable can hold –How the data is stored in computer memory –What operations can be performed on the data

Working with Variables (cont’d) In this text, two data types are used – num – string Must declare variables before using them in a program Sample statements containing declarations. – num mySalary – string myName 16An Object-Oriented Approach to Programming Logic and Design

Working with Variables (cont’d) Initializing the variable – Provide a starting value for the variable Sample statements containing declarations with initializations – num yourSalary = – string yourName = “Pat” If a variable is declared but not initialized – The variable contains unknown value called garbage – Usually illegal to use a garbage-holding variable or display it as output 17An Object-Oriented Approach to Programming Logic and Design

The following complete program declares variables before they are initialized 18An Object-Oriented Approach to Programming Logic and Design Working with Variables (cont’d) Figure 2-6 A complete number-doubling program

Naming Variables Choose descriptive, meaningful names Interpreter associates names with specific memory addresses To name variables, follow same rules as naming classes and methods 19An Object-Oriented Approach to Programming Logic and Design

Understanding a Variable’s Data Type Each variable has a data type: numeric or string Numeric variable – Holds digits and can be used in mathematical operations – Can hold a decimal point and a sign (plus or minus) – Example: testScore = 96 String variable – Holds text and special characters – Can hold digits but not used in mathematical operations – Example: zipcode = “08202” 20An Object-Oriented Approach to Programming Logic and Design

Understanding a Variable’s Data Type (cont’d) Can assign data to a variable only if correct type num taxRate = 2.5 OK num taxRate = “2.5” invalid Can set a variable to the value of another variable of the same data type num testScore1 num testScore2 testScore1 = 96 testScore2 = testScore1 21An Object-Oriented Approach to Programming Logic and Design

Declaring Named Constants Similar to a variable, except named constant is assigned a value only once Used to identify values that will not be changed during program execution Make programs easier to understand Text’s convention – All uppercase letters with underscore separating words for readability – Example: SALES_TAX 22An Object-Oriented Approach to Programming Logic and Design

Declaring Named Constants (cont’d) Benefits of using named constants – Ease of program maintenance Change value and all references change automatically – Naming constant provides type of documentation – Helps prevent typographical errors 23An Object-Oriented Approach to Programming Logic and Design

Assigning Values to Variables Assignment statement – Example: myAnswer = myNumber * 2 – Makes the calculation – Stores the result in memory location: myAnswer Assignment operator (=) – Binary Operator – Always operates from right to left – Right side is evaluated before the assignment is made – Value to the left must be a memory address 24An Object-Oriented Approach to Programming Logic and Design

Performing Arithmetic Operations Standard arithmetic operators + (plus sign) – addition - (minus sign) – subtraction * (asterisk) – multiplication / (slash) – division Sample Valid Assignment Statements – someNumber = 2 * 20/5 – someNumber = anotherNumber + 3 – totalScore = 0 – totalScore = totalScore + 10 Increases the value of totalScore 25An Object-Oriented Approach to Programming Logic and Design

Performing Arithmetic Operations (cont’d) Sample Invalid Assignment Statements – = someNumber – anotherNumber + 3 = answer – value on the left side of the assignment operator is not a memory location. 26An Object-Oriented Approach to Programming Logic and Design

Performing Arithmetic Operations (cont’d) Rules of operator precedence – Parentheses evaluated first – Multiplication and division next, left to right – Addition and subtraction next, left to right 27An Object-Oriented Approach to Programming Logic and Design

Features of Good Program Design Good practices that make programs easier to write and maintain – Use comments where appropriate – Choose meaningful identifiers – Strive to design clear program statements – Write clear prompts and echo input – Maintain good programming habits as skills improve 28An Object-Oriented Approach to Programming Logic and Design

Features of Good Program Design Internal documentation – Comments that explain and clarify code that are included within the same documents as the program’s source code. External documentation – Documentation written outside the program Using program comments – Aid in identifying the purpose of variables – Explain complex calculations – Allow a new programmer to understand existing code 29An Object-Oriented Approach to Programming Logic and Design

Choosing Identifiers General guidelines – Variables or constants should be named with nouns or combination of nouns and adjectives – Methods should be named with verbs or combined verb and noun – Use meaningful names (self-documenting) – Use pronounceable names – Use abbreviations sparingly 30An Object-Oriented Approach to Programming Logic and Design

Choosing Identifiers (cont’d) General guidelines (cont’d) – Avoid digits in a name – Separate words in long, multi-word identifiers – For variables that hold status, use a form of the verb “to be,” such as “is” or “are” – Constants in all uppercase with underscores between words – Different conventions may be dictated by the organization 31An Object-Oriented Approach to Programming Logic and Design

Designing Clear Statements Avoid confusing line breaks – Most languages are free-form, so arrange code clearly Use temporary variables to clarify long statements – Temporary variable: an intermediate or work variable – Used during program execution – Not used for input or output – Advantage: if final output is incorrect, can display temporary results to determine where the problem occurs 32An Object-Oriented Approach to Programming Logic and Design

Designing Clear Statements (cont’d) Example of using temporary variables 33An Object-Oriented Approach to Programming Logic and Design Figure 2-7 Two ways of achieving the same salespersonCommission result

Writing Clear Prompts and Echoing Input Prompts – Messages that ask the user for a response – Should give clear directions to the user – Should explain how to format response when applicable – Used in command line and GUI interactive programs – Not needed if input comes from a file Echoing input – Repeating input back to the user in a prompt or output – Useful as an aid in identifying input errors 34An Object-Oriented Approach to Programming Logic and Design

Designing Clear Statements (cont’d) Example: Using prompts and echoing input 35An Object-Oriented Approach to Programming Logic and Design Figure 2-10 Program segment that accepts a customer’s name and uses it in the second prompt

An Introduction to Structure Structure – Basic unit of programming logic – Three structures: sequence, selection, and loop Sequence structure – Perform one action after another with no decision points – Perform all steps until the sequence ends – Example: driving directions 36An Object-Oriented Approach to Programming Logic and Design Figure 2-11 The sequence structure

An Introduction to Structure (cont’d) Selection structure – Follows one of two branches of logic based on a decision Loop structure – Instructions repeat based on a decision 37An Object-Oriented Approach to Programming Logic and Design Figure 2-11 The selection structure Figure 2-11 The loop structure

38An Object-Oriented Approach to Programming Logic and Design Figure 2-12 Flowchart of the NetPayCalculator application class Figure 2-13 Typical execution of the NetPayCalculator program

Summary Application: a program that accomplishes a task Method: a set of statements that performs tasks in an application. Example: main() method Data values: stored as literals, variables, and named constants, which are numeric or string Variables: named memory locations with contents that can differ over time Data type describes values that a variable can hold Declaration statement provides data type and identifier for a variable 39An Object-Oriented Approach to Programming Logic and Design

Summary (cont’d) Named constant: assigned a value only once Four standard arithmetic operators ( +, -, *, / ) Rules of precedence dictate order of arithmetic operations Examples of good programming practices: – Including appropriate comments – Choose identifiers wisely – Design clear statements – Write clear prompts and echo input Programming structures: sequence, selection & loop 40An Object-Oriented Approach to Programming Logic and Design