0% found this document useful (0 votes)
19 views28 pages

CSC201Compter Programming1

The document is a study material for the CSC201: Computer Programming 1 course at Kaduna State University, outlining key concepts in computer programming, including definitions, features of good programs, programming languages, and the phases of program development. It covers both high-level and low-level programming languages, their advantages and disadvantages, and introduces tools like algorithms and flowcharts for programming. Additionally, it includes learning outcomes and review questions to assess understanding of the material.

Uploaded by

Hafsat Jaafar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views28 pages

CSC201Compter Programming1

The document is a study material for the CSC201: Computer Programming 1 course at Kaduna State University, outlining key concepts in computer programming, including definitions, features of good programs, programming languages, and the phases of program development. It covers both high-level and low-level programming languages, their advantages and disadvantages, and introduces tools like algorithms and flowcharts for programming. Additionally, it includes learning outcomes and review questions to assess understanding of the material.

Uploaded by

Hafsat Jaafar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

KADUNA STATE UNIVERISITY

DEPARTMENT OF COMPUTER SCIENCE


CSC201: COMPUTER PROGRAMMING 1
FIRST YEAR
FIRST SEMESTER 2022/2023 ACADEMIC SESSION

Study Material for CSC 201

Study Material Prepared


by

Mr. Habibu Yahaya Muhammad


Mr. Dams Gabriel Lazarus
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

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

2.1.1 Object Oriented Programming Paradigm ...................................... 15


2.2 Object Oriented Programming...................................................... 16
2.2.1 Object Oriented Programming Features ....................................... 16
2.3 Object Oriented Programming Languages........................................ 16
2.3.1 Java ................................................................................... 16
2.3 Object Oriented Programming Concepts/Key Attributes ..................... 18
2.4 Review Questions ..................................................................... 26

Week 1-4
Learning Outcome for this weeks:

The concept of programming and computer programming

Features of a good program

Systems development cycle

Explain the distinguish features of different programming languages

The concept of Algorithm

Features of an Algorithm

Methods of representing algorithm

Distinguish between systems commends and program statements.

Advantages and disadvantages of different levels of programming

languages

Explain machine language, low-level language and High level languages

2
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

COM PUTER PROGRAM M ING

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.

1.1 Computer program


A computer program is a set of instructions written in a programming
language in a sequential order, so as to control the activities of a computer
system. In simplest term, computer program is a step-by-step list of
instructions written for particular computer architecture in a particular
computer programming language. A layman equivalent example will be
writing a step-by-step list of instructions in English, instructing a student to
register for courses for the academic session (the human being the specific
architecture). In other words, computer program can also be said to a process
of taking the steps in a procedure and putting them in the coded language of
the computer. MS Word, MS Excel, Adobe Photoshop, Internet Explorer,
Chrome, etc., are examples of computer programs.

3
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

1.1.1 Features of Good Computer program


 Reliability: it can be relied upon to do what it is expected to do.
 M eeting users’ needs: a developed program must meet the objectives
for which it is proposed and designed, that is, if the potential users
cannot use it either because it is too complex or too difficult.
 Usability: managing of the program should be user friendly. Provide
step by step guide for users.
 Efficiency/Cost saving: it must not cost more than its benefits and
enables problem to be solved appropriately, quickly and efficiently.
 Robustness: how well a program anticipates situations of data type
conflict and other incompatibilities that result in run time errors and
program halts. The focus is mainly on user interaction and handling of.
 Storage saving: a good program design is not to be verbous, that is, it
will not be allowed to be unnecessary long, thereby consuming much
storage that will be required for processing data and storage of
information produced from processing.
 Readability: It must be readable and understandable by other
programmers and end users.
 Flexibility: a good program must be flexible adaptable and
maintainable in order to suit user’s need. Modification must be possible
and very easy.
1.1.2 Uses of Computer Programs
Today computer programs are being used in almost every field, household,
agriculture, medical, entertainment, defense, communication, etc. Listed
below are a few applications of computer programs:
 Computer programs are being used to develop graphics and special
effects in movie making.
 Computer programs are being used to perform Ultrasounds, X-Rays,
and other medical examinations.
 Computer programs are being used in our mobile phones for SMS,
Chat, and voice communication.
 Computer programs enhances learning experience.
 Computer programs helps with effective data management.

4
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

1.2 Computer Programming


Computer programming (often shortened to programming or coding), is the
process of writing a set of instructions in sequential manner using
programming language to control the activity of a computer system. In other
word, computer programming is the process of writing, testing,
debugging/troubleshooting and maintaining act of instructions (source code)
for solving a problem with the computer. A source code is written in an
acceptable computer programming language. The code may be a modification
of an existing source or something completely new.
The purpose of programming is to create a program that exhibits a certain
described behavior (customization). The process of writing source code
requires expertise in many different subjects, including knowledge of the
application domain. Alternatively. Programming is the craft of transforming
requirements into something that a computer can execute. Problem solving
on computer is a task of expressing the solution to the problem in terms of
simple concepts, operations and computer code (program) to obtain the
results.
1.2.1 Phases of program development (Programming)
The process of developing a computer program (software) may be divided into
eight phases or stages:
 Problem definition/Analysis: this is the first stage in trying to write
a program. At this stage, the problem is identified, analyzed and must
be properly understood.
 Selection/Development of algorithm: after understanding the
problem to be solved, plans are made on what method to go about
solving the problem and develop the algorithm for the program.
 Designing of program: this is the first stage where the prototype (flow
chart, storyboard, DFD and so on) of the program is develop.
 Coding the program statement: at this stage the programmer will
select the program language that best suits his or her requirements The
instruction can be written in high level language, low level language,
low level machine code (language).

5
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

 Compiling/Compilation: a program which has been written in a high


level language must either be: compiled into a machine code form, for
input into the computer or Input into its existing form to the computer,
which will translate the instruction into machine code form using an
interpreter.
 Testing/Running and debugging of the program: At this stage the
program that has been written will have to be tested. The programmer
must test the object program with the test data. A diagnostic routine or
debugging routine provides for routine program testing and error
correction during program development. When the program does not
run well, the error must be located. Diagnostic routine will find out what
the program was doing at the time it failed, and what the contents of
store were. Test packs are used to test the effective working of new
computer programs before they are sued operationally.
 Documentation: documenting all the work involved in producing the
Program. Starting from the point of understanding the program to be
solved, to the point where the program becomes effective is
documented.
 M aintenance: at this stage the workable program must maintained.
This is important because the computer user may decide that I program
ought to be capable of doing additional processing routine or six process
some data in a different way.
1.2.2 Tools in Computer Programming
Followings are the tools which the computer programmers adopt in writing
computer programs:

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

For instance, an algorithm for a simple payroll program in which the


variables: employee name, monthly salary, allowance and income tax are
supplied as inputs. There is a possible algorithm for the above task.
Flow charts
A flowchart is a pictorial or graphical tool or model that represents the events
in a process.
Decision tree
Decision table is a tabular method of communicating the action which is
appropriate to each of a set of a given condition. A decision table thus
conveniently summaries complicated decision procedures.
Concept of Algorithm
An algorithm is a set of instructions to obtain the solution of a given problem.
Computer needs precise and well-defined instructions for finding solution of
problems. If there is any ambiguity, the computer will not yield the right
results. It is essential that all the stages of solution of a given problem be
specified in details, correctly and clearly moreover, the steps must also be
organized rightly so that a unique solution is obtained.
Features of Algorithm
 It should be simple
 It should be clear with no ambiguity
 It should head to unique solution of the problem
 It should involve a finite number of steps to arrive at a solution
 It should have the capability to handle unexpected situation.
M ethods of Representing Algorithm
Algorithms are statements of steps involved in solving a particular problem.
The steps to the solutions are broken into series of logical steps in English
related form. Programs are written to solve real life problems. There can’t be
a solution if there is no recognized problem and once a problem exist, one
must take certain step in order to get a desired solution. The following
methods could be used to represent an algorithm.
 Methods of English like form
 Methods of Flowchart
 Methods of Pseudo code

7
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

 Methods of Decision table


 Methods of Data flow Diagram (DFD)
Pseudo Code
A pseudo code is the English-like representation of the program logic. It does
not make use of standard symbols like the flowchart. It is a sequential step
by step arrangements of the instructions to be performed to accomplish a
task. It is an informal and artificial language that helps programmers develop
algorithms.

1.3 Programming Language


Programming language is an artificial language which is used in writing a set
of instructions to control the activities of a computer system. Programming
languages are various by which the programs are written. Each program is
written in a particular language, but there are quite a large number of
different languages, just as spoken languages include English, French,
German, Russia and Cantonese and so on. When a program is written, it is
written in a programming language. This is neither the normal written
language of humans, nor it is usually the machine code language of
computers. e.g FOTRAN, COBOL, BASIC. ADA, Java, PASCAL, Perl, Python
and so on.
1.3.1 Types of Programming Language
There are two main types of computer programming languages; they are:
 Low level language
 High level language

1.3.1.1 High level Language


A high level language is a problem orientated programming language, and
sometimes called third generation languages. The source programs are
written in human readable languages like English instead of mere symbols.
In other words, a high level language is a convenient and simple means of
describing the information structures and sequences of actions required to
perform a particular task. High level language is a machine independent
language which means they are compatible with any type of machine. This
generation of computer languages uses extensive vocabulary of words and

8
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

symbols are used to instruct a computer to carry out the necessary


procedures, regardless of the type of machine being sued (although the
program that translates a high level language into a machine code form known
as a compiler or interpreter is different for each make of computer. Examples
of High level Languages are FORTRAN, COBOL, QBASIC, VISUAL BASIC,
JAVA , ADA, and PASCAL e.t.c.
Features of High level programming language
The following are the main features of high level programming language:
 Machine independent
 Problem oriented
 Ability to reflect clearly the structure of program written in it
 Readability
 Programs are portable
Advantages of High level programming language
 It makes programming easier for the human being.
 High level instructions are easier to understand and faster to code.
 Error correction and resting of program is easier
 They are machine independent. That is, program written for computer
can be transferred to another computer with little or no modification.
Disadvantages of High level programming language
 High level language tends to be inefficient in the use of CPU and other
facilities.
 Machine code instructions are produced and then requires more
storage spaces.
 More time is required to run the program as it has to be translated.
1.3.1.2 Low level Language
This type of language is closer to the machine compared with the human
natural language. The two major examples are the Machine language and the
Assembly language.

9
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

Features of Low level programming language


The following are the main features of high level programming language:
 Machine dependent
 Machine oriented
 Inability to reflect clearly the structure of program written in it
 unreadability
 Programs are non-transferrable
Advantages of low level programming language
 Program translation is easier than high level language
 It affords the programmer the opportunity to understand the internal
structure of the hardware and its registers.
Disadvantages of low level programming language
 It is machine dependent, that is, cannot be transferred to another
computer.
 Program development is slow as the programmer must have detailed
knowledge of the hardware structure.
 Program maintenance is slow and error prone.

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

machine language instruction has an equivalent command in assembly


language. Assembly language makes programming much easier, but an
assembly language program must be translated into machine code before it
can be understood and run by the computer. Special utility programs called
assemblers perform the function of translating assembly language code into
machine code. Like machine code, the specific set of instructions that make
up an assembly language depend on the make and model of the computer’s
CPU. Other programming languages such as Fortran, BASIC, and C++, make
programming even easier than with assembly language and are used to write
the majority of programs. These languages, called high-level languages, are
closer in form to natural languages and allow very complicated operations to
be written in compact notation.
Advantages of assembly language
 The symbolic programming of Assembly Language is easier to
understand and saves a lot of time and effort of the programmer.
 It is relatively easier to correct errors and modify program instructions.
 Assembly Language has almost the same efficiency of execution as the
machine level language because this is one-to-one translator between
assembly language program and its corresponding machine language
program.
Disadvantages of assembly language
 It is machine dependent; programmer has to be knowledgeable in both
subject area and the operations of the machine.
 It is cumbersome though less cumbersome than that of machine
language.
 Very expensive to develop
 It consumes time
M achine language
Machine language is as old as the computer itself. It is the computer’s own
language. This is the only language computer understands. The computer
directly executes a program written in machine language. Such programs are
coded using strings of O’s and 1’s. It doesn’t need a translator. It consists of
the code that designates the electrical states in the computer (i.e, on or off):
this is expressed as combination of Os and 1s. It is called the computer’s own

11
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

language because codes or instructions written in machine language can be


executed directly by the computer; without the need for any translation. This
is the only language that has this characteristic.

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

1.4 Review Questions


1. What is computer programming? Explain the concept of computer
programming with examples.
2. Explain the relation between computer program, machine language,
and assembly language.
3. Illustrate the phases of program development.
4. What do you mean by programming language? Enlighten the types of
programming language.
5. Make distinction between high level and low level programming
language.
6. Elucidate the advantages of machine language and assembly language .
7. Explain the concept of algorithm and its features.

13
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

Week 5-8
Learning Outcome for this weeks:

An insight into Computing using OOP and OOD


Discussion on programming paradigm
The meaning of object oriented paradigm
The concept of OOP
OOP concepts in Java

14
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

COM PUTING USING OBJECT-ORIENTED DESIGN AND OBJECT-


ORIENTED PROGRAM M ING

2.0 Object-Oriented Design


Object–Oriented Design (OOD) involves implementation of the conceptual
model produced during object-oriented analysis. In OOD, concepts in the
analysis model, which are technology−independent, are mapped onto
implementing classes, constraints are identified and interfaces are designed,
resulting in a model for the solution domain, i.e., a detailed description of how
the system is to be built on concrete technologies.
The implementation details generally include:
 Restructuring the class data (if necessary),
 Implementation of methods, i.e., internal data structures and
algorithms,
 Implementation of control
 Implementation of associations.
Grady Booch has defined object-oriented design as “a method of design
encompassing the process of object-oriented decomposition and a notation for
depicting both logical and physical as well as static and dynamic models of
the system under design”.
2.1 Programming Paradigm
Programming paradigm refers to the style or way of programming. Some
paradigm gives more importance to procedure whereas others give importance
to data. The various approaches in programming are modular, top-down,
bottom–up and structured programming.
2.1.1 Object Oriented Programming Paradigm
Object Oriented Programming binds data and function into a single unit called
Object. In OOP program is divided into objects. Objects communicate with
each other by using functions.

15
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

2.2 Object Oriented Programming


Object-oriented programming (OOP) is a programming paradigm based upon
objects (having both data and methods) that aims to incorporate the
advantages of modularity and reusability. Objects, which are usually
instances of classes, are used to interact with one another to design
applications and computer programs.
Grady Booch has defined object–oriented programming as a method of
implementation in which programs are organized as cooperative collections of
objects, each of which represents an instance of some class, and whose
classes are all members of a hierarchy of classes united via inheritance
relationships.
Object-oriented programming began with Simula, a programming language
developed from 1962 to 1967. Simula introduced definitive features of OOP,
including objects and inheritance. Graphical user interface (GUI) is a feature
that allows the user to select commands using a mouse. GUIs became a
central feature of operating systems such as Macintosh OS and Windows.
2.2.1 Object Oriented Programming Features
The important features of object–oriented programming are:
 Bottom–up approach in program design
 Programs organized around objects, grouped in classes
 Focus on data with methods to operate upon object’s data
 Interaction between objects through functions
 Reusability of design through creation of new classes by adding features
to existing classes
2.3 Object Oriented Programming Languages
Some examples of object-oriented programming languages are C++, Java,
Smalltalk, Delphi, C#, Perl, Python, Ruby, VB, PASCAL, COBOL, Ada95,
FORTRAN, Scala, Simula, and PHP.
2.3.1 Java
Java is a general-purpose computer programming language that is
concurrent, class-based, object-oriented, and specifically designed to have as
few implementation dependencies as possible.

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!");
}
}

class Car extends Vehicle {


private String modelName = "Mustang"; // Car attribute
public static void main(String[] args) {

// Create a myCar object


Car myCar = new Car();

// 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");
}
}

class Pig extends Animal {


public void animalSound() {
System.out.println("The pig says: wee wee");
}
}

21
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

class Dog extends Animal {


public void animalSound() {
System.out.println("The dog says: bow wow");
}
}

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

system actually executes several statements in order to display a message on


the console.

if we had to repeatedly output a header such as:


System.out.println("Hab");
System.out.println ("ABC");
System.out.println ("Bauchi Road");
We could put it all in a method like this:
public static void printHeader()
{
System.out.println("Hab");
System.out.println("ABC");
System.out.println("Bauchi Road");
}
Interface
An interface is a contract or specification without any implementation. An
interface can't have behavior or state. Interface is a collection of method
declarations and constants that one or more classes of objects will use. We
can implement multiple inheritance using interface.
Declaration of interface:
Access interface name
{
Type finalvariablename=initialization;
Return type member-name1(parametelist); Return type member-name2(parametelist);
…..}
access class classname [extends superclass]
[implements interface [,interface...]] {
class-body
}
Or
1. interface <interface_name>{
2.
3. // declare constant fields
4. // declare methods that abstract
5. // by default.

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.

double[] myList = new double[10];

25
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

2.4 Review Questions


1. What is OOP? Explain the basic concept of OOP.
2. Explain the relation between access specifier and access modifier.
3. What do you by OOD? Enlighten the components of OOD.
4. Make distinction between programming paradigm and OOP paradigm.
5. Make distinction between different types of stream.
6. Explain with examples basic OOP concept.

26
CSC201: Com puter Programming 1 Habibu Y.M & Dams G.L

LABWORK: JAVA APPLICATION USING VISUAL BASIC/VISUAL STUDIO

27

You might also like