Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICT Introduction to Programming

Similar presentations


Presentation on theme: "ICT Introduction to Programming"— Presentation transcript:

1 ICT1412 - Introduction to Programming
Chapter 1 – Program Design

2 What is Programming? Programming is applied problem-solving
Understand a problem Identify relevant characteristics Design an algorithm (step-by-step sequence of instructions to carry out a task) Implement the algorithm as a computer program Test the program by repeated (and carefully planned) executions GO BACK AND REPEAT AS NECESSARY In short: programming is the process of designing, writing, testing and debugging algorithms that can be carried out by a computer Prepared by K.T. Ng

3 Understanding the Programming Process
The programmer’s job can be broken down into six programming steps as follows: Understand the problem Plan the logic Code the program Translate the program into machine language Test the program Put the program into production Prepared by K.T. Ng

4 1. Understand the Problem
Really understanding the problem may be one of the most difficult aspects of programming The description of what the user needs may be vague The user may not even really know what he or she wants Users who think they know what they want frequently change their minds after seeing sample output Prepared by K.T. Ng

5 2. Plan the Logic Programmer plans the steps to the program, deciding what steps to include and how to order them The two most common tools are flowcharts and pseudocode Both tools involve writing the steps of the program in English Prepared by K.T. Ng

6 3. Code the Problem Some very experienced programmers can successfully combine the logic planning and the actual instruction writing, or coding of the program, in one step A good final project report needs planning before writing, and so do most programs Prepared by K.T. Ng

7 4. Translate the Program into Machine Language
Languages such as Java or C translate the programmer’s English-like high-level programming language into the low-level machine language that the computer understands If you write a programming language statement incorrectly (for example, by misspelling a word, using a word that doesn’t exist in the language, or using “illegal” grammar), the translator program doesn’t know what to do and issues an error message identifying a syntax error Prepared by K.T. Ng

8 4. Translate the Program into Machine Language (Cont.)
All syntax errors are caught by the compiler or interpreter When writing a program, a programmer might need to recompile the code several times An executable program is created only when the code is free of syntax errors When you run an executable program, it might also typically require input data Prepared by K.T. Ng

9 Creating an Executable Program
Prepared by K.T. Ng

10 5. Test the Program A program that is free of syntax errors is not necessarily free of logical errors Once a program is free from syntax errors, the programmer can test it — that is, execute it with some sample data to see whether the results are logically correct Prepared by K.T. Ng

11 6. Put the Program into Production
Putting a program into production might mean simply running the program once, if it was written to satisfy a user’s request for a special list The process might take months if the program will be run on a regular basis, or it is one of a large system of programs being developed Conversion, the entire set of actions an organization must take to switch over to using a new program or set of programs, can sometimes take months or years to accomplish Prepared by K.T. Ng

12 Algorithms The central concept underlying all computation is that of the algorithm An algorithm is a step-by-step sequence of instructions for carrying out some task Programming can be viewed as the process of designing and implementing algorithms that a computer can carry out A programmer’s job is to: Create an algorithm for accomplishing a given objective, then Translate the individual steps of the algorithm into a programming language that the computer can understand Prepared by K.T. Ng

13 Algorithms in the Real World
The use of algorithms is not limited to the domain of computing e.g., recipes for baking cookies e.g., directions to your house In order for an algorithm to be effective, it must be stated in a manner that its intended executor can understand a recipe written for a master chef will look different than a recipe written for a college student As you have already experienced, computers are more demanding with regard to algorithm specifics than any human could be Prepared by K.T. Ng

14 Representing Algorithms
What language to use? Natural language (e.g. English). Does not provide the accuracy and precision needed to represent an algorithm. Formal programming languages (e.g. C++, Java). At this level we don’t think at the abstract level More concerned with punctuation, grammar, and syntax Pseudocode Does not matter how you choose to write the instructions: set, store, etc Not a precise set of notation rules to be memorized and rigidly followed Prepared by K.T. Ng

15 Using Flowchart Symbols and Pseudocode Statements
Flowcharts (pictorial representations) and pseudocode (English-like representations) are used by programmers to plan the logical steps for solving a programming problem Some professional programmers prefer writing pseudocode to drawing flowcharts, because using pseudocode is more similar to writing final statements in programming language Prepared by K.T. Ng

16 Using Flowchart Symbols and Pseudocode Statements (Cont.)
Almost every program involves the steps of input, processing, and output, necessitating some graphical way to separate them Arithmetic operation statements are examples of processing in a flowchart, where you use a rectangle as the processing symbol containing a processing statement To represent an output statement, you use the parallelogram, which is also the same symbol used for input statements Prepared by K.T. Ng

17 Using Flowchart Symbols and Pseudocode Statements (Cont.)
Prepared by K.T. Ng

18 Using Flowchart Symbols and Pseudocode Statements (Cont.)
In flowcharts: Arrows, or flowlines, connect and show the appropriate sequence of steps A terminal symbol, or start/stop symbol, should be included at each end Often, “start” or “begin” is used as the first terminal symbol and “end” or “stop” is used in the other Prepared by K.T. Ng

19 Using Flowchart Symbols and Pseudocode Statements (Cont.)
Figure below shows a complete flowchart for the program that doubles a number, and the pseudocode for the same problem Prepared by K.T. Ng

20 Using the Connector By using just the input, processing, output, decision, and terminal symbols, you can represent the flowcharting logic for many diverse applications When drawing a flowchart segment, you might use only one other symbol, the connector You can use a connector when limited page size forces you to continue a flowchart in an unconnected location or on another page By convention, programmers use a circle as an on-page connector symbol, and a symbol that looks like a square with a pointed bottom as an off-page connector symbol Prepared by K.T. Ng

21 Using the Connector (Cont.)
If a flowchart has six processing steps and a page provides room for only three, you might represent the logic as shown below: Prepared by K.T. Ng

22 How to Write Pseudocode
When designing a solution algorithm, you need to keep in mind that a computer will eventually perform the set of instructions written If you use words and phrases in the pseudocode which are in line with basic computer operations, the translation from pseudocode algorithm to a specific programming language becomes quite simple Prepared by K.T. Ng

23 Six Basic Computer Operations
A computer can receive information When a computer is required to receive information or input from a particular source, whether it is a terminal, a disk or any other device, the verbs Read and Get are used in pseudocode A computer can put out information When a computer is required to supply information or output to a device, the verbs Print, Write, Put, Output, or Display are used in pseudocode Usually an output Prompt instruction is required before an input Get instruction Prepared by K.T. Ng

24 Six Basic Computer Operations (Cont.)
A computer can perform arithmetic Most programs require the computer to perform some sort of mathematical calculation, or formula, and for these, a programmer may use either actual mathematical symbols or the words for those symbols To be consistent with high-level programming languages, the following symbols can be written in pseudocode: + for Add - for Subtract * for Multiply / for Divide ( ) for Parentheses When writing mathematical calculations for the computer, standard mathematical ‘order of operations’ applies to pseudocode and most computer languages Prepared by K.T. Ng

25 Six Basic Computer Operations (Cont.)
A computer can assign a value to a variable or memory location There are three cases where you may write pseudocode to assign a value to a variable or memory location: To give data an initial value in pseudocode, the verbs Initialize or Set are used To assign a value as a result of some processing the symbols '=' is written e.g. GPA = sum/count To keep a variable for later use, the verbs Save or Store are used Prepared by K.T. Ng

26 Six Basic Computer Operations (Cont.)
A computer can compare two variables and select one or two alternate actions An important computer operation available to the programmer is the ability to compare two variables and then, as a result of the comparison, select one of two alternate actions To represent this operation in pseudocode, special keywords are used: if, if … else Prepared by K.T. Ng

27 Six Basic Computer Operations (Cont.)
A computer can repeat a group of actions When there is a sequence of processing steps that need to be repeated, special keywords for leading decision loops (do…while), trailing decision loops (repeat…until) and counted loops (do), are used in pseudocode The condition for the repetition of a group of actions is established in the do...while clause, and the actions to be repeated are listed beneath it Prepared by K.T. Ng

28 Pseudocode for Decimal Addition
Prepared by K.T. Ng

29 What Operations Do We Need?
Getting input and producing output Get the two numbers Display the outcome Referring to values within our algorithm Add together the rightmost digits of the two numbers Add together a0 and b0 Doing something if some condition is true If the outcome is greater or equal to 10 then ... Doing something repeatedly Do this for all the digits in the numbers ... Prepared by K.T. Ng

30 Pseudocode Primitives
The Structure Theorem states that it is possible to write any computer program by using only three basic control structures that are easily represented in pseudocode: Sequence Selection Repetition Prepared by K.T. Ng

31 The Three Basic Control Structures
Sequence The sequence control structure is the straightforward execution of one processing step after another In pseudocode, we represent this construct as a sequence of pseudocode statements Selection The selection control structure is the presentation of a condition and the choice between two actions; the choice depends on whether the condition is true or false In pseudocode, selection is represented by the keywords if; if … else Prepared by K.T. Ng

32 Using and Naming Variables
Variables are memory locations, whose contents can vary or differ over time For instance, in our previous doubles a number example, inputNumber can hold a 2 and calculatedAnswer will hold a 4; at other times, inputNumber can hold a 6 and calculatedAnswer will hold a 12 A variable name is also called an identifier Variable names used here follow only two rules: Must be one word Have some appropriate meaning Prepared by K.T. Ng

33 Ending a Program By Using Sentinel Values
An infinite loop is a repeating flow of logic with no end To end the program, set a predetermined value for inputNumber that means “Stop the program!” The program can then test any incoming value for inputNumber and, if it is a 0, stop the program Testing a value is also called making a decision Represented in flowchart by diamond shape called a decision symbol Prepared by K.T. Ng

34 Ending a Program By Using Sentinel Values (Cont.)
A pre-selected value that stops the execution of a program is often called a dummy value since it does not represent real data, but just a signal to stop Sometimes, such a value is called a sentinel value because it represents an entry or exit point, like a sentinel who guards a fortress Prepared by K.T. Ng

35 Understanding Data Types
Computers deal with two basic types of data —character and numeric When you use a specific numeric value, such as 43, within a program, you write it using the digits and no quotation marks A specific numeric value is often called a numeric constant because it does not change — a 43 always has the value 43 When you use a specific character value, or string of characters, such as "Chris" you enclose the string, or character constant, within quotation marks Prepared by K.T. Ng

36 Understanding Data Types (Cont.)
Some languages allow for several types of numeric data Languages such as Pascal, C++, C#, and Java distinguish between integer (whole number) numeric variables and floating-point (fractional) numeric variables containing a decimal point Prepared by K.T. Ng

37 Algorithm Characteristics
Those characteristics were: Ordered set of instructions Effective instructions Unambiguous instructions Terminates There are other concerns for algorithms besides those characteristics Prepared by K.T. Ng

38 Algorithm Characteristics (Cont.)
Efficiency: This can be a very important concern for algorithms. Two key types: Time efficiency Space efficiency Correctness: Of course, this tends to be the most important concern! If the algorithm is not correct, and does not do the task it was intended for, what good is it? Prepared by K.T. Ng

39 From Pseudo-Code to Java
Once satisfied with an algorithm, the programmer translates the algorithm into a program The algorithm was written in pseudo-code The program is to be written in a programming language of some kind In our case, we are particularly interested in translating algorithms into Java Prepared by K.T. Ng

40 From Pseudo-Code to Java (Cont.)
To do this, one needs to know how each algorithmic construct: data storage/retrieval data manipulation sequential execution conditional execution repetition is represented in Java; that is: Java syntax We will be seeing these in more detail later Prepared by K.T. Ng

41 Understanding the Evolution of Programming Techniques
Old programming languages required programmers to work with memory addresses and to memorize awkward codes associated with machine languages Newer programming languages look much more like natural language and are easier to use Prepared by K.T. Ng

42 Understanding the Evolution of Programming Techniques (Cont.)
Currently, there are two major techniques used to develop programs and their procedures Procedural programming focuses on the procedures that programmers create Object-oriented programming, focuses on objects, or “things”, and describes their features, or attributes, and their behaviors Prepared by K.T. Ng

43 Java Java is based upon C++ (which in turn is based on C)
Unlike C++ which is really a hybrid language, Java is purely Object-Oriented This results in significant advantages Most HLL (High Level Language) programs are compiled to run on a single platform Java programs can run on multiple platforms after compilation --- i.e. its compiled format is platform independent Java and C++ are the dominant languages in industry Prepared by K.T. Ng

44 History of Java Java was developed by J. Gosling at Sun Microsystems in 1991 for programming home appliances (variety of hardware platforms) With the advent of the WWW (1994), Java’s potential for making web pages more interesting and useful was recognized. Java began to be added to web pages (as applets) that could run on any computer (where the browser was running) Since then it has been more widely accepted and used as a general-purpose programming language, partly due to its platform-independence, and it is a truly OO language (unlike C++) Prepared by K.T. Ng

45 Platform-Independence
Notion of a “Java Virtual Machine” (JVM) Java programs are compiled to run on a virtual machine (just a specification of a machine). This code is called Byte Code Each physical machine that runs a Java program (byte code) must “pretend” to be a JVM This is achieved by running a program on the machine that implements the JVM and interprets the byte code to the appropriate machine code This interpreting is done at run-time which can cause a slow down! Prepared by K.T. Ng

46 Regular Programming Languages
Prepared by K.T. Ng

47 Java Prepared by K.T. Ng

48 Object-Oriented Programming
The OOP paradigm uses the notion of objects and classes as basic building blocks Other important components of OOP are Encapsulation (next semester) Inheritance (next semester) Polymorphism (next semester) Prepared by K.T. Ng

49 Classes and Objects Object-oriented programs use objects
An object represents a concept that is part of our algorithm, such as an Account, Vehicle, or Employee Similar objects share characteristics and behavior. We first define these common characteristics for a group of similar objects as a class A class defines the type of data that is associated with each object of the class, and also the behavior of each object (methods) A class is a template for the objects An object is called an instance of a class Most programs will have multiple classes and objects Prepared by K.T. Ng

50 Banking Example In a banking application, there may be numerous accounts There is common behavior (as far as the bank is concerned) for all these accounts Deposit Check balance Withdraw Overdraw? There are also common types of data that the bank in interested in Account holder’s name(s), HKID, … Current balance, recent transactions, … Instead of defining these data and behavior for each account separately, we simple define them once --- this is the notion of the account class Each account will be an instance of this class and will have its own values for the data items, but the same behavior (defined once) Prepared by K.T. Ng

51 Java Program Structure
In the Java programming language: A program is made up of one or more classes A class contains one or more methods A method contains program statements These terms will be explored in detail throughout the course A Java application always contains a method called main Prepared by K.T. Ng

52 Java Program Structure (Cont.)
// comments about the class public class MyProgram { } class header class body Comments can be placed almost anywhere Prepared by K.T. Ng

53 Java Program Structure (Cont.)
// comments about the class public class MyProgram { } // comments about the method public static void main (String[] args) { } method header method body Prepared by K.T. Ng

54 Comments Comments in a program are called inline documentation
They should be included to explain the purpose of the program and describe processing steps They do not affect how a program works Java comments can take three forms: // this comment runs to the end of the line /* this comment runs to the terminating symbol, even across line breaks */ /** this is a javadoc comment */ Prepared by K.T. Ng

55 An Example from Pseudo-Code to Java
Write a program that computes the circumference and area of circles given their diameter Step 1 write Pseudo-Code Ask user to enter a diameter, D, or enter zero to stop Repeat until D=0 Set circumference to pi * D Set area to pi * (D/2)2 Output circumference and area Prepared by K.T. Ng

56      double circumference, area;
Ask user to enter a diameter, D, or enter zero to stop Repeat until D=0 Set circumference to pi * D Set area to pi * (D/2)2 Output circumference and area double circumference, area; //Compute and print the circumference of circles, //given their diameter as input from the user double D = Console.readInt(“Please enter a diameter, or enter zero to stop ”); while (D>0) //user doesn’t want to stop { circumference = 3.14 * D; area = 3.14 * (D/2) * (D/2); //pi * radius2 System.out.println(“the circumference is ” + circumference + “ and the area is ” + area); D = Console.readInt(“Please enter a diameter, or enter zero to stop ”); } //end of while loop Last, and definitely least, add the mumbo-jumbo that goes around every Java program … Prepared by K.T. Ng

57 To get started with Java, type in this program and run it…
public class Circles { public static void main(String[] args) //Compute and print the circumference of circles, //given their diameter as input from the user double D = Console.readInt(“Please enter a diameter, or enter zero to stop ”); double circumference, area; while (D>0) //user doesn’t want to stop circumference = 3.14 * D; area = 3.14 * (D/2) * (D/2); //pi * radius2 System.out.println(“the circumference is ” + circumference + “ and the area is ” + area); D = Console.readInt(“Please enter a diameter, or enter zero to stop ”); } //end of while loop } To get started with Java, type in this program and run it… Prepared by K.T. Ng

58 Software Development Tools
Software Development Tools or Kits (SDK’s) are specialized application programs that allow programmers to write and test programs Experienced programmers generally prefer an “Integrated Development Environment” (IDE) Examples (that we’ll be using in this course): Sun’s Java SDK (sometimes called JDK) JCreator LE (Version 4.0) Prepared by K.T. Ng

59 Styles of User Interface
There are two predominant styles of User Interface for any type of program: Command Line Interface (CLI) Graphical User Interface (GUI) As a computer programmer, you must be able to use and write programs for both styles of user interface Prepared by K.T. Ng

60 Software Development Tools
Using Sun Java SDK alone Programmer Command Line Interface Source File(s) (.java) Compiler (javac) Class (.class) Virtual Machine (java) Editor Program executes Parts of Sun Java SDK bytecode Prepared by K.T. Ng

61 Software Development Tools (Cont.)
We will use a combination of the JCreator LE IDE and the Sun Java SDK (both of them are freeware!) Source File(s) (.java) Programmer JCreator LE Compiler (javac) Class (.class) Virtual Machine (java) Edit Build Run Program executes Parts of Sun Java SDK Graphical User Interface bytecode Prepared by K.T. Ng

62 Types of Programming Errors
A program can have three types of errors: The IDE editor and/or compiler will find syntax errors and other basic problems (compile-time errors) A problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally (run-time errors) A program may run, but produce incorrect results, perhaps using an incorrect formula (logical errors) Prepared by K.T. Ng

63 Compiler Errors and Warnings
An executable program will not be generated by the compiler if compile-time errors exist in your program View the error messages, and deal with them one at a time, starting with the first error When a compiler reports Warnings you should take note of them Warnings alone will not prevent the generation of an executable program, but they do point to possible problems You should ALWAYS aim for 0 warnings and 0 errors Prepared by K.T. Ng

64 More On Java Programming
More details about Java programming: Regarding file names: Each Java program has a name A program must be stored in a file named with the program name and the extension .java e.g. program name: HelloWorld file: HelloWorld.java contains the Java code Regarding compiling the source code: The compiler creates the bytecode version of the code and stores it in a file with the extension .class e.g. javac HelloWorld.java creates the file HelloWorld.class (assuming, of course, there are no syntax errors!) Prepared by K.T. Ng


Download ppt "ICT Introduction to Programming"

Similar presentations


Ads by Google