05 – problem solving & data types

43
Mark Dixon Page 1 05 – Problem Solving & Data Types

Upload: paula-burton

Post on 30-Dec-2015

25 views

Category:

Documents


0 download

DESCRIPTION

05 – Problem Solving & Data Types. Questions: Expressions. What is the result of: 10 * Int(3.1973265765) How many functions are in the following: Int(12.472) * Sqr(9) + 8 / 2 How many operators are in the following: Int(12.472) * Sqr(9) + 8 / 2 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 05 – Problem Solving  & Data Types

Mark Dixon Page 1

05 – Problem Solving & Data Types

Page 2: 05 – Problem Solving  & Data Types

Mark Dixon Page 2

Questions: Expressionsa) What is the result of:

10 * Int(3.1973265765)

b) How many functions are in the following:

Int(12.472) * Sqr(9) + 8 / 2

c) How many operators are in the following:

Int(12.472) * Sqr(9) + 8 / 2

d) Write an expression to:

divide 15 by 3 and multiply the result by 6

30

2

3

(15 / 3) * 6

Page 3: 05 – Problem Solving  & Data Types

Mark Dixon Page 3

Can't touch this• Student work from last week:

based on work done by Daniel Thornton & David Orrock

Page 4: 05 – Problem Solving  & Data Types

Mark Dixon Page 4

Questions• name: event, object, property, event handler,

operator, function, expression

<script language="vbscript"> Sub window_onLoad() imgHammer.style.posLeft = 500 imgHammer.style.posTop = 100 imgHammer.style.height = 100 imgHammer.style.width = 75 sndPlayer.URL = "Hammer.wav" End Sub

Sub imgHammer_onMouseOver() imgHammer.style.posLeft = Rnd() * (document.body.clientWidth - imgHammer.width) imgHammer.style.posTop = Rnd() * (document.body.clientHeight - imgHammer.height) End Sub</script>

Page 5: 05 – Problem Solving  & Data Types

Mark Dixon Page 5

Session Aims & Objectives• Aims

– to introduce the idea of types of data– to provide a more explicit understanding of problem solving

skills and strategies• Objectives,

by end of this week’s sessions, you should be able to:

– recognise different types of data• numeric• string (text)

– correct errors relating to data types– recognise the key aspects of a problem

• start state• goal state• operations

– be able to use typical strategies to solve unfamiliar programming problems

Page 6: 05 – Problem Solving  & Data Types

Mark Dixon Page 6

Example: AddNum v1<html> <head><title>Add Numbers</title></head> <body> <input type="text" id="txtNum1" /><br /> <input type="text" id="txtNum2" /><br /> <input type="button" id="btnAdd" value="Add" /> <p id="lblResult"></p> </body></html>

<script language="vbscript"> Sub btnAdd_OnClick() lblResult.innerText = txtNum1.value + txtNum2.value End Sub</script>Doesn't work!

Page 7: 05 – Problem Solving  & Data Types

Mark Dixon Page 7

Types of Information• Numeric (numbers) 29 (integer/whole) 56.23 (real/decimal)

• String (text) "Hello there!""BOO"

• Pictures (numbers)

• Sound (numbers)

Page 8: 05 – Problem Solving  & Data Types

Mark Dixon Page 8

AddNum problem• The + operator works with:

– numbers, and

– text

23 + 16 39

"23" + "16" "2316"

double quotes enclose text

• Text input boxes store text: txtNum1.value + txtNum2.value

• We need to convert text to numbers

Page 9: 05 – Problem Solving  & Data Types

Mark Dixon Page 9

String Functions CInt("63") convert to integer result is 63

Left("boo",2) left string result is "bo"

Right("hello",3) right string result is "llo"

Mid("hello",2,3) middle string result is "ell"

Len("S Smith") length result is 7

Space(5) spaces result is " "

Page 10: 05 – Problem Solving  & Data Types

Mark Dixon Page 10

String Expressions

Page 11: 05 – Problem Solving  & Data Types

Mark Dixon Page 11

String Expressions & Errors "What is " & txtN1.value & " times "

"What is twice " txtN1.value & "?"

"What is 6 minus " & & txtN1.value & "?"

"This is a number & txtN1.value

dataoperator

data data

operator

ERROR! missing data

ERROR! missing operator

ERROR! missing "

Page 12: 05 – Problem Solving  & Data Types

Mark Dixon Page 12

Questions: String Expressionsa) What is the result of:

Mid("what is the time?", 3, 4)

b) What is the result of: Left("bob", 2) & Right("sal", 1)

c) Write an expression to:convert "16" to a number

d) Write an expression to:give the first two letters of "Mr John Smith"

"at i"

"bol"

CInt("16")

Left("Mr John Smith", 2)

Page 13: 05 – Problem Solving  & Data Types

Mark Dixon Page 13

Example: AddNum v2<html> <head><title>Add Numbers</title></head> <body> <input id="txtNum1" type="text" /><br /> <input id="txtNum2" type="text" /><br /> <input id="btnAdd" type="button" value="Add" /> <p id="lblResult"></p> </body></html>

<script language="vbscript"> Sub btnAdd_OnClick() lblResult.innerText = CInt(txtNum1.value) + CInt(txtNum2.value) End Sub</script>

Page 14: 05 – Problem Solving  & Data Types

Mark Dixon Page 14

Nested functions• nested functions (one inside another): Right(Left("Hello there", 5), 2)

• do what is in the brackets first

Right(Left("Hello there", 5), 2)

= Right( "Hello" , 2)

= "lo"

Page 15: 05 – Problem Solving  & Data Types

Mark Dixon Page 15

Types of problem• two types of problem:

– Known problems:which we have successfully dealt with before, and can remember the solution

– Unknown problems:which we have never seen before, andtherefore have to discover / invent a solution for ourselves

Page 16: 05 – Problem Solving  & Data Types

Mark Dixon Page 16

What is a problem?• All problems different

• However, have key parts:– Start state– Goal state– set of available operations

• Problem solving is the process of searching for a sequence of operations that will take us from the start state to the goal state

Page 17: 05 – Problem Solving  & Data Types

Mark Dixon Page 17

Example: Light• Start state: light is off• Goal state: light on• Set of operations:

– Push switch up (turns light on)– Push switch down (turns light off)

• Solution:1. Push switch up

Simple problems – small number of operations to solve

Page 18: 05 – Problem Solving  & Data Types

Mark Dixon Page 18

Important steps• It is essential to understand the problem (start

state, goal state, and operations) prior to finding a solution

• It is also often essential to be able to break a problem down into smaller sub-problems, i.e. identify intermediate sub-goal states

• failure to solve a problem is often due to these

Page 19: 05 – Problem Solving  & Data Types

Mark Dixon Page 19

More Complex Problems• More complicated problems involve

– the use of multiple operations to get from the start to the goal state

– conditional execution of operations only do this if… (future lecture)

– repeated operations do this until… (future lecture)

– abstraction – more general description

Page 20: 05 – Problem Solving  & Data Types

Mark Dixon Page 20

Example: Horizontal mid point• Start state: an image on the screen• Goal state: to calculate its horizontal mid point• Set of operations:

– posLeft: gives the position of its left edge– posTop: gives the position of its top edge– Width: gives the distance between its left and right edges– Height: gives the distance between it top and bottom edges

• Solution:1. get the posLeft value2. add half the Width

Page 21: 05 – Problem Solving  & Data Types

Mark Dixon Page 21

Example: First Character• Start state: the piece of text (e.g. "Hello")• Goal state: extract the first character of a

piece of text• Set of operations:

– Len(s): gives the number of characters in s– Right(s, n): gives n characters from the right of s– Left(s, n): gives n characters from the left of s– Mid(s, p, n): gives n characters from s, starting at

position p

• Solution:1. Left(text, 1)

Page 22: 05 – Problem Solving  & Data Types

Mark Dixon Page 22

• Goal state

• Start state

• Operations– Move Up– Move Down– Move Left– Move Right

• Solution:

Example: Movement

1. Move Up2. Move Up3. Move Up4. Move Right5. Move Right6. Move Right

1. Move Up2. Move Right3. Move Up4. Move Right5. Move Up6. Move Right

or

Page 23: 05 – Problem Solving  & Data Types

Mark Dixon Page 23

Concrete vs. Abstract problems• People can solve concrete problems easily

– what is the first letter of hello– what is the first letter of you surname– an object's left edge is at position 100

the object is 50 widewhere is its mid point?

• It is often difficult for them to describe the general (abstract) process they use

Page 24: 05 – Problem Solving  & Data Types

Mark Dixon Page 24

Concrete vs. Abstract code• we have:

– posLeft– posTop

• right = left + width

• middle = left + (width / 2)

• concrete code (works for window 800px wide): picBall.style.posLeft = 400

• vs. abstract code (works for all window widths): picBall.style.posLeft = document.body.clientWidth / 2

Page 25: 05 – Problem Solving  & Data Types

Mark Dixon Page 25

Humans vs. Computers• Humans and Computers work very differently• Humans

– declarative (goals): flexible sequence– intelligent: adaptive, questioning, rational– instinctive (without conscious thinking)– easily deal with incomplete and incorrect data– error prone (especially mundane repetitive tasks)

• Computers– procedural / algorithmic: fixed sequence– do exactly what they are told– cannot deal with errors– no imagination or creativity

Page 26: 05 – Problem Solving  & Data Types

Mark Dixon Page 26

Algorithms• algorithm: step-by-step sequence of

instructions, to solve a problem

• it describes how input data is to be processed in order to produce a desired output

• similar to recipe– ingredients (similar to data)– method (is a type of algorithm)

Page 27: 05 – Problem Solving  & Data Types

Mark Dixon Page 27

Algorithms• Making a cup of tea:

1. Fill the kettle with water 2. Plug the kettle in 3. Switch the kettle on 4. Wait for the kettle to boil 5. Put water in cup 6. Put a tea bag into the cup 7. Add sugar to the cup 8. Add milk to the cup 9. Stir 10. Take the tea bag out

Page 28: 05 – Problem Solving  & Data Types

Mark Dixon Page 28

What vs. How• what vs. how:

– What: increase value of a text box by 1– How:

• read the current value• add 1• put the result back in the text box

• For example: swap, search, sort

Page 29: 05 – Problem Solving  & Data Types

Mark Dixon Page 29

Example: Swap v1

<html> <head><title>Swap</title></head> <body> <input id="txtA" type="text" value="Bob" /> <input id="txtB" type="text" value="Sally" /> <input id="btnSwap" type="button" value="Swap" /> </body></html>

<script language="vbscript"> Sub btnSwap_onClick() txtA.value = txtB.value txtB.value = txtA.value End Sub</script>

• put txtB into txtA• put txtA into txtB

• does not work!

Page 30: 05 – Problem Solving  & Data Types

Mark Dixon Page 30

Example: Swap v1 (why?)

Page 31: 05 – Problem Solving  & Data Types

Mark Dixon Page 31

Example: Swap v2<html> <head><title>Swap</title></head> <body> <input id="txtA" type="text" value="Bob" /> <input id="txtB" type="text" value="Sally" /> <input id="btnSwap" type="button" value="Swap" /> <input id="txtTemp" type="text" disabled="disabled" /> </body></html>

<script language="vbscript"> Sub btnSwap_onClick() txtTemp.value = txtA.value txtA.value = txtB.value txtB.value = txtTemp.value End Sub</script>

• put txtA into temp• put txtB into txtA• put temp into txtB

• works!

Page 32: 05 – Problem Solving  & Data Types

Mark Dixon Page 32

Example: Swap v2 (Why?)

Page 33: 05 – Problem Solving  & Data Types

Mark Dixon Page 33

The Empty String• Two double quotes (no space between)

""

• Used to clear contents:

txtNum.value = ""

Page 34: 05 – Problem Solving  & Data Types

Mark Dixon Page 34

Example: Student Loan (Analysis)

• What: Calculate annual student load repayment from salary

• How:

• Algorithm:– read annual salary– deduct £15000– calculate 9%– display result

Page 35: 05 – Problem Solving  & Data Types

Mark Dixon Page 35

Example: Student Loan (Design)• When Calculate button

is clicked:– read annual salary text

box– deduct £15000– calculate 9%– put result in paragraph

• Test Data: Input Process Output

– £15000 15000-15000*0.09 = £0– £16000 16000-15000*0.09 = £90

Page 36: 05 – Problem Solving  & Data Types

Mark Dixon Page 36

Generating Assignment Code• put "Hello" into txtA

txtA.value = "Hello"

• get txtA and join it with txtB, and put the result in parRes parRes.innerText = txtA.value + txtB.value

• put into txtNum2, the result of multiplying txtNum1 by 2

txtNum2.innerText = txtNum1.value * 2

Page 37: 05 – Problem Solving  & Data Types

Mark Dixon Page 37

Questions: Assignment• What is the code for:

– put 0 into the posLeft property of picHouse

– increase the posTop property of picHouse by 5

– decrease the posTop property of picCar by 9

picHouse.style.posLeft = 0

picHouse.style.posTop = picHouse.style.posTop + 5

picCar.style.posTop = picCar.style.posTop - 9

Page 38: 05 – Problem Solving  & Data Types

Mark Dixon Page 38

Errors• txtTemp.value = "" + 5

type mismatch (cannot add "" to 5)

• txtTemp.value = "7" + 5 OK – VB converts "7" into 7 automatically

• txtTemp.value = 7 + 5 OK – 7 plus 5 is 12

• value.txtTemp = 7 + 5 object required 'value' (object comes first)

Page 39: 05 – Problem Solving  & Data Types

Mark Dixon Page 39

Example: Text Shift<html> <head><title>Text Shift</title></head> <body> <p id="parH"></p> </body></html>

<script language="vbscript"> Sub window_onLoad() parH.innerText = "Hello There" & Space(100) window.setInterval "TextShift()", 50 End Sub Sub TextShift() parH.innerText = Mid(parH.innerText,2) & Left(parH.innerText,1) End Sub</script>

Page 40: 05 – Problem Solving  & Data Types

Mark Dixon Page 40

Tutorial Exercises: AddNum• LEARNING OBJECTIVE:

use a function to convert string (text) to integer (number) data

• Task 1: get the addnum examples (v1 and v2) working

Page 41: 05 – Problem Solving  & Data Types

Mark Dixon Page 41

Tutorial Exercises: Swap• LEARNING OBJECTIVE:

use an algorithm to solve a problem

• Task 1: get the swap examples (v1 and v2) working• Task 2: change v2 of the swap page so that the temporary text

box is hidden

Page 42: 05 – Problem Solving  & Data Types

Mark Dixon Page 42

Tutorial Exercises: Student Loan• LEARNING OBJECTIVE:

implement an algorithm in code

• Task 1: Create the user interface (page design) for the Student Loan example (from the lecture), using HTML tags (you will need a text box, a button, and a paragraph).

• Task 2: Add code that implements the following algorithm:When the Calculate button is clicked:– read annual salary text box– deduct £15000– calculate 9%– put result in paragraph

• Task 3: Modify your program so that it calculates and displays monthly income and repayment amounts (as well an annual).

Page 43: 05 – Problem Solving  & Data Types

Mark Dixon Page 43

Tutorial Exercises: Text Shift• LEARNING OBJECTIVE:

develop and implement an algorithm to solve a problemuse string manipulation functions, and sound

• Task 1: get the Text Shift example (from the lecture) working

• Task 2: modify your program so that the text goes the other way.

• Task 3: modify your program so that a noise is made when the user moves the mouse over the text.