Object Oriented Programming Lecture 1 Introduction to Programming

Object Oriented Programming Lecture 1 Introduction to Programming Concepts Dr. Msury Mahunnah e-mail: msury. mahunnah@ifm. ac. tz course website: https: //mahunnah. wordpress. com/

Basic Information • Contacts: Block A, Office # 604 – request appointment by e-mail msury. mahunnah@ifm. ac. tz

Basic Information : • Meeting times and locations – Lectures: Thursdays at 10. 00 in Lab 2 – Tutorials: Thursdays at 13. 00 in Lab 3

Sub-enabling outcomes • Describe elements of Object Oriented Programming. • Differentiate between Procedural Programming and Object Oriented Programming. • Design a program that communicates with multiple applications.

References • Weisfeld, M. (2013). The Object-Oriented Thought Process, 4 th. Edition. Addison-Wesley Professional. • Farell, J. (2012). An Object-Oriented Approach to Programming Logic and Design, 4 th. Edition. Cengage Learning. • Wu, C. (2009). An Introduction to Object-Oriented Programming with Java, 5 th. Edition. Mc. Graw-Hill Science/Engineering/Math

ASSESSMEMT • • Written Class Test – Week 5 – 20% Practical Lab Test – Week 10 – 10% 5 Random Homework Exercises – 10% Written Semester Exam – Week 14 – 60%

Course policies • Students’ responsibilities – Attend all sessions – Do the exam, all the exercises, and the test • Collaboration – Encouraged for exercises – Not allowed during tests and exams

Course policies • Switch off or put your cell phone under vibration • You are free to go out during class session but without disturbing others • During lab or class session DO NOT: – – Make or receive phone calls Send or receive text messages Eat or chew anything Make noise or disturb others

Special needs • Accommodation for any type of physical or learning disability – Contact me to discuss on necessary modifications.

Flowcharts

What is a Flowchart? • A flowchart is a diagram that depicts the “flow” of a program. • The figure shown here is a flowchart for the pay-calculating program. START Display message “How many hours did you work? ” Read Hours Display message “How much do you get paid per hour? ” Read Pay. Rate Multiply Hours by Pay. Rate. Store result in Gross. Pay. Display Gross. Pay END

Basic Flowchart Symbols • Terminals START Display message “How many hours did you work? ” Read Hours – represented by rounded rectangles – indicate a starting or ending point Display message “How much do you get paid per hour? ” Read Pay. Rate Multiply Hours by Pay. Rate. Store result in Gross. Pay. START Display Gross. Pay END Terminal

Basic Flowchart Symbols • Input/Output Operations – represented by parallelograms – indicate an input or output operation START Display message “How many hours did you work? ” Read Hours Display message “How much do you get paid per hour? ” Read Pay. Rate Display message “How many hours did you work? ” Multiply Hours by Pay. Rate. Store result in Gross. Pay. Read Hours Display Gross. Pay END Input/Output Operation

START Basic Flowchart Symbols Display message “How many hours did you work? ” Read Hours • Processes Display message “How much do you get paid per hour? ” – represented by rectangles – indicates a process such as a mathematical computation or variable assignment Read Pay. Rate Process Multiply Hours by Pay. Rate. Store result in Gross. Pay. Display Gross. Pay END

Four Flowchart Structures • • Sequence Decision Repetition Case

Sequence Structure • A series of actions are performed in sequence • The pay-calculating example was a sequence flowchart.

Decision Structure • The flowchart segment below shows how a decision structure is expressed in Scratch as an if/else statement. Flowchart NO Scratch Code YES x < y? Calculate a as x plus y. Calculate a as x times 2.

Decision Structure • The flowchart segment below shows a decision structure with only one action to perform. It is expressed as an if statement in Scratch code. Flowchart NO Scratch Code YES x < y? Calculate a as x times 2.

Repetition Structure • The flowchart segment below shows a repetition structure expressed in Scratch as a while loop. Scratch Code Flowchart x < y? OR YES Add 1 to x

Controlling a Repetition Structure • • • The action performed by a repetition structure must eventually cause the loop to terminate. Otherwise, an infinite loop is created. In this flowchart segment, x is never changed. Once the loop starts, it will never end. QUESTION: How can this flowchart be modified so it is no longer an infinite loop? YES x < y? Display x

Controlling a Repetition Structure • ANSWER: By adding an action within the repetition that changes the value of x. YES x < y? Display x Add 1 to x

Case Structure If years_employed = 2, bonus is set to 200 If years_employed = 1, bonus is set to 100 1 bonus = 100 If years_employed = 3, bonus is set to 400 If years_employed is any other value, bonus is set to 800 CASE years_employed 2 3 bonus = 200 bonus = 400 Other bonus = 800

Connectors • The “A” connector indicates that the second flowchart segment begins where the first segment ends. START A END A

Modules • The position of the module symbol indicates the point the module is executed. START Read Input. • A separate flowchart can be constructed for the module. Call calc_pay function. Display results. END

Combining Structures • This flowchart segment shows two decision structures combined. NO YES x > min? Display “x is outside the limits. ” NO YES x < max? Display “x is outside the limits. ” Display “x is within limits. ”




I/O , variables, simple computing

The SENSING menu Getting information from the user or some other machine

Asking the age of the user • Program wants age of user (maybe to set a level of difficulty in a game) • Program asks user for age • User types in age • Program stores the answer in a variable named “answer”

Use the SENSING menu 1) Ask the user for age; 2) User types in age; 3) Result stored in “answer”

User types “ 13” and the script stores the answer “answer” is a Special Scratch variable

OUTPUT: Giving the user information with say answer Say is in Looks menu; drag “answer” from Sensing menu; click

Using the Variables menu We can save an answer in our own named variable; Then we can program with many data items using many variables

What is your age • • • Depends on who you are For one person it’s 12 For another person it’s 27 For yet another, it’s 19 IT’S VARIABLE!

Making an “age variable” 1) Click Variables menu; 2) Click “Make a variable” 3) Type in “age” and click OK Do you want to display for all sprites or just one?

We have an “age variable” Operations for “age” Displaying variable “age”

We can join text and a number to say things From Looks From Operators From Sensing Make the join Build this instruction from the parts above it.

Storing a value in a variable; then saying it to the user 1) Set from Variables menu; 2) Say from Looks menu; 3) Join from the Operators menu joins your text with the “answer”

Using variables in programs • • • A script might have to remember information How far is the cat from the dog? How fast is the rocket moving? Where did the user click on the website? What is my score in the game? What is the user’s skill level?

Computing C = A + B (1 of 3) • • • Make 3 variables, A, B, and C Type in the values of A and B Set the value of C to the sum of A and B

Computing C = A + B (2 of 3) • • • Make 3 variables, A, B, and C Type in the values of A and B Set the value of C to the sum of A and B

Computing C = A + B (3 of 3) • • • Make 3 variables, A, B, and C Type in the values of A and B Set the value of C to the sum of A and B Drag + operator from Operators menu From Variables menu drag variables A and B into the slots

Algorithm for computing the average of two numbers • • Let the first number be A; say A=12 And the second number be B; say B=15 To compute the average _______ So the average of 12 and 15 = ____

Programming the average A is the first “variable”: A = _____ B is the second “variable”: B = _____ V is the third variable: V = _____ Take the value of A, add the value of B, divide this sum by 2, then set the value of V to this answer. • V = (A+B)/2 (how to do it in Scratch? ) • •

Script from Variables and Operators menus Drag the variable names into the slots of the operators Script not yet executed A formula from the Operators menu

After clicking to execute script The variable V now stores the computed average of A and B

Exercise: average program • • • Modify the average program Ask the user for a value of A Ask the user for a value of B Compute V as the average Report the average to the user Run the program for different As and Bs

Computing perimeter of a rectangle: algorithm • • Ask the user for length L Ask the user for width W Compute P = (L+W)*2 Report P to the user W = 10 example L=15 P = (15+10)*2 = 50

Review: Getting length from user Drag from Sensing menu • Ask “What’s the length” and wait • Set length to answer Create this variable Drag from sensing menu

Review: Reporting the value of a variable to the user Looks menu You type this in • Say join “length =“ length for 2 sec Operators menu Drag length variable from Variables menu

Review: Program with user input and output

Exercise: create the Scratch program we designed • • Ask the user for length L Ask the user for width W Compute P = (L+W)*2 Report P to the user

If-then-else statements and interactions

If Statement • If you are 20, raise your hand • If you like soccer, raise your hand

If Statement Programming View • If ( you are 20) { raise your hand } • If ( you like soccer) { raise your hand }

If Statement Scratch View • The If Statement – Located in the Control Menu True or false test Statements to do only if test is true

If Statement Practice • • Let’s create an If Statement Create a variable A Set A to 0 Create an If Statement If A > 2 Perform some Motion Change A to 3

If Else Statement • If you like soccer, raise your hand Else stand up • If you are 20, raise your left hand Else raise your right hand

If Else Statement • If ( you are 20 ) { raise your right hand } Else { raise your left hand }

If Else Statement Practice • Loop forever • If the mouse is clicked – Move 10 steps • Else – Turn 15 degrees
- Slides: 62