Programming 1 Chapter 1 PDF

Summary

This document is a chapter (1) from a university-level programming course at the Universidad de Dagupan, focusing on fundamental principles of programming, types, and paradigms. The chapter discusses problem definition, designing solutions, algorithms, and flowcharting.

Full Transcript

UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan PROGRAMMING 1 for a solution to a particular problem. The Chapter 1: Fundamental Principles of problem is thoroughly analyzed in order to Progra...

UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan PROGRAMMING 1 for a solution to a particular problem. The Chapter 1: Fundamental Principles of problem is thoroughly analyzed in order to Programming: Generations, Types, determine what is required for its solution. and Paradigms. To better understand and analyze the problem we must conduct a study of the Every computer program has to be nature of the problem and we must be able well-planned and executed correctly. to identify the necessary outputs required in Instructions must be ordered in a logical the program. In conducting a study or sequence that can easily be understood by investigation of the problem we must talk to the computer. The problem must be the end-user of the proposed program we analyzed to such a level of detail that all are trying to develop since he understands logical conditions that may be encountered the problem prior to writing the necessary during the process are taken into code for the program itself. consideration. As we all know computers are just a tool, it does not have its own The programmer must determine mind. It is very dependent on the what data are needed, what form they are to programmer who gives instruction to it. We be in, what information should the program must make sure we analyze the problem generate, and how the data are to be very well before we start writing some codes manipulated to produce this result. using programming languages such as Java. Designing a Solution When the problem is defined There are six stages in developing a properly, we can start designing the solution computer program. to the problem. In designing the solution, we must be able to break the problem into 1. Defining the problem several steps so that it is easier for us to 2 Designing a solution to the problem solve the problem in smaller pieces. This 3. Writing the program method is called the divide and conquer 4 Compiling, debugging and testing the principle. This sequence of steps for the program solution to the problem is called an 5 Documenting the program Algorithm. 6 Maintaining the program Algorithm Defining the Problem It refers to well-defined procedures The first step in defining a problem or instructions to solve a problem. begins with recognizing the need for information. As we all know information is Flowchart very important primarily because such One way a programmer can information can help us in making a good illustrate the sequence of steps in an decision. This information may be requested algorithm is with a flowchart. A flowchart is a B.C.N.V 1 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan graphical representation of the sequence of executable program that can be loaded into operations where a computer is to perform. a given environment and executed Flowchart uses easily recognizable symbols independently of other programs. to represent the type of processing performed in a program. Despite the fact that programming languages differ in the commands they use, Pseudocode high-level programming languages must A pseudocode is a version of the have certain types of programming instructions describing each step that the statements in common. These are computer must follow. It is written in an comments, declarations, input/output abbreviated form of spoken language and statements, computations, transfer of there lies somewhere commands written in control, and comparison. ordinary English and those in a computer language. Comments are statements that have no effect on the program. They are Writing the Program simply used to make the program easier to After the programmer has defined understand. They are inserted at the key the problem and designed a solution, the points in the program and serve as an next step is to write the program in a internal documentation of the program. specific programming language. Although the definition and solution of the problem The programmer uses declarations are independent of the programming to define items used in the program. language, the proposed solution to the Examples include definitions of files, problem may limit the choices of languages records, initial values, reusable functions that can be used. and the like. Program Input/output statements transfer 1. A program is a list of instructions written data to and from the primary storage for use in a programming language that a computer by the program, as well as to and from other can execute so that the machine acts in a I/O devices like the monitor and the predetermined way. keyboard. Commands such as READ and PRINT are examples of these types of 2. Program is synonymous with software. A statements. program is also a sequence of instructions that can be executed by a computer. The Computational instructions term may also refer to the original source perform arithmetic operations such as code or to an executable (machine addition, subtraction, multiplication, division language) version. The word program also and exponentiation. Different programming implies a degree of completeness, that is, a languages vary in the way they invoke the source code program compressing all computer's arithmetic capabilities. statements and files necessary for complete interpretation and compilation, and an B.C.N.V 2 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan Another type of instruction allows the incorrect output. For example, how would a sequence of execution to be altered by program react if a person's age is between transferring control. A conditional transfer 16, 9, or -2 Or instead of accepting of control alters the sequence only when a numerical values such as 1, 10, or 45 the certain condition is met. An unconditional person gives letter A? transfer of control always changes the sequence of execution. 5. Programs should be maintainable. They should be easy to update and modify. Comparisons allow two items to be Programs should be written in independent compared. Based on the result of the modules so that a change in one module comparison, input/output, computation, or does not mean change of other modules. transfer of control could occur. Compiling, Debugging and As the program is being coded, the Testing the Program programmer should be aware that although The instructions written in a generating the correct output is the primary programming language such as Java must goal of the program, it is not the only be translated into machine language before requirement of a good program. The they can be executed. A compiler is a programmer should try to incorporate the special program for each programming following qualities into any program: language that is loaded into the computer when that language is used. It translates 1. Programs should be easy to read and each line of code into machine instruction understand. Data names should be that can easily be understood by the descriptive. Statements should be placed in computer. a format that is easy to read and follow. Placing enough comments can help in Compiler making the program easier to understand. 1. A computer program (or set of programs) that transforms source code written in a 2. Programs should be efficient. Programs programming language (the source should execute in as little time as possible. language) into another computer language (the target language, often having a binary 3 Programs should be reliable. Programs form known as object code). should consistently produce the correct output. All formulas and computations, as 2. Refers to any program that transforms well as all logic tests and transfer control, one set of symbols into another by following must be accurate. a set of syntactic and semantic rules. In the most common sense, a compiler is a 4. Programs must be robust. Programs program that translates all the source codes should work under all conditions. Reliability of a program written in a high-level alone is no guarantee for a successful language into object codes prior to the program. Internal logic may be correct but execution of the program. incorrect data item could produce an B.C.N.V 3 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan Debugging During the problem definition state, 1. In computers, debugging is the process the problem should be described clearly in a of locating and fixing or bypassing bugs short and narrative statement. The (errors) in computer program code or the objectives of the program should be engineering of a hardware device. To debug included with the problem statement. a program or to fix the hardware is to start Several other descriptions are needed. with a problem, isolate the source of the problem, and then fix it. 1. A complete description of the contents and formats of all data inputs, outputs and 2. It is the process of correcting files to be used. programming errors. 2. A statement of hardware requirements for Testing running the program, as well as estimated 1. A method of assessing the functionality of processing time and storage requirements. a software program. 3. A statement of software requirements, 2. The process of checking if a program such as utility programs and library actually performs its functions as planned. programs.In the planning phase, the most important documentation produced is the Documentation of the Program flowchart and pseudocode. Descriptive Documentation consists of written comments may be included in each descriptions and explanations of programs processing step. The test data used to test and other materials associated with an the program should also be included. organization's data processing system. Documentation of a system and program Maintenance designs are one of the most important During the implementation of the requirements for success in data processing system, there are some changes in the applications. Proper program program that will occur depending on the documentation serves as a guide for nature of the system that is being programmers and system analysts who developed. Maintenance is one of the most must modify or update the existing important aspects in developing a computer programs and system procedures. program in a sense that if there are errors or bugs that are spotted by the user it should The process of documentation is an be fixed as soon as possible in order to ongoing process in any system being avoid big problems along the way of using developed and implemented. It begins with the system. Let’s say, for example, the user the initial request for information. The has discovered that every time a new record individual making the request should be is being added in the database the record is identified. So should those responsible for not being saved correctly having this designing the system and required problem is very critical in any business programs. organization. This problem should be corrected right away from the programmer, B.C.N.V 4 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan so that it will not hamper the day-to-day Generations of Programming Languages operation of the company. A. First Generation: Machine Language B.Second Generation: Assembly Bugs refers to errors in a computer Language program. C. Third Generation: High-Level Language Types of Programming Errors D. Fourth Generation: Object-Oriented 1. Syntax errors - Errors due to the fact Programming that the syntax of the language is not E. Fifth Generation: Logic Programming respected. A. First Generation (1GL) 2. Semantic errors - Errors due to an 1. Machine Language improper use of program statements. 2. Uses binary codes that consist of strings 3. Logical errors - Errors that occur when of only zeroes and ones. the specification is not respected. 3. These are languages that a computer can 4. Compile time errors - Syntax errors and understand and execute directly. static semantic errors indicated by the compiler. 4. Machine dependent 5. Runtime errors - Dynamic semantic 5. Low-level Language errors and logical errors that cannot be detected by the B. Second Generation (2GL) compiler (debugging). 1. Assembly Language Programming Language 2. Instead of zeroes and ones, it uses 1. A programming language is an artificial mnemonics or very short words for language designed to communicate commands. instructions to a machine, particularly a computer. 3. Programs written in assembly language must be converted by an ASSEMBLER. 2. Programming languages can be used to create programs that control the behavior of 4. Low level language TASM, MASM a machine and/or to express algorithms precisely. C. Third Generation (3GL) 1. High-Level Language 3. Software used to create another software. 2. Can write programs that are more or less independent of a particular type of computer B.C.N.V 5 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan 3. It is considered high-level because they about problems and solutions in software are closer to human languages and they are development. The main paradigms include: easier to read, write, and maintain. Imperative Functional 4.Uses a COMPILER to translate the Logic source code into machine readable code Object-oriented Concurrent and Distributed 5. FORTRAN, BASIC, COBOL, Pascal, C, C+, Algol, Ada Imperative Programming Focuses on how to perform tasks D. Fourth Generation (4GL) through a sequence of instructions that 1. Object Oriented Programming Language change a program’s state. It emphasizes explicit commands for computation, typically 2. Organize coding around objects using loops, conditionals, and variable assignments. Common languages include 3. Event driven event triggers the outcome C, Python (in imperative style), and Java. of the program Refers to a mood of expressing an 4. JAVA, C+, C#, Visual Basic, VB.NET essential order or command E. Fifth Generation (5GL) It describes computation in terms of a 1. Logic Programming program state and actions that change the state. 2. Artificial Language The program consists of explicit 3. Operates on the concept of solving commands or instructions to be executed, problems based on constraints or rules that performing operations on data and have been declared in the program modifying values of program variables and the external environment. 4. The focus is on making the computer program solve the problem for you. Functional Programming Centers on computing results using 5. Uses knowledge bases and expert pure functions, avoiding mutable state and system side effects. It treats computation as the evaluation of mathematical functions and 6. PROLOG, Mercury, LISP emphasizes immutability and higher-order functions. PROGRAMMING PARADIGMS Programming paradigms are fundamental Computations are specified through styles or approaches to programming that mathematical functions that evaluate input guide how developers write and structure expressions and convert them into output code. They provide a framework for thinking values. B.C.N.V 6 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan Functional programming languages are A group of objects that have the same "what" -oriented. properties and behaviors is called a CLASS They describe the problem to be solved Java, C+, Visual Basic, VB.Net, C# rather than specify the actual steps required for the solution. Concurrent & Distributed Programming Deals with the execution of LISP, Haskell, Scheme, Standard ML processes in parallel (concurrently) or across multiple computing nodes Logic Programming (distributed systems). It focuses on Based on formal logic, it involves managing the complexities of defining a set of facts and rules, and multi-threading, synchronization, and computation is performed by querying these communication between processes. rules. It’s often used for problems requiring complex condition checking, such as in Program technique that allows for the artificial intelligence. execution of two or more operations at the same time Programs are written in logical statements that describe the properties that solutions Concurrent means occurring at the same must have. time Based on the concept of logical deduction Parallel suggests that the processes run in symbolic logic, or the manipulation of side-by-side yet independently with each symbols other. These paradigms provide different ways to approach problem-solving in PROLOG, Constraint Logic Programming programming, each with its strengths (CLP) depending on the task at hand. Object-Oriented Programming TYPES OF PROGRAMMING Organizes code around objects, LANGUAGES which are instances of classes. Objects A. Machine Language encapsulate data and behavior, promoting It is the only language that the reuse, modularity, and abstraction. OOP computer understands. It consists only of supports principles like inheritance, binary numbers 0 and 1. The use of binary polymorphism, and encapsulation. numbers as the basis of machine language was proposed by Dr. John Von Neumann. Data structures are viewed as objects, and Each different type of CPU (Central programmers create relationships between Processing Unit) has its own unique these objects. machine language. Programs written in machine language are very fast in terms of program execution and use only a minimal amount of computer resources such as B.C.N.V 7 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan memory, hard drive and CPU but it is not expressed for certain early microprocessors easy to write, debug and test programs like the 6502. written in machine language on the part of the programmer. Machine language was B. Low Level Language widely used in the 1940s up to early 1950s Low-level language is also known as primarily because of the small storage assembly language. This is a type of capacity and CPU speed of computers low-level programming language in which during that time. each program statement corresponds to an instruction that the microprocessor can Advantage of Machine Language: carry out. Assembly languages are thus The only advantage of machine language specific to a given microprocessor or CPU is it runs very fast because no translation (Central Processing Unit) of the computer. program After writing an assembly language is required for the CPU. program, the programmer must use the assembler specific to the processor to Disadvantages of Machine Language: translate the assembly language into a It is very difficult to program in machine corresponding machine code that language. The programmer has to know computers understand. An assembly details of language provides precise control of the the computer hardware to write the computer, but assembly language program. programs written for one type of computer The programmer has to remember a lot of must be rewritten to operate on other types. codes to write a program which usually An assembly language might be used results in instead of a high-level language for any of program errors. these three major reasons: speed, control, It is difficult to debug the program. and preference. Programs written in an It is very difficult to maintain and modify assembly language usually run faster than the program. those generated by a compiler. The use of It is designed to work in a particular assembly language allows a programmer to computer hardware architecture. interact directly with the hardware, including the processor, memory display, and Example of Machine Language: input/out ports. Advantages of Low-Level Languages It consists of binary digits and some symbols. Very fast program execution Each of these lines represents an instruction or data in hexadecimal format, It depends on the machine. typical of how machine language might be B.C.N.V 8 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan Machine language directly executes on a problem rather than knowing how to computer. program a computer. Ultimately, programs written in a high-level language must be No need for a language translator for translated into machine language, the machine language. language that computers understand. A good example of high-level languages are Program written in a low level language is COBOL, RPG, FORTRAN, Modula-2, called object code. LOGO, C, C+, Java, C#, JavaScript, BASIC and Pascal. Disadvantages of Low-Level Languages It is not easy to understand. Since their development in the 1950's, many other languages have been It is difficult to modify. developed today. These include Visual Basic, Visual Foxpro, Ruby, Perl, Python, Programs written in low level language can Lisp, PHP, ASP.NET, C#, Objective C, run only on specific microprocessors or Prolog and Java. CPUs. Most programming languages were Example of Assembly Language: created to serve specific purposes such teaching basic computer programming at schools, creating database applications for business use, weather forecasting system, airport reservation system, creating graphic images and animations, creating computer games, creating video presentations, communication tools such as email, chat, text message, video conferencing, device drivers for interface different computer hardware such as printers, scanners, digital C. High-Level Programming Language cameras and controlling input and output High level programming languages devices in the computer system. enable the programmer to write programs that are more or less independent of a However, programmers found that particular type of computer. Such languages languages could develop for one purpose are considered high level because they are with few modifications serving equally well closer to human language than the in writing other programs. The question of machine languages the computer truly what programming language to use understands. The main advantage of a depends on the type of problem you want to high-level language over, low-level solve. language is that they are easier to understand, modify and debug. They allow the programmers to focus on solving the B.C.N.V 9 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan Every language has its advantages Example of High-Level Language: and disadvantages. For example, Pascal is very good at teaching fundamentals in computer programming in schools, but it is not good at creating web applications. Java is a very good language to use in writing secure, robust and web ready applications. Interpreters and Compilers It is also a great language to learn Regardless of what type of language object-oriented programming that is very you are using, eventually you will convert helpful in creating large software your program into a machine language so applications. that the computer can understand and execute your program. Advantages of High-Level Languages: It is close to a human being. When we write our programs in a high-level language the computer does not It is easy to understand. understand the language because it is only operated with bits or one's and zero's. It consists of an English language-like There is a need for a translation process structure. that will convert the programs written in high-level language into machine language. It does not depend upon the machine. There are two ways of doing this, either It is easy to modify. compile the program or interpret the program. The process of compiling and The programs written in high level interpreting a program can be accomplished languages are called source code. by using other programs called compilers and interpreters, these programs translate Disadvantages of high-level languages: the program written in high-level language It needs a language translator to translate into machine language. the program into machine language. Compiler It is very slow in terms of program A compiler is a program that execution compared to machine or translates a program written in a high-level assembly language. language (source code) and translates into machine language (object code). The It needs big amount of system resources compiler derives its name from the way it such as memory, disk space and CPU works. speed to run the program properly. The first practical compiler was It does not execute directly on the written by Commodore Grace Murray computer. Hopper of the United States Navy in 1952 she named it A compiler. It analyzes the B.C.N.V 10 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan entire piece of source code and then coalesce multiple object files into a single reorganizes the instructions and then executable or library. translates into machine code. Every high-level programming language (except Programming languages that use strictly interpretive languages) comes with a compilers are Java, C, C+, BASIC, Fortran, compiler. In effect, the compiler is the COBOL, Go, Felix, D, Oberon, Pascal, language because it defines which Python, Eiffel, Smalltalk, Haskell, Batch and instructions are acceptable and which are CLI. not. Source Code is any series of statements written in some high-level computer programming language. The phrase is usually employed within the scope of specific computer software. Source code refers to the assortment of documents Interpreter that embody the heart of a computer The most common way to translate program, which can be translated from a a high-level language to machine language language understandable by humans to a is to compile the program; the other format executable by a machine. This method is to pass the program through an conversion either results in an executable interpreter. The interpreter translates and file via a compiler tailored for a specific executes the program line by line. An computer structure, or the code is directly interpreter translates into an intermediate run from its human-comprehensible state form, which it then executes. In contrast, a with the assistance of an interpreter. compiler translates high-level instructions or commands directly into machine language. Object Code is an object file, The first interpreter was written by Steve another term for a code's intermediary Russell on the IBM 704 computers in representation, that is produced by a 1958. The first high-level language that compiler once it has interpreted a source uses an interpreter to translate its code into code file. These files encapsulate machine code is LISP or List Processing compressed, pre-analyzed code, frequently language written and developed by John termed as binaries. These binaries can be McCarthy in 1958. combined with others to construct a final executable or code library. The advantage of an interpreter, however, is that it does not need to go Predominantly, an object file through the compilation stage during which consists of machine code, which can be machine instructions are generated. This executed immediately by a computer's compilation process can be time consuming Central Processing Unit (CPU). Beyond just if the program is long. The interpreter, on the object code, the file also includes the other hand, can immediately execute relocation data which the linker leverages to high level programs. B.C.N.V 11 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan Programming languages that use b. Selection / Conditional Logic interpreters are LISP, BASIC, Java, Pascal, This principle involves executing Ruby and Python. certain code blocks based on the current state of the program. It typically uses specific keywords such as "if-then-else" and "switch-case" statements to dictate the flow of the program based on various conditions. c. Iteration / Looping Mechanism Here, a particular set of instructions or a block of code is repetitively executed until a predefined condition is met or operations have been carried out on all elements of a collection. This is usually represented by keywords like "while", "do-while", "repeat-until, "for". It is Two Types of Programming Paradigm commonly suggested that each loop should 1. Structured programming maintain a singular entry point. Originally, A programming approach that structural programming also proposed only strives to enhance the lucidity, caliber, and one exit point, and this rule is enforced in creation efficiency of a computer program several programming languages. Examples by leveraging a plethora of subroutines, of structure programming languages are block structures, and looping constructs like COBOL, ALGOL FORTRAN, SNOBOL, 'for and 'while. This method, counterposing LOGO, Pascal, BASIC, Modula-2, C, Perl, the utilization of basic tests and jumps such PL/I. as the 'goto' statement, mitigates the risk of spawning 'spaghetti code' - a convoluted 2. Object Oriented Programming and hard-to-maintain coding structure.Also Object-Oriented Programming known as procedural programming that (OOP) is a programming approach that uses the principles of Top-Down Design revolves around the concept of 'objects'. Principle was first created by Dr. Edsger These objects are essentially data W. Dijktstra in 1960's. structures housing two main components: data, often referred to as attributes, and code, typically known as methods. What It uses the following control structures: sets objects apart is their unique ability for a. Sequence - This refers to a structured their methods to access and frequently series of instructions or procedures modify the data attributes they're connected carried out in a specific order. The actions with, embodying a sense of 'this' or are executed one after the other, self-reference. In crafting software using sequentially. the OOP paradigm, programs are structured as a collaborative network of these objects, which engage and interact with each other. B.C.N.V 12 UNIVERSIDAD DE DAGUPAN Arellano St., Dagupan City, Pangasinan e. Abstraction - A mental facility that Despite the considerable diversity in permits one to view problems with varying OOP, it is predominantly class-based in degrees of detail depending on the current most popular programming languages. This context of the problem. indicates that objects are created as instances of classes, typically defining their f. Encapsulation - A modeling and type. In essence, these classes serve as implementation technique (information blueprints that shape the nature and hiding) that separates the external aspects behavior of their instantiated objects. This of an object from the internal, object-oriented programming concept was implementation details of the object. first introduced in the s in a programming language called Simula B that was created Examples of object-oriented programming by Ole-Johan Dahl and Kristen Nygaard languages are: at the Norwegian Computing Center in Java, Simula, C+, C#, PHP 7, Scala, Oslo, Norway. Objective - C, Object Pascal, ADA 95, Go, F#, ]#, Gambas, SmallTalk, Ruby, Python, ABAP and Oberon. Major Components of Object-Oriented Programming a. Object - A concept, or thing with crisp boundaries and meanings for the problem at hand; an instance of a class. b. Class - An abstraction of an object that specifies the static and behavioral characteristics of it, including their public and private nature. A class is an ADT with a constructor template from which object instances are created. c. Inheritance - The relationship between classes whereby one class inherits part or all of the public descriptions of another base class, and instances inherit all the properties and methods of the classes which they contain. d. Polymorphism - The ability of a function/operator, with one name, to refer to arguments, or return types, of different classes at run time. B.C.N.V 13

Use Quizgecko on...
Browser
Browser