1 CS 106, Winter 2009 Class 5, Section 4 Slides by: Dr. Cynthia A. Brown,...

29
1 CS 106, Winter 2009 Class 5, Section 4 Slides by: Dr. Cynthia A. Brown, [email protected] Instructor section 4: Dr. Herbert G. Mayer, [email protected]
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    219
  • download

    0

Transcript of 1 CS 106, Winter 2009 Class 5, Section 4 Slides by: Dr. Cynthia A. Brown,...

1

CS 106, Winter 2009Class 5, Section 4

Slides by: Dr. Cynthia A. Brown, [email protected] section 4: Dr. Herbert G. Mayer, [email protected]

2

Creating a Specification

• Begin with developing use cases and tests. Identify what internal state the process will need to keep track of (requirements phase)

• List all the objects in the interface, and their events. Create a variable or other way of keeping track of internal state

• Create a flowchart for each event• Go through the tests using the flowcharts to

validate the specification

3

Implementation

• Once we have a good specification, the next step is implementation

• We will create VB projects to implement processes

• Running and testing the program will give an independent verification of our approach, plus we will have a working implementation of the process

4

VB Projects

• A VB project is more than just a single file of code

• VB has many great features that assist us in easily creating nice user interfaces that interact well with the Windows environment

• It automatically creates components that the operating system will need to run the program properly

5

VERY IMPORTANT!!!

• To be able to upload your project, you must create and save it in the proper way.

• The instructions are on the front page of the blackboard site. Read them before you start your first project, and follow them carefully.

• Be especially careful to follow the instructions for creating and saving your project.

6

Controls

• Controls are what VB calls the objects that can appear in the user interface

• Chapter 2.2 has a nice tutorial on the most common controls– Text box– Button– Label– List box

7

More controls

• We will also be interested in – Radio buttons– Check boxes– Timers

• There are plenty of other interesting ones (like menus) that we will look at if we have time

8

Let’s take a look…

• We are going to pause and take a look at the controls and how to work with them

9

Getting the code

• A zipped file containing a project like the one we just worked on is available on the blackboard site in the course contents area

• You can download it, right click, and use extract to unzip it

• Once it is unzipped you can open it. One way to do this is to open the controls.sln file and open the program from there

10

VB Code Editor Features

• Color codes keywords and comments (and text strings, as we’ll see later)

• Indents code nicely• Has a completion feature that suggests

allowable values • Has context-sensitive help, lots of other nice

features described in the book

11

Naming Controls

• Notice we have a naming convention for controls• The name starts with a three-letter abbreviation for

the type of control. For a button, it’s but. For a label lbl. For a timer, tmr. Etc.

• The rest of the name is descriptive of the use of the control. For example, butRed for the button that turns things red. In a longer name, capitalize each word. For example butRedWhiteBlue.

• Don’t be afraid to use long, descriptive names! They make your program much easier to use and understand.

12

Event Procedures

• To create an event procedure for a control, double click on it in the interface. The code will show up on the code page as an empty event procedure, which you can complete.

• For buttons, the event procedure will be invoked when the user clicks the button

13

Event Procedure Headers

• Each event procedure has a header that follows a standard format:

Private SubbutGreen_Click(ByVal sender AsSystem.Object, ByValeAsSystem.EventArgs) HandlesbutGreen.Click

• The blue words are keywords reserved for use by the system

• For now we will ignore most of the contents of the header

14

Using Comments

• Each program must start with a comment including the name of the author, the author’s section number, and the date, followed by a brief overall description of the project

• Each procedure must be preceded by a descriptive comment

• Format:‘**************************************‘ Comment here. Use more lines if you need to.‘**************************************

15

Text Box versus Label

• Use a text box when you want the user to enter some information, or the information in the box will be changed by the program

• Use a label for text that does not change during the execution of the program

16

Read the Book!

• There is a lot of information in the book, much of it of a rather detailed nature

• Read over it but don’t try to memorize it. You will learn the important things by using them a lot, and reading will give you a feeling for what is there so you can look it up when you need it

• Work some of the problems! Sit there at the computer with the book and try them. This is the best way to learn, even if you are not required to hand them in.

17

Break

10 minutes

18

Relating VB to Processes

• Our first step is to gather requirements by developing use cases and an interface design, and figuring out what internal state our process needs

• Once this is done, we can develop flow charts to specify the process

• Finally, we represent our objects by VB controls, and translate the event flow charts into code which can be tested using the tests

19

Internal State

• It’s reasonably clear how our objects and events will be represented in VB, but what about internal state?

• To represent any internal state the process needs to remember, we use variables

• Variables are containers for values. They are an abstract representation of computer memory

20

Physical Memory

• The physical memory in a computer is made up of tiny devices that have two distinct states

• These states are used to represent two values: 0 and 1

• Each such value is called a bit• The bits are grouped into “words” of 8, 16, or 32 bits

(other values are also possible)• Words are treated as a unit by parts of the hardware

21

Internal Representation

• One bit can have two values: 0 or 1• 2 bits have 4 values: 00, 01, 10, 11• 3 bits have 8 values: 000, 001, 010, 011, 100,

101, 110, 111• Each time you add a bit, you double the

number of values that can be represented

22

Eight Bits

• With 8 bits you can represent 28 = 256 values• It could be 256 characters (see Appendix A in

the book)• It could be 256 numbers, 0-255• Or we might want some negative numbers, so

it could be -127 to 128. • These are all the same 256 8 bit patterns: the

key is how we interpret them

23

Using information

• When our program uses information, it has to know what kind of information it is: number, character, character string, etc

• When we name our variables, we also declare them, giving the name a type which describes the kind of information we are storing

24

Numbers

• There are several useful internal representations of numbers

• The number of values that can be represented depends on the number of bits used

• The range of values depends on the encoding• We’ll use two main types: double and integer

25

Data Type Double

• The double type works on the same basis as scientific notation (3.14159 x 1030)

• Usually two words are used to store the mantissa (3.14159), hence the name double, and one word is used for the exponent (30)

• A number variable declared as double can hold very large or small numbers, but computations with it can lose accuracy

DimaVarAs Double

26

Data Type Integer

• Integers are usually stored in one or two words depending on the implementation

• They are used when we need to count something

• Typical names are i, j, m, n, count• They are always precise, but the range of

values is much smaller than with DoubleDimi, jas Integer

27

Numeric Operations

• Addition and subtraction: +, -• Multiplication: *• Versions of division: / (normal division), \ (integer

division), mod (integer division remainder)• Power: ^ where a^b means ab

• Built-in functions like Math.sqrt, Math.round, Int• Parentheses can be used to change precedence, as in

a*b +c versus a*(b+c)

28

Types of Results

• Adding two integers gives an integer, adding two doubles gives a double

• Dividing two integers using / gives a double!• Which is why we have \ and mod: 14 \ 4 = 3, and 14 mod 4 = 2• The exponential function always gives a

double as a result

29

Why???

… am I telling you these picayune details?

Because there are times when having Integer or Double correct makes your program work, or not.