COM214J2: Introduction to Programming Object Oriented Programming - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

COM214J2: Introduction to Programming Object Oriented Programming

Description:

The OO principle is based on the observation that most tasks in the ... System.out.println('Isosceles Triangle'); else. System.out.println('Scalene Triangle' ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 22
Provided by: sarabjots
Category:

less

Transcript and Presenter's Notes

Title: COM214J2: Introduction to Programming Object Oriented Programming


1
COM214J2 Introduction to ProgrammingObject
Oriented Programming
  • Sarabjot Singh Anand
  • 16E17
  • ss.anand_at_ulster.ac.uk
  • http//ijsr32.infj.ulst.ac.uk/e95346/COM214J2/ind
    ex.html

2
The Object Oriented Programming Paradigm
  • The OO principle is based on the observation that
    most tasks in the world are achieved through
  • interaction/interchange of information between
    various actors or objects
  • Processing of the interchanged information by the
    various actors/objects

3
Encapsulation
  • A further observation is the fact that each
    object that plays a role in the task
  • does not need to explain to the interacting
    objects as to how the processing takes place
  • It just needs to define a clear interface through
    with
  • processing can be requested
  • results can be passed back to the requesting
    object
  • This is referred to as encapsulation

4
OOP Objectives
  • By modelling a computer program on such a
    principle of interacting objects will make the
    program
  • Reusable
  • Reliable
  • Understandable

5
The Role of Classes in OOP
  • A Class is a template used to create an object.
  • Every object created from the same class will
    have similar if not identical features
  • For example, a class human can be defined as
    consisting of
  • some data e.g. name, date of birth, gender,
    planetOfDomisile, nationality ..
  • some behaviour e.g. talks(), fights(), sleeps(),
    votes().

6
More on Classes
  • Instances/Examples of the class human are Saddam
    Hussain, George Bush, Michael Jackson
  • The name,date of birth, nationality and gender of
    these instances may be different (hence referred
    to as Instance variables)
  • Other attributes may be the same (arguably) e.g.
    planetOfDomisile (hence referred to as Class
    variables)

7
The Human Class
public class Human private String
name public int domicile static String
planetOfDomicile "Earth" Human(String nam,
int country) name nam domicile
country public int vote()
return(1)
8
Example Program Using Objects of type Human
import Human import Nationality public class
BadFolks public static Human Population
new Human5 public static void main(String
arguments) Human guy createPopulation()
for (int i 0 i lt Population.length i)
guy Populationi if
(guy.domicile Nationality.IRAQ) System.out.p
rintln("Mr. "guy.name" is one of the Folks
George doesn't like") else if
(guy.domicile Nationality.AMERICAN) System.o
ut.println("Mr. "guy.name" is one of the nice
guys") else System.out.println("Mr.
"guy.name" comes from one of the counties that
George didn't know existed")
9
private static void createPopulation() Populati
on0 new Human("Saddam Hussein",Nationality.IRA
Q) Population1 new Human("Tony
Blair",Nationality.OTHER) Population2 new
Human("George Bush", Nationality.AMERICAN) Popul
ation3 new Human("Tony Benn",Nationality.OTHER
) Population4 new Human("Tony
Banks",Nationality.OTHER)
10
Nationality
public class Nationality static final int IRAQ
0 static final int AMERICAN 1 static
final int OTHER 2
11
Packages
  • In Java, a package is a set of like-minded
    classes
  • For example the Java language consists of a
    number of packages, one of which is called
    java.io consisting of a number of classes to
    enable Input/Output with files
  • Class names within a package must be unique
    avoiding conflict in class names across packages
    as a class can be identified by the
    ltpackageNamegt.ltclassNamegt
  • Generally a package is developed by a single
    organisation hence it is easier to ensure class
    names are unique in a package

12
Modifiers
  • Modifiers implement Encapsulation defining the
    visibility and usage of variables and methods
  • The key modifiers are
  • static
  • final
  • public
  • private
  • protected
  • ltdefaultgt
  • abstract

13
Levels of Access Control
14
Other Modifiers
  • Final
  • Normally used with static as there is not much
    point in storing a constant with each instance of
    a class
  • Static
  • A class variable/method
  • Abstract
  • Only used with class and method declarations

15
public class CountInstances private
static int numInstances 0
CountInstances() CountInstances.addInstance()
private static void addInstance()
numInstances public static
int getNumInstances() return numInstances
public static void main(String
arguments) for (int i 0 i lt 10 i)
new CountInstances()
System.out.println("And then there were
"CountInstances.getNumInstances())

16
Special Methods
  • Accessor Methods
  • Provide access to private variables
  • int getVariable(), setVariable(int)
  • Checking validity of value that the variable is
    being set to
  • Makes the usage of the class independent of the
    actual storage of the variable
  • Constructor
  • Called whenever a new instance is created using
    the new keyword

17
Investigating Modifiers for Class Triangle
import uuInOut class TriangleClass1 private
double sides TriangleClass1() sides
new double 3 TriangleClass1(double
values) boolean validSides sides
new double 3 validSides setSides(values)
// if (!validSides) throw an Exception
18
public boolean setSides(double values) for
(int i 0 i lt 3 i) if (valuesi lt 0)
return false else sidesi
valuesi return true public
double getSides() return sides public
boolean readSides () for (int i 0 i lt 3
i) System.out.print("Type in the length of
side " i " - ") System.out.flush() sid
esi uuInOut.ReadDouble() if (sidesi lt
0) return false order(sides) return
true
19
private void order (double A) double
temp if (A0 gt A1) temp A0 A0
A1 A1 temp if (A1 gt A2)
temp A1 A1 A2 A2 temp
if (A0 gt A1) temp A0 A0
A1 A1 temp public boolean
validTriangle() if (sides2 lt sides0
sides1) return (true) else return
(false)
20
public void printTriangleType() if
(sides0 sides1 sides1
sides2) System.out.println("Equilateral
Triangle") else if (sides0 sides1
sides1 sides2) System.out.println("Isosc
eles Triangle") else System.out.println("Sc
alene Triangle")
21
import TriangleClass1 class iterativeTriangle
public static void main(String arguments)
TriangleClass1 triangle new
TriangleClass1() double values
1.2,1.2,2.1 double
triangleValues triangleValues
triangle.getSides()
System.out.println(triangleValues0"
"triangleValues1" "triangleValues2)
if (triangle.setSides(values))
triangleValues triangle.getSides() System.o
ut.println(triangleValues0" "triangleValues1
" "triangleValues2) if (triangle.validTrian
gle()) triangle.printTriangleType() else
System.out.println("Not a Valid Triangle")
else System.out.println("A
Side of a Triangle cannot have a Negative
Length")
Write a Comment
User Comments (0)
About PowerShow.com