Chapter 2 and 11

The performance of a student in each semester shall be evaluated subject ? wise
with a maximum of 100 marks for theory course and 75 marks for laboratory and
...... ?Learn Object Oriented Programming Using Java: An UML Treatment using
Live Examples from Science and Engineering,? Dr. N.B. Venkateswarlu, Dr. E.V. ...

Part of the document


Lesson 1: Introduction to Programming


Computer Systems

A computer system consists of all the components (hardware and software)
used to execute the desires of the computer user. Hardware is the
electronic physical components that can retrieve, process and store data.
It is generally broken down into five basic components:

Central Processing Unit (C.P.U.) This is the unit where programs are
executed.
It consists of the control unit, which oversees the overall
operation of program execution and the A.L.U. (Arithmetic/Logic
Unit), which performs the mathematical and comparison
operations.

Main Memory ------- where programs and data are stored for
use by the CPU
Secondary Storage---- where programs and data are filed (stored) for
use at a later time.
Input Devices---------- devices used to get programs and data into
the computer (keyboard)
Output Devices------- devices used to get programs and data from the
computer (printer)


Programming

Computers in this twenty-first century will be guided by new and innovative
programs that will radically change every aspect of our lives. Medical
diagnostics, legal research, business applications, weapons development and
even sermon preparations are but a few areas that will be dramatically
altered as software and the hardware where they run become more
sophisticated.

A computer program is a set of instructions that tell computers what to do.
Software is the general term given to these instructions. Programming is
the term given to the process of developing software.

Computer Languages

Computer languages, like human languages, are guided by a precise set of
grammatical rules that must be strictly adhered. There are various levels
of computer languages. Ultimately all programs are translated into a
series of ones and zeros for this is the only format that a computer
understands. Despite all their sophistication, a computer basically goes
to a location and determines one of two condition (a one or zero).
Primitive programming was done as a series of ones and zeros and was
labeled machine language. Machine code was very difficult to develop and
understand. The sophisticated programs that we know today would not be
possible had it not been for the development of high-level languages. These
languages are geared more for human understanding and logical development.
High-level programming languages allow the use of vocabulary that is common
to human communication thus making the task of programming easier.
Although easier, these programs must be translated into the machine code
described above. A compiler or interpreter are programs that make those
translations.

Types of computer errors

Compilers and interpreters also detect and indicate grammatical errors
whenever the language is used incorrectly. It is thus very important to
learn the vocabulary and syntax rules for a particular language. The
complete translation will not take place as long as there are grammatical
errors. Once the program is free of such errors the translation will take
place and give us an executable code ready to run.

Once we have the executable code, the program is ready to be run.
Hopefully it will run correctly and everything will be fine; however that
is not always the case. During "run time", we may encounter a second kind
of error called a run time error. This error occurs when we ask the
computer to do something it cannot do. Look at the following sentence:

You are required to swim from Naples, Italy to New York in five
minutes.

Although this statement is grammatically correct, it is asking someone to
do the impossible. Just as we cannot break the laws of nature, the
computer cannot violate the laws of mathematics and other binding
restrictions. Asking the computer to divide by 0 would be an example of a
run time error. We would get executable code; however, when the program
tries to execute the command to divide by 0, the program will stop with a
run time error. Run time errors are usually more challenging to find than
syntax errors.

Once we run our program and get neither syntax nor run time errors, are we
free to rejoice? Not exactly. Unfortunately, it is now that we may
encounter the worst type of error: the dreaded Logic error. Whenever we
ask the computer to do something, but mean for it to do something else, we
have a logic error. Just as there needs to be a "meeting of the minds"
between two people for meaningful communication to take place, there must
be precise and clear instructions that generate our intentions to the
computer. The computer only does what we ask it to do. It does not read
our minds or our intentions! If we ask someone to cut down the tree when
we really meant for them to trim the bush, we have a communication problem.
They will do what we ask, but what we asked and what we wanted are two
different things. The same is true for the computer. Asking it to
multiply by 3 when we want something doubled is an example of a logic
error. Logic errors are the most difficult to find and correct because
there are no error messages to help us locate the problem. A great deal of
programming time is spent on solving logic errors.

Procedural Programming

There are two basic approaches to writing computer programs.

Procedural programming allows the use of memory locations in the computer
to be reserved for variables: storage locations containing values that can
be altered during the course of the program. Such a memory location is
given a name that reflects the nature of the data stored there. For
example, hourlyRate may be the name of a location that holds the amount of
pay a person is paid per hour. Values will also have operations performed
on them. The variable hourlyRate and numOfHours may have the
multiplication operation performed on them to create a new value, totalPay.
This could be accomplished in a language such as Java by the following
statement:

totalPay = hourlyRate * numOfHours;

Operations can be grouped together into logical units called procedures.
The instructions to input an hourly rate and number of hours worked and to
calculate the total pay could be placed in one procedure. Another procedure
could determine the Federal withholding tax. Call or invokes are used to
activate these procedures. Although Java is not a procedural language, it
uses these concepts that will be explored later. The following is a simple
code that demonstrates these concepts. The code is not a true language
code but rather pseudo (artificial) code. It consists of three
procedures(methods): Main, FindPay, FindFedTax. The whole purpose of Main
is to call the other two procedures. FindPay is a procedure( method) that
inputs the values of pay rate & hours worked and then calculates and
displays the total pay. FindFedTax determines the federal tax based on a
30% tax rate. Both FindPay and FindFedTax either return or need the value
of totalPay. This is the reason that totalPay is placed in parenthesis in
the call and title of those procedures. TotalPay is an argument of the
methods (procedures) . These concepts are dwelt with at greater length in a
later lesson.

Example:

Main program
FindPay(totalPay);
FindFedTax(totalPay);

FindPay(totalPay)
Read(hourlyRate); // This reads hourly Rate into hourlyRate
variable location
Read(numOfHours); // This reads the number of hours worked
into numOfHours variable
totalPay = hourlyRate * numOfHours; // This calculates total
pay and stores it at totalPay
Write(totalPay); // This
displays (somewhere) the total pay

FindFedTax(totalPay)
fedTax = totalPay * .30;
Write(fedTax);


Lesson 1 Summary Outline

I. Computer Systems

A. Central Processing Unit (C.P.U.) Consists of the control unit
and Arithmetic Logic Unit. The unit where programs are
executed.
A. Main Memory where programs and data
are stored for use by the C.P.U.
B. Secondary Memory where programs and data
are filed for use at a later time
C. Input Devices devices used to
get programs and data into the computer
D. Output Devices devices used to get
programs and data out from the computer

II. Programming Languages

A. Computer Languages
1. Machine Languages (ones and zeros)
2. High Level Languages (human oriented languages)
B. Compilers translate high level languages into machine code


III. Computer Errors

A. Grammatical Errors syntax errors
B. Run time Errors errors exposed during
the program execution (dividing by 0)
C. Logic Errors

I Procedural Programming

A. Variables memory locations
that contain values that can change
B. Procedures