Review of Last Week - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Review of Last Week

Description:

Review of Last Week. Last week you will have worked through the Electricity Bill ' ... You will devise pseudo code and VB 'code' for making a cup of tea. Overview ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 29
Provided by: cseD1
Category:
Tags: last | review | week

less

Transcript and Presenter's Notes

Title: Review of Last Week


1
Review of Last Week
  • Last week you will have worked through the
    Electricity Bill Mini Project. By working
    through this material you will have been given a
    chance to apply all of the skills learnt during
    the first six weeks of the module. You should
    have created an application that has a suitable
    range of controls with correctly set
    properties.You will have created code that does
    some processing of data and displays the
    result.The program should be validated and
    crash proof.

2
Overview of This Week Lab 1
  • In the first lab you will be introduced to the
    idea of pseudo code and stepwise refinement.
  • These techniques may prove helpful in developing
    your code.
  • You will devise pseudo code and VB code for
    making a cup of tea.

3
Overview
  • Preview of Next Week
  • Next week you will be introduced to using loops
    and list boxes.
  • Reading for this week
  • Tutorial 11
  • Other Comments
  • Remember you need to spend six hours a week of
    your own time working on this module.
  • Additional Work (Should you complete the set
    exercises!)

4
Lab 1 Introductory Quiz
  • 1. List five events that may possibly be used in
    a VB application.

5
Lab 1 Introductory Quiz
  • 2. What is wrong with the following lines of
    code?
  • a.
  • MyVariable txtInput
  • b.
  • Dim MyName as Integer
  • MyName Bill
  • c.
  • reads in the first name from a text box
  • txtFirstName.Text FirstName
  • d.
  • should show an error if the value is between 3
    and 10
  • AValue txtAValue.Text
  • If AValue lt 3 And AValue gt10 Then
  • MessageBox.Show(There was an error)
  • End If

6
Pseudo Code
  • Possibly one of the hardest skills to get to grip
    with in learning a programming language is
    developing the ability to take a problem and
    break it down into a sequence of programming
    commands.
  • If the task is simple there usually isnt too
    much of a problem, for example

7
Pseudo Code
  • Problem
  • Declare a variable to store the number of
    students in a class.
  • Solution
  • Dim StudentCount as Integer
  • The above problem is fairly simple and so is the
    solution.

8
Pseudo Code
  • However the more complex the problem, the harder
    it is to formulate a solution. For example
  • Devise a program that accepts monetary values
    from payments. The payments represent part
    payment of money repaid against multiple loans.
    As the repayments come in, the oldest loan should
    be paid off first. Once the oldest loan is paid
    off the next oldest will be paid off, and so on
    until the most recent loan is paid. Any loans
    not paid off within an agreed time will require a
    reminder letter to be generated to the customer.
    After two reminders legal action will be taken.
  • This scenario is based on a real life business
    problem which required a code based solution.

9
Pseudo Code
  • Why is it more difficult to code compared to the
    first example?
  • Some reasons are
  • The problem is complex.
  • There is no obvious 1 to 1 relationship between
    the problem and the code.
  • The terminology of the problem is not always
    clear on the first read and will almost certainly
    need clarification.
  • You may not be sufficiently familiar with the
    programming language to know where to begin.

10
Pseudo Code
  • The issue is that as programmers we cant simply
    only take on the easy problems.
  • Nearly every real system you write will have
    some element of complexity which may not have
    been immediately obvious when the system was
    proposed.
  • So if a problem is complex, how can we deal with
    it?

11
Pseudo Code
  • The principle is simple
  • If a problem is complicated break it down into
    smaller simpler problems.
  • More importantly, keep breaking down the problems
    until the logic of the problem becomes apparent
    and code may be generated.

12
Pseudo Code - Problem
  • Write a section of code that reads in a users
    name and age from a form. The messages You are
    21 or older or You are younger than 21 should
    be displayed.
  • To write this problem as Pseudo code you would
    need to think logically about the steps required
    to solve the problem.

13
Pseudo Code Solution 1
  • The steps might be something like
  • read in the name from the form
  • read in the age from the form
  • if the age is 21 or over display a message
  • if the age is less than 21 display another
    message
  • Notice the above steps have been written as VB
    comments.

14
Pseudo Code
  • There are two nice things about Pseudo code.
  • The pseudo code often becomes your comments.
  • The pseudo code is not tied to any one language.
    The logic above could be expressed in any number
    of languages, not just VB.

15
Pseudo Code - Problem
  • So having taken our problem and expressed it as
    a sequence of logical steps, the next question to
    ask is
  • Can I write this as code?
  • In the above example, the answer is almost.
  • The question to consider is can we express each
    step using programming constructs.

16
Pseudo Code - Problem
  • There are certain things our steps dont tell us.
  • The names of any controls used.
  • The names / data types of any variables required.

17
Pseudo Code - Problem
  • So a slight tweak of the steps might be.
  • declare some variables to store our data
  • read in the name from the form
  • read in the age from the form
  • if the age is 21 or over display a message
  • if the age is less than 21 display another
    message
  • Once the steps look like it could be code, we can
    write the code to solve the problem.

18
Pseudo Code - Problem
  • declare some variable to store our data
  • Dim YourName as String
  • Dim YourAge as Byte
  • read in the name from the form
  • YourName txtYourName.Text
  • read in the age from the form
  • YourAge txtYourAge.Text
  • if the age is 21 or over display a message
  • If YourAge gt 21 Then
  • MessageBox.Show(You are 21 or older)
  • End If
  • if the age is less than 21 display another
    message
  • If YourAge lt21 Then
  • MessageBox.Show(You are younger than 21)
  • End If

19
Pseudo Code - Problem
  • Spend a few minutes modifying the steps
  • declare some variables to store our data
  • read in the name from the form
  • read in the age from the form
  • if the age is 21 or over display a message
  • if the age is less than 21 display another
    message
  • To include some level of validation on the age in
    case a none numeric value is input.

20
Pseudo Code - Problem
  • The steps may look something like
  • declare some variables to store our data
  • if the age on the form is a number
  • read in the name from the form
  • read in the age from the form
  • if the age is 21 or over display a message
  • if the age is less than 21 display another
    message
  • otherwise display an error
  • Try writing the code for the above steps.

21
Pseudo Code - Problem
  • You should get something like this
  • declare some variables to store our data
  • Dim YourName as String
  • Dim YourAge as Byte
  • if the age on the form is a number
  • If IsNumeric(txtYourAge.Text) Then
  • read in the name from the form
  • YourName txtYourName.Text
  • read in the age from the form
  • YourAge txtYourAge.Text

22
Pseudo Code - Problem
  • if the age is 21 or over display a message
  • If YourAge gt 21 Then
  • MessageBox.Show(You are 21 or older)
  • End If
  • if the age is less than 21 display another
    message
  • If YourAge lt21 Then
  • MessageBox.Show(You are younger than 21)
  • End If
  • Else
  • otherwise display an error
  • MessageBox.Show(The age is not a valid number)
  • End If

23
Pseudo Code - Problem
  • One thing to notice is how the steps are indented
    for ease of reading. VB.NET is very kind to you
    with indentation as it does it all for you. When
    writing Pseudo code you need to indent yourself.
  • The advantage is that the code is easier to read.

24
Pseudo Code - Problem
  • Compare
  • If IsNumeric(ADay) Then
  • If ADay6 Then
  • MessageBox.Show(Today is Friday)
  • Else
  • MessageBox.Show(It is another day)
  • End IF
  • Else
  • MessageBox.Show(The day is not a number)
  • End IF

25
Pseudo Code - Problem
  • With
  • If IsNumeric(ADay) Then
  • If ADay6 Then
  • MessageBox.Show(Today is Friday)
  • Else
  • MessageBox.Show(It is another day)
  • End IF
  • Else
  • MessageBox.Show(The day is not a number)
  • End IF

26
Pseudo Code - Problem
27
Try writing the pseudo code for the following
problem.
  • Write a design to determine a students mark,
    using the following table.
  • 0 to lt40 Fail
  • gt40 to lt50 Pass
  • gt50 to lt60 Lower 2nd
  • gt60 to lt70 Upper 2nd
  • gt70 to 100 First
  • Generate only the pseudo code for the problem.

28
If mark on form is a number
If mark is less than 40
display fail message
Otherwise if mark is less than 50
display pass message
Otherwise if mark is less than 60
display Lower Second message
Otherwise if mark is less than 70
display Upper Second message
Otherwise display First message
Otherwise Display message a mark is not a number
Write a Comment
User Comments (0)
About PowerShow.com