int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Displaying a matrix int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i<a.length;i++) { for(int j=0;j<a[i].length;j++){ System.out.print(a[i][j]+" ");} System.out.println(); }

Transcript of int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Page 1: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Displaying a matrix

int a[][]={{1,2,3},{4,5,6},{7,8,9}};

for(int i=0;i<a.length;i++)


for(int j=0;j<a[i].length;j++){

System.out.print(a[i][j]+" ");}



Page 2: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Sorting Algorithms

Bubble Sort public static void bubbleSort(int[] data)



boolean changed = false;

for (int k = 0; k < data.length - 1; k++)


if (data[i] > data[i + 1])


int aux= data[i];

data[i] = data[i + 1];

data[i + 1] = aux;

changed = true;





Page 3: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i


A solution of a problem depends to smaller solutions to the same problem

Recursive function = function that calls itself

• base cases

• recursive cases

Example: factorial function

fact(n)= 1, n=0

n*fact(n-1), n > 0 public static int factorial(int n){

if(n==0) return 1;

else return n*factorial(n-1);


Page 4: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i



0, n=0

fib(n) = 1, n=1

fib(n-1) + fib(n-2), n>=2

public static int fib(int k) {

if (k < 2) return k;

return fib(k-1) + fib(k-2);


Page 5: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i


Greatest Common Divisor public static int gcd(int a, int b){

if(b==0) return a;

else return gcd(b, a%b);


gcd(14, 21) is evaluated as follows:

gcd(14, 21)

if (21 == 0) 14 else gcd(21, 14 % 21)

if (false) 14 else gcd(21, 14 % 21)

gcd(21, 14 % 21)

gcd(21, 14)

if (14 == 0) 21 else gcd(14, 21 % 14)

gcd(14, 7)

gcd(7, 0)

if (0 == 0) 7 else gcd(0, 7 % 0)

Page 6: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Tail Recursion

If a function calls itself as its last action, the function’s stack frame can be reused. This is called tail recursion.

In general, if the last action of a function consists of calling a function (which may be the same), one stack frame would be sufficient for both functions. Such calls are called tail-calls.

Challenge: Write a tail recursive version of factorial!

Page 7: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i


A concept is an idea or notion that we apply to entities in the real world or our imagination, in our awareness.


Person Employee






Efficient Organization


can be a can be a

owns employs



is an instance of





lives in


registered at


Page 8: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i


1. For us, concepts are a recognition device. The concepts may be:

Tangible (person, car, table), Intangible (time, quality, company), Roles (doctor, patient, owner), Judgments (high pay, good example, productive job), Relational (marriage, partnership, ownership), Events (sale, purchase, market crash), Other types of concepts (string, number, icon, image, signal, process)

2. For us concepts are also a test for reality: we can apply concepts to entities in our reality and discard concepts that no longer apply. Each concept is based on tests that determine when it applies.

Page 9: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i


We use concepts as units of knowledge in which each has an intension and an extension. There two sides of the same coin. • Intension (definition, understanding): the complete definition of the

concept and the test that determines whether or not the concept applies to an entity.

• Extension: the set of all entities to which the concept applies.

A domain is a collection of entities in a selected area of interest. A domain specification is the collection of concepts that apply to a

domain. An instance of a concept is an entity in the real world or our

imagination to which a concept applies.

9 { Extension }

{ Intension }

A shared notion or idea that applies to certain objects in our awareness.

Type (i.e. Concept)



Event Type


Order Item


{ Symbolizes }


Page 10: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Concept Instance’s Lifecycles Most entities in the real world have periods of existence. Entities appear in our awareness when we first apply a concept to

them, and objects disappear when concepts no longer apply. Real entities undergoes changes. Sometimes for such a change an

other concept may apply. In this way, concept instances can seem to have their own lifecycles. To describe these changes, we can employ sets.

In its lifetime a concept instance may be a member of several sets and, on many occasions, change its set membership. In other words: • Multiple classification: a concept instance can have multiple concepts that apply

to it at any one moment. • Dynamic classification: the collection of concepts that applies to an entity in the

real world can change over time.


Susan Mary




Paul Peter



Man Employee


Employee Set

Property Owner Set



Page 11: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Objects Objects in OOP programming are computer-oriented representations of

concept instances. Each object in our programs should abstract a concept instance in the real

world or our imagination. Three key characteristics of objects derived from characteristics of the real

entities: • The object's behaviour - what can you do with this object, or what methods can

you apply to it? The behaviour of an object is defined by the methods that you can call. The state of an object can influence its behaviour .

• The object's state - how does the object react when you apply those methods? the object's state is all stored information about what it currently looks like. A change in the state of an object must be a consequence of method calls.

• The object's identity - how is the object distinguished from others that may have the same behaviour and state? The state of an object does not completely describe it, since each object has a distinct identity.

A class of objects is the description of a set of similar objects with the same state components and behaviour.

Classification is the act or result of applying a concept (i.e. type) to one of its instances. Further we can define classification in the OOP programming:


Class Object





Classification Relationship

Page 12: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i



• abstraction of a property of an object in real life

• Type

• Name

• Value

• (Constraints)

• Intrinsic / Extrinsic


• Responsabilities

• Offer services

Page 13: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

The Object Implementation

Point’s interface

Point’s Implementation






Implementation of the Operations





Implementation of the Attributes


y: Point’s


Page 14: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Class Declaration in Java

<class modifiers> class <class name><formal type parameter list>

<extends clause> <implements clause> // Class header

{ // Class body

<field declarations>

<method declarations>

<nested class declarations>

<nested interface declarations>

<nested enum declarations>

<constructor declarations>

<initializer blocks>


Page 15: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Class Implementation in Java class Employee {

// variables

private String name;

private double salary;

private Date hireDay;

// constructor

public Employee(String n, double s, int year, int month, int day) {

name = n;

salary = s;

GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);

hireDay = calendar.getTime();


// methods

public String getName() {

return name;


public double getSalary() {

return salary;


public void raiseSalary(double perc){




public string toString(){

return "name=" + getName()+ ",salary=" + getSalary() + ",hireDay=" + hireDay);



Employee john= new Employee(“Smith John”, 2534, 2001, 3, 26);








“Smith John”



class NameOfClass { field1 field2 . . . constructor1 constructor2 . . . method1 method2 . . . }

Page 16: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Object Instantiation

instantiation = create objects from a class

reference value = identifies a particular object

object reference = variable that stores a reference value

new() operator

Constructor syntax <accessibility modifier> <class name> (<formal parameter list>)

<throws clause> // Constructor header

{ // Constructor body

<local variable declarations>

<nested local class declarations>



Page 17: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i


no other modifiers than accesibility modifiers

do not return a value

must have the same name as the class name

Default constructor

<class name>()

Implicit default constructor

<class name>() { super(); }

Page 18: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Overloaded Constructors public Employee(String n, double s, int year, int month,

int day) {…}

public Employee(String n, double s, int year, int month)


public Employee(String n, double s) {…}

public Employee(String n) {…}

Page 19: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Relationships Between Classes

The most common relationships between classes are: • Dependence ("uses–a"): a class depends on another class if its

methods manipulate objects of that class. • Association ("knows–a"): A user-defined relationship. • Aggregation ("has–a"): the act or result of forming an object

whole using other objects as its parts • Generalization ("is–a"): the act or result of distinguishing a

concept (i.e. type) that completely includes or encompasses another.




Item Account check for credit





Page 20: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Packages You can group classes in a collection called a package. Packages are convenient for organizing your work and for separating your

work from code libraries provided by others. The standard Java library is distributed over a number of packages,

including java.lang, java.util,, and so on. The standard Java packages are examples of hierarchical packages.

The main reason for using packages is to guarantee the uniqueness of class names.

To absolutely guarantee a unique package name, it is recommended that you use your company’s Internet domain name (which is known to be unique) written in reverse. Ex: becomes

Class Importation A class can use all classes from its own package and all public classes from

other packages. Two ways to access the public classes in another package: 1. java.util.Date today = new java.util.Date();

2. import java.util.*;

Date today = new Date();

Addition of a Class into a Package package;

public class Employee {

. . .


Classes are stored in subdirectories of the file system. The path to the class must match the package name.


Page 21: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Modifiers final – For a class, indicates that it cannot

be subclassed – For a method or variable, cannot be

changed at runtime or overridden in subclasses

synchronized – Sets a lock on a section of code or

method – Only one thread can access the

same synchronized code at any given time

transient – Variables are not stored in serialized

objects sent over the network or stored to disk

native – Indicates that the method is

implement using C or C++ volatile -synchronizes all cached copies of the

variable in main memory


Visibility Modifiers

Page 22: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

Visibility Modifiers public – This modifier indicates that the variable or method can be accessed

anywhere an instance of the class is accessible – A class may also be designated public, which means that any other class can

use the class definition – The name of a public class must match the filename, thus a file can have

only one public class private – A private variable or method is only accessible from methods within the same

class – Declaring a class variable private "hides" the data within the class, making

the data available outside the class only through method calls protected – Protected variables or methods can only be accessed by methods within the

class, within classes in the same package, and within subclasses – Protected variables or methods are inherited by subclasses of the same or

different package [default] – A variable or method has default visibility if a modifier is omitted – Default visibility indicates that the variable or method can be accessed by

methods within the class, and within classes in the same package – Default variables are inherited only by subclasses in the same package


Page 23: int a[][]={{1,2,3},{4,5,6},{7,8,9}}; for(int i=0;i

protected Cake, ChocolateCake, and Pie inherit a

calories field. However, if the code in the Cake class had a reference to object of type Pie, the protected calories field of the Pie object could not be accessed in the Cake class

• Protected fields of a class are not accessible outside its branch of the class hierarchy (unless the complete tree hierarchy is in the same package)

Even through inheritance, the fat data field

cannot cross the package boundary – Thus, the fat data field is accessible through any

Dessert, Pie, and Cake object within any code in the Dessert package

– However, the ChocolateCake class does not have a fat data field, nor can the fat data field of a Dessert, Cake, or Pie object be accessed from code in the ChocolateCake class 23