CS 110 D PROGRAMMING LANGUAGE I Computer Science

  • Slides: 45
Download presentation
CS 110 D: PROGRAMMING LANGUAGE I Computer Science department Lecture 1: Introduction to computers

CS 110 D: PROGRAMMING LANGUAGE I Computer Science department Lecture 1: Introduction to computers & problem solving

Lecture Contents 2 Course Info. Problem Solving Techniques � Pseudocode � Algorithm � Flow

Lecture Contents 2 Course Info. Problem Solving Techniques � Pseudocode � Algorithm � Flow charts Examples Elements of a Computer system. Evolution of programming languages The code Life Cycle dr. Amal Khalifa, Fall 14

Course info 3 Books and references Course plan Assessment methods and grading Labs and

Course info 3 Books and references Course plan Assessment methods and grading Labs and practical assignments Practical exam

Books and references 4 [1] Java. TM Programming: From Problem Analysis to Program Design

Books and references 4 [1] Java. TM Programming: From Problem Analysis to Program Design (Introduction to Programming) 5 th Edition [1] https: //www. amazon. com/Java. TMProgramming-Problem-Analysis. Introduction/dp/111153053 X [2] Deitel P. J. , Deitel H. M. - Java. How to Program, 9 th Edition http: //staff. cs. psu. ac. th/iew/cs 344481/Java%20 How%20 to%20 Program%20 9 th%20 Edition. pdf [2]

5 How Computers Solve Problems Create a solution (called an Algorithm) which manipulates Data

5 How Computers Solve Problems Create a solution (called an Algorithm) which manipulates Data into Information Computers use Algorithms to solve problems, and change data into information Computers can only perform one simple step at a time Dr. Amal Khalifa, 2014

Algorithms 6 Algorithm: A step-by-step problem-solving process in which a solution is arrived at

Algorithms 6 Algorithm: A step-by-step problem-solving process in which a solution is arrived at in a finite amount of time Example: Accelerating in a car 1. 2. 3. 4. Move right foot to gas pedal Apply pressure to gas pedal with right foot If speed is too high, apply less pressure. If speed is too low, apply more pressure. Dr. Amal Khalifa, 2014

Problem Solving Approach 7 Problem-solving process involves the following steps: 1. Analyze the problem

Problem Solving Approach 7 Problem-solving process involves the following steps: 1. Analyze the problem and outline the problem and its solution requirements. 2. Design an algorithm to solve the problem. 3. Implement the algorithm in a programming language, such as Java. 4. Verify that the algorithm works. 5. Maintain the program by using and improving it, and modifying it if the problem domain changes. Dr. Amal Khalifa, 2014

Expressing the Algorithms 8 A “Standard” way of describing an algorithm must exist if

Expressing the Algorithms 8 A “Standard” way of describing an algorithm must exist if we expect our solution to be understood by others easily There are standards in programming: � PSEUDOCODE � FLOWCHARTS Dr. Amal Khalifa, 2014

Pseudo Code 9 “Pseudo” means “pretend” or “false” Pseudo Code is pretend or false

Pseudo Code 9 “Pseudo” means “pretend” or “false” Pseudo Code is pretend or false computer code; uses generic English-like terms Pseudo Code is not as standardized as flowcharts, and does not facilitate the breaking down of problems as well as a flowchart does Dr. Amal Khalifa, 2014

Example 10 Write an algorithm to find the area and the perimeter of a

Example 10 Write an algorithm to find the area and the perimeter of a rectangle dr. Amal Khalifa, Fall 14

11 Pseudo Code Example design an algorithm to find the perimeter and area of

11 Pseudo Code Example design an algorithm to find the perimeter and area of a rectangle Dr. Amal Khalifa, 2014

12 That’s all for Today!! Text book [1] chapter 1 (pages 13 -16) Dr.

12 That’s all for Today!! Text book [1] chapter 1 (pages 13 -16) Dr. Amal Khalifa, 2014

Flowcharts 13 Graphical representations of algorithms or a tool to translate algorithm A Flowchart

Flowcharts 13 Graphical representations of algorithms or a tool to translate algorithm A Flowchart uses easy-to-understand symbols to represent actions on data and the flow of data Flowcharts aid in breaking down a problem into simple steps Dr. Amal Khalifa, 2014

Flowchart Symbols 14 Dr. Amal Khalifa, 2014

Flowchart Symbols 14 Dr. Amal Khalifa, 2014

flowcharting 15 Dr. Amal Khalifa, 2014

flowcharting 15 Dr. Amal Khalifa, 2014

Example 16 Example 1: Write an algorithm and draw a flowchart to convert the

Example 16 Example 1: Write an algorithm and draw a flowchart to convert the length in feet to centimeter. hint: I feet = 30 cm Dr. Amal Khalifa, 2014

Pseudocode 17 Input the length in feet (Lft) � Calculate the length in cm

Pseudocode 17 Input the length in feet (Lft) � Calculate the length in cm (Lcm) by multiplying LFT with 30 � Print length in cm (LCM) � Dr. Amal Khalifa, 2014

Algorithm 18 Flowchart Step 1: Input Lft Step 2: Lcm Lft x 30 Step

Algorithm 18 Flowchart Step 1: Input Lft Step 2: Lcm Lft x 30 Step 3: Print Lcm START Input Lft Lcm Lft x 30 Print Lcm STOP Dr. Amal Khalifa, 2014

Example 2 19 Write an algorithm and draw a flowchart that will read the

Example 2 19 Write an algorithm and draw a flowchart that will read the radius of a circle and calculate its area. Dr. Amal Khalifa, 2014

Example 2 20 Pseudocode Input the radius (r) of a circle Calculate the area

Example 2 20 Pseudocode Input the radius (r) of a circle Calculate the area (A) : A Pi x r Print A Dr. Amal Khalifa, 2014

Example 2 21 Algorithm Step 1: Input r Step 2: A Pi x r

Example 2 21 Algorithm Step 1: Input r Step 2: A Pi x r Step 3: Print A START Input r A Pi x r Print A STOP Dr. Amal Khalifa, 2014

Example 3 22 Write an algorithm and draw a flowchart that will calculate the

Example 3 22 Write an algorithm and draw a flowchart that will calculate the roots of a quadratic equation Hint: the roots are: x 1 = (–b + d)/2 a and x 2 = (–b – d)/2 a where, d = sqrt ( ) Dr. Amal Khalifa, 2014

Example 3 START 23 Algorithm: Input a, b, c Step 1: Input the coefficients

Example 3 START 23 Algorithm: Input a, b, c Step 1: Input the coefficients (a, b, c) of the quadratic equation Step 2: d sqrt ( Step 3: x 1 (–b + d) / (2 x a) Step 4: x 2 (–b – d) / (2 x a) Step 5: Print x 1, x 2 ) d sqrt(b x b – 4 x a x c) x 1 (–b + d) / (2 x a) X 2 (–b – d) / (2 x a) Print x 1 , x 2 STOP

24 Elements of a Computer System Hardwar e Comput er Software dr. Amal Khalifa,

24 Elements of a Computer System Hardwar e Comput er Software dr. Amal Khalifa, Fall 14

Hardware 25 dr. Amal Khalifa, Fall 14

Hardware 25 dr. Amal Khalifa, Fall 14

Memory Unit 26 Ordered sequence of cells or locations Bits & Bytes Stores instructions

Memory Unit 26 Ordered sequence of cells or locations Bits & Bytes Stores instructions and data in binary Types of memory � Read-Only Memory (ROM) � Random Access Memory (RAM) dr. Amal Khalifa, Fall 14

27 Binary Data Bit: A binary digit 0 or 1. A sequence of eight

27 Binary Data Bit: A binary digit 0 or 1. A sequence of eight bits is called a byte. dr. Amal Khalifa, Fall 14

ASCII Code 28 Every letter, number, or special symbol (such as * or {)

ASCII Code 28 Every letter, number, or special symbol (such as * or {) on your keyboard is encoded as a sequence of bits, each having a unique representation. The most commonly used American Standard Code for Information Interchange (ASCII). dr. Amal Khalifa, Fall 14

Central Processing Unit (CPU) 29 Executes stored instructions Arithmetic/Logic Unit (ALU) � Performs arithmetic

Central Processing Unit (CPU) 29 Executes stored instructions Arithmetic/Logic Unit (ALU) � Performs arithmetic and logical operations Control Unit � Controls the other components � Guarantees instructions are executed in sequence dr. Amal Khalifa, Fall 14

Input and Output Devices 30 Interaction with humans Gathers data (Input) Displays results (Output)

Input and Output Devices 30 Interaction with humans Gathers data (Input) Displays results (Output) dr. Amal Khalifa, Fall 14

Software 31 System programs loads first when you turn on your PC Also called

Software 31 System programs loads first when you turn on your PC Also called the operating system. The operating system monitors the overall activity of the computer and provides services, such as memory management, input/output activities, and storage management. Application programs perform specific tasks Examples : � � � Word processors spreadsheets, and games Both operating systems and application programs are written in programming languages. dr. Amal Khalifa, Fall 14

Are Computers Intelligent? 32 Do we really need to be involved in programming computers?

Are Computers Intelligent? 32 Do we really need to be involved in programming computers? � They have beaten world chess champions. � They help predict weather patterns. � They can perform arithmetic quickly. So, a computer has an IQ of _____. dr. Amal Khalifa, Fall 14

33 What is Computer Programming? Planning or scheduling a sequence of steps for a

33 What is Computer Programming? Planning or scheduling a sequence of steps for a computer to follow to perform a task. Basically, telling a computer what to do and how to do it. A program: �A sequence of steps to be performed by a computer. � Expressed in a computer language. dr. Amal Khalifa, Fall 14

Computer Languages 34 A set of � Symbols (punctuation), � Special words or keywords

Computer Languages 34 A set of � Symbols (punctuation), � Special words or keywords (vocabulary), � And rules (grammar) used to construct a program. dr. Amal Khalifa, Fall 14

35 Evolution of Programming Languages differ in � Size (or complexity) � Readability �

35 Evolution of Programming Languages differ in � Size (or complexity) � Readability � Expressivity (or writability) � "Level" closeness to instructions for the CPU dr. Amal Khalifa, Fall 14

Machine Language 36 Binary-coded instructions Used directly by the CPU Lowest level language Every

Machine Language 36 Binary-coded instructions Used directly by the CPU Lowest level language Every program step is ultimately a machine language instruction Address Contents 2034 10010110 2035 11101010 2036 00010010 2037 1010 2038 10010110 2039 11101010 2040 1111 2041 0101 2042 10101101 dr. Amal Khalifa, Fall 14

Assembly Language 37 Each CPU instruction is labeled with a mnemonic. Very-low level language

Assembly Language 37 Each CPU instruction is labeled with a mnemonic. Very-low level language � Almost 1 to 1 correspondence with machine language Assembler: A program that translates a program written in assembly language into an equivalent program in machine language. Sample Program Mnemonic Instruction ADD 10010011 MUL ADD STO SUB X, 10 X, Y Z, 20 X, Z dr. Amal Khalifa, Fall 14

38 Examples of Instructions in Assembly Language and Machine Language dr. Amal Khalifa, Fall

38 Examples of Instructions in Assembly Language and Machine Language dr. Amal Khalifa, Fall 14

High-Level Languages 39 Closer to natural language Compiler: A program that translates a program

High-Level Languages 39 Closer to natural language Compiler: A program that translates a program written in a high-level language into the equivalent machine language. dr. Amal Khalifa, Fall 14

40 Examples of High-Level Languages Language Pascal C++ FORTRAN PERL Primary Uses Learning to

40 Examples of High-Level Languages Language Pascal C++ FORTRAN PERL Primary Uses Learning to program General purpose Scientific programming Web programming, text processing Java Web programming, application programming COBOL Business dr. Amal Khalifa, Fall 14

The java Language 41 source program. Saved in File Class. Name. j ava dr.

The java Language 41 source program. Saved in File Class. Name. j ava dr. Amal Khalifa, Fall 14

The Code life Cycle!! 42 Edit compile run dr. Amal Khalifa, Fall 14

The Code life Cycle!! 42 Edit compile run dr. Amal Khalifa, Fall 14

43 Processing a java program dr. Amal Khalifa, Fall 14

43 Processing a java program dr. Amal Khalifa, Fall 14

IDE 44 The programs that you write in Java are typically developed using an

IDE 44 The programs that you write in Java are typically developed using an integrated development environment (IDE). dr. Amal Khalifa, Fall 14

45 That’s all for Today!! Text book [1] chapter 1 (pages 1 -13) dr.

45 That’s all for Today!! Text book [1] chapter 1 (pages 1 -13) dr. Amal Khalifa, Fall 14