1 / 44

Internet Programming

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

janelle
Download Presentation

Internet Programming

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. Internet Programming Chapter 1 Introduction to Programming Logic

  2. Objectives • Understand the nature of computers and programming • Explore the programming process • Use pseudocode • Use and name variables • Describe data types

  3. Objectives (cont.) • Understand decision-making and loop execution • Understand modularization and abstraction in procedural programs • Describe object-oriented programming

  4. Understanding the Nature of Computers and Programming • Together, hardware and software accomplish: • Input • Processing • Output • Storage

  5. 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?

  6. 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

  7. 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.

  8. Compiling and Interpreting • Language-translation software (a compiler or interpreter) changes your programming statements into machine language. • What's the difference between the two?

  9. IPO Get inputNumber Compute calculatedAnswer as inputNumber times 2 Print calculatedAnswer

  10. 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)

  11. Using Pseudocode • English statements • Uses terminal statements and indentation Start Get inputNumber Compute calculatedAnswer as inputNumber times 2 Print calculatedAnswer End

  12. 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

  13. 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

  14. Using and Naming Variables • Variable names must have some appropriate meaning Good examples: finalBalance initialInvestment Bad example: f

  15. 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

  16. 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.

  17. 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

  18. Understanding Decision Making • A single-alternative, unary, or if-then selection: if the answer to the question is yes then do something

  19. 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 (<)

  20. 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)

  21. 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.

  22. 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.

  23. Understanding Loop Execution • A typical loop: count = 1 while count < 5 print “Danger, Will Robinson!” count = count + 1

  24. 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.

  25. 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.

  26. 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.

  27. 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.

  28. Let's make a pass… getDrill() num usersChoice print “Enter 1 for addition drill, 2 for subtraction” read usersChoice displayDrill(usersChoice) return

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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.

  34. 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.

  35. 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.

  36. 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

  37. 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()

  38. 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.

  39. 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.

  40. 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.

  41. 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

  42. 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

  43. 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

  44. 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.

More Related