Full Transcript

Programming 1 By JOE G. LAGARTEJA, LPT, DIT About the Course I. Course Title: Programming 1 II. Course Description: This course provides fundamental concepts in computer programming. This course will also cover the techniques being applied in programming. III. Prerequisite:...

Programming 1 By JOE G. LAGARTEJA, LPT, DIT About the Course I. Course Title: Programming 1 II. Course Description: This course provides fundamental concepts in computer programming. This course will also cover the techniques being applied in programming. III. Prerequisite: None IV. Contact Hours: 2 Lecture and 3 Hour Lab a week V. Credit: 3 Units Course Content/Outline Definition of Programming Purpose of Making Programs Programming Language Types of Programming Language Generations of Programming Language Examples of Programming Language Compiler Interpreter Continuation…. Choosing a programming Language How to make programs Flowchart Algorithm The JAVA programming Language What is JAVA? Continuation Structure of a JAVA program Statements Syntax Variables in JAVA (Declaration, Assignment) JAVA Data Types Continuation JAVA Operators (Mathematical, Assignment, Relational, Boolean) Looping (While, Do, For) Arrays (Single, Mult-dimensional Arrays) Textfiles Course Requirements Quizzes Laboratory Exercise Seatwork Project Midterm and Final Examinations Create a simple system using JAVA Programming Language CHAPTER 1 Introduction to Computer Programming What is a Program? An organized list of instructions that, when executed, causes the computerto behave in a predetermined manner. A computer program, or just a program, is a sequence of instructions, written to perform a specified task on a computer. A program is like a recipe. It contains a list of ingredients (called variables) and a list of directions (called statements) that tell the computer what to do with the variables. The variables can represent numeric data, text, or graphical images. Without programs, computers are useless! an app, application, software application or software program is the most common software on the computer that performs a special function or task. For example, Microsoft Word is a word processor program that allows users to create and write documents and the browser you are using to view this page is a program. Without programs, a computer would still work with the operating system, but you would not be able to do anything. What is Programming? It is an act of making programs App vs Program An App is an Apple term for program. What is software? Generic General does not necessarily always mean "program" or "application" (e.g. a software "library" or "framework" is not a "program" or "application", but are used to facilitate the functional requirements of "programs" or "applications"). Why do programmers make computer programs? To simplify task Provide accurate result Aides in facilitating day to day activities LLL vs HLL Low Level Language – language that can convert to machine code without a compiler or interpreter, and the resulting code runs directly on the CPU/Processor. (also known as the machine language or assembly language – the zeros and ones (0,1)) High Level Language – needs a compiler or interpreter to convert the instruction into language that could be understood by CPU/Processor. What is CPU or Processor? “Brain” of the computer Executes commands or instructions Compiler It is a program that converts high level language to low level language that could be understood by the CPU (this process is called “Compiling”) Commonly compiles instruction known as the “Code” Interpreter It is a program that interprets(without undergoing the process of compiling) high level language to low level language that could be understood by the CPU. Commonly compiles instruction known as the “Script” Types of Programming Language Web languages Software languages The different generations of languages Procedure oriented programming Object oriented programming Web Languages HTML XML JAVASCRIPT VBSCRIPT PHP ASP JAVA Software Languages C C++ Visual Basic JAVA The different generations of languages 1st Generation Language (1GL) 2nd Generation Language (2GL) 3rd Generation Language (3GL) 4th Generation Language (4GL) First generation languages (abbreviated as 1GL) Represent the very early, primitive computer languages that consisted entirely of 1's and 0's - the actual language that the computer understands (machine language). Second generation languages (2GL) Represent a step up from the first generation languages. Allow for the use of symbolic names instead of just numbers. Second generation languages are known as assembly languages. Code written in an assembly language is converted into machine language (1GL). Third generation languages (3GL) With the languages introduced by the third generation of computer programming, words and commands (instead of just symbols and numbers) were being used. These languages therefore, had syntax that was much easier to understand. Third generation languages are known as "high level languages" and include C, C++, Java, and Javascript, among others Fourth generation languages (4GL) The syntax used in 4GL is very close to human language, an improvement from the previous generation of languages. 4GL languages are typically used to access databases and include SQL and ColdFusion, among others. Fifth generation languages (5GL) Fifth generation languages are currently being used for Neural Networks. A Neural Network is a form of Artifical Intelligence that attempts to imitate how the human mind works. Example Code of Visual Basic Python JAVA CHAPTER 2 Designing and Creating a Program Algorithm A formula or set of steps for solving a particular problem. To be an algorithm, a set of rules must be unambiguous and have a clear stopping point. Algorithms can be expressed in any language, from natural languages like English We use algorithms every day. For example, a recipe for baking a cake is an algorithm. Most programs, with the exception of some artificial intelligence applications, consist of algorithms. Inventing elegant algorithms -- algorithms that are simple and require the fewest steps possible -- is one of the principal challenges in programming. Examples of algorithm Cookbook Recipe Booklet Choreography In the same way, algorithms executed by computer can combine millions of elementary steps, such as additions, subtractions, multiplications and divisions into complicated mathematical calculations. Here is one algorithm for the usual process of saving a document in a disk. 1. Find a new or unused disk. 2. Insert the disk in drive A. 3. From the word environment, select file menus then select save as. 4. From the save as dialog box, select the directory where you save the document. Select 3½ floppy disk. 5. Position the pointer inside the filename textbox then type the filename of the document. 6. Then click the save button to formally save the document. Sample Problem #1 Create an algorithm on how to kill a chicken. Sample Problem #2 Create an algorithm on how to get the sum of two numbers. Sample Problem #3 Create an algorithm on how to get the average of the three (3) numbers. Flowchart A flow chart is a graphical or symbolic representation of a process. Each step in the process is represented by a different symbol and contains a short description of the process step. The flow chart symbols are linked together with arrows showing the process flow direction. Basic Symbols Example of Flowchart Notations Used in Flowchart Notation Meaning : comparison & logical and Y yes N no EOF End of File Relational Test Operators Notation Meaning > is greater than < is less than = or => is greater than or equal to or >< is not equal to = is equal to Arithmetic Operators Notations Meaning + Addition - Subtraction * Multiplication / Division () Grouping ** or ^ Exponentiation Precedence of the Operators The computer follows this order of priority 1. Grouping () 2. Exponentiation 3. Multiplication or division 4. Addition or subtraction When operators are all of equal priority, the computer evaluate the expression from left to right. When operators are of different priorities, the computer performs those operations with higher priorities first. Operations enclosed in parentheses will take place before other operations. Note (if there are multiple parentheses, the innermost parenthesis will be evaluated first). Valid or Invalid? Example #2.1 1+-2 Example #2.2 3/-2 Both are invalid because no two arithmetic operators may appear side by side. Write clearly the computation desired Example #2.3 2L / 3G Answer? ( 2 * L ) / (3 * G ) Sample Problem #4 Create a flowchart that would ask for a legal age to access a particular porn site. If age is greater than or equal to 18 then print “Yehey, you are allowed, happy Viewing”, otherwise, print “Totoy ka palang”. Sample Problem #5 Create a flowchart that would determine if the grade of student is Passed or Failed. If grade is greater than or equal to 75, print “P”, else “F”. Sample Problem #6 Create a flowchart that would get the average of student’s grade from 5 subjects (Math, Science, Social Sciences, Biology, P.E) and determine whether the average is passed or failed. If the average is greater than 75, display “Passed”, otherwise display “Failed”. Loops and Counters Looping is used when it is desired to make the same calculation on more than one set of data. It consists of repeating a program, or a section of a program, and substituting new data for each repetition. A counter is set up in a program loop to keep track of the number of times the program segment is repeated. Steps in Loop Control 1. Initialization – the value of a counter used is initially set equal to zero (or one). This process is always done outside the loop. 2. Test for Limit Conditions – before logic flow gets out of the loop, a loop – terminating condition must first be satisfied. The process of testing is usually found either at the beginning or at the end of a loop. 3. Incrementation – after each loop is executed, 1 is added to the counter. Thus the counter reflects the number of times the operation has been performed. This process is always done within the loop. Sample Problem #7 Draw a flowchart which will read and print the names and individual scores of 40 students for a particular examination. Determine their average and print it out. The algorithm for this particular requirement could be listed as follows: 1. Initialize the counter (ctr) and accumulator (sum) to zero. 2. Read in student’s name and score. 3. Print out the student’s name and score. 4. Accumulate the data value (score) into sum. 5. Test if the desired amount of data (40) has been reached. 6. If the value of counter (ctr) is less than 40, repeat steps 2 through 5. However, if the desired amount of data has been reached (40), compute the average score by dividing the sum by the value of counter. 7. Print out the average score. Equivalent flowchart of the problem Loops and Accumulators It would often be required to determine the sum of certain numerical quantities that are read or calculated one at a time. This is accomplished by first choosing a variable, and initializing it to zero. As the flow of logic through the flowchart loop is followed, a repetitive operation is performed until the loop is terminated. The variable used is thereby continuously accumulating the numerical quantities and is thus called an accumulator. The final value accumulated will be the sum of all the numerical quantities. Sample Problem #8 Draw a flowchart to compute and print the sum of the squares of positive integers from 1 to 100. Sample Problem #9 Create a flowchart that would change the initial value of the variable X which is 10 to 100 when the value of variable Y is 50. Sample Problem #10 Draw a flowchart that would convert the a particular number to words. Numbers may be from 1 to 5. Example, if the value is 1, then it will display the word “One”, “Two” for 2 and so on… Sample Problem #11 Find the amount to charge people of varying ages for a food ticket. When the person is under 16, the charge is Php 7.00, when the person is 65 or over, the charge is Php 5.00, all others are charged Php 10.00. The conditions are the following: Age Charge =16 and =65 5 Sample Problem #12 Calculate the commission rate for a salesperson given the amount of sales. When the salesperson has sold less than of equal to 2,000.00 worth of goods, the commission is 2%. When the sales total is more than 2,000.00 and less than or equal 4,000.00 the commission is 4%. When the sales total is more than 4,000.00 and less than or equal to 6,000.00, the commission is 7%. When the salesperson sold more than 6,000.00, the commission is 10%. Sample Problem #13 Create a flowchart that would determine if a particular number is an Odd number. When the number is Odd, print “The Number is an Odd”. Sample Problem #14 Create a flowchart that would determine if a particular number is an Even number. When the number is Even, print “The Number is an Even”. Sample Problem #15 Draw a flowchart that would count and display all the even and odd numbers from 1 to 10.

Use Quizgecko on...
Browser
Browser