0% found this document useful (0 votes)
26 views16 pages

CSC 305 Slides 2

Csc 305 lesson lecture slides

Uploaded by

festustobi18
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)
26 views16 pages

CSC 305 Slides 2

Csc 305 lesson lecture slides

Uploaded by

festustobi18
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/ 16

Department of Computer Science

Adekunle Ajasin University, Akungba-Akoko


CSC 305

Course Lecturer: Dr. A.O. Ige


What is OOP?

Object-Oriented Programming (OOP) is a programming paradigm in


computer science 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. There are many object-oriented programming
languages, including JavaScript, C++, Java, and Python.

Object oriented programming is a concept that was created because of the


need to overcome the problems that were found with using procedural
programming techniques.
Differences btw OOP and Procedural Programming

S/N Object Oriented Programming Procedural Programming


1 Follows the bottom-up approach Follows the top-down approach
2 the program is divided into small parts the program is divided into small parts called
called objects. functions.

3 uses access specifiers like private, no access specifier


public, protected, etc.

4 provides data hiding and inheritance does not have proper way of hiding data so it
is less secure

5 Object-oriented programming is used Procedural programming is used for designing


for designing large and complex medium-sized programs.
programs.

6 Object-oriented programming uses the Procedural programming uses the concept of


concept of data abstraction. procedure abstraction.
Building blocks of OOP
❖Class
❖Objects
❖Methods
❖Attributes
Class
A class is essentially a user-defined data type that encapsulates data and the
methods that operate on that data.
Objects
When you create an object from a class, you are creating a specific occurrence
of that class with its own set of attribute values.
Methods
These are the functions that define the behavior of the class.
Attributes
These are the data members or variables that define the characteristics of the class.
Principles of Object Oriented Programming

1. Encapsulation
Encapsulation refers to the bundling of data (attributes) and the methods (functions or
procedures) that operate on that data into a single unit known as an "object." The
object acts as a self-contained entity, and the internal details of its implementation are
hidden from the outside world. This promotes information hiding and reduces
complexity.

2. Abstraction
Abstraction involves simplifying complex systems by modeling classes based on the
essential properties and behaviors they share. It focuses on what an object does rather
than how it achieves its functionality. Abstraction allows programmers to create a high-
level model that hides the implementation details and emphasizes the essential
features of an object.
Principles of Object Oriented Programming (Cont’d)

3. Polymorphism
Polymorphism allows objects of different types to be treated as objects of a common
type. It enables a single interface to represent different types of objects, providing
flexibility and extensibility. There are two types of polymorphism: compile-time
(method overloading) and runtime (method overriding).

4. Inheritance
Inheritance is a mechanism that allows a new class (subclass or derived class) to inherit
the properties and behaviors of an existing class (superclass or base class). This
promotes code reuse and the creation of a hierarchy of classes, where common
functionalities are shared among related classes. Types of inheritance include; single
inheritance, hierarchical inheritance, and multilevel inheritance
Principles of Object Oriented Programming (Cont’d)
Single Inheritance Hierarchical Inheritance
One child class is derived from one base class. Multiple child class can be derived from one base class.
Principles of Object Oriented Programming (Cont’d)
Multilevel Inheritance
Here, the parent class has a grandchild
Java
Java Character Set
Java character set implies that the set of characteristics, symbols, notations applicable to Java
e.g. Digit -0-9
Alphabet –A-Z (upper & lower case)
Arithmetic Expression symbols =+, -, *, / e.t.c.
Special characters =, ^, *, % e.t.c.

Arithmetic Operation Expression Relational Operation Expression Logical operation Expression

Addition + Greater than > AND &&

Subtraction - Greater than or equal to >= OR II

Multiplication * Less than < NOT !

Division / Less than or equal to <= Equivalence ==

Modulus % or MOD Equal =

Not Equal !=
I/O Statements in JAVA
We have (3) class declaration in JAVA
(i) Public (ii) Private (iii) Protected

PUBLIC:
In public, the program is visible to all members of the class and other external classes

PRIVATE:
In private, the program is only visible to the member of that class and to friend classes.

PROTECTED:
Here the program is visible to the members of that class, friend classes and desired classes

COMMENTING STYLE IN JAVA


We have two commenting styles which are;
1. /* Insert your comment (one line commenting style)*/
2. // Insert your comment (two or more commenting style)
I/O Statement in Java
OUTPUT STATEMENT IN JAVA: we have three types, which are
(i) print: The print method is used to print the specified content to the console without moving the cursor to the next
line.
System.out.print("Hello, ");
System.out.print("world!");

Displays: Hello, world!

(ii) println: The println method is used to print the specified content to the console and then move the cursor to the next
line. It automatically appends a newline character (\n) at the end.
System.out.println("Hello, ");
System.out.println("world!");

Displays: Hello,
world!
(iii) printf: The printf method is used for formatted output. It allows you to specify a format string with placeholders.
String name = "John";
int age = 25;
System.out.printf("Name: %s, Age: %d", name, age);

Displays: Name: John, Age: 25


I/O Statements in Java
INPUT STATEMENT IN JAVA:
There are two ways to input in JAVA;
i. Input: uses scanner to read input to the console. (java.util.scanner). ii. Input dialogue box: Utilizes the JOptionPane class from the Swing library to
display a dialog box for user input. (java x.swing. joptionpane).

Example:

Class import java.util.scanner Import javax.swing.joptionpane;


Public class INPUTPRO Public class INPUT PRO 2
{ {
Public static void (string arg[s] ) Public static void main (string arg ( ) );
{ {
int a, b, sum; int a, b, sum;
scanner my data=newscanner (system.ln) system.out.print (“enter String a2, b2
value for A”); a2 = joption.InputDialogueBox (Enter first number:”)
a=my data.next int ( ); b2 = joption.InputDialogueBox (Enter second number:”)
System.out.print (“Enter value for B”); a = Integer.parse.Int (a2)
b=my data.next int( ); b = Integer.parse.Int (b2)
sum = a+b; Sum = a+b
system.out.printf (“the sum is = %d”; sum); Msg1 = string.format (“the sum= %d” = sum);
} }
Joption.showmessage Dialog (null title caption)
}
Control Structure in Java
Control structure exists in three different ways which are;
• Selection
• Iteration
• Sequence
SELECTION: It has 4 different types.
NESTED IF – ELSE (IF – ELSE-IF):
o IF statements
o IF- ELSE statement A = 70& above, B =60-69, C = 50-59, D =45-49, E =44-40, F <40.
o IF- ELSE-IF (nested if - else)statement If (score >= 70 && score <101)
System.out.print(“A”);
o SWITCH case
Else
If (score >= 60 && score <70)
IF STATEMENT: It is a one directional statement. System.out.print(“B”);
Else
EXAMPLE;
If (score >= 50 && score <60)
If (CGPA > 3.49)
System.out.print(“C”);
System.print(“you passed out with 2:1”);
Else
IF- ELSE: it is a two directional statement. If (score >= 45 && score <50)
System.out.print(“D”);
EXAMPLE; Else
If (score >= 40) If (score >= 50 && score <45)
{ System.out.print(“E”);
System.out.println(“ passed ”); Else
System.out.print (“ promoted to next class ”); If (score >= 0 && score <40)
} System.out.print(“F”);
Else { Else
System.out.println(“ failed ”); System.out.print(“wrong input”);
System.out.print (“repeat the class ”); }
}
Control Structure in Java
SWITCH CASE STATEMENT: This is a multi dimensional branching statement
Example
FORMAT
A = 70& above, B =60-69, C = 50-59, D =45-49, E =44-40, F <40.
SWITCH (switch case)
SWITCH (Score)
{
{
Case label 1: Program statement 1;
Case I (score >= 70 && score <101)
Case label 2: Program statement 2;
System.out.print(“A”);
-
break;
-
Case II (score >= 60 && score <70)
Case label n: Program statement n;
System.out.print(“B”);
Default: comment/message/output statement.
break;
}
Case III (score >= 50 && score <60)
System.out.print(“C”);
break;
Case IV (score >= 45 && score <50)
System.out.print(“D”);
break;
Case V (score >= 50 && score <45)
System.out.print(“E”);
break;
Case VI (score >= 0 && score <40)
System.out.print(“F”);
Default: comment/message/output statement.
}
Control Structure in Java
ITERATION: In java we have three types of iteration are;
o WHILE statement
o DO- WHILE statement
o FOR statement.

WHILE STATEMENT DO-WHILE STATEMENT FOR STATEMENT: This statement is basically


Format Format for execution of loop of program. It could be
WHILE (condition) DO (condition) used in any of these ways;
{ { a. Control or initial variable
Program statement 1; Program statement 1; b. Loop conditional variable.
Program statement 2; Program statement 2; c. Loop counter or incremental variable.
- - Example:
- - Serial no for 20 people.
Program statement n; Program statement n; For (I =1, I =20, i++)
} } WHILE {
System.out.print(“i”);
}.
ALTERATION OF LOOPS
To alter loop in JAVA, we have two method of doing these which are;
a. Break
b. Continue

BREAK: This is used to exit from a loop


CONTINUE: This is used to skip a program statement and transfer control back to
the next loop.

EXAMPLES
Write a program for 90 students in which the 1st 25 student’s numbers will only be
generated

For break: For continue:


For (i =1, i =90, i++) For (i =1, i =90, i++)
{ {
System.out.printf(“my student no %d\n”;i) If (i=13) continue
If (i>25) System.out.printf(“my student no %d\n”;i)
break If (i>25)
} break
}.

You might also like