Implementation - Binus Repository
Download
Report
Transcript Implementation - Binus Repository
Implementation
Di Bab ini terfokus kepada
Bagaimana mengkonversi model final menjadi
sebuah code (program) dan mendemontrasikan
bahai mana peta hubungan antara mode dan
program, dalam hal ini mencakup
Bagaimana sebuah class dan hubungan antar
class di terjemahkan dalam sebuah code/program
Bagaimana sebuah object dibuat dari sebuah
class
Bagaimana sebuah message melakukan passing
dan alur kontrol yang dimodelkan dalam sequence
diagram di implementasikan didalam program
Dalam bab ini implementasinya menggunakan
bahasa java
Mengimplementasikan class
diagram
CarParkSystem
ValidCards
Card
-number
+validCard()
+addCard()
+deleteCard()
+getNumber()
+delete()
CarPark
-capacity
-spaces
+decSpaces()
+incSpaces()
+spacesLeft()
Sensor
+carArriving()
+carLeaving()
+carPresent()
CardReader
+cardReadOK()
Simulator
FullSign
Barrier
-MaxCardNo
-LightOn
-up
+arrivalgenerated()
+departuregenerated()
+getcardno()
+stillthegenerated()
+SwitchOn()
+SwitchOff()
+raise()
+lower()
ExitBarrier
+raise()
+lower()
EntranceBarrier
+raise()
+lower()
VisitorCard
-currentDate
+delete()
StaffCard
-name
-departement
-expirydate
+delete()
Card Class
Perbedaan utama antara model dengan program
adalah operasi konstruktor
Operasi konstruktor adalah sebuah operasi yang
dilakukan otomatis ketika sebuah obyek atau
instance sebuah class dibuat.
Sebuah obyek dibuat dengan menggunakan kata
new(dalam programming)
Operasi konstruktor dapat diinisialisasi oleh
programer bisa juga otomatis
Ketika melihat code berikut, perlu diingat point
berikut ini
Ketika sebuah obyek card baru dibuat,
konstruktor mengeset nilai attribut number
dengan nilai yang ada dari parameter num
Segala sesuatu yang berada diantara /* dan */
adalah sebuah komentar
Segala sesuatu yang berada diantara // sampai
pindah garis baru adalah komentar
Card
number : int
/* parrent class for cards */
public class Card
{
int number; // card number
getNum()
public Card(int num)
{
number = num; // constructor copies parameter to number
}
// constructor not shown in class diagram
int getNum() // gives card number
{
return number;
}
}
Card
Staff card class
number : int
/* staffCard inherits the attribute number from card, staff
card contain holder name, department and expiry date */
public class StaffCard extends Card
{
string name;
// holders name
string department; // holders department
string expiryDate; // card expiry date
getNum()
Inherits relationship
StaffCard
attributes
name : string
departement : string
expirydate : string
StaffCard (string belonsTo,string inDept,string expiresOn, int cardNo)
{
super(carNo); // call parent class constructor to assign card number
name = belongTo; // StaffCard constructor copies parameter to name
departement = inDept;
expiryDate = expiresOn;
}
}
Card
VisitorCard Class
number : int
getNum()
/* VisitorCard inherits from Card */
public class VisitorCard extends Card
{
string expiryDate;
VisitorCard()
{
super(9);
expiryDate=“01-01-2003”;
}
}
VisitorCard
expirydate : string
Mengimplementasikan model
dinamik
Membuat sebuah obyek
Mengikuti urutan message
Bagian2 program
Creation of the objects
Di bagian ini akan didemontrasikan bagaimana mengcreate
sebuah obyek
Ref.
Creating Class / Object
New Object
A
CarParkSystem
:CarPark
B
:CarPark
:ValidCards
C
:ValidCards
:Card
D
:Card
:CardReader
E
:CardReader
:EntranceBarier
:CarParkSystem
new()
A
:CarPark
B
new()
:ValidCards
new()
C
:Card
new()
D
:CardReader
new()
E
:EntranceBarrier
mengikuti urutan message
Dibagian ini mendemontrasikan bagaimana
urutan message di sequence diagram
berhubungan dengan code program.
Mencari class yang mengirim message dan
menerima message seperti mencari harta
karun
Ref. No.
Sending Class
Pg.
Message/Return
Receiving Class
1
CarPark
153
CarArriving()
inSensor:Sensor
2
Sensor
157
arrivalGenerated()
Simulator
3
Simulator
158
Returns true
Sensor
4
Sensor
157
Returns true
CarPark
5
CarPark
153
spaceLeft()
CarPark
6
CarPark
154
Return true
CarPark
7
CarPark
153
cardReadOK()
MainCardReader :CardReader
8
CardReader
156
getCardNo()
Simulator
9
Simulator
157
Retruns cardNo
CardReader
10
CardReader
156
validCardNo(cardNo)
okCard : ValidCards
11
ValidCards
155
* getNum()
knowCard[i] : Card
12
Card
156
Returns number
ValidCards
13
ValidCards
155
Returns true
CardReader
14
CardReader
156
raise()
inBarrier : EntranceBarrier
15
EntranceBarrier
157
carPresent()
inSensor : Sensor
16
Sensor
158
stillThereGenerated()
Simulator
17
Simulator
158
Returns false
Sensor
18
Sensor
158
Returns false
EntranceBarrier
19
EntranceBarrier
157
lower()
EntranceBarrier
20
EntranceBarrier
157
decSpaces()
ownerCarPark : CarPark
21
CarPark
153
spacesLeft()
CarPark
22
CarPark
154
Returns true
CarPark
:CarPark
:Simulator
:Sensor
:CarReader
:ValidCards
:Card
EntranceBarrier
carArriving()
1
arrivalGenerated()
2
true
3
true[carArriving=true]
4
5
spacesLeft()
true
6
cardReadOK()
7
getCardNo()
8
cardNo
9
ValidCard(cardNo)
10
*getNum()
11
true
number
12
13
raise()
14
carPresent()
stillThereGenerated()
17
false
15
16
false
18
19
lower()
decSpaces()
20
21
spacesLeft()
true
22
Sequence diagram showing message sequence for the “enter the car park” use cases
Bagian code program
Classes listed
Class
Page
CarParkSystem.java
152
CarPark.java
152
ValidCards.java
154
Card.java
155
CardReader.java
156
EntranceBarrier.java
157
Sensor.java
157
Simulator.java
158
CarParkSystem.java
/* main driver class for car park simulation*/
public class CarParkSystem
{
public static CarPark aCarPark;
public static void main(string[] args)
{
// set up car park with 1 entrance, 1 exit ,
// 5 spaces, 3 of which are full initially
aCarPark = new CarPark(1,1,5,3);
}
}
A
CarPark.java
/* class representing car park */
public class CarPark
{
private int capacity; // max number of spaces
private int spaces; // actual spaces
Sensor inSensor; // sensor for entry
Sensor outSensor; // sensor for exit
ExitBarrier outBarrier; // barrier for exit
ValidCards currentCards; // list of valid cards
CardReader mainCardReader; // card reader on entry
FullSign fullLight;
// class to be continued in next page
public CarPark(int noExits, int noEnts,int in Capacity, int noSpaces)
{
simulator.setMaxCardNo(10); // set max number for attempted cards
capacity = inCapacity; // give value to capacity
spaces = noSpaces; // give value to spaces
B
currentCards = new ValidCards(); // create list of valid cards
mainCardReader = new CardReader(this,currentCards); // create card reader
// ‘this’ is standard java term used to refer to the class currently being defined
InSensor = new Sensor(); // create sensor
outSensor = new Sensor();
fullLight = new FullSign();
outBarrier = new ExitBarrier(this); // create exit barrier
// class definition for car park to be continued
D
for(int carNo; carNo<50; carNo) // simulate for 50 car
{
if(inSensor.carArriving()) // if next event is car arrivall
{
System.out.print(“\Car Arriving “);
if(spaceLeft()) // if any room in park
{
if(mainCardReader.cardReadOK()) // if valid card entered
{ // the card reader will handle cars entry
if(!spaceLeft()) // switch on full light if full
fullLight.switchOn();
fullLight.display(); // display full sign status
}
}
else
System.out.print(“No Entry – Car Park Full”);
};
// for block statement to be continued
1
5
7
21
if(spaces != capacity && outSensor.CarLeaving()) // if car park not empty and exit simulated
{
System.out.print(“\nCar Leaving”);
outBarrier.raise();
if(spaces == 1) // if only space is one just vacated
fullLight.switchOff();
fullLight.display();
}
} // end of for block statement
} // end of car park constructor
public void decSpace()
{
spaces = spaces – 1;
System.out.print(“Spaces Left = “ + spaces + “ “);
}
public void incSpaces()
{
// add 1 to spaces available
spaces = spaces + 1;
System.out.print(“Spaces left = “+ spaces +” ”);
}
public boolean spaceLeft()
{
// indicates if spaces available
if(spaces>0)
{
return true;
}
else
{
System.out.print(“No Space Left”);
return false
}
} // end of class CarPark….
6 and 22
ValidCard.java
/* class representing list off valid cards */
public class ValidCards
{
Card knownCards[]; // list of valid card
int i;
ValidCards()
{
// set up lift of known card. Better system would store in linked list or file
knownCard = new Card[8]; // assign memory for valid cards
// add 6 staff cards
knownCard[0] = new StaffCard(“Fred”,”Sales”,”17-11-1999”,1);
knownCard[1] = new StaffCard(“Sue”,”Sales”,”21-03-2001”,2);
knownCard[2] = new StaffCard(“Frank”,”Research”,”03-06-2000”,3);
knownCard[3] = new StaffCard(“Mary”,”Testing”,”29-02-2000”,4);
knownCard[4] = new StaffCard(“Bill”,”Research”,”05-12-2000”,5);
knownCard[5] = new StaffCard(“Jill”,”Testing”,”13-01-2000”);
// add 2 visitor card
knownCard[6] = new VisitorCard();
knownCard[7] = new VisitorCard();
} // end of constructor ValidsCard
C
public boolean validCard(int CardNo) // function to check the valid card
{
boolean valid = false; // initial value
// search known card to see if number corresponds
for( i = 0; i < 8; i++)
{
if(knownCard[i].getNum() == cardNo)
{
valid = true;
}
}
if(valid)
{
System.out.print(“Valid Card “+ cardNo + “.”);
return true;
}
else
{
System.out.print(“Invalid Card “+ cardNo + “.”);
return false;
}
}
}
11
13
Card.java
public class Card
{
int number; // card number
public Card(int num)
{
number = num;
}
int getNum()
{
return number;
}
}
12
StaffCard.Java
/* staffCard inherits the attribute number from card, staff
card contain holder name, department and expiry date */
public class StaffCard extends Card
{
string name;
// holders name
string department; // holders department
string expiryDate; // card expiry date
StaffCard (string belonsTo,string inDept,string expiresOn, int cardNo)
{
super(carNo); // call parent class constructor to assign card number
name = belongTo; // StaffCard constructor copies parameter to name
departement = inDept;
expiryDate = expiresOn;
}
}
VisitorCard.java
/* VisitorCard inherits from Card */
public class VisitorCard extends Card
{
string expiryDate;
VisitorCard()
{
super(9);
expiryDate=“01-01-2003”;
}
}
CardReader.java
/* class representing Card reader */
public class CardReader
{
private ValidCard okCards; // list of valid cards
EntranceBarrier inBarrier; // barrier controled by card reader
public CardReader(CarPark aCarPark, ValidCards centralList) // constructor
{
okCards = centralList; // create association
inBarrier = new EntranceBarrier(aCarPark); // create aggregation
}
E
public boolean CardReadOK() // is card valid ?
{
int cardNo; // number of card in machine
cardNo = Simulator.getCardNo();//simulate reading
if(okCards.validCard(cardNo) // validate card
{
inBarrier.raise();
return true; // let car in if ok
10
}
else
{
System.out.print(“Entry refused, invalid card number”); // deny entry
return false;
}
}
}
8
14
Barrier.java
/* class to represent car park barrier parent class for entrance and exit barrier */
public class Barrier
{
boolean up = false; // status off barrier
CarPark ownerCarPark;// car park to which barrier belongs
public Barrier(CarPark aCarPark) // constructor
{
ownerCarPark = aCarPark;
}
public void raise()
{
up = true;
System.out.print(“Barrier raised”);
}
// barrier class to be continued
public lower()
{
up = false;
System.out.print(“Barrier lowered”);
}
} // end of barrier class
EntranceBarrier.java
/* Entrance Barrier Class which specialises Barrier */
public class EntranceBarrier extends Barrier
{
public EntranceBarrier(CarPark aCarPark)
{
super(aCarPark); // pass parameter to parent barrier class
}
public void raise() // let a car in
{
super.raise() // use parents method to raise barrier
while(OwnerCarPark.inSensor.carPresent())
{
// do nothing just wait
}
lower();
}
19
15
void lower() // card
{
super.lower();
ownerCarPark.decSpaces();
}
} // end of class barrier
20
ExitBarrier.java
/* class for exit barrier */
public class ExitBarrier extends Barrier
{
public ExitBarrier(CarPark aCarPark)
{
super(aCarPark);
}
public void raise()
{
super.raise();
while(ownerCarPark.outSensor.CarPresent())
{
// do nothing,just wait
}
lower();
}
void lower()
{
super.lower();
onwerCarPark.incSpaces();
}
Sensor.java
/* class to indicate presence of car at entrance or exit sensor */
public class Sensor
{
public boolean carArriving() //simulated arrival of car
{
return(Simulator.arrivalGenerated());
}
2 and 4
public boolean carLeaving() //simulated departure of car
{
return(Simulator.departureGenerated());
}
public boolean carPresent() // simulated sensor detecting car
{
System.out.print(“Car present”);
return(Simulator.stillThereGenerated());
}
}
16 and 18
FullSign.java
/* class for the full sign */
class FullSign
{
boolean lightOn;
FullSign()
{
lightOn = false; // initialy
}
void switchOn()
{
lightOn = true;
}
void switchOff()
{
lightOn = false;
}
void display()
{
System.out.print(“Full Light is “);
if(lightOn)
System.out.println(“ON”);
else
System.out.println(“OFF”);
}
} // end of class FullSign
Simulator.java
/* Class to produce random arrival, departures, and car number */
public class Simulator
{
static int maxCarNo; // higest card number can be issued
public static boolean arrivalGenerated()
{
//this method true if an arrival has occurred.
return(Math.random() <0.7) ;// 0.7 is empirical value to
}
//give full range of car park states
public static boolean departureGenerated()
{
return(Math.random() >0.7);
}
3
public static boolean stillThereGenerated()
{
// this method true if car is still detected by sensor
return(Math.random() < 0.5);
}
public static int getCardNo()
{
return(int) (maxCardNo*Math.random());
}
} // end of class simulator
17
9