470 likes | 724 Views
Internet Programming. Chapter 1 Introduction to Programming Logic. Objectives. Understand the nature of computers and programming Explore the programming process Use pseudocode Use and name variables Describe data types. Objectives (cont.). Understand decision-making and loop execution
E N D
Internet Programming Chapter 1 Introduction to Programming Logic
Objectives • Understand the nature of computers and programming • Explore the programming process • Use pseudocode • Use and name variables • Describe data types
Objectives (cont.) • Understand decision-making and loop execution • Understand modularization and abstraction in procedural programs • Describe object-oriented programming
Understanding the Nature of Computers and Programming • Together, hardware and software accomplish: • Input • Processing • Output • Storage
Syntax Errors • Syntax errors are mistakes in a software code’s grammar. Just as misspelling a word is a mistake when writing, misspelling a command word or forgetting to close a module will cause a syntax error. If you’re supposed to use a semi-colon (;) and you use a colon (:) instead, you’ve made a syntax error. • Where is the train station? • Station is the train where?
Faulty Logic • Harder to find than syntax errors • Why? • Stop at Pump • Open Gas Cap • Turn off engine • Close Gas Cap • Pump Gas • Leave Pump • Turn on Engine
Run-Time Errors • Run-time errors are mistakes that occur when you run the software code. • Software not displaying a window correctly is a run-time error.
Compiling and Interpreting • Language-translation software (a compiler or interpreter) changes your programming statements into machine language. • What's the difference between the two?
IPO Get inputNumber Compute calculatedAnswer as inputNumber times 2 Print calculatedAnswer
Exploring the Programming Process • Understand the problem • Plan the logic • Code the program • Translate the program into machine language • Test the program • Put the program into production • Maintain the program • Prepare to start the process again (iterative)
Using Pseudocode • English statements • Uses terminal statements and indentation Start Get inputNumber Compute calculatedAnswer as inputNumber times 2 Print calculatedAnswer End
Pseudocode Guides • Use simple English. • Put one command on a line. • Place any important words in bold. • Reserved words, for example • Start from the top and work toward the bottom. • Separate processes with spaces to form modules. • Use indentation
Using and Naming Variables • Naming rules vary between languages • We will follow camel casing • Classes start with Caps • Variable names must be one word Good examples: myRate interestRate Bad example: interest Rate
Using and Naming Variables • Variable names must have some appropriate meaning Good examples: finalBalance initialInvestment Bad example: f
Assigning Values to Variables • The equal sign (=) often is used as the assignment symbol (or operator) • Whatever operation is performed to the right of the assignment operator results in a value that is assigned to the memory location named to the left of the assignment operator • Example: answer = inputValue * 2
Describing Data Types • A specific numeric value is a numeric constant, for example: 43 • A specific character value is a character constant, or string, and often enclosed in double quotes, for example: “Chris” • Exceptions: Java uses ' ' for character and " " for strings • Similarly, programming languages allow at least two types of variables – numeric, and character, text, or string.
Understanding Decision Making • A dual-alternative, binary, or if-then-else decision structure: if the answer to the question is yes then do something else do something else
Understanding Decision Making • A single-alternative, unary, or if-then selection: if the answer to the question is yes then do something
Understanding Decision Making • A Boolean expression is one that represents only one of two states: true or false • For any two values, you can decide whether: • they are equal (=) • the first is greater than the second (>) • the first is less than the second (<)
Understanding Decision Making • In addition to the three basic comparisons, most programming languages support three more. For any two values you can decide whether: • The first is greater than or equal to the second (>=) • The first is less than or equal to the second (<=) • The two are not equal (!= or <>0)
Understanding Loop Execution (aka repetition control structure) • A loop is a structure than repeats actions while some condition continues. • Almost every program contains a main loop – a basic set of instructions that is repeated for every record. • Additionally, loops are used any time you need to perform a similar task several times.
Understanding Loop Execution • Three steps must occur in every loop: 1. You must initialize a variable, called the loop control variable, that controls the loop. 2. You must compare the loop control variable to some value that stops the loop – a sentinel value. 3. Within the loop, you must alter the loop control variable.
Understanding Loop Execution • A typical loop: count = 1 while count < 5 print “Danger, Will Robinson!” count = count + 1
Understanding Modularization and Abstraction in Procedural Programs • No longer need to write an entire program from start to finish in a series of steps – this is procedural • Use units or modules of code • Modularization allows multiple programmers to work on a problem, each contributing one or more modules that later can be combined into a whole program. • Modularization allows you to reuse your work; you can call the same module from multiple locations within a program.
Scope in Modules • Global variables are known to an entire program. • Local variables are known only to their own module. • Declaring local variables employs the principle known as encapsulation, information hiding, or data hiding.
Why Use Encapsulation? • Using encapsulation provides advantages: • Programmers can create modules without knowing variable names used in other modules. • Programmers can use the same names for variables as those in other modules, and no conflicts arise.
Passing Values to a Module • Passing a variable means that you send a copy of data in one module to another module for use. • You declare a name for a passed value in a module header, or introductory title statement. • The passed value is called a parameter or argument.
Let's make a pass… getDrill() num usersChoice print “Enter 1 for addition drill, 2 for subtraction” read usersChoice displayDrill(usersChoice) return
Send it on back… displayDrill(num option) num usersAnswer if option = 1 print “What is 3 + 4?” else print “What is 8 – 1?” read usersAnswer if usersAnswer = 7 then print “Very good” return
Returning a Value from a Module • Just as you can pass a value into a module, you can pass back, or return, a value from a module. getDrill() num usersChoice num answer print “Enter 1 for addition drill, 2 for subtraction” read usersChoice answer = displayDrill(usersChoice) if answer = 7 then print “Very good” return
Returning a Value from a Module displayDrill(num option) num usersAnswer if option = 1 print “What is 3 + 4?” else print “What is 8 – 1?” read usersAnswer return usersAnswer
Understanding the Advantages of Encapsulation • When you use others’ modules you deal with only the interface. • Self-contained modules are reusable. • Pre-written and tested modules are reliable. • Code reuse is the basis for most contemporary software development environments and techniques
What is OOP? • Object-oriented programming is a style of programming that focuses on an application’s data and the methods you need to manipulate that data.
OOP Tenets • You analyze the objects you are working with and the tasks that need to be performed with and on those objects. • You pass messages to objects, requesting the objects to take action. • The same message works differently (and appropriately) when applied to different objects.
OOP Tenets (cont.) • A module or procedure can work appropriately with different types of data it receives, without the need to write separate modules. • Objects can share or inherit traits of objects that have already been created, reducing the time it takes to create new objects. • Encapsulation and information hiding are more complete than with the modules used in procedural programs.
OOP Overview • In order to understand object-oriented programming, you must understand four concepts that are integral components of all object-oriented programming languages: • Classes • Objects • Inheritance • Polymorphism
Classes • A class is a category of things. • An object is a specific instance of a class. • A class contains three parts: • Every class must have a name. • Employee, Dog, Car • Although not required, most classes contain data. • employeeID, dogSpecies, carModel • Although not required, most classes contain methods • calculatePay(), teachTrick(), lockDoors()
Defining Classes • Programmers often use a class diagram to illustrate class features. • The class diagram contains the class name, and the attributes and the methods of the class.
Defining Classes • The Employee class diagram to the right is a typical class diagram. • Usually programmers use public class methods to set and return private class data.
Instantiating and Using Objects • When you declare an object, you instantiate it. • Each object automatically contains all the data fields, or attributes, that are part of the class. • Each object automatically has all the capabilities, or methods, that are part of the class.
Understanding Inheritance • You can create new classes that are descendants of existing classes. • Descendant, or child, classes have all the attributes and methods of their parent class. • Within a child class you can override any attributes or methods of the parent that are not appropriate. • Square versus Cube
Understanding Polymorphism • Polymorphism allows the same operation to be carried out differently depending on the context. • Method overloading occurs when you create multiple methods with the same name but different argument lists. • Much time spent at the front end, but creates a multi-use module
Understanding Polymorphism • Three polymorphic methods: changeData(char desc) itemDescription = desc return changeData(num pr) price = pr return changeData(char desc, num pr) itemDescription = desc price = pr return
The Advantages of Object-Oriented Programming • When you use pre-existing objects, you save development time. The objects are already tested and reliable. • When you use inheritance, you develop new classes more quickly. • When you overload methods, you can concentrate on the purpose of your programs.