Docsity

Java Programming Fundamentals: A Comprehensive Guide to Core Concepts and Syntax, Summaries of Object Oriented Programming

Partial preview of the text

Download Java Programming Fundamentals: A Comprehensive Guide to Core Concepts and Syntax and more Summaries Object Oriented Programming in PDF only on Docsity!

Object Oriented

Programming

Prof. S. O. Akinola

1: Fundamental Principles of Programming

 In this Lecture, the basic principles of programming are

explained.

 We start by defining the word ‘programming’ and then

the various tools needed for writing programs are

discussed.

 Steps of programming as well as programming tools are

also discussed.

 Good programming styles are not left out in this Lecture.

 The criteria for judging good programs concludes the

Lecture.

2

 Therefore, in learning a programming language’s syntax and semantics, we look for the commands or statements that we will use to perform inputs, i.e., how to read in data into the computer via the keyboard, files, databases or any other source.  We then learn the commands or statements to process the data, i.e., the process logic.  Finally, we learn how to display our results as outputs to users of our programs. That is, how to display via the monitor, store results into a file or database and/ or send the results to other computing system(s).

1.4 What is Programming? …

4

Is Programming an Art or a Science or Both?  Programming is on one hand an artistic or creative endeavour, and on other hand, a scientific endeavour.  As a scientist, the programmer begins with a clearly defined set of principles, or axioms of programming.  He or she then applies them systematically to a problem, in such a way that at any stage in the process the (mathematical) correctness of the program can be routinely demonstrated.  For instance, in testing our programs and their designs, scientific approaches are usually employed. 5

Programming is both scientific and artistic  These two philosophies of programming have some implications.  The art of programming demands a variety of “tools” or a “programming environment” within which the total process of program development can take place.  The science philosophy demands a language, which supports the axiomatic proof of program behaviour 7

Programming Environment  The programming environment includes all the facilities and tools, which a programmer requires in the design, development, testing, implementation and maintenance of a system of programs.  Thus, text editors, compilers, interpreters, diagnostic tools, optimizers, measurement tools and other aids comprise a complete programming environment.  Apart from the tools mentioned as comprising the programming environment, the following are additional tools needed in programming:  A previous knowledge of the pros and cons of programming languages syntax and semantics;  Logical reasoning ability; and  Algorithms and flow charts. 8

Algorithms

 An algorithm literarily means the step-by-step procedure of

solving a problem. Technically, it is defined as a finite solution

steps to a problem. As a matter of fact, an algorithm must have the

following properties:

(i)Finiteness: It must have a terminating point after some

points.

(ii)Definiteness: It must be clear and unambiguous.

(iii)Input: It must have allowable set of data input to the

system.

(iv)Output: It must properly define the nature of result

expected; that is, the format.

(v)Efficiency: Its implementation must be space (memory) and

time efficient. 4/30/

10

Illustration: suppose we want to add some numbers together continuously until when we encounter any negative data, which shall terminate the addition. In addition, we also want to report the total number of valid data added so far before the program halts.  The Algorithm:

**1. Set up a counter, count = 0

  1. Initialize an accumulator, sum = 0
  2. Read in data, d
  3. If (d**  0 ) then 5. Sum = sum + d 6. Count = count + 1 7. Goto 3 8. Else **9. Print out Results, Sum, Count
  4. Terminate** 11

Steps of Programming

Step 1. Analysis and understanding of the problem to be solved. This involves the following subtasks: (i)Having a basic understanding of the problem. If you don’t understand a problem, then you cannot solve it even with a computer. The problem we are solving should be well understood before we can write a meaningful functional program for it. (ii)Identification and designing of input data. All the data (variables) to be used in the program must be well analysed. What type(s) they are, in which format are they going to be, from which medium (keyboard, files in disks, etc) will they come from, etc. By types, we mean whether they are whole numbers (called integers), numbers with decimal points, for example 3. 6 (called Real), characters or texts. The data input could come from the keyboard, a file or some signals from other electronic systems. 4/30/ 13

(iii) Identification of any suitable solution model. If it is mathematics-based, is there any model formula for solving the problem? For instance, we have a formula for solving quadratic equations. Can you remember the ‘almighty formula’? We shall soon come to it. (iv) Identification and designing of outputs. We have to decide on their type(s), in which format they are going to be, from which medium (monitor, printer, files in disks, etc) will they be directed to, etc.

Steps of Programming …

14

Step 6. Documenting the program.  Documentation is very essential in programming.  We have two types – internal and external.  Internal documentation involves putting comments and white spaces in our program source codes.  They are there to introduce the functionality of the program as well as to introduce the different sections of the code for readability and maintenance of the code.  External documentation are in form of a manual for operating and maintaining the program by any user of the program.  The user will specify the functionality of the program, inputs and their formats, expected outputs and their formats, operating instructions for the program, etc.  To maintain any code, the documentation on the code will have to be consulted first.

Steps of Programming …

16

Good Programming Style

Choose appropriate names for your variables.  The names you use for your variables should be meaningful and suggest clearly the purpose of the variable.  They should communicate what the variable is meant for at least.  For example, Area_of_Rect suggests that the variable is for the area of a rectangle.  For a program to work properly, it is of utmost importance to know exactly what each variable represents, and to know exactly what each subprograms must be included with their names chosen with care to identify their meanings clearly. 17

Be careful in the use of the letter i (small letter i), l (small

ell), O (capital O) and 0 (zero).

Choose appropriate data types for your variables. All

variables must be associated with types such as Integer,

Real, Character or Logical. The type of a variable

indicates how it will be physically stored in memory and

what type of data to be logically assigned to it during

the execution of the program in which it is declared.

The program must be well structured and readable. Well-

structured programs are aesthetically readable and

maintainable. The use of indentations and white spaces

to separate the parts of a program promotes structuring

of programs.

Good Programming Style …

4/30/ 19

 Modularize the program for easy maintenance. The program should be broken down into various functional units. This is achieved through the use of subroutines and functions in programs.  Appropriate documentation formats such as comments and white spaces (blank lines) must be used in the program. The white spaces separate the program into logical functional sections for ease of debugging and maintenance.  Test the program with different sets of data (both valid and invalid) to ascertain that the program is error-free.

Good Programming Style …

20