Upload
others
View
18
Download
0
Embed Size (px)
Citation preview
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();
}
Sorting Algorithms
Bubble Sort public static void bubbleSort(int[] data)
{
do{
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;
}
}
while(changed);
}
Recursion
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);
}
Recursion
Fibonacci
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);
}
Recursion
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)
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!
Concepts
A concept is an idea or notion that we apply to entities in the real world or our imagination, in our awareness.
7
Person Employee
Company
Headquarter
Car
Model
Patient
Efficient Organization
Contract
can be a can be a
owns employs
characterizes
has
is an instance of
subject
owns
subscribes
Town
lives in
registered
registered at
Marriage
Concepts
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.
Concept
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)
Person
Employee
Event Type
Company
Order Item
Order
{ Symbolizes }
Manager
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.
10
Susan Mary
Jane
Alice
Woman
Paul Peter
Martin
John
Man Employee
Mary
Employee Set
Property Owner Set
Person
Set
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:
11
Class Object
classifier
instance
1..*
*
Classification Relationship
Objects
Attributes(fields)
• abstraction of a property of an object in real life
• Type
• Name
• Value
• (Constraints)
• Intrinsic / Extrinsic
Operations(methods)
• Responsabilities
• Offer services
The Object Implementation
Point’s interface
Point’s Implementation
read_x
Point
read_y
change_x
change_y
Implementation of the Operations
read_y
write_x
write_y
read_x
Implementation of the Attributes
x:
y: Point’s
semantics
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>
}
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){
salary=(1+perc/100)*salary;
return;
}
public string toString(){
return "name=" + getName()+ ",salary=" + getSalary() + ",hireDay=" + hireDay);
}
}
Employee john= new Employee(“Smith John”, 2534, 2001, 3, 26);
john
:Date
:Employee
name
salary
hireDay
:String
“Smith John”
26/3/2001
2534,00
class NameOfClass { field1 field2 . . . constructor1 constructor2 . . . method1 method2 . . . }
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>
<statements>
}
Constructors
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(); }
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) {…}
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.
19
Order
RushOrder
Item Account check for credit
<<uses_a>>
*
OrderManager
*
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, java.net, 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: foo.com becomes com.foo.javacourse
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 com.foo.javacourse;
public class Employee {
. . .
}
Classes are stored in subdirectories of the file system. The path to the class must match the package name.
20
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
21
Visibility Modifiers
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
22
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