CSC201Compter Programming1
CSC201Compter Programming1
CONTENTS
COMPUTER PROGRAMMING.............................................................. 3
1.0 Introduction............................................................................. 3
1.1 Computer program..................................................................... 3
1.1.1 Features of Good Computer program ............................................ 4
1.1.2 Uses of Computer Programs ....................................................... 4
1.2 Computer Programming .............................................................. 5
1.2.1 Phases of program development (Programming) .............................. 5
1.2.2 Tools in Computer Programming ................................................. 6
Algorithm ................................................................................ 6
Flow charts .............................................................................. 7
Decision tree ............................................................................ 7
Concept of Algorithm ................................................................. 7
Features of Algorithm................................................................. 7
Methods of Representing Algorithm ............................................... 7
Pseudo Code............................................................................. 8
1.3 Programming Language ............................................................... 8
1.3.1 Types of Programming Language ................................................. 8
1.3.1.1 High level Language .............................................................. 8
Features of High level programming language ................................... 9
Advantages of High level programming language ............................... 9
Disadvantages of High level programming language ............................ 9
1.3.1.2 Low level Language ............................................................... 9
Features of Low level programming language ................................... 10
Advantages of low level programming language ................................ 10
Disadvantages of low level programming language ............................ 10
Assembly language .................................................................... 10
Advantages of assembly language ................................................. 11
Disadvantages of assembly language .............................................. 11
Machine language ..................................................................... 11
Advantages of machine language .................................................. 12
Disadvantages of machine language .............................................. 12
1.4 Review Questions ..................................................................... 13
COMPUTING USING OBJECT-ORIENTED DESIGN AND OBJECT-ORIENTED
PROGRAMMING ............................................................................ 15
2.0 Object-Oriented Design .............................................................. 15
2.1 Programming Paradigm .............................................................. 15
1
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
Week 1-4
Learning Outcome for this weeks:
Features of an Algorithm
languages
2
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
1.0 Introduction
Almost all programs consist of the same basic 'building blocks', assembled in
different ways to achieve a particular goal. Variables, data types, sequence,
selection, and iteration are examples of these concepts, which all new
programmers need to learn. In order to compare and manipulate data, and to
make decisions in a program, programmers use a variety of different logical,
relational, and arithmetic operators. It is also useful to be able to generate
randomness in a program, for example selecting a random number or a
random item from a list. Programming concepts can be defined as techniques,
ideas and know-how embodied and expressed in any computer programs or
modules included in the software, including software structure, sequence and
organization, but excluding any modifications which are expressly paid for by
organization.
3
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
4
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
5
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
1. Algorithm
2. Flowcharts
3. Decision table
Algorithm
Algorithm is a listing of the sequence of steps in their proper order of
occurrence of the actions, which are necessary for the successful execution of
a given task. Essentially, an algorithm is a finite series of logical steps required
to solve a particular problem. An example of the use of computer algorithm:
6
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
7
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
8
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
9
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
Assembly language
Assembly language is type of low-level computer programming language in
which each statement corresponds directly to a single machine instruction.
Assembly languages are thus specific to a given processor. After writing an
assembly language program, the programmer must use the assembler specific
to the microprocessor to translate the assembly language into machine code.
Assembly language provides precise control of the computer, but assembly
language programs written for one type of computer must be rewritten to
operate on another type. Assembly language might be used instead of a high-
level language for any of three major reasons: speed, control, and preference.
Programs written in assembly language usually run faster than those
generated by a compiler; use of assembly language lets a programmer interact
directly with the hardware (processor, memory, display, and input/output
ports).
Assembly language uses easy-to-remember commands that are more
understandable to programmers than machine-language commands. Each
10
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
11
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
Each type of computer has its own machine language. That is to say, that
different brands of computers cannot understand programs written in
another brand’s machine language. Talking specifically, a computer made by
IBM Company has its own language which is different from the one of NCR
Company. Even two different models of computers made by the same
company do not usually have the same machine language. Thus programs
written in machine language are said to be machine dependent.
Every instruction in machine language programs must specify both the
operation to be carried out as well as the storage locations of the data items
to be used in the operation. In form, it consists of a series of numbers. The
operation part is called opcode or operation code and the remaining part gives
the addresses of the data items in memory that will be affected by the
operation. Due to these special requirements machine language programming
is extremely complex, tedious and time consuming.
Advantages of machine language
Less code is produced
Storage is saved
User has direct control of machine instruction
Execution is faster as no translation is needed
The programmer knows all the registers and instruction that use them.
Disadvantages of machine language
Cumbersome i.e., tedious and difficult to learn
Programmer’s fluency is affected, thereby making the programs
developed inefficient.
The developed programs are error prone and difficult to debug (correct)
The performance of the system is unreliable.
12
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
13
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
Week 5-8
Learning Outcome for this weeks:
14
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
15
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
16
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
Java applications are typically compiled to byte code that can run on any Java
virtual machine (JVM) regardless of computer architecture. The first publicly
available version of Java (Java 1.0) was released in 1995. Java is most similar
to C. However although Java shares much of C's syntax.The concepts are
related to c++. Java was originally developed by James Gosling at Sun
M icrosystems (which has since been acquired by Oracle Corporation) and
released in 1995 as a core component of Sun Microsystems' Java platform.
The language derives much of its syntax from C and C++, but it has fewer low-
level facilities than either of them.
Java facilitates the distribution of both data and small applications programs,
called applets, over the Internet. Java applications do not interact directly
with a computer’s central processing unit (CPU) or operating system and are
therefore platform independent, meaning that they can run on any type of
personal computer, workstation, or mainframe computer. This cross-platform
capability, referred to as “write once, run everywhere,” has caught the
attention of many software developers and users. With Java, software
developers can write applications that will run on otherwise incompatible
operating systems such as Windows, the Macintosh operating system, OS/2,
or UNIX.
To use a Java applet on the World Wide Web (WWW)—the system of software
and protocols that allows multimedia documents to be viewed on the
Internet—a user must have a Java-compatible browser, such as Navigator
from Netscape Communications Corporation, Internet Explorer from
Microsoft Corporation, or HotJava from Sun Microsystems. A browser is a
software program that allows the user to view text, photographs, graphics,
illustrations, and animations on the WWW. Java applets achieve platform
independence through the use of a virtual machine, a special program within
the browser software that interprets the bytecode—the code that the applet is
written in—for the computer’s CPU. The virtual machine is able to translate
the platform-independent bytecode into the platform-dependent machine
code that a specific computer’s CPU understands.
Applications written in Java are usually embedded in Web pages, or
documents, and can be run by clicking on them with a mouse. When an applet
is run from a Web page, a copy of the application program is sent to the user’s
17
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
computer over the Internet and stored in the computer’s main memory. The
advantage of this method is that once an applet has been downloaded, it can
be interacted with in real time by the user. This is in contrast to other
programming languages used to write Web documents and interactive
programs, in which the document or program is run from the server
computer.
2.3 Object Oriented Programming Concepts/Key Attributes
Object Oriented Programming Concepts are very important. Without having
an idea about OOPS concepts, you will not be able to design systems in the
object-oriented programming model. It simplifies software development and
maintenance. Basic Concepts/atrributes of OOP are:
Class
Object
Method
Inheritance
Abstraction
Encapsulation
Polymorphism
Message Passing
Data Types
Variables
Expressions
Assignments
I/O operations
Conditional and iterative control structures
Functions and parameter passing operations
Strings
Arrays
Object
Any entity that has state and behavior is known as an object. An Object can
be defined as an instance of a class. An object contains an address and takes
up some space in memory. Objects can communicate without knowing the
details of each other's data or code. The only necessary thing is the type of
18
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
message accepted and the type of response returned by the objects. For
example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical.
Real world example: A dog is an object because it has states like color, name,
breed, etc. as well as behaviors like wagging the tail, barking, eating, etc.
class Bicycle {
// state or field
private int gear = 5;
// behavior or method
public void braking() {
System.out.println("Working of Braking");
}
Class
In object-oriented programming, a class is a group of objects which have
common properties. It is a class is a programming language constructs that
is used as a blueprint to create objects This blueprint includes attributes and
methods that the created objects all share Usually, a class represents a
person, place, or thing - it is an abstraction of a concept within a computer
program Fundamentally, it encapsulates the state and behavior of that which
it conceptually represents It encapsulates state through data placeholders
called member variables; it encapsulates behavior through reusable code
called methods. A real-world example is Book. Let’s look at an example of a
class and analyze its various parts in a below diagram. This example declares
the class Book, which has the member-variables x, y, and ID of type Integer
and the two member-methods, read () and write ().
class Book {
// fields
// methods
}
Abstraction
Abstraction means hiding lower-level details and exposing only the essential
and relevant details to the users. Real world example, the first example, let's
consider a Car, which abstracts the internal details and exposes to the driver
only those details that are relevant to the interaction of the driver with the
Car. The second example, consider an ATM Machine; All are performing
operations on the ATM machine like cash withdrawal, money transfer, retrieve
mini-statement…etc. but we can't know internal details about ATM. Another
19
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
way, it shows only important things to the user and hides the internal details
for example sending sms, you just type the text and send the message. You
don't know the internal processing about the message delivery.
abstract class Car{
abstract void accelerate();
}
//concrete class
class Suzuki extends Car{
void accelerate(){
System.out.println("Suzuki::accelerate");
}
}
class Main{
public static void main(String args[]){
Car obj = new Suzuki(); //Car object =>contents of Suzuki
obj.accelerate(); //call the method
}
}
Or
abstract class <classname>
{
public abstract void abstractMethod();
public void normalMethod()
{
//method body
}
}
Encapsulation
Encapsulation is a process of wrapping of data and methods in a single unit
is called encapsulation. In OOP, data and methods operating on that data are
combined together to form a single unit, which is referred to as a Class. Real
world example, Capsule, it is wrapped with different medicines. In a capsule,
all medicine is encapsulated inside a capsule.
public class Person {
private String name; // private = restricted access
// Getter
public String getName() {
return name;
}
// Setter
public void setName(String newName) {
this.name = newName;
}
Inheritance
Inheritance is a mechanism in which one object acquires all the properties
and behaviors of parent (super) object. The idea behind inheritance in java is
that you can create new classes that are built upon existing classes. When
20
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
you inherit from an existing class, you can reuse methods and fields of parent
class, and you can add new methods and fields also. It is one of the
fundamental features of object-oriented programming. The real-life
example of inheritance is child and parents, all the properties of a father are
inherited by his son.
class Vehicle {
protected String brand = "Ford"; // Vehicle attribute
public void honk() { // Vehicle method
System.out.println("Tuut, tuut!");
}
}
// Call the honk() method (from the Vehicle class) on the myCar object
myCar.honk();
// Display the value of the brand attribute (from the Vehicle class)
and the value of the modelName from the Car class
System.out.println(myCar.brand + " " + myCar.modelName);
}
}
Polymorphism
Polymorphism is an OOP concept by which one can perform a single action
by different ways. Polymorphism is derived from 2 greek words: poly and
morphs. The word "poly" means many and "morphs" means forms. So
polymorphism means many forms. Real world example, Suppose if you are
in a classroom that time you behave like a student, when you are in the
market at that time you behave like a customer, when you at your home at
that time you behave like a son or daughter, Here one person present in
different-different behaviours.
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
21
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
M essage passing
Message passing is a method by which an object sends data to another object
or requests other object to invoke method. This is also known as interfacing.
It acts like a messenger from one object to other object to convey specific
instructions. Message Passing involves specifying the name of objects, the
name of the function, and the information to be sent.
Data type
A data type is used to indicate the type of data value stored in a variable. In
OOP, there are two types of data types:
Primitive data types
Non-primitive data types
Variables
A variable is the holder that can hold the value while the java program is
executed. A variable is assigned with a datatype. It is name of reserved area
allocated in memory. In other words, it is a name of memory location. A
variable provides us with named storage that our programs can manipulate.
Each variable in Java has a specific type, which determines the size and
layout of the variable’s memory; the range of values that can be stored within
22
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
that memory; and the set of operations that can be applied to the variable.
There are three types of variables: local, instance and static.
int a=10;
float x=2.3;
int a, b, c; // Declaration of variables a, b, and c.
int a = 20, b = 30; // initialization
byte B = 22; // Declaratrion initializes a byte type variable B.
Operators and expressions
An operator is a symbol which represents a particular operation that can be
performed on data. An operand is the object on which an operation is
performed. For example: +, -, *, / etc. By combining the operators and
operands we form an expression. An expression is a sequence of operands
and operators that reduces to a single value. Expressions are essential
building blocks of any Java program, usually created to produce a new value,
although sometimes an expression simply assigns a value to a variable .
Expressions are built using values, variables, operators and method calls.
Those that produce a value, i.e. the result of (1 + 1). Those that assign a
variable, for example (v = 10)
Constructors
Constructors are the method. It is used to give initial values to instance
variables of the objects. It is automatically called at the time of object creation.
Constructors are used for initializing new objects. Fields are variables that
provide the state of the class and its objects, and methods are used to
implement the behavior of the class and its objects.
Constructor name (arguments)
{
Statements;
Initial
values;
}
M ethods
A method is a collection of statement that are grouped together to performs
specific task. In Java, each method is a part of a class and they define the
behavior of that class. In Java, method is a jargon used for method. Methods
are similar to functions or procedures that are available in other programming
languages. When you call the System.out.println() method, for example, the
23
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
24
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
Access specifier
Access specifiers or access modifiers in java specifies accessibility (scope) of a
data member, method, constructor or class. It determines whether a data or
method in a class can be used or invoked by other class or subclass.There are
four (4) types of java access specifiers:
Private
Default (no speciifer)
Protected
Public
class PrivateEx{
private int x; // private data
public int y; // public data
private PrivateEx(){} // private constructor
public PrivateEx(int a,int b){ // public constructor
x=a;
y=b;
}
}
Stream
A stream can be defined as a sequence of data. The File Input Stream is used
to read data from a source and the File Output Stream is used for writing data
to a destination.
File f = new File("C:/java/hello");
InputStream f = new FileInputStream(f);
File f = new File("C:/java/hello");
OutputStream f = new FileOutputStream(f);
Array
An array is a group of like-typed variables that are referred to by a common
name. Arrays of any type can be created and may have one or more
dimensions. A specific element in an array is accessed by its index. Arrays
offer a convenient means of grouping related information.
25
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
26
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L
27