OOP Chapter I
OOP Chapter I
School of Informatics
Department of Computer Science
OOP is a programming paradigm that relies on the concept of classes and objects.
It is used to structure a software program into simple, reusable pieces of code blueprints
(usually called classes), which are used to create individual instances of objects.
Object receive messages, processes data, and sends messages to other objects.
There are many object-oriented programming languages including C++, Java, JavaScript
Python etc. •
7 Introduction to Object-Oriented Programming
The simplest way to explain object-orientated programming is to use something like a
car as an example.
Examples: Shape, Circle, and Rectangle in a drawing program, Employee, Faculty, Staff
in a university personnel system
A car has a model name, a colour, a year in which it was manufactured, an engine size and
so on.
8 Introduction to Object-Oriented Programming
Benefits of OOP
OOP models complex things as reproducible, simple structures
Inheritance: child classes inherit data and behaviors from parent class
Abstraction: only exposing high level public methods for accessing an object
Computers do not understand the languages (C++, Java, etc) that programs are written in.
Programs must first be compiled (converted) into machine code that the computer can run.
Multiple Compilers
Because different operating systems (Windows, Macs, Unix) require different machine
code, you must compile most programming languages separately for each platform
12 1.3. Editing, Compiling and Interpreting
Java interpreter
Java is a little different.
Java compiler produces bytecode not machine code.
Bytecode can be run on any computer with the Java interpreter installed.
13 1.3. Editing, Compiling and Interpreting
It is currently known as the Java Platform, Standard Edition 8.0 (Java SE 8).
Versions of Java SE are available for various platforms, including Linux, Windows, and
macOS computers. Free downloads are available at Sun’s Web site at
http://www.oracle.com/technetwork/java/.
14 1.3. Editing, Compiling and Interpreting
In some cases, the individual programs that make up the Java SE are available in a single
program development environment, known as an integrated development environment (IDE).
Some examples include Eclipse, jGrasp, and Oracle’s own NetBeans IDE.
Each of these provides a complete development package for editing, compiling, and running
Java applications on a variety of platforms, including Linux, macOS, and Windows.
15 1.3. Editing, Compiling and Interpreting
Java is fast, secure, and reliable, therefore, It is widely used for developing
Java applications in laptops, data centers, game consoles, scientific
supercomputers, cell phones, etc.
History of Java Programming Language
16
Here are important landmarks from the history of the Java language:
Originally, it was developed for handling portable devices and set-top boxes. Oak was a
massive failure.
In 1995, Sun changed the name to “Java” and modified the language to take advantage
of the burgeoning www (World Wide Web) development business.
Later, in 2009, Oracle Corporation acquired Sun Microsystems and took ownership of
three key Sun software assets: Java, MySQL, and Solaris.
James Gosling developed the Java platform at Sun Microsystems, and the Oracle
Corporation later acquired it.
Java Programming
17
The Java Programming Language is a high-level language. Its Syntax is similar to C and
C++, but it removes many of the complex , confusing features of C and C++.
The Java Programming Language includes the feature of automatic storage management by
using a garbage collector.
The Java programming language source code is compiled into the bytecode instruction set
which can be run inside the Java Virtual Machine (JVM) process.
Java Application
In the Java Language, all of the source code is written in plain text files with the .java
extension
Java Programming …
18
The java source code files are then compiled into .class extension files by the command
javac
The Java Virtual Machine gives runtime support to the application and can make the
application independent from different hardware systems.
22 Data types and variables
Java has primitive and non-primitive data types. Primitive data types are a special group of
data types that do not use the keyword new when initialized.
There are eight primitive data types that are used to store data during a program's operation.
23 Data types and variables…
Java creates them as automatic variables that are not references, which are stored in
memory with the name of the variable.
To display Variables
The println() method is often used to display variables. To combine both text and a
variable, use the + character:
All elements must be of the same type BUT there is no restriction on which type this is.
Length of the array is set when the array is declared, so the size is fixed.
arrays can be used to hold a collection of int values; or a collection of char values;
BUT they cannot be used to hold a mixture of int and char values.
Declaring arrays
30
Need to state
the size of the array ;
the type of each individual array element .
The array type and size are then put together with a special new operator.
For example
Declaring arrays
31
Example 2
This is the only instance in which all the elements of an array can be assigned
explicitly by listing out the elements in a single assignment statement.
Accessing array elements
Array can be used like any other variable of the given type in Java.
The assignment operator can be used to enter a value.
You must specify which element to place the value in.
For example: Allowing the user of the program to enter the value of the first temperature:
Java provides a rich set of operators to manipulate variables. We can divide all the Java
operators into the following groups:
Arithmetic operators: are used in mathematical expressions in the same way that they are
used in algebra. These are addition, Subtraction, multiplication, division, modulus,
increment and decrement operators.
Operators in Java
35 The following program is a simple example which demonstrates the arithmetic operators.
Operators in Java
36
output
Operators in Java
37 Relational operators
Java has six relational operators used to test primitive or literal numerical values.
Logic Operators
Java has three logic operators used to combine
Boolean expressions into complex tests.
Operators in Java
38
If expression is false, statement is not executed and the program continues
at rest_of_program
Decision and Repetition statement
41 If-Else Statement If-Else Statement Example
if (expression) {
statement1;
}
else{
statement2;
}
next_statement;
if-else chains can be sometimes be rewritten as a “switch” statement. switches are usually
simpler and faster
Decision and Repetition statement
43 switch Statement switch Statement Example
The switch statement enables you to test
several cases generated by a given expression.
switch (expression) {
case value1:
statement1;
case value2:
statement2;
default:
default_statement;
}
Every statement after the true case is executed
The expression must evaluate to a char, byte,
short or int, Character, Byte, Short, Integer,
String, enum but not float, or double or long
The break statement tells the computer to exit the switch statement
44 Decision and Repetition/Itration statement
Iteration is the form of program control that allows us to repeat a section of code.
This form of control is often also referred to as repetition.
The programming structure that is used to control this repetition is often called a loop.
There are three types of loops in Java:
for loop;
while loop;
do…while loop.
The ‘for’ loop
If we wish to repeat a section of code a fixed number of times we would use Java's for
loop.
The for loop is usually used in conjunction with a counter to keep track of how many
times we have been through the loop so far:
The ‘for’ loop
45 for (init_expr; loop_condition; increment_expr) {
statement;
}
The control of the for loop appear in parentheses and is made up of three parts:
1. The first part, the init_expression, sets the initial conditions for the loop and is executed
before the loop starts.
2. Loop executes so long as the loop_condition is true and exits otherwise.
3. The third part of the control information, the increment_expr, is usually used to
increment the loop counter. This is executed at the end of each loop iteration.
Example
output
Decision and Repetition statement
46
while loop: while loop statement in Java programming language repeatedly executes a
target statement as long as a given condition is true.
while Statement syntax Here, statement(s) may be a single statement or a block of
statements.
The condition may be any expression, and true is any non
zero value. When executing, if the boolean_expression result
is true, then the actions inside the loop will be executed. This
will continue as long as the expression result is true.
When the condition becomes false, program control passes
to the line immediately following the loop
Decision and Repetition statement
47 while Statement Example
output
Decision and Repetition statement
48
do...while: A do...while loop is similar to a while loop, except that a do...while loop is
guaranteed to execute at least one time.
do…while Statement syntax Notice that the Boolean expression appears at the end of
the loop, so the statements in the loop execute once before
the Boolean is tested.
If the Boolean expression is true, the control jumps back up
to do statement, and the statements in the loop execute
again.
This process repeats until the Boolean expression is false.
49 Decision and Repetition statement
do…while Statement Example
output
50