chapter 1 · exercise vi - draw a flowchart that ... ii- write a pseudo code to read the ages of (...

25
-1-

Upload: tranquynh

Post on 06-May-2018

251 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-1-

   

Page 2: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-2-

Chapter 1 An Introduction to Programming

1- Information system:

Definition of information system: It iIs the combination of technology (computers) and people that enable an organization to collect data, store them, and transform them into information.

The difference between data and information 1- Data:

a. Raw facts that collected and stored by the information system b. Can be numbers, letters, images, sound clips, or video clips. Names, dates, prices,

temperatures, and grades in an examination c. Are not very meaningful d. Convert into information by software

2- Information: a. Information is meaningful

Definition of Software: Composed of on or more lists of instructions called programs, and the process of creating these lists of instructions is termed programming.

2- Computer Operations: The six operations that a computer can are:

1- Input data (reading data) 2- Store data in internal memory 3- Perform arithmetic operation on data 4- Compare two values and select one of two alternative actions 5- Repeat a group of actions any number of times 6- Output the results of processing

Page 3: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-3-

The six operations are shown in the following figure:

3- Programs and Programming:

Definition of Programming : Is a form of problem solving which aim is to develop a step by step process (logic) to solve the problem.

4- Programming Languages: • Computers understand one thing, machine language, which is entirely composed of zeroes

and ones. • Programming languages like BASIC enable people to write programs in English-like

languages • Many kinds of programming languages, including FORTAN, COBOL, C++, C#, JGAVA,

and BASIC • Change between English language and machine language by interpreter or compilers.

1- Interpreted the high-level language statements are converted into equivalent machine-language statements one at a time as the program is executed.

2- Interpreted program is slower than executing a compiled, 3- Compiler :The entire program is translated into machine language before any attempt

to execute it. 4- Compiled program will run faster than an interpreted program once all errors have

been found and corrected.

By keyboard or mouse

like addition, subtraction,

multiplication, division

choice between two sets of instructions to execute.

Displayed on a monitor, printed on paper, stored

Page 4: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-4-

Chapter 2 Flow Charting

The Five Steps To Solve Problems :

1- Define the problem 2- Assemble know quantities and assign variable names 3- Discards unimportant data 4- Establish relationships and express them as equations 5- Determine the proper algorithm be arranging the equations in correct sequence

The Five Basic Flowchart Symbols Are:

Terminal block Input / Output ( Start / End ) Process block Decision block

Flow Line

Advantages ( Benefit ) Of Using Flowchart:

1- Communication: good communicate the logic of system to all concerned 2- Effective analysis: problem can be analyzed in more effective way 3- Proper documentation; serve as a good program documentation 4- Efficient coding : flowcharts act as guide during the system’s analysis and program development 5- Proper debugging : helps in debugging process 6- Efficient program maintenance : maintenance the program become easy

Example On Flowcharting :

Study will the exercises 2.1 , 2.2 , 2.3 , 2.4 , 2.5and 2.6 From page 19 to 24

Page 5: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-5-

Example I - Draw a flow chart to find largest of three numbers ( A, B and C ) ?

Answer

Yes No Yes Yes

Start

READ A , B , C

Print B Print C Print A

is B>C ?

is A>B ?

is A>C

No No

END

Page 6: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-6-

Example II : Ahlia company desires to generate its payroll via computer . The information collected that

every payroll period includes the employee social security number , pay rate , and working hours . The

payroll department would like a report listing these items . Add to this , overtime pay , gross pay , tax

withheld ( 20 % of gross Pay ) , and net pay . Note that the employee should be given overtime pay 1-5 of

pay rate in case that he worked a number of hours greater than 150 hours .

Answer SSN Social security الرقم القومى

PR Pay Rate االجر فى الساعةWH Working hours عدد ساعات العملGP Gross pay اجمالى المستحقOP Overtime pay األجر االضافى

TAX Tax withheld الضرائبNP Net pay صافى المستحق

Stop

TRUE

Start

Input SSN W H < PR

is WH >

OP = ( WH – 150 ) * 1.5 * PR

GP = PR * W H

GP = 150 * PR + OP

TAX = GP* 20

NP = GP – TAX

Print a line for h l

Any more Employee

FALS

TRUE

FALS

Page 7: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-7-

Exercise III- Donia drew flowchart to add two numbers and printed the result as flows

correct the flowchart ? Answer

Correct flowchart Wrong flowchart

Start Start Read number 1 Result = number 1 + number 2 Read number 2 Read number 1 Result = number 1 + number 2 Read number 2 Output the result Output the result

END END

Page 8: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-8-

Exercise IV- Draw a flowchart to read the age of ( Hany ) and ( Hesham ) then it prints the name of the older ? Answer

Start Read Age of Hany Read Age of Hisham

Is Hany older

than Hisham yes No

Is Hisham older yes Print ( Hisham ) Hany No

Out put the name of both of them END

Print ( Hany )

Page 9: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-9-

Exercise VI - Draw a flowchart that reads a temperature in Fahrenheit degrees and convert it into Celsius degrees, using the formula ْC=5/9( Fْ-32)

Answer:

Start

A = ( F – 32 )

Read F

C = 5/9 *A

Print C

END

Page 10: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-10-

Chapter 3

Pseudo code

Definitions: 1- Pseudo code is statement show the steps required to solve problems 2- Pseudo code means “false code” 3- Pseudo code write as close as English language 4- Pseudo code consists of: a- words b- clauses c- sentences 5- Pseudo code is flexible and easy to understand and use name of variables describe what the variable represents. 6- Pseudo code consists of number of structures :

a- Sequence : statement are done is sequence b- Selection : in the case an IF statement is represent there can be only two

outcomes related to ( True or False ) IF…THEN…ELSE MAIN STRUCTURE c- Iterations: redo the certain action number of times or until the certain condition is met

Advantage of Pseudo code: 1- Simple : because it uses English 2- No special symbols are used 3- No specified syntax is used 4- Easy to translate its statements

Disadvantage of Pseudo code:: It can be lengthy for complex problems

If condition bening tested is true Then Do all statements listed under “ then “ ( executed when the tested condition is “true”) Else Do all statements listed under “ else “ ( executed when the tested condition is “False”)

Page 11: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-11-

Important Notices : 1- Terms : المتغير 2- Increment : اضف 3- Read , Input : ادخال 4- Print , Output : النتيجة 5- Initialize sum : ادخل قيمة متغيرة 6- If statement : اختيار بين احتمالين

Examples

1- Write a pseudo code to represent the process of reading two numbers dividing them , and then displaying the result ?

Answer 1- Input the first number . 2- Input the second number . 3- If the value of number 1 = that of number 2 then print " Division is impossible because number 2 is zero " Got to step 4 Else

Result = number 1 / number 2 Print number 1 , number 2 , and result

END If 4- Stop processing .

2- Write a pseudo code to find the sum of first 100 natural numbers . Find the sum where sum is given by sum = ( 1 + 2 + 3 + ……………………….. + 99 + 100 ) Answer 1- Initialize the sum

sum = 0 2- Initialize the term number N , which is the term its elf

N = 0 3- Increment the term number

N = N + 1 4- Add the new term to sum

Sum = sum + N 5- If N = 100 then Go to step 6 Else Perform step 3 through 4 until N is equal to 100 END If 6- Print an output line showing the sum of the first 100 terms of the series . 7- Stop processing .

Page 12: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-12-

3- Write a pseudo code to find the sum of first 25 odd natural numbers Sum= 1+3+5+………………..up to 25 odd numbers Answer

1- Initialize the sum Sum=0

2- initialize the number N and first term T : N=0 T=1 3- Add the new term to sum Sum = sum + T Increment the number of terms by 1 , and term value by 2:

N= N + 1 T = T + 2

5- IF N = 25 Then Go to step 6

Else Go to step 3 End If 6-Print an output line showing the sum of the first 25 odd term of the series 7-Stop processing

4- Write a pseudo code to find the largest of three numbers A , B , and C ?

Answer 1- Input the values of numbers A , B and C Initialize the sum . 2- If the alue of A that of B then Go to step 3 Else Go to step 4 END If

3- If the value of A that of C then Print " the maximum value is that of A " Go to step 5

Else Print " The maximum value is that of C " Go to step 5 END If

4- If the value of B that of C then Print " the maximum value is that of B " Go to step 5 ELES Print " The maximum value is that of C " END If

5- Stop Processing

Page 13: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-13-

5- Write a pseudo code to find the sum of all terms of the following series greater than or equal 0.01 Sum = 1 + ½ + 1/3 + ¼ + …………….. Answer 1- Initialize the sum :

sum = 0 2- Initialize the term number N , which is the term its elf : N = 0 N = 1

T = 1/N 3- Add the new term to sum

Sum = Sum + N 4- Increment the term number N then find the corresponding term T

N = N +1 T = 1/ N

5- If T <0.01 Then Go to step 6 Else

Go to step 3 END If 6- Print an output line showing the sum of all terms . until value reach 0.01 7- Stop processing .

Page 14: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-14-

Exercises I- Write a pseudo code for flowchart shown in Figure 3.14 page 57 ?

1- Input ( Read ) the first number " number 1 ". 2- Input ( Read ) the second number " number 2 " 3- If Addition operation

Result = number 1 + number 2 Go to step 7

End IF 4- If subtraction operation

Result = number 1 – number 2 Go to step 7

End IF 5- If Multiplication operation

Result = number 1 * number 2 Go to step 7

End IF 6- If Division operation

Result = number 1 / number 2 Go to step 7

END IF 7- Print number 1 , number 2 , result 8- Steop processing

II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart Exercise 2 Chapter 2

1- Input ( Read ) the age of Hany 2- Input ( Read ) the age of Hesham 3- If age of Hany > age of Hesham

Print age of Hany Go to step 4

Else If age of Hesham > Hany Print age of Hesham Go to Step 4

Else Print "Tow of them are same in age " END IF 4- Stop processing

III – Write the pseudo code that reads a temperature in Fahrenheit degrees and convert it into Celsius degrees, using the formula ْC=5/9( Fْ-32)

1- Input ( Read ) temperature in Fahrenheit ( ْF ) 2- Calculate A = ( F – 32 ) 3- Calculate c = 5/9*A 4- Print C 5- Stop processing

Page 15: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-15-

Chapter 4 Object Oriented Programming

1- Object : Object is something that exists . We deal with object in our daily

life In real world objects can be parts of other objects . 2- Property : It's characteristic or attribute of an object like : name , color , size

or location 3- Method : It's a predefined actions provided with an object . 4- Event : It's action that may be caused by the user , such as a click , drag , or

key press . Example :

Cassette is one of the important devices in your home that we can use it to hear tapes . We can start the tape or stop it . we can increase its volume or reduce it . assume that its size was 40 cm * 20 cm . Please answer the following : Fill in the space using one of the following words :

Object – property – method – event 1- The cassette is considered ……………………o………………. 2- Turning the cassette on is considered …………m………………. 3- Stopping the cassette is considered ………………m………….. 4- Pausing the cassette is considered ………………e………..….. 5- The sound we hear from the cassette is considered ……e…….. 6- The color of the cassette is considered ………………p……..… 7- The volume of the cassette is considered ………….p……….….

Class : A class is a template used to create a new object . Class contain the

definition of all available properties .

Page 16: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-16-

The object oriented Terminology 1- Encapsulation : It's OOP feature that specifies that all methods and properties of a class be coded within the class . 2- Inheritance : Is reusability ,which a big advantage of OOP over traditional programming . Exercises: 1- Put ( √ ) and ( × ) : 1. The class is constructed out of object . [ X ] 2. The computer is considered an object that is consisting of many object . [ √ ] 3. The color of a pen is a " Method " [ X ] 4. Making cassette on is considered " Event " . [ √ ] 5. The shape of an object is determined by its properties . [ √ ] 6. Object has properties , Methods , and Events . [ √ ] 7. Object is constructed out of a class . [ √ ] 8. Encapsulation is used to hide information of the class . [ X ] 9. Derived class inherits from the Base class properties only . [ X ] 10. Encapsulation is used to protect the information of the class . [ √ ] 2- Fill in the spaces using the following words : object – event – property – method – class – encapsulation – inheritance 1. Clicking the mouse inside a window is considered ………….. ( event ) 2. The width of the window is considered ………… ( method ) 3. Pressing a key from the key board inside the window is considered …… (event ) 4. Properties that the son takes from his father is considered ………( inheritance) 5. Going to school is considered ……………… ( object ) 6. Amount of water in a lake is considered …………… ( method 7. Circuit diagram of your cassette is considered …………. ( property ) 8. ……………. ( encapsulation ) is considered hiding for information in a class .

Page 17: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-17-

Chapter 5

Introduction to integrated Development Environment

( IDE )

• To open the program follows the steps below.

• Then create a new project as the steps below.

Page 18: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-18-

• Visual basic interface:

• Title bar • Menu bar • Tool bar • Tool box • Form design • Properties bar • Solution explorer

Page 19: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-19-

• If you by any way didn’t see your tool box or may be your properties window or any other windows

• You can simply display it by the view menu or just by

select on the window shortcut on the tool bar

Page 20: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-20-

• There are three ways to

create your tools or your objects from the tool box

o Double click on the

tool you want to create , then it will appear on your form

o Or select and drag the

tool

o Or just select the tool then draw it on your form

Page 21: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-21-

• If you just create a button, to change the text of button or the font or just his size, you have to use the properties window.

Page 22: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-22-

Now you will create your first Visual Basic projects

1. From File menu select New .

2. From sub menu select project .

3. In the “Project types” pane choose Windows .

4. In “Templates” pane double click on Windows Application

5. Type the name of the project in the Name text box .

6. Click Ok

3 4

5

Page 23: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-23-

7. Visual Basic loads the default form “ Form1”.

8. Click the Toolbox on the left side of the screen and choose “Button control” by double click

on it .

9. On the button’s face is written “ Button 1” which means that the value of its default Text

property “Button1.Text” is “Button1”.

Page 24: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-24-

10. Double click the Button Control to enter Code View

11. Change the Text property of “Button1” to "Hello Egypt".

12. Save your application by choose “Save All” from “File menu” .

13. Running your Windows Application

A) Inside the Visual Studio Environment:

1. Press F5 key on your keyboard 2. Visual Studio will open your project as a Windows program. 3. Click the button on your form will change into “Hello Egypt”

B) Outside the Visual Studio Environment:

1. From “Build menu” choose “ Build Hello” 2. Visual Studio will compile your application into a Windows program with file extension

.exe . We will get executable file Hello.exe. 3. Go to the folder that contains your Windows application. 4. Double click Hello.exe to run it.

Page 25: Chapter 1 · Exercise VI - Draw a flowchart that ... II- Write a pseudo code to read the ages of ( Hany ) and ( Hesham ) then it prints the name of the older ? See flowchart

-25-

Exercises 1- What is the mnemonic " IDE " Stands for ?

- The visual Basic environment is where you Create and test your projects . The development environment is called the

Integrated Development Environment (IDE ) . I D E

( Integrated Development Environment ) 2-Mention the uses of the properties window and tool Box ? 1- Properties window uses to set the properties of objects in your project . 2- Tool Box its contain buttons that are shortcuts for menus commands . 3- Create a user interface ( form ) with just a single button control . When the button is clicked , form will look as follows .

Note that the following properties of the button were changed into the corresponding values in the table below .

Property New value Text " East or West , Home is best "

Fore color Color –Red Back color Color . Yellow

Good Luck