0% found this document useful (0 votes)
9 views52 pages

Intro

Uploaded by

Raj Gami
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)
9 views52 pages

Intro

Uploaded by

Raj Gami
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/ 52

Introduction to OOP I

|
Overview
• Sub divided into two sections:
– Intro to Java: basic Java concepts

– Intro to OOP: Basic OOP concepts

Thursday,
August 31,
2017
OOP I | 2
Sections
• Intro to Java: basic Java concepts

• Intro to OOP: Basic OOP concepts

Thursday,
August 31,
2017
OOP I | 3
Introduction to Java Programming

|
Objectives
• Describe the basic Java development cycle.

• Use input and output statements.

• List Java primitive types.

• Apply basic concepts of control structures and


functions in Java.

• Write simple java applications.

Thursday,
August 31,
2017
OOP I | 5
Java Development Lifecycle

|
Java Development Lifecycle

http://ee402.eeng.dcu.ie/introduction/chapter-5---introduction-to-java

Thursday,
August 31,
2017
OOP I | 7
IDEs for Java
• Netbeans
– Comes configured with JDK
• Eclipse
– Needs configuration for Java

Thursday,
August 31,
2017
OOP I | 8
How do I start coding in Java?

|
Hello World
public class Welcome

//main method begins execution of Java Application

public static void main(String args[])

System.out.println("Hello World");

} //end of main method

} //end of Class Welcome


Thursday,
August 31,
2017
OOP I | 10
Application Distribution
• Unlike C/C++, Java applications can be distributed
to users as .java files. C/C++ applications must be
converted into executables for distribution (.exe).

• The user must however install JVM for him/her to


be able to run the Java application.

• JVM installs OS environment variables that


enables Java applications to be executed from
Command Prompt.
Thursday,
August 31,
2017
OOP I | 11
Data Types & Variables

|
Primitive Types
• int

• float

• double

• char

• Other variable types:


– String
Thursday,
August 31,
2017
OOP I | 13
Input & Output

|
Output
System.out.println("Hello World");

System.out.print("Hello World\n");

System.out.printf(“Sum is %d“, sum);

• Java uses escape sequences to format the


output

Thursday,
August 31,
2017
OOP I | 15
Input
• Most frequently the Scanner class is used to
implement input functions.
Scanner input = new Scanner(System.in);

int number = input.nextInt();

float dec= input.nextFloat();

double num = input.nextDouble();

String str = input.nextLine();

Thursday,
August 31,
2017
OOP I | 16
Class Work (15 Minutes)
• Write a java application that will prompt
the user to enter two numbers. The
program should add the two numbers and
display the result to the user.

Thursday,
August 31,
2017
OOP I | 17
Solution
import java.util.Scanner;
public class Addition {
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int number1;
int number2;
int sum;

System.out.print("Enter first number: ");


number1 = input.nextInt();

System.out.print("Enter second number: ");


number2 = input.nextInt();

sum = number1 + number2;


System.out.printf("Sum is %d\n", sum);
}
}

Thursday,
August 31,
2017
OOP I | 18
Operators & Control Structures

|
Operators

• Assignment (=)

• Arithmetic (+, -, /, %, *)

• Relational (<, >, >=, <=, !=, ==)

• Logical (&&, ||, !)

Thursday,
August 31,
2017
OOP I | 20
Control Structures
• Selection control structures
– if-else

• Repetitive control structures


– while

– for-loop

– do-while
Thursday,
August 31,
2017
OOP I | 21
Write down the
output of this
program

Thursday,
August 31,
2017
OOP I | 22
Class Work (15 Minutes)
• Write a java application that will prompt
the user to enter two integer numbers
– The program should display the largest of
them all

– The program should display the even


numbers between the two numbers

Thursday,
August 31,
2017
OOP I | 23
Sections
• Intro to Java: basic Java concepts

• Intro to OOP: Basic OOP concepts

Thursday,
August 31,
2017
OOP I | 24
Introduction to Object-oriented
Programming

|
Objectives
• Explain the concept of OOP.

• Briefly explain the basic Object-oriented


concepts:
– Classes & objects

– Attributes & behaviors

– encapsulation, inheritance, polymorphism

Thursday,
August 31,
2017
OOP I | 26
Video
• https://www.youtube.com/watch?v=NUl8lcb
eN2Y

Thursday,
August 31,
2017
OOP I | 27
Classifications and Real-world Objects

|
Real World Objects
• Most human beings think in terms of objects.
We can learn about existing objects by studying
their attributes and observing their behaviors.

• An object can be defined as anything that has


attributes and exhibit certain behaviors (e.g.
people, animals, cars, plants etc.)

• Objects can be classified into categories


Thursday,
August 31,
2017
OOP I | 29
Real World Relationships
• Different objects can be grouped together
depending on the relationship between/among
them.

• The two main relationships to keep in mind is:


– Is-a: inheritance (car is a vehicle)

– Has-a: composition (car has a steering-wheel)

Thursday,
August 31,
2017
OOP I | 30
Class Demo
• Using the examples below, model a real-
world classification-object structure by
their attributes, behaviors and relationship:
– Vehicle, FastCar, audi, bugatti, vitz

Thursday,
August 31,
2017
OOP I | 31
Class Demo
Classification: Vehicle
Attributes: Doors
Wheels

Behaviors: Starts()
Stops() – in 10 seconds

Classification: FastCar inherits Vehicle


Attributes: All attributes of Vehicle
Seats

Behaviors: All behaviors of Vehicle


Accelerates()
Stops() – in 2 seconds
Thursday,
August 31,
2017
OOP I | 32
Class Demo
Objects of classifications Vehicle and FastCar
Vehicle audi Has: 4 doors, 4 wheels
audi.starts(), audi.stops()

FastCar bugatti Has: 2 doors, 4 Wheels, 2 seats


bugatti.starts(), bugatti.accelerates(),
bugatti.stops()

Vehicle vitz Has: 2 doors, 4 wheels


vitz.starts(), vitz.stops()

Thursday,
August 31,
2017
OOP I | 33
Class Exercise (5 Minutes)

• What is the difference between structured


languages and object-oriented languages?

• What is the difference between an object


and a classification?

Thursday,
August 31,
2017
OOP I | 34
Class Work (15 Minutes)
• Using the examples below, model a real
world classification-object structure by
their attributes, behaviors and
relationships:
– Animal, Carnivore, cow, lion, pig

Thursday,
August 31,
2017
OOP I | 35
Basic Object-oriented design concepts

|
OOP Definitions
• Object-oriented design models software in terms
similar to those people use to describe real-world
objects.

• Object-oriented programming is the process of


writing programs using a language that enables the
implementation of object oriented design.

• Java is an example of an object-oriented


language.
Thursday,
August 31,
2017
OOP I | 37
Object-oriented Design
• In order to write programs using an object-oriented language
like Java; one needs to understand the general object-
oriented design concepts.

• There are 6 main object-oriented design concepts:


1. User-defined types (classes)

2. Inheritance

3. Polymorphism

4. Data hiding

5. Encapsulation

6. Abstraction
Thursday,
August 31,
2017
OOP I | 38
Object-oriented Design - Classes
1. User-defined types: object-oriented design enables a
programmer to define his/her own custom type (class);
and then use it to create any number of objects.
Objects of a certain class, such as a class of vehicles,
have same characteristics (attributes and behavior).

2. Inheritance: this is where a new classes of objects are


derived by absorbing the characteristics of an existing
class and adding unique characteristics of their own.

Thursday,
August 31,
2017
OOP I | 39
Class Demo
• Create the two classes: Automobile and
Convertible. List down their attributes and
behaviors. {user-defined class & inheritance}
– Automobile: attributes & behaviors

– Convertible: has the characteristics of class


Automobile but more specifically the roof
goes up and down
Thursday,
August 31,
2017
OOP I | 40
Class Demo
Class Automobile
Attributes: Doors
Wheels

Behaviors: Starts()
Stops()

Class Convertible inherits Automobile


Attributes: All attributes of Automobile
Seats

Behaviors: All behaviors of Automobile


roofMoves(direction) – moves up or down

Thursday,
August 31,
2017
OOP I | 41
Object-oriented Design - Classes
3. Polymorphism: This is where new classes first inherit
the characteristics of an existing class and then modify
the behavior of the existing class to suite its own
behavior. This is process is known as overriding
behaviors.

4. Data hiding: Classes have the ability to limit the


access of its attributes in other classes using access
modifiers.

Thursday,
August 31,
2017
OOP I | 42
Class Demo
• From the demo, one can create a class
Nonconvertible that inherits all the attributes and
behaviors of class Convertible.
– Then modify class Nonconvertible so that the roof
stays permanently down. {Polymorphism}

– the access of the attributes of class Automobile be


restricted in classes Convertible, Nonconvertible
using access modifiers. {Data hiding}
Thursday,
August 31,
2017
OOP I | 43
Class Demo - Polymorphism
Class NonConvertible inherits Convertible
Attributes: All attributes of Convertible

Behaviors: All behaviors of Convertible


roofMoves(direction) – does not move

Thursday,
August 31,
2017
OOP I | 44
Class Demo- Data Hiding
Class Automobile
Attributes: Doors: private
Wheels: public

Behaviors: Starts(): public


Stops(): private

Class Convertible inherits Automobile


Attributes: All attributes of Automobile. But cannot access Doors

Behaviors: All behaviors of Automobile. Cannot change/access Stops()


roofMoves(direction) – moves up or down

Thursday,
August 31,
2017
OOP I | 45
Object-oriented Design - Objects
5. OOD encapsulates (wraps) attributes and behaviors
into objects. An object’s attributes and operations are
intimately tied together.

6. Objects have the property of abstraction,


implementation details are hidden within the objects
themselves.

• This means objects may know how to communicate with


one another across interfaces, but they are not allowed
to know how other objects are implemented.
Thursday,
August 31,
2017
OOP I | 46
Class Demo
• Create at least one object from each of the
three classes: Automobile, Convertible,
Nonconvertible. {Encapsulation & Abstraction}

Thursday,
August 31,
2017
OOP I | 47
Class Demo
Objects of classes Automobile, Convertible and Nonconvertible
Automobile nissan Has: 4 doors, 4 wheels
nissan.starts(), nissan.stops()

Convertible bmw Has: 2 doors, 4 Wheels, 2 seats


bmw.starts(), bmw.roofMoves(up),
bmw.roofMoves(down), bmw.stops()

Nonconvertible lexus Has: 2 doors, 4 wheels, 7 seats


lexus.starts(), lexus.roofMoves(none),
lexus.stops()

Thursday,
August 31,
2017
OOP I | 48
Class Work (15 Minutes)
• Using the categories listed below and model a
class-object relationship using OOD concepts.
Illustrate inheritance, polymorphism and data
hiding.
– Plant, Flowering, Nonflowering

– sunflower (flowering), moss (nonflowering),


maize (plant)
Thursday,
August 31,
2017
OOP I | 49
Key Points
• In java attributes and behaviors are defined
inside a class.

• Attributes are implemented as Instance


Variables and behaviors as
methods/functions.

• Objects are used to instantiate a class


Thursday,
August 31,
2017
OOP I | 50
References

P.J. Deitel and H.M. Deitel (2007), Java How


to Program. 7th Edition. Pearson Education
Inc.

Thursday,
August 31,
2017
OOP I | 51
Ole Sangale Road, Madaraka Estate. PO Box 59857-00200, Nairobi, Kenya
Tel: (+254) (0)703 034000/200/300 Fax : +254 (0)20 607498
Email: [email protected] Website: www.strathmore.edu
|

You might also like