DCS 212 Lecture Note
DCS 212 Lecture Note
FOR
DIPLOMA II STUDENTS
KSUSTACONS
1
What is a Program?
A program is a set of unambiguous instructions written in any specific language (e.g. C, C++,
Java, Python) to perform a specific task.
The advantage of machine language is that it helps the programmer to execute the programs
faster than the high-level programming language.
Assembly Language
Assembly language (ASM) is also a type of low-level programming language that is designed
for specific processors. It represents the set of instructions in a symbolic and human-
understandable form. It uses an assembler to convert the assembly language to machine
language.
The advantage of assembly language is that it requires less memory and less execution time
to execute a program.
The main advantage of a high-level language is that it is easy to read, write, and maintain.
High-level programming language includes Python, Java, JavaScript, PHP, C#, C++, Objective
C, Cobol, Perl, Pascal, LISP, FORTRAN, and Swift programming language.
2
a. Procedural Oriented programming language
The advantage of POP language is that it helps programmers to easily track the program flow
and code can be reused in different parts of the program.
The advantage of POP language is that it helps programmers to easily track the program flow and
code can be reused in different parts of the program.
The main advantage of object-oriented programming is that OOP is faster and easier to
execute, maintain, modify, as well as debug.
TRANSLATOR
A computer program is a set of instructions for the computer to perform a certain task. Most
programs are written in high level languages or assembly language. These programs are easy
to read and understand by programmers but not understandable by the computer. The
computer only understands machine language. It consists of binary which is ones and zeros.
Therefore, the high level or assembly program should be converted into machine language
for the computer to understand the instructions and this is done by a program called
translator.
3
TYPES OF TRANSLATOR
❖ Compiler
❖ Interpreter and
❖ Assembler
Compiler and interpreter are translators that convert high level language based programs
to machine executable codes.
Java is an Object-Oriented programming language developed by James Gosling in the early 1990s. The
team initiated this project to develop a language for digital devices such as set-top boxes, television,
etc. Originally C++ was considered to be used in the project but the idea was rejected for several
reasons (For instance C++ required more memory). Gosling endeavoured to alter and expand C++
however before long surrendered that for making another stage called Green. James Gosling and his
team called their project “Greentalk” and its file extension was .gt and later became known as “OAK”.
Why “Oak”? The name Oak was used by Gosling after an oak tree that remained outside his office.
Also, Oak is an image of solidarity and picked as a national tree of numerous nations like the U.S.A.,
France, Germany, Romania, etc. But they had to later rename it as “JAVA” as it was already a
trademark by Oak Technologies. “JAVA” Gosling and his team did a brainstorm session and after the
session, they came up with several names such as JAVA, DNA, SILK, RUBY, etc. Java name was decided
after much discussion since it was so unique. The name Java originates from a sort of espresso bean,
Java. Gosling came up with this name while having a coffee near his office. Java was created on the
principles like Robust, Portable, Platform Independent, High Performance, Multithread, etc. and was
called one of the Ten Best Products of 1995 by the TIME MAGAZINE. Currently, Java is used in internet
programming, mobile devices, games, e-business solutions, etc. The Java language has experienced a
few changes since JDK 1.0 just as various augmentations of classes and packages to the standard
library. In Addition to the language changes, considerably more sensational changes have been made
to the Java Class Library throughout the years, which has developed from a couple of hundred classes
in JDK 1.0 to more than three thousand in J2SE 5.
Features of Java
Following are some of the useful and advanced features of java:
1. Simple
Java is a very simple programming language, it is easy to learn, read and write in Java. The syntax of
Java is clean and easy to understand.
2. Platform Independent
4
Java is a platform independent language. Compiler (javac) converts source code (.java file) to the byte
code (.class file). JVM executes the bytecode produced by compiler. This byte code can run on any
platform such as Windows, Linux, Mac OS etc. Which means a program that is compiled on windows
can run on Linux and vice-versa.
3. Secure
Security is one of the biggest concern in programming language as these programming languages are
used to develop some of the critical and sensitive applications that needs to be secured such as
banking applications. Java is more secure than C/C++ as does not allow developers to create pointers,
thus it becomes impossible to access a variable from outside if it’s not been initialized.
Object oriented programming is a way of organizing programs as collection of objects, each of which
represents an instance of a class.
5. Robust
Robust means reliable. Java programming language is developed in a way that puts a lot of emphasis
on early checking for possible errors, that’s why java compiler is able to detect errors that are not easy
to detect in other programming languages.
6. Distributed
Using java programming language, we can create distributed applications. RMI (Remote Method
Invocation) and EJB(Enterprise Java Beans) are used for creating distributed applications in java.
7. Multithreading
Java supports multithreading. Multithreading is a Java feature that allows concurrent execution of two
or more parts of a program for maximum utilisation of CPU.
8. Portable
As discussed above, java code that is written on one machine can run on another machine. The
platform independent byte code can be carried to any platform for execution that makes java code
portable.
9. Architectural Neutral
As we know Java is a platform independent language, which means program written and compiled on
one machine can run on any other machine having different operating system. Java follows the
principle of “Write once run anywhere“
10. Dynamic
Java is a dynamic programming language. OOPs allows developers to add new classes to the existing
packages, add new methods to the existing classes as well as modifying the method without changing
the original method code by using the concept of method overriding.
5
12. Performance
Java is significantly faster than other traditional interpreted programming languages. Compiled java
code which is known as byte code is like a machine code, that allows a faster execution. Java uses Just
in Time compiler which can execute the code on demand, this allows to execute only the method that
is being called, which makes it faster and efficient.
In the previous example, we created a Java file called Main.java, and we used the following code to
print "Hello World" to the screen.
6
Example explained
Every line of code that runs in Java must be inside a class. In our example, we named the class Main.
A class should always start with an uppercase first letter.
The name of the java file must match the class name. When saving the file, save it using the class
name and add ".java" to the end of the filename.
Identifiers
All Java variables must be identified with unique names. These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
The general rules for naming variables are:
7
Java Variables
Variables are containers or named memory space for storing data values.
In Java, there are different types of variables based on values they hold or store, for example:
• String - stores text, such as "Hello". String values are surrounded by double quotes
• int - stores integers (whole numbers), without decimals, such as 123 or -123
• float - stores floating point numbers, with decimals, such as 19.99 or -19.99
• char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
• boolean - stores values with two states: true or false.
Where type is one of Java's types (such as int or String), and variableName is the name of the variable
(such as x or name). The equal sign is used to assign values to the variable.
8
However, variables can be classified based on accessibility as follows:
• Local variables: Variables defined inside methods, constructors or blocks are called local variables.
The variable will be declared and initialized within the method and the variable will be destroyed
when the method has completed.
• Instance variables: Instance variables are variables within a class but outside any method. These
variables are instantiated when the class is loaded. Instance variables can be accessed from inside
any method, constructor or blocks of that particular class.
• Class variables: Class variables are variables declared with in a class, outside any method, with
the static keyword.
9
Java Keywords:
The following list shows the reserved words in Java. These reserved words may not be used as
constant or variable or any other identifier names.
Java Operators
Java divides the operators into the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators
10
Java Assignment Operators
Assignment operators are used to assign values to variables.
11
The Bitwise Operators:
String Concatenation
The + operator can be used between strings to combine them. This is called concatenation
Java Methods
A method is a block of code which only runs when it is called. You can pass data, known as parameters,
into a method. Methods are used to perform certain actions, and they are also known as functions.
Why use methods? To reuse code: define the code once, and use it many times.
Create a Method
A method must be declared within a class. It is defined with the name of the method, followed by
parentheses (). Java provides some pre-defined methods, such as System.out.println(), but you can
also create your own methods to perform certain actions.
12
13
14
15
OOPs (Object-Oriented Programming) CONCEPTS
Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-
Oriented Programming is a methodology or paradigm to design a program using classes and
objects. It simplifies software development and maintenance by providing some concepts:
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
16
What is an object in Java?
An entity that has state and behaviour is known as an object e.g., chair, bike, marker, pen,
table, car, etc. It can be physical or logical (tangible and intangible). The example of an
intangible object is the banking system.
For Example, Pen is an object. Its name is Reynolds; colour is white, known as its state. It is
used to write, so writing is its behaviour.
An object is an instance of a class. A class is a template or blueprint from which objects are
created. So, an object is the instance(result) of a class.
Object Definitions:
• Fields
• Methods
• Constructors
• Blocks
• Nested class and interface
Inheritance: when one object acquires all the properties and behaviours of a parent object, it is known
as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object. It is an important part of OOPs (Object Oriented programming
17
system). The idea behind inheritance in Java is that you can create new classes that are built
upon existing classes. When you inherit from an existing class, you can reuse methods and
fields of the parent class. Moreover, you can add new methods and fields in your current class
also. Inheritance represents the IS-A relationship which is also known as a parent-child
relationship.
The extends keyword indicates that you are making a new class that derives from an
existing class. The meaning of "extends" is to increase the functionality. In the terminology
of Java, a class which is inherited is called a parent or superclass, and the new class is called
child or subclass.
On the basis of class, there can be three types of inheritance in java: single, multilevel and
hierarchical.
18
Polymorphism: If one task is performed in different ways, it is known as polymorphism. For example:
to convince the customer differently, to draw something, for example, shape, triangle, rectangle, etc.
In Java, we use method overloading and method overriding to achieve polymorphism. Another
example can be to speak something; for example, a cat speaks meow, dog barks woof, etc.
Polymorphism in Java is a concept by which we can perform a single action in 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.
There are two types of polymorphism in Java: compile-time polymorphism and runtime
polymorphism. We can perform polymorphism in java by method overloading and method
overriding.
Abstraction: Abstraction is a process of hiding the implementation details and showing only
functionality to the user. Another way, it shows only essential things to the user and hides the
internal details, for example, sending SMS where you type the text and send the message.
You don't know the internal processing about the message delivery. Abstraction lets you focus
on what the object does instead of how it does it.
A class which is declared with the abstract keyword is known as an abstract class in Java. It
can have abstract and non-abstract methods (method with the body). Before learning the
Java abstract class, let's understand the abstraction in Java first.
Encapsulation: Binding (or wrapping) code and data together into a single unit are known as
encapsulation. For example, a capsule, it is wrapped with different medicines. A java class is the
example of encapsulation. Java bean is the fully encapsulated class because all the data members are
private here.
19