Computer Programming Class Notes
Computer Programming Class Notes
programs
11 January 2021
SYNTAX: Are rules governing how words/characters in a programming
statement.
programming language.
Proximity simply means how close the languages are from the real natural
languages that are used by man. They are further classified int two namely
- This are languages that are close to natural languages (uses symbols and
- These are languages that are close to the native language of the
computer.
- The native language of the computer is the binary language which uses
14 January 2021
1. SOURCE CODE
2. MACHINE CODE
- This is the code which is in the binary language of string of zero and
Bytecode -
What is Translation?
- Is the process of converting source code into machine code.
CODE TRANSLATOR
1byte=8bits
Kilobyte=1000(1024)
Code translation get classified based on the types of translation which is used
TYPES OF TRANSLATOR
1. COMPILERS
- Is a translator which converts source code by reading all the lines of code
Compilation => Is the process by which source code gets converted into object
programming languages
2. INTERPRETERS
1) The object code (end product code) is in zeros and ones (machine code)
After loading the source code through all 100 lines from 1 to
100 depending how many your source code has and it has no
mistakes, no syntax errors, no grammatical errors then the
compiler will Covent the set of instruction into machine code.
Machine code produced is permanent.
2) Programs generated are speed critical.
There is fast execution relative to programs that are interpreted
in nature.
They execute (run) with speed since they instruction are in zeros
and ones which is the native languages of the computer hence the
computer does not have to translate again and will simply go to
the language and run it hence the execution is fast.
E.g. machine used in hospital theaters.
3) Once the program is compiled it can be used many times. No need for
recompiling of the program.
NB: If you compile your program on the RISC processors, it may not respond
well or it may not respond at all on a CISC processors and Vice-versa.
COMPILATION PROCESS IN JAVA.
Lexical analyzer => the source code gets scanned and then gets converted
into lexemes.
Syntax analyzer => the lexemes produced are taken as inputs and generates
a syntax tree where the lexeme arrangement is checked against source code
grammar.
Semantic analyzer => checks whether the syntax tree constructed follows
the rules of the language and also keeps track of identifiers their types and
expressions.
Code generator => the optimized intermediate code is now mapped to the
target machine language.
a) With the interpreter translation of source code is done line by line and
results are generated for each line and with this interpreted
programming languages execute (run) slowly since each line has to be
read and decoded and results are given and therefore a lot of time is
involved to read decode and generate results.
b) They are easy to write: - They do not demand strict adherence to
syntax, when writing the source code, you can violate a few things and
the program will still run.
c) It’s easy to identify code errors: - Since the interpretation happens line
by line, so when an error is discovered in a line, the interpreter does
not continue translating, therefore it stops and the error is easily
located.
Easy to debug when using interpreted program languages.
d) The end product is not an object (a program) code which is
permanently in the machine code in zeros and ones because the
interpretation is line by line thing and at the end of the day you do not
have a package of 0s and 1s.
f) Reinterpretation is done => When it comes to executing the program,
each time you want to use the program you have to re-interpret it,
retranslate it because it’s not permanently in the machine code.
g) Machine independent => Programs that comes from interpreted
programming languages are machine independent.
They can run on any operating system platform whether android,
Linux, windows or mac OS.
NB: in conclusion if you are looking for a program and your interest is
that the program should be able to run in any hardware platform E.g.
internet-based programs, therefore your program must be developed
using an interpreted programming language.
If your interest is to develop a program which must execute relatively
faster, than compiled programming languages must be used.
Programs are developed as a set or package of objects that are told what to
do.
Objects: based what you know in real life e.g. stone and phones.
One must focus on developing objects that can be told what to do which are
the services that the end user is looking for.
Public
Is also known as the access modifier, giving access to anyone.
Class
Object are put classes based on their functionalities
Object have attributes (specific properties).that are used to
classify objects.
Is a way of telling the computer that you are developing a
class which will contain objects of the same attributes which
will coming up with a class?
A class is key word which specifies that you’re opening up a
class.
Keyword
Is a special word in a programming language which has a
unique meaning so that it can only be used according to the
meaning it possesses in the language and not otherwise.
NB: The statement name above must be written just b4 the class
declaration statement.
2) Use the input dialog box to display the input window using the
following statement.
THE CPU ARCHITECTURE & THE FETCH, DECODE
EXECUTE CYCLES
THE CPU
1) FETCH
Instructions and data are fetched from the memory and
buffing registers to the processor.
2) DECODE
The instruction are interpreted (translated) according to the
architecture of the CPU.
3) EXECUTE
The instructions are then executed e.g. applying them on
data to generate demanded results.
The processor all the time goes through the stages above in order to process
information.
Sequential
Selective or conditional (if statements and others)
Interactive or looping (while, for loops)
SEQUENTIAL PROGRAMS
a. IF STATEMENT
- If selection uses the if keyword
SYNTAX:
If (condition) {
NB:
A. The code in the block will execute only when the set condition is true.
B. The condition is given as the set of :
Test variable (variables whose value will be tested)
Relational operation
Value to be tested
RELATIONAL OPERATORS (inbuilt piece whose value will be tested)
EXAMPLE:
Double grade;
grade=52.8;
If (grade>50) {
SYNTAX:
if (condition) {
//Code execute if condition TRUE
else {
EXAMPLE:
else {
When creating a range, we use the ampersand or two ampersand sign &&
EXAMPLE:
FAIL
BARE PASS
CREDIT
DISTINCTTION
Int grade;
If (grade>=0 && grade<=39) {
SYNTAX:
If (condition1) {
Else if (condition2) {
. This dots are called ellipsis this means the codes are continuing
Else if (condition X) {
//Code to execute when all condition above condition X are false out condition
X is true
Else {
}
ITERACTIVE PROGRAMS
Also known as looping programs.
CODE:
Int num = 0;
While (num<=10) {
System.out.println(num);
Num++;
}
NB:
CODE:
Int two =1;
While (two<=12)
{
System.out.println(two+”*2=”+” ”+two*2);
Two++;
}
THE FOR LOOP
NB:
IntialValue: is an initialized variable that contains an entry point value
into the loop and is also called the test variable
Condition: is the condition to be tested whose TRUE or FALSE status
will determine the flow of the program.
Postexpression: implements what must happen after the code in the
FOR block is executed.
Usually the Postexpression takes the form of an increment or
decrement operator.
EXAMPLE:
Int a;
For (a = 0; a <= 10; a++) {
System.out.println(a);
}
SYNTAX:
Do {
Code to execute
}
While (condition);
The code in the while loop will not execute if the condition is false while in
do while loop the condition will execute once because the code executes
before the condition.
STEPS:
1) Create a project—file
2) New project
3) Java application
4) Give your project a name – GUIAPP
5) Uncheck the create main class checkbox
6) Click finish
7) Go to the project panel or window
8) Identify your project by name (project you are working on)
9) Click on source packages
10) Right click on default package
11) In the window which appears choose new
12) Then go to Jframe form in the next window
13) Click finish
14) Populating a form with controls
Go into the palette window
Pick a control of interest
Drag it into the form
Release the button to the control position on the form
FUNCTIONS IN PROGRAMMING
- Function is a module of code for implementing a specific task
Or
Used for:
PARTS OF FUNCTION
DECLARATION OF FUNCTIONS
}
WHERE:
1. ACCESS MODIFIER.
Is a keyword which specifies the accessibility of the function e.g. public,
private.
2. RETURN DATA TYPE
specifies the type of data that a function will have when return back
execution power to the caller function e.g. int, double, float, string,
byte, char.
3. METHOD NAME
Any user defined function is supposed to be given a unique name
which reflect what the function will implement.
4. PARAMETER
List of local variables used as storage area of data required by the
function during execution.
Is a variable which is associated with function.
EXAMPLE:
Code
Int add;
System.out.println(add);
Return o;
NB:
Return height;
VOID FUNCTIONS
Is a function which does not return anything to its caller function i.e.
when return it does not carry any data to give to the function which
invoked it.
Void function are specified by the keyword void.
The old keyword is placed where the return datatype is written in the
declaration statement of a method
e.g. public static void main (string [] args)
PROPERTIES OF VOID FUNCTIONS
Declared by using the void keyword.
Has no return statement.
May or may not contain parameters.
IMPORTANCE OF VOID FUNCTIONS
Used for:
Generating prompt messages
Generating warning messages
Generating system status messages
Generating general informative messages
ENTERNAME ( );
RECURSION
If (y = = 0) {
Return;
Else{
System.out.println (y)
X (y-1);
X (10);
RECURSIVE FUNCTIONS
ENTERNAME ( );
ASSIGNMENT:
DATA STRUCTURES
fast insertion
fast retrieval
fast modification
fast deletion
fast traversal
Diagrammatic example:
Or
TYPES
DECLARATION OF ARRAYS
Int x [];
e.g.
Element = value;
e.g.
OUTPUT
Firstnames
JOHN
JACOB
JAMES
JOEL
JONATHAN
Datatype ArraysName[]={values1,value2,.,.,.,valueN}
EXAMPLE:
This is done by writing the name of the array element inside on output
stream
The length is an input method for declaration of the size of the array.
System.out.println(Firstnames[i]);
Example 2:
Int Age [] = {20,10,30,40,60,7};
Int Sum=o;
Sum = Sum+Age[i];
System.out.println(Sum);
These are arrays that span in two dimensions(i.e. the x and y plane)
With 2-D arrays:
The 2-D array size is given in terms of the number of rows and the
number of columns
E.g.
3x3→ row x column → read as 3 by 3 array → 3 rows and 3
columns
4x7 → 4 by 4 || →4 rows and 7 columns
Class work
i. Int x [] [] = new int [1] [10];
Result:
DISADVANTAGES OF ARRAYS
Cannot be used to store different type of data
It is a static data structure →has a fixed size once declared.
We need to know in advance the number of elements for an array
Diagrammatic representation
NATURE OF A NODE
As follows
Drawing………
The data part is used for storage of data
The address part contains the address of the next Node memory
The first Node is called the HEAD
The last NODE is called the TAIL
- add first ()
- add last ()
>> for use with linked list the following class has to be imported into the
program.
syntax;
example.
import java.util.LinkedList;
Records.add("TINASHE");
Records.add("MWANGUULA");
Records.add("22");
Records.add("FEMALE");
Records.add("99000.22");
Records.add("CHRISTIAN");
Records.add("P");
Pseudocode
Flowcharts
properties of pseudocode
- Statements must be short. (remove all joining words i.e. the, is >>.)
- All action statements must have word that are in block letters.
example. 1
2. INITIALIZE length.
3. INITIALIZE width.
4. CALCULATTE area.
5. DISPLAY area.
example 2.
2. SET width to ()
3. SET Area to ()
4. READ lenth
5. READ width
7. OUTPUT Area.
KEYWORDS USED FOR EXECUTING CERTAIN PROGRAMMING STAGES
AND CONSTRUCTS.
- INIT
- INITIALIZE
- SET
- CAPTURE
- GET
- READ
- OUTPUT
- DISPLAY
- WRITE
- IF (condition)THEN
//// pseudocode
ENDIF
5. words used with iteration.
a. for construct;-
- FOR
END FOR
b. while construct;
- WHILE
END WHILE
c. do ---- while;
- DO
///////pseudocode here
END DO
1. SET Sam to 0
2. FOR i = 0 to I = Array.length – 1 (since array.length goes 1 less the array size )
5. ENDIF
6. END FOR
7. PRINT Max
Q1. An array contains the value Y, write a pseudocode search for the value
in the Array & Display it.
Q2. Write a pseudocode for displaying all values in a 5X5 2-D Array.
Q3. Write a pseudocode to check for common value in a data set of 2 Array.
Q4. Write a pseudocode which calculate the average of the grade values in
an Array.
FLOWCHART SYMBOLS
DELETE => it’s used to indicate stages that require some processing.
4. the CONNECTOR
A straight line with a arrow head. To indicate direction of an algorithm
flow and connecting chart symbols.
The given are key flowchart symbols but there are also others symbols
but not frequently used.
E.g.
CODE TRACING,
- Code tracing is the art of going though the source code manually in order to
test the execution of the code.
- This implies that code tracing is a method for checking the execution of a
piece of code before compilation
ADVANTAGES
- There are several methods used for code tracing, most popular of which is a
trace table.
EXAMPLE OF A TRACETABLE
- Algorithm of an area of rectangle program,
Trace table for a while loop which implements the first five natural numbers
21 5
22 no
23 ------
assignment
VALUES
- This is the actual value stored in a pointer and is used as an address of the
location of a variable in memory
PROGRAMMING PARADIGMS
- Also known as the programming approach
- Programming languages are classified according to their approach.
- In computer programming a paradigm, is;
i. Approach
ii. Style
iii. Mode
iv. Technique
A paradigm is an approach which can be used by a programmer in order
to come up with a program.
CLASS IN OOP
OBJECTS
INSTANCE
- This is when a class acquires the characteristics and methods that are found
in another class.
- The class containing the characteristics and methods to be acquired by
another class in known as the parent class [base or primary class]
- The class which acquires the characteristics and behaviors of the parent class
is know as the child class [derived class or class secondary]
Advantages of instance.
i. Code is reused many times
ii. Helps to develop programs faster.
Disadvantage of an instance
i. When the parent class has been affected, the child class will be affected
as well since it will depend on the parent class.
ENCAPSULATION
POLYMORPHISM
Poly = many
Morphie = terms
DYNAMIC DISPATCH
COUPLING
COHESION
VISIBILITY MODIFIERS
+ username: string
+ password: int
_ submit ()
_ validate ()