Programming Concepts PDF
Document Details

Uploaded by InterestingHeisenberg
Tags
Summary
This document explains fundamental programming concepts, including strategies for approaching complex problems, sub-procedures, modular programming, and conditional statements. It also discusses and contrasts procedural with object-oriented programming as well as concepts like variables, arrays, and program collections. The document also touches upon mathematical modeling and computer simulation.
Full Transcript
4.1. o When a par,cular problem has to be solved, an effec,ve method or procedure should be iden,fied. o This procedure reduces the solu,on to a series of simple steps à steps have to be followed in the correct order to obtain the desired output o A good approach for confron,ng a complex probl...
4.1. o When a par,cular problem has to be solved, an effec,ve method or procedure should be iden,fied. o This procedure reduces the solu,on to a series of simple steps à steps have to be followed in the correct order to obtain the desired output o A good approach for confron,ng a complex problem is to develop a method of breaking up the problem into smaller sub-problems o Top-down design/stepwise refinement – a strategy where sub-problems can be further divided into smaller and smaller sub-problems un,l finally they can be dealt with individually - With this approach, the complex problem is decomposed o Sub-procedure – contains a series of commands that perform a task - when is called all statements included in the par,cular sub-procedure are executed o Modular programming – all sub-procedures that represent different parts of the problem solu,on can be used at the right ,me using their iden,fiers à procedure is divided into a series of sub-procedures o Iden;fier – the name that is used by the programmer to uniquely iden,fy a variable, an object, a sub-procedure,… o The addi,on process does not require any decision to be taken à but some problems require the use of the decisions o Itera;on – the process of repea,ng a series of instruc,ons - Used to repeat a statement or a block of statements within an algorithm - Expressed used “from to loop” and “while loop” o Condi;onal statement – performs different instruc,ons depending on a Boolean test o The if-then-else condi,onal statement is common across many programming languages and human logic o Object-oriented programming uses abstrac,on à uses programming objects that describe data (proper,es) and behavior (methods) of real objects, and facilitates code reusability and abstrac,on - based on the principle that all everyday tasks can be considered en,,es - En,,es are either objects or events - It makes complex soNware faster and easier to develop o Procedural (structural) programming – uses procedures that can interact and exchange data as building blocks of programs o Collec;on – a data structure that consists of the data and the predefined methods which operate on the data - Used to add, store, manage, retrieve, manipulate, and communicate the data using predefined methods anaum o Abstract Data Type (ADT) – group of opera,ons and data o Mathema;cal modeling – a process where a system is understood well enough and scien,sts describe it using mathema,cal language - A set of mathema,cal models contains only the necessary details, rules, and objects for studying the real system or an aspect of it - A mathema,cal model could be transformed into an algorithm and then into a program that replicates the behavior of a real system o Computer simula;on – runs on a computer and reproduces the behavior of a real system - Uses an abstract mathema,cal model that is expressed as a computer model to simulate a system o Modular program – easier to understand and facilitate the use of abstrac,on - The programmer can focus on the important things and ignore all unnecessary details o Thema;c map – an abstrac,on of reality that shows the spa,al distribu,on and emphasizes a par,cular theme, such as the average distribu,on of income in a specific geographic area o Topographic map – shows abstrac,ons of selected physical features of the three- dimensional real world at a reduced scale in two-dimensions, paper or a screen o Poli;cal maps – designed to show data such as the boundaries of countries and states and the loca,on of major ci,es - These maps are an abstrac,on of poli,cal territory 4.2 o Variables – acts as a storage loca,on that can be used to store a value - each variable has a name – used to refer to the stored value - the value of the variable can be changed during the program execu,on - each variable can store a par,cular type of data like strings, reals, Boolean, integers - one carriable = one data element of a program o Array – has a name, a size that cannot be changed during program execu,on, and a data type that can be described as the type of data that it can store - All elements of an array should be of the same data type - Data is organized as a table - Perfect choice when a programmer wishes to store the same type of informa,on o One-dimensional array – type of linear array - A single line of elements - Not able to store all the necessary informa,on because it can only store one data type and only store one field o Parallel arrays – extremely useful when a programmer wants to store different proper,es of an en,ty Linear search - sorted and unsorted > o The array of objects – an array of reference variables - Each reference variable is an element of the array and it’s a reference to an object o Sequen;al search – a very simple method to find a par,cular element in an array à the simplest search algorithm o Binary search – the searching method used only in sorted arrays - Relies on divide and conquer strategy to accomplish its purpose o In each itera,on, the algorithm 1. Compares the search value with the value of the middle element of the array a. If the values match, then the value was found b. If the search value is less than the middle element of the array Then the algorithm repeats its ac,on on the sub-array to the leN to the middle element c. If the search value is greater than the middle element of the array Then the algorithm repeats its ac,on on the sub-array to the right of the middle element 2. If the remaining array to be searched is empty, then the value was not found ADD TABLE à 213 PDF – CORE o Bubble sort – simple sor,ng algorithm that repeatedly steps through the array to be sorted - Compares adjacent items and exchanges them if they are not in the correct order (ascending or descending) - The algorithm makes mul,ple passes un,l no swaps are necessary and the elements of the array are sorted - Very slow o Selec;on sort – a very simple and inefficient sor,ng algorithm that divides the input array into two sub-arrays - the first sub-array contains the already sorted elements, and the second sub-array contains the unsorted elements and occupies the rest of the array o Collec;ons – allow custom specifica,ons of the collec,on item elements 1. Generic collec,ons – can only hold data of the same type 2. Non-generic collec,ons – can hold elements of different data types - Advantage of the collec,ons: they act like a resizable array - The programmer doesn’t need to know in advance the number of items that will be placed in the data structure STANDARD OPERATIONS COLLECTIONS § addItem( ) – used to add an item to the collec,on § getNext () – used to return the first item in the collec,on when it is first called - will not remove the item from the collec,on ADDITIONAL OPERATIONS § resetNext ( ) – used to restart the itera,on through the collec,on § hasNext ( ) – used to iden,fy if there are remaining elements in the collec,on that have not been accessed by the present itera,on § isEmpty ( ) – used to test if the collec,on is empty o Efficiency – refers to the amount of the computer resources required to perform its func,ons - Minimizing the use of various resources such as the CPU and computer’s memory is very important o Correctness – refers to the extent to which the algorithm sa,sfies its specifica,on - Free from faults - Fulfills all objec,ves set during the design and implementa,on phase o Reliability – refers to the capability of the algorithm to maintain a predefined level of performance and perform all required func,ons under stated condi,ons, having a long mean ,me between failures o Flexibility – refers to the effort required to modify the algorithm for other purpose than those for which it was ini,ally developed o Big O nota;on – useful when analyzing algorithms as it is a measure of the efficiency of an algorithm - It means that the growth rate of the instruc,ons in this par,cular algorithm shall be executed n ,mes o Lists – used to store a sequence of values under a single name - Allow duplicates to act as containers and to be typically implemented either as linked lists or as arrays - Very similar to the array 4.3. o A computer can perform some fundamental opera6ons o The condi,onal instruc,ons and basic opera,ons of Boolean algebra (AND, OR, XOR, NOT) are cri,cal for the development of useful programs o By combining fundamental computer opera,ons, computers can perform compound opera;ons à finding the max value of four numbers is a compound opera,ons o Programming language – the combina,on of its seman6cs, which refers to the meaning of every construc,on that is possible in the language and its syntax, which relates to its structure o Grammar – meta-language that is used to define the syntax of a language o Syntax – the rules of statement construc,on o A command wricen in a computer language always means the same and there is no ambiguity in meaning o Each computer has its own machine language à made up of 0s and 1s o Machine language – low-level language and is the only language that can be understood directly by a computer à difficult to maintain and debug o Machine language programmer has to keep track of memory loca,ons and write from the very beginning all the mathema,cal func,ons required by a program à one kind is not suitable for another o The next evolu,onary step in programming came with the replacement of binary code for the development of instruc,ons and references to address loca,ons with symbols called mnemonics o Mnemonics/assembly languages – low-level computer languages o Assembler - used to convert the assembly language mnemonics to machine code o A high-level programming language – a programming language that uses elements of natural language - Easy to use - Facilitates abstrac,on by hiding significant areas of compu,ng systems - Makes the program development simpler - Faster and more understandable o Transla,on process – will convert the program into the machine language of the computer on which it will run o Source program/source code – the original program development in a selected high- level language o Object program/target program – the translated program in machine language o 2 methods used for transla,on: compila;on and interpreta;on o A compiler – translator that executes the transla,on process only once - Translates the whole source program into the object program - The object program is saved and the next ,me a programmer wants to use the object program no recompila,on is necessary o An interpreter – translator that goes through the process of transla,on every ,me the program is run o Interpreta;on – the process of reading each line of the source program, analyzing it, transla,ng it into the corresponding line of the object program, and execu,ng the line o Java combines compila,on and interpreta,on - Source code is compiled to Java Virtual Machine bytecode o Variable – used to store a data element of a program o Constant – represents things and quan,,es that don’t change - Non-modified variables o Operator – used to manipulate operands - Can be arithme,c, rela,onal, logical, etc. o Object – a comprised of data and ac,on - Ac;ons – opera,ons that can be performed by the object o Data members – used to store the current state of an object and methods are used to change or access those data members o The scope of a variable – the visibility of that variable - Defines which parts of the algorithm can store, access and retrieve the data of the variable - A global variable – visible to all parts of your program - A local variable – has a limited scope o A collec;on/container – consists of zero or more elements such as objects and values and it is equipped with the necessary opera,ons to handle data - Allow duplicate elements and may contain ordered and unordered data elements o Each type of collec,on is equipped with its own specific opera,ons o A fixed-size array – is not considered a collec,on because it holds a fixed number of items o A sub-program – a unit that contains a sequence of computer instruc,ons that perform a specific and predefined procedure - May be defined within programs, or separately in libraries that can be used by mul,ple programs - In most cases a func,on returns a value and a procedure just executes commands o Code reuse – allows programmers to take advantage of exis,ng code and solu,ons developed by other programmers to speed up their tasks - Saves ,me and resources - Allows the comple,on of demanding projects in the shortest period of ,me o SoUware libraries – contain subprograms that can be used by different types of programs o Advantages of breaking a program into sub-programs: - Breaking down a complex programming job into simpler jobs - Distribu,ng a very large programming problem among various programmers all over the world - Enabling code reuse across mul,ple programs - Facilita,on of abstrac,on by hiding implementa,on details from users of the subprogram - Improving maintainability and traceability - Reducing programming code duplica,on within the same program o Advantages of using collec;ons: - The methods of collec,ons are predefined algorithms that the programmer can immediately use - Performance is increased by the data management capabili,es provided by the collec,on - SoNware reuse is facilitated because the use of methods is based on a common language and a standard interface o Java uses methods to represent sub-programs o A Java method may return a value and the main program may pass one or more variables to a method o A parameter – the name of the informa,on that is used in a method, func,on or procedure o An argument – the value that is passed into a method, func,on or procedure