uqi107s3 object orientation lecture 2. plan review tutorial 1 testing mayday development –latlong...

23
UQI107S3 Object Orientation lecture 2

Post on 20-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

UQI107S3

Object Orientation

lecture 2

Plan

• Review Tutorial 1

• Testing

• Mayday development– latlong type, boat type, boats table– procedures

• 3 Tier application

• Some distinctions

Review tutorial 1

• Using SQLPLUS -See notes

• Latitude and Longitude, Degrees and minutes

• Variables and values - see later

• Additions to tests -– asString() not tested– should be able to predict answer to test

new method -asDegrees()

• Add to definition of dm type member function asDegrees return real,

• Add to body member function asDegrees return real is

begin

return degrees + sign(degrees) * minutes/60;

end;

• Compile sqlplus user/password < dm.sql

• Test it select dm(0,30).asDegrees() from dual;

Review Tutorial - constructors

• Test scripts use the constructor to create an object of the new type:– dm(30,45) – creates an object of type dm with

• degrees = 30

• minutes = 45

• same thing happens when you enter a date - ‘04/02/03’ in a date field - a date value is constructed

Testing

• Develop - test– develop small units, bottom-up– develop test scripts so that tests can be repeated whenever a change

is made - ‘regression testing’• select dm(30,30).asMin() from dual;

• Predict output before running test• value should be 30*60 + 30 = 1860• select dm(30,30).asMin()- 1830 from dual;

• output should be 0

• Testing strategy part of ‘Extreme Programming’ and ‘Agile Development’

Mayday development– Define base degrees/minutes type

• test

– Define two further types - latlong and boat• test

– Define table of boat types - to store objects– Define some boats

• test

– Create PL/SQL procedures to access data from a browser

• test

create or replace type latLong as object (

latitude dm,

longitude dm,

member function distanceTo(x latLong) return real ,

member function asString return varchar

); parameterparameter

Define the ‘Interface’ to the type:it supplies all information a user of this type needs to know

Values of these variables will be objects of type dm

Values of these variables will be objects of type dm

Values and variables

• Base types - in the type dm:– degrees is a variable of type number– 30 is a value of type number

• Defined types – latitude is a variable of type dm– dm(30,0) is a value (an object) of type dm

create or replace type body latLong as member function distanceTo(x latLong) return real is dlat real; dlong real;-- this is an approximation for short distances begin dlat:= latitude.asMin()- x.latitude.asMin(); dlong:=longitude.asMin() - x.longitude.asMin(); dlong:=dlong*cos((latitude.asRad()+x.latitude.asRad())/2); return round(sqrt((dlat*dlat) + (dlong*dlong))); end;

Local variablesLocal variables

Return computed distance

Return computed distance

This calculation is a bit tricky, so good to put it in one place,test it and then simply use it:

Now define the ‘Implementation’ of each function - the code thatmakes it produce the result:

call the asMin() function on the object in variable ‘latitude’

call the asMin() function on the object in variable ‘latitude’

member function asString return varchar is hemi char(2); pole char(2); begin if (latitude.degrees < 0) then pole:='S '; else pole:='N '; end if; if (longitude.degrees < 0) then hemi:='E '; else hemi:='W '; end if; return pole || latitude.abs().asString() || ' ' || hemi || longitude.abs().asString(); end;

select latlong(dm(20,0), dm(30,0)).asString() from dual;

Construct dm object

Construct dm object

Construct dm object

Construct dm object

Construct latlong object

Construct latlong object

All these objects are ‘transient’

- exist for the calculation only

All these objects are ‘transient’

- exist for the calculation only

Test latlong.distanceTo(latlong)

select latlong(dm(0,0), dm(0,0))

.distanceTo(

latlong(dm(1,0), dm(0,0)) )

from dual;

Surprisingly, can have blanks here!

Surprisingly, can have blanks here!

Receiving Object

Receiving Object

Object as Parameter

Object as Parameter

Function callFunction call

Result should be 60

Creating persistent ObjectsDefinecreate or replace type boat as object ( name varchar(20) ,pos latlong );

Test select boat('Perdika', latlong(dm(53,20), dm (2,48))) from dual;

Create table create table boats of boat;

Create some persistent objectsinsert into boats values('Perdika',latlong(dm(20,30),dm(10,20)));

insert into boats values('Flash',latlong(dm(21,35),dm(12,20)));

UML Class Model

Using Rational Rose

dm

degrees : number(3)minutes : real

asMin() : realasString() : StringasRad() : real

boat

name : Stringposition : latlong

latlong

latitude : dmlongitude : dm

asString() : StringdistanceTo(position : latlong) : real

+latitude +longitude

+position

3-tier system

User interface using Browser

Business rules using P|L/SQL

Requests + data HTML

Relational Database using Oracle 9

SQL Relation (accessed by cursor)

create or replace procedure chooseboat is

cursor allboats is select * from boats b; begin htp.print('<form action=getnearestboats method=get>'); htp.print('<br>Target boat name <select name=reqname>'); for b in allboats loop htp.print('<option>' || b.name || ' </option> ' ); end loop; htp.print('</select>'); htp.print('Range to search <input type=text name=reqrange size=5 value=100>'); htp.print('<input type=submit value=‘search’>'); htp.print('</form>'); end;

The procedure to make a form to select a boat and range

SQL statementSQL statement

htmlhtml

Procedure to find boats in range

create or replace procedure getnearestboats (reqname varchar, reqrange varchar) is target boat; cursor selboats is select * from boats b where b.name !=target.name -- dont want target and b.pos.distanceTo(target.pos) < reqrange order by b.pos.distanceTo(target.pos); begin select value(b) into target from boats b where name=reqname; …..

Generating the output

htp.print('<h3>'|| target.name || ' at position ' || target.pos.asString() || '</h3>');

htp.print('<h4>Boats within ' || reqrange || ' miles</h4>');

htp.print('<ul>'); for b in selboats loop htp.print('<li><b>' || b.name || '</b> at ' ||

b.pos.asString() || ' is ' || b.pos.distanceTo(target.pos)

|| ' miles away</li>'); end loop; htp.print('</ul>');end;

Distinctions -1

• Type and Object– type defines the structure and behaviour of all

the objects of that kinds– object is a single instance of that type – compare with:

• Base type String and ‘String’

• Table EMP and tuple (3456,’Smith’,’Accounting’)

Distinctions - 2

• Variable and Value– a value is fixed, a variable is a name for a place

to store values

• Interface and Implementation– interface defines what a user of an object can

do, implementation defines how it works internally

Distinctions - 3

• Transient and Persistent– Transient objects exist only while a program is

executing – Persistent objects are stored in permanent

storage (e.g. a Database) and are deleted on command

Next week

• Tutorial– install this application and make small changes

to the procedures

• Lecture– Inheritance– UML diagrams