OOP Notes:                                                                                   Page 1 of 4
Introduction to OOP
Lecture 1
Introduction to OOP
OOP Language
OOP Stands for "Object-Oriented Programming." refers to a programming methodology based on objects,
instead of just functions and procedures. These objects are organized into classes, which allow individual objects
to be group together.
Structured Programming Language
It is a programming language based on the use of block structures, subroutines, for and while loops.
Difference between Structured Programming and Object Oriented
            Structured Programming                             Object Oriented Programming
    Structured Programming is designed which focuses        Object Oriented Programming is designed
    on process/ logical structure and then data required    which focuses on data.
    for that process.
    Structured programming follows top-down                 Object oriented programming follows bottom-
    approach.                                               up approach.
    Structured Programming is also known as Modular         Object Oriented Programming
    Programming and a subset of procedural                  supports inheritance, encapsulation,
    programming language.                                   abstraction, polymorphism, etc.
    In Structured Programming, Programs are divided         In Object Oriented Programming, Programs are
    into small self contained functions.                    divided into small entities called objects.
    Structured Programming is less secure as there is       Object Oriented Programming is more secure as
    no way of data hiding.                                  having data hiding feature.
    Structured Programming can                              Object Oriented Programming can solve
    solve moderately complex programs.                      any complex programs.
    Structured Programming provides less reusability,       Object Oriented Programming provides more
    more function dependency.                               reusability, less function dependency.
    Less abstraction and less flexibility.                  More abstraction and more flexibility.
In this course Java Language will be used to introduce Object Oriented Programming.
The Top-Down approach vs The Bottom-Up approach programming
While the top-down approach focuses on breaking down a big problem into smaller and understandable chunks,
the bottom-up approach first focuses on solving the smaller problems at the fundamental level and then
integrating them into a whole and complete solution.
Misurata University  2022                                             Department of Computer Science
OOP Notes:                                                                                                Page 2 of 4
Introduction to OOP
Java Editions
1- J2SE (Java 2 Standard Edition):
Java Platform Standard Edition is known as Core Java and is the most basic and standard version of Java. It is
the purest form of Java and is the foundation of all the other editions. It contains a variety of general-purpose
APIs, including Java.Lang, Java.util, etc.
2- J2ME (Java 2 Micro Edition):
This version is used for the applications that run on the embedded system, smartphones, and small devices.
There are many reasons for that, mostly because it limits the processing power, battery limitations, small display,
etc. In a way, it was built for developing apps for small devices.
3- J2EE (Java 2 Enterprise Edition):
The enterprise version of Java is widely used and is more versatile than any other version of Java. It is used in
the development of web services, networking apps, server-side scripting, and also other various types of web-
based applications.
Structure of a Java Class
This week you should be entering, compiling, and executing a simple Java class which produced output to the
screen.
The following line says that this program is a class called HelloLibya
                           public class HelloLibya
                           {
                                     public static void main(String[] args)
                                     {
                                                // welcome the user
                                                System.out.println(“Hello Libya.”);
                                     }
                           }
A slightly different class is given below
                           public class OutExample
                           {
                                     // simple Java class to illustrate difference between print and println
                                     public static void main(String[] args)
                                     {
                                                // this is the body of the main method
                                                System.out.println(“This will appear on the first line” + “ so will this”);
                                                System.out.println(“ and this”);
                                                System.out.print(“This will appear on the next line”);
                                     }
                           }
SAQ 1
    What do you understand by the terms, comment, special symbol, identifier and keyword? Annotate the above
classes showing examples of each. Which elements of the above classes are essential in order to get the class to
compile?
Integer Expressions
Integer expressions are formed using integer values (whole numbers) and integer operators (+ - / * %).You may
directly output the results of arithmetic expressions using println and print as shown in the class fragment below:
Misurata University  2022                                                     Department of Computer Science
OOP Notes:                                                                                        Page 3 of 4
Introduction to OOP
                           System.out.println(5 / 3 * 2); // outputs 2
                           System.out.println(7 / 3 + 2); // outputs 4
SAQ 2
  Identify the operators available for use with integers; put the operators in order of precedence.
Exercise 1
Evaluate each of the following integer expressions
                  17 / 5 + 2
                  19 / 5 + 4
                  5 * (3 + 6) / 2
                  5* 3+6/2
                  14 - 10 * 3 / 4
                  36 + 4 * 2 – 17
The only operator that some of you are likely to be unfamiliar with is the modulo operator. For positive numbers
it gives the remainder of an integer division, e.g. 7 % 2 yields 1. For negative integers the modulo operator
requires a little bit more consideration.
Exercise 2
Consider Exercise 1.2 in Bailey and Bailey. This requires you to write a program to discover the various results
of using the % operator. e.g. – 16 % 3, 16 % -3, -16 % -3. Work out what results you would expect, and then
modify your first Java program to test the results.
Variables and Constants
Virtually every class you will ever write will require you to declare some data. To declare variables of type
integer you use the keyword int as shown in Bailey and Bailey p 15.
If you wish to declare a data item whose value may not change (a constant) then use the keyword final and assign
a value to it.
                           public class GameScores
                           {
                                     public static void main(String [] args)
                                     {
                                                final int scores = 6;            // a constant
                                                int score1, score2, score3, score4, score5, score6;
                                                int total; //variable declaration
                                                score1 = 10;          // assigns value to variable
                                                score2 = 20;
                                                score3 = 15;
                                                score4 = 17;
                                                score5 = 11;
                                                score6 = 14;
                                                total = score1 + score2 + score3 + score4 + score5 + score6;
                                                System.out.println(“Total Scored : ” + total);
                                     }
                           }
Exercise 3
Amend the above class so that it also calculates and outputs a mean for the scores.
SAQ 3
What is the meaning of the following terms : final values; declaration; assignment.
Misurata University  2022                                                Department of Computer Science
OOP Notes:                                                                                      Page 4 of 4
Introduction to OOP
Consider the class on page 16-17 of Bailey and Bailey, which provides a sequence of assignment statements.
You should notice a key feature of the statements in this class is that the same identifier appears on the left hand
and right hand side of the = operator.
                                              sum = sum + 1;
Exercise 4
Consider what would be output at the end of the following fragment of code:
                                              Sum = 26;
                                              Sum = sum + sum * 2;
                                              System.out.println(sum);
Finally, consider what happens when you attempt to assign out of range values to an integer variable. Bailey and
Bailey show on page 13 what would happen when you attempted to give an integer an explicit value larger than
2147483647. The compiler would not however, detect the following violation:
                                              big = 2147483647;
                                              big = big +1;
Try this, and see what happens.
Misurata University  2022                                               Department of Computer Science