© Jalal Kawash 2010 2 Programming Peeking into Computer Science 1.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C Programming
ITEC113 Algorithms and Programming Techniques
James Tam Functions: Decomposition And Code Reuse (Part I) This section of notes shows you how to write functions that can be used to: decompose large.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 2: Input, Processing, and Output
Introduction to C Programming
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
COMPUTER SCIENCE I C++ INTRODUCTION
DCT 1123 Problem Solving & Algorithms
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs.
© Jalal Kawash Programming Peeking into Computer Science 1.
Introduction to Python
INTRODUCTION TO ALGORITHMS PROGRAMMING. Objectives Give a definition of the term algorithm Describe the various parts of the pseudocode algorithm or algorithm.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Visual Basic.NET BASICS Lesson 4 Mathematical Operators.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Input, Output, and Processing
Introduction to Programming with RAPTOR
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
© Jalal Kawash Programming Peeking into Computer Science 1.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
© Jalal Kawash Database Queries Peeking into Computer Science.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
Variables 1. What is a variable? Something whose value can change over time This value can change more than once over the time period (no limit!) Example:
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Bill Tucker Austin Community College COSC 1315
Topics Designing a Program Input, Processing, and Output
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Design & Technology Grade 7 Python
Variables, Expressions, and IO
Introduction to Scripting
Chapter 2 - Introduction to C Programming
Number and String Operations
T. Jumana Abu Shmais – AOU - Riyadh
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2: Input, Processing, and Output
Primitive Types and Expressions
Presentation transcript:

© Jalal Kawash Programming Peeking into Computer Science 1

© Jalal Kawash 2010Peeking into Computer Science Objectives At the end of this section, you will be able to: 1. Understand the concept of variables 2. Create properties and local variables in Alice

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Variables Are used by a program to temporarily store information. Many types of information can be stored by a variable. ◦For this class you will be mostly concerned with: numbers, Booleans (true or false), string (series of characters). (Lists will also be covered this term).

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Example Use Of A Variable Distance is a variable with an initial value of 1. The no. of meters forward that the troll moves is determined by the current value stored in distance (1). Variable distance now stores the number 5. The no. of meters forward that the dragon moves is determined by the current value stored in distance (which is now 5). Example 3: variables

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Variable Naming Conventions Should be meaningful ◦E.g., ‘ x ’, ‘ y ’ (bad) vs. ‘ age ’, ‘ distance ’ (better) Variable names should generally be all lower case. For variable names composed of multiple words separate each word by capitalizing the first letter of each word (save for the first word) or by using an underscore. (Be consistent!) ◦ netIncome ◦ gross_pay ◦ (Avoid using spaces).

© Jalal Kawash 2010Peeking into Computer Science Property Variables Properties of an object: 6

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Memory Is Compartmentalized Normally the information associated with a program (memory for that program) only lasts while that program runs. Image in PowerPoint Word Movie maker While the program runs the text and image can be accessed elsewhere

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Memory Is Compartmentalized Normally the information associated with a program (memory for that program) only lasts while that program runs. Word Movie maker The program (PowerPoint) ends and it’s data is no longer accessible (localized to the program unless copied)

© Jalal Kawash 2010Peeking into Computer Science Local Variables Local to methods ◦Do not exist outside the method in which they are contained 9

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Local Variables 10 Methods of the example program: ‘start’, ‘jamesNewMethod’ Local variable of ‘jamesNewMethod’: ‘jamesAge’ Local variable ‘jamesAge’ not in the ‘start’ method

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: World Level Variables As the name implies they can be accessed anywhere in the world. In practice it means that these variables can be used in any method of that world. 11

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: World Level Variables 12 World-level variables: properties of the world Example 4: Local vs. World variables, string conversion World level variable can be directly accessed in the methods.

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: String Conversion An object can only display string information when it ‘says’ or ‘thinks’ something. Numeric information must be converted to a string before it can be displayed: ◦World->functions->”what as string” 13

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: String Conversion (2) 14

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Captioning Output Sometimes when many variables are being displayed (say/think) it’s hard to track the information. Captioned or labeling which variable is being displayed can be useful. World->functions->”joined with” (You join the caption with the display of the variable’s contents) ,000Net income $ Labeling output Contents of variable

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Captioning Output (2) 16

© Jalal Kawash 2010Peeking into Computer Science JT’ Extra: Getting User Input 17 Example 5: captioning output (applying ‘join’), getting user input Getting user input: functions of class world Drag the appropriate function when setting the value of variables Source of information for the variable is the user.

© Jalal Kawash 2010Peeking into Computer Science Assignment Statement & User Input 18

© Jalal Kawash 2010Peeking into Computer Science Assignment Statement 19

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Mathematical Expressions Most any arbitrary mathematical expression can be created in Alice. Expressions are created graphically. Example:

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Common Mathematical Operations Mathematical operationSymbol used in Alice Addition+ Subtraction- Multiplication* Division/

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Copying From a Variable Is An Expression 22 Example: We want to add the expression: num = jamesAge Num= Set the variable ‘num’ to the current value of the variable ‘jamesAge’ Yields the expression: Num = jamesAge

© Jalal Kawash 2010Peeking into Computer Science User Input 23 Drag and drop ask user for a number function to replace the value 1

© Jalal Kawash 2010Peeking into Computer Science The Complete Program 24

Methods and Functions Defining Behaviour 25

© Jalal Kawash 2010Peeking into Computer Science Objectives At the end of this section, you will be able to: 1. Use methods and parameters 2. Understand and use functions 3. Create methods and functions

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Parameters Parameters is information that is passed into a function or a method. Example: exponent function of world (base, exponent) ◦Based raised to the power of the exponent. ◦Based and exponent is information passed to the exponent function. 27 Parameters to the function

© Jalal Kawash 2010Peeking into Computer Science Methods A method is defined inside an object It has a name And zero or more parameters A method is called within an object: 28

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Function Definition A function definition indicates what a function will do when it is run. Examples (functions of class world which have already been defined by the creators of Alice). 29 Execute some instructions and return a value

© Jalal Kawash 2010Peeking into Computer Science JT’s Method Definition A method definition indicates what a method will do when it is run. Examples (methods of class world which have already been defined by the creators of Alice). 30 Methods act on or do something to the object that it belongs to.

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Method Call When a method is actually run. 31 This is the one special method that automatically executes when the program is run. Example 6: method definitions

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Method Definitions And Calls 32 Defines the instructions that will execute when the start method executes

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Method Definitions And Calls 33 Method call: runs the animateRabbitMethod Method call: runs the animateTurtleMethod

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Method Parameters 34 Method parameters: the number entered by the user will be passed into each method.

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Method Definitions 35 Method parameter: the number passed in determines the distance moved and the number of rotations.

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Good Style (Creating Your Own Methods) 1. Each method should have one well defined task. If it doesn’t then it may be a sign that it should be decomposed into multiple sub-methods. a)Clear method: A method that converts lower case input to capitals. b)Ambiguous method: A method that prompts for a string and then converts that string to upper case.

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Good Style (Creating Your Own Methods): 2 2. (Related to the previous point). methods should have a self descriptive name: the name of the method should provide a clear indication to the reader what task is performed by the method. a)Good: isNum, isUpper, toUpper b)Bad: doIt, go 3. Try to avoid writing methods that are longer than one screen in size. a)Tracing methods that span multiple screens is more difficult. 37

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Good Style (Creating Your Own Methods): 3 4. The conventions for naming variables should also be applied in the naming of methods. a)Lower case characters only. b)With methods that are named using multiple words capitalize the first letter of each word but the first (most common approach) or use the underscore (less common).

© Jalal Kawash 2010Peeking into Computer Science Parameters Parameters of a method are like local variables in the method The difference is that a value must be assigned to parameters when a method is called 39

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Parameters Also you can think of parameters as a mechanism for communicating information into a method. 40 Start method Num = A value from the user Knight’s move method Pass value entered by user into ‘move’

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Parameter Passing 41 Example 7: Parameters Get value for distance from user in ‘start’ method Pass the distance into the move method as a parameter Now ‘distance’ can be used in the ‘move’ method

© Jalal Kawash 2010Peeking into Computer Science Functions Functions are methods that return values Mathematically, f(n) = 2n f(5) returns 10 42

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Useful Functions Of Class World 43

Do in order and Do together Sequential and parallel execution 44

© Jalal Kawash 2010Peeking into Computer Science Objectives At the end of this section, you will be able to: 1. Understand the difference between sequential and parallel execution 2. Make use of the Do in order and do together statements 3. Draw flowcharts for story lines with parallel and sequential actions

© Jalal Kawash 2010Peeking into Computer Science Sequential and Parallel Execution This we have created is sequential Straight-line code Finish one instruction before starting the next 46

© Jalal Kawash 2010Peeking into Computer Science Parallel Execution Two methods executed at the same time ◦In parallel, or concurrently The windmill blades roll concurrently with the rest of the movie actions 47 Cow turns her head while Trex was running

© Jalal Kawash 2010Peeking into Computer Science Do in order Default mode of execution Can group statements in a Do in order statement 48

© Jalal Kawash 2010Peeking into Computer Science Do together Can group statements in a Do together statement to run concurrently 49 Example 8: simultaneous actions

© Jalal Kawash 2010Peeking into Computer Science Intertwining in order and together 50

© Jalal Kawash 2010Peeking into Computer Science Flowcharts for Story Lines 51

© Jalal Kawash 2010Peeking into Computer Science Flowcharts for Story Lines 52

© Jalal Kawash 2010Peeking into Computer Science Do together and in order movie 53

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Flowcharts It’s a graphical notation that’s used to specify how a computer program will execute. Symbols: ◦Terminal (for the start and end of the program). ◦Processing (for a step in the program) Arrows are used to connect the symbols (show the order and direction of execution). Start/end Processing Start Instruction End

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Example Program: Flowchart Start/end Homer: move forward 1 meter End Homer: “Doh!” Bart: “Don’t have a cow man!”

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Flowchart (Do-Together) 56 Start/end Homer: move forward 1 meter End Homer: “Doh!” Bart: “Don’t have a cow man!” Encompasses all the instructions that execute at the same time (encompassed by the ‘do together’ container)

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Program Documentation English statements inserted into a computer program. They are not written in a programming language. They are for the reader of the program and won’t be executed by the computer. It describes ‘what’ the program does (but not ‘how’ it does it. ◦Correct: the program sorts contacts into alphabetical order ◦Incorrect: the program uses the quicksort algorithm.

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: Creating Program Documentation Create comments for the documentation graphically.

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: What Should Be In The Program Documentation What does the program do e.g., tax program. What are it’s capabilities e.g., it calculates personal or small business tax. What are it’s limitations e.g., it only follows Canadian tax laws and cannot be used in the US. In Canada it doesn’t calculate taxes for organizations with a yearly gross earnings over $1 billion. Author What is the version of the program

© Jalal Kawash 2010Peeking into Computer Science JT’s Extra: What Should Be In The Program Documentation (2) If you don’t use numbers for the different versions of your program then consider using dates (tie this with program features i.e., list the features of version ‘x’ of the program). ◦How does the program work. ◦This is often a description in English (or another high- level) language that describes the way in which the program operates. ◦The purpose of this description is to help the reader quickly understand how the program works. ◦Typically used to describe things that are not immediately self evident from the program code. For an example of a completely documented program see “ The Simpsons ” example program.