DATA AND ABSTRACTION Today you will learn : How to work with variables How to break a program down Good program design
VARIABLES Variables are a named memory location Before you use a variable you must declare it A declaration is a statement that provides a data type and an identifier for the variable Declaring a variable with a starting value is called initializing the variable Declaration Examples num mysalary num yoursalary = 14.55 String myname String yourname = Julian
VARIABLES CONTINUED NAMING CONVENTIONS All programming languages have rules for naming variables. Most generally consist of letters and numbers. Symbols maybe be allowed in special circumstances Choose descriptive names of a reasonable length Camel Casting to make multiple words seem as one word. num hourlywage DATA TYPES num Numeric values Integers and floating point decimals Strings Collection of characters char Single character boolean True or False value Every language has its own set of data types it uses. These are some of the most common
TWO TRUTHS AND A LIE 1. A variable s data type describes the kind of values the variable can hold and the types of operations that can be performed with it 2. If name is a string variable, then the statement set name = Ed is valid 3. If salary is a numeric variable, then the statement set salary = 12.50 is valid In this statement the salary is set as a string variable not numeric. Remove the quotation marks for it to be a numeric value
VARIABLE ASSIGNMENTS This is an assignment statement set myanswer = mynumber * 2 The = (equal sign) is an assignment operator It has right to left associativity The expression on the right is evaluated first then assigned to the memory address the named variable on the left references It is an example of a binary operator It requires two operands, one on each side of it
ARITHMETIC OPERATIONS Most programming languages use the standard arithmetic operators + (plus sign) Addition - (minus sign) Subtraction *(asterisk) Multiplication / (division) division Some languages have different interpretations of some of these / real division // integer division % remainder division The order of operations is the same as you know from your math classes PEMDAS
TWO TRUTHS AND A LIE 1. The assignment operator always operates from right to left; programmers would say it has right to left associativity 2. The operand to the right of an assignment operator must be a name that represents a memory address 3. The following adds 5 to a variable name points: points = points + 5 The operand to the left of the assignment operator must be a variable
MODULARIZATION Programmers often break up a project in to smaller more reasonable parts These parts are often called modules, methods, function, or subroutines This allows many programmers to work on a project (reduces your work) This allows you to reuse your work This also makes your work more reliable Modularization provides for the idea of Abstraction Process or paying attention to the important ideas while ignoring nonessential details
ABSTRACTION EVERY DAY Think about what you have done today and share it with someone or the class Try to explain it in every little detail WITH ABSTRACTION Got dressed Cleaned myself up Ate breakfast Drove to school WITHOUT ABSTRACTION Picked out pants, shirt and socks Put on pants Put on shirt Put on socks Walked to bathroom
TWO TRUTHS AND A LIE 1. Modularization eliminates abstraction, a feature that makes programs more confusing 2. Modularization makes it easier for multiple programmers to work on a project 3. Modularization allows you to reuse your work more easily Modularization promotes abstraction and makes large projects easier to understand and program
MODULARIZING A PROGRAM Programs usually consist of a main program which dictates the basic steps of the program, or the mainline logic This main program access modules that provide more refined details for actions of the program Modules can access or call other modules to make the programming logic clear and easy to use
WHAT MAKES A MODULE? A header includes an identifier and any other necessary information the module needs to execute A body these are the executable statements of the module A return statement marks the end of the module and identifies the point at which control returns to the mainline logic Predefined Process or Module Symbols
MODULARIZING A BILLING PROGRAM
BILLING PROGRAM PART 2 Each module is like a small program They can declare variable, perform actions, check conditions, iterate, and return information Variables declared in a module can only be used in that module We say they are local variables While variable declared outside a module can be used in any module These are global variables Where a variable can be seen or used is known as the variables scope
COMPLEX PROGRAMS Many programs can be large and complex in terms of their overall objective Breaking them into modules makes it easier, but tracking them can be complex as well A hierarchy chart may help with these complexities
TWO TRUTHS AND A LIE 1. You can use a hierarchy chart to illustrate modulus relationships 2. A hierarchy chart tells you what tasks are to be performed within a module 3. A hierarchy chart tells you only which modules call other modules A hierarchy chart tells you nothing about the tasks that are to be performed in a module. It only shows the number of modules and which call each module
FEATURES OF GOOD DESIGN Programming comments written explanations that are not part of program logic but serve as documentation for the readers of the program In pseudocode we often use to forward slashes to begin comments In a flow chart we use an annotation symbol
FEATURES OF GOOD DESIGN Names of variables and modules should be self-documenting Utilize camel casting to display multiple words as one Utilize temporary variables when solving complex problems Use clear and direct prompts when getting user input Echo (display) the input users submitted so they can be reminded of their input